view modules/ui.tp @ 332:ead24192ed45

Initial work on a UI module
author Michael Pavone <pavone@retrodev.com>
date Sat, 28 Mar 2015 14:21:22 -0700
parents
children 79a14e41b79a
line wrap: on
line source
{
    _visibleWindows <- []
    _needsInit <- true
    _initRes <- 0
    _checkInitSDL <- {
        if: _needsInit {
            _initRes <- (sdl init: ((sdl subsystems) video)) = 0
            _needsInit <- true
        }
        _initRes
    }
	#{
        import: [
            r:g:b
            r:g:b:a
        ] from: sdl
        _styles <- []
		window <- :properties {
            _wind <- option none
            _renderer <- option none
            base <- #{
                title <- "Window"
                width <- 640
                height <- 480
                x <- 0
                y <- 0
                color <- (ui r: 255u8 g: 255u8 b: 255u8)
                children <- #[]
                
                show <- {
                    if: (_checkInitSDL: ) {
                        _wind <- sdl createWindow: title pos: x y size: width height flags: 0u32
                        _wind value: :window {
                            _renderer <- window createRenderer: -1 flags: ((window renderOpts) accelerated)
                            draw:
                        } none: {
                            false
                        }
                    }
                }
				
				draw <- {
					print: "Draw!\n"
					_renderer value: :renderer {
						print: "Rendering!\n"
						renderer drawColor!: color
						renderer clear
						
						foreach: children :_ child {
							child draw: renderer
						}
						renderer present
						true
					} none: { false }
				}
                
                styles <- { _styles }
                
                styles! <- :newstyles{
                    //TODO: apply styles
                    _styles <- newstyles
                }
            }
            foreach: (object propertiesOf: base) :_ name {
                if: (object does: properties understand?: name) {
                    object setProperty: name on: base to: (object sendMessage: name to: properties)
                }
            }
            base
		}
        
        enterEventLoop <- {
            continue? <- true
            
            _handlers <- dict hash
            _handlers set: ((sdl eventTypes) quit) :event {
                continue? <- false
            }
            _handlers set: ((sdl eventTypes) window) :event {
                if: (event event) = ((sdl windowEventTypes) exposed) {
					foreach: _visibleWindows :_ wind {
						print: "Redrawing window\n"
						wind draw
					}
				}
            }
            while: { continue? } do: {
                (sdl waitEvent) value: :event {
					_handlers ifget: (event type) :handler {
						handler: event
					} else: {
						print: "Unhandled event type: " . (event type) . "\n"
					}
				} none: {}
            }
        }
	}
}