view modules/ui.tp @ 335:79a14e41b79a

Add image element and placeholder text element to ui module
author Michael Pavone <pavone@retrodev.com>
date Mon, 30 Mar 2015 19:12:51 -0700
parents ead24192ed45
children b8f721bde066
line wrap: on
line source

{
    _visibleWindows <- []
    _needsInit <- true
    _initRes <- 0
    _checkInitSDL <- {
        if: _needsInit {
            _initRes <- (sdl init: ((sdl subsystems) video)) = 0
            _needsInit <- true
        }
        _initRes
    }
	
	_applyProps <- :base properties {
		foreach: (object propertiesOf: base) :_ name {
			if: (object does: properties understand?: name) {
				object setProperty: name on: base to: (object sendMessage: name to: properties)
			}
		}
		base
	}
	#{
        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)
							layout:
                            draw:
                        } none: {
                            false
                        }
                    }
                }
				
				layout <- {
					yPos <- 0
					xPos <- 0
					rowMaxHeight <- 0
					foreach: children :_ child {
						softMax <- (width - xPos)
						child softMaxWidth: softMax maxWidth: width maxHeight: (height - yPos) renderer: _renderer
						if: (child width) > softMax {
							yPos <- yPos + rowMaxHeight
							xPos <- 0
							rowMaxHeight <- 0
						}
						child x!: xPos
						child y!: yPos
						xPos <- xPos + (child width)
						if: (child height) > rowMaxHeight {
							rowMaxHeight <- (child height)
						}
						if: xPos >= width {
							yPos <- yPos + rowMaxHeight
							xPos <- 0
							rowMaxHeight <- 0
						}
					}
				}
				
				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
                }
            }
            _applyProps: base properties
		}
		
		text <- :properties {
			#{
				text <- ""
				//TODO: replace this with font family and style once fontconfig is hooked up
				font <- "/usr/share/fonts/truetype/droid/DroidSans.ttf"
				fontSize <- 12.0
				width <- -1
				height <- -1
				x <- 0
				y <- 0
				
				softMaxWidth:maxWidth:maxHeight <- :softMax :maxWidth :maxHeight {
					
				}
			}
		}
		
		image <- :properties {
			_texture <- option none
			_applyProps: #{
				source <- ""
				width <- -1
				height <- -1
				x <- 0
				y <- 0
				
				softMaxWidth:maxWidth:maxHeight:renderer <- :softMax :maxWidth :maxHeight :_renderer {
					_renderer value: :renderer {
						(sdl loadBMP: source) value: :surface {
							(surface asTexture: renderer) value: :texture {
								_texture <- option value: texture
								width <- texture width
								height <- texture height
								if: (width > maxWidth) {
									width <- maxWidth
								}
								if: (height > maxHeight) {
									height <- maxHeight
								}
							} none: {
								width <- 0
								height <- 0
							}
						} none: {
							print: "Failed to load " . source . "\n"
							//Should this have some kind of placeholder as a fallback?
							width <- 0
							height <- 0
						}
					} none: {
					}
				}
				
				draw <- :_ {
					_texture value: :texture {
						print: "Rendering bitmap to " . x . ", " . y . ", size: " . width . ", " . height . "\n"
						texture copyTo: (sdl rect: x y size: width height)
					} none: {
					}
				}
			} properties
		}
        
        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: {}
            }
        }
	}
}