comparison 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
comparison
equal deleted inserted replaced
334:98534108b854 335:79a14e41b79a
7 _initRes <- (sdl init: ((sdl subsystems) video)) = 0 7 _initRes <- (sdl init: ((sdl subsystems) video)) = 0
8 _needsInit <- true 8 _needsInit <- true
9 } 9 }
10 _initRes 10 _initRes
11 } 11 }
12
13 _applyProps <- :base properties {
14 foreach: (object propertiesOf: base) :_ name {
15 if: (object does: properties understand?: name) {
16 object setProperty: name on: base to: (object sendMessage: name to: properties)
17 }
18 }
19 base
20 }
12 #{ 21 #{
13 import: [ 22 import: [
14 r:g:b 23 r:g:b
15 r:g:b:a 24 r:g:b:a
16 ] from: sdl 25 ] from: sdl
30 show <- { 39 show <- {
31 if: (_checkInitSDL: ) { 40 if: (_checkInitSDL: ) {
32 _wind <- sdl createWindow: title pos: x y size: width height flags: 0u32 41 _wind <- sdl createWindow: title pos: x y size: width height flags: 0u32
33 _wind value: :window { 42 _wind value: :window {
34 _renderer <- window createRenderer: -1 flags: ((window renderOpts) accelerated) 43 _renderer <- window createRenderer: -1 flags: ((window renderOpts) accelerated)
44 layout:
35 draw: 45 draw:
36 } none: { 46 } none: {
37 false 47 false
38 } 48 }
39 } 49 }
40 } 50 }
51
52 layout <- {
53 yPos <- 0
54 xPos <- 0
55 rowMaxHeight <- 0
56 foreach: children :_ child {
57 softMax <- (width - xPos)
58 child softMaxWidth: softMax maxWidth: width maxHeight: (height - yPos) renderer: _renderer
59 if: (child width) > softMax {
60 yPos <- yPos + rowMaxHeight
61 xPos <- 0
62 rowMaxHeight <- 0
63 }
64 child x!: xPos
65 child y!: yPos
66 xPos <- xPos + (child width)
67 if: (child height) > rowMaxHeight {
68 rowMaxHeight <- (child height)
69 }
70 if: xPos >= width {
71 yPos <- yPos + rowMaxHeight
72 xPos <- 0
73 rowMaxHeight <- 0
74 }
75 }
76 }
41 77
42 draw <- { 78 draw <- {
43 print: "Draw!\n" 79 print: "Draw!\n"
44 _renderer value: :renderer { 80 _renderer value: :renderer {
45 print: "Rendering!\n" 81 print: "Rendering!\n"
59 styles! <- :newstyles{ 95 styles! <- :newstyles{
60 //TODO: apply styles 96 //TODO: apply styles
61 _styles <- newstyles 97 _styles <- newstyles
62 } 98 }
63 } 99 }
64 foreach: (object propertiesOf: base) :_ name { 100 _applyProps: base properties
65 if: (object does: properties understand?: name) { 101 }
66 object setProperty: name on: base to: (object sendMessage: name to: properties) 102
67 } 103 text <- :properties {
68 } 104 #{
69 base 105 text <- ""
106 //TODO: replace this with font family and style once fontconfig is hooked up
107 font <- "/usr/share/fonts/truetype/droid/DroidSans.ttf"
108 fontSize <- 12.0
109 width <- -1
110 height <- -1
111 x <- 0
112 y <- 0
113
114 softMaxWidth:maxWidth:maxHeight <- :softMax :maxWidth :maxHeight {
115
116 }
117 }
118 }
119
120 image <- :properties {
121 _texture <- option none
122 _applyProps: #{
123 source <- ""
124 width <- -1
125 height <- -1
126 x <- 0
127 y <- 0
128
129 softMaxWidth:maxWidth:maxHeight:renderer <- :softMax :maxWidth :maxHeight :_renderer {
130 _renderer value: :renderer {
131 (sdl loadBMP: source) value: :surface {
132 (surface asTexture: renderer) value: :texture {
133 _texture <- option value: texture
134 width <- texture width
135 height <- texture height
136 if: (width > maxWidth) {
137 width <- maxWidth
138 }
139 if: (height > maxHeight) {
140 height <- maxHeight
141 }
142 } none: {
143 width <- 0
144 height <- 0
145 }
146 } none: {
147 print: "Failed to load " . source . "\n"
148 //Should this have some kind of placeholder as a fallback?
149 width <- 0
150 height <- 0
151 }
152 } none: {
153 }
154 }
155
156 draw <- :_ {
157 _texture value: :texture {
158 print: "Rendering bitmap to " . x . ", " . y . ", size: " . width . ", " . height . "\n"
159 texture copyTo: (sdl rect: x y size: width height)
160 } none: {
161 }
162 }
163 } properties
70 } 164 }
71 165
72 enterEventLoop <- { 166 enterEventLoop <- {
73 continue? <- true 167 continue? <- true
74 168