changeset 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 98534108b854
children 2a0463c46913
files modules/ui.tp samples/ui.tp
diffstat 2 files changed, 105 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/modules/ui.tp	Mon Mar 30 19:12:40 2015 -0700
+++ b/modules/ui.tp	Mon Mar 30 19:12:51 2015 -0700
@@ -9,6 +9,15 @@
         }
         _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
@@ -32,6 +41,7 @@
                         _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
@@ -39,6 +49,32 @@
                     }
                 }
 				
+				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 {
@@ -61,12 +97,70 @@
                     _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
+            _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 <- {
--- a/samples/ui.tp	Mon Mar 30 19:12:40 2015 -0700
+++ b/samples/ui.tp	Mon Mar 30 19:12:51 2015 -0700
@@ -3,6 +3,11 @@
 		wind <- ui window: #{
 			title <- "Quiche UI Test"
 			color <- ui r: 192u8 g: 192u8 b: 192u8
+			children <- #[
+				ui image: #{
+					source <- "944.bmp"
+				}
+			]
 		}
 		
 		wind show: