annotate modules/ui.tp @ 364:e44f65abaf0e

Support labels in non-branch instructions. String literals now work and so does the llhello sample
author Michael Pavone <pavone@retrodev.com>
date Sun, 26 Apr 2015 11:16:14 -0700
parents b8f721bde066
children d61b1f0e1936
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
332
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 _visibleWindows <- []
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 _needsInit <- true
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 _initRes <- 0
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 _checkInitSDL <- {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 if: _needsInit {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 _initRes <- (sdl init: ((sdl subsystems) video)) = 0
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 _needsInit <- true
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 _initRes
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 }
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
12
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
13 _applyProps <- :base properties {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
14 foreach: (object propertiesOf: base) :_ name {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
15 if: (object does: properties understand?: name) {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
16 object setProperty: name on: base to: (object sendMessage: name to: properties)
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
17 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
18 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
19 base
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
20 }
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
21 _ft <- option none
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
22 _getFT <- {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
23 _ft value: :ft { ft } none: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
24 ft <- freetype init
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
25 _ft <- option value: ft
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
26 ft
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
27 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
28 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
29
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
30 //TODO: Fix this for multi window
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
31 _atlasDict <- dict hash
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
32 _currentTex <- option none
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
33 _texX <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
34 _texY <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
35 _texRowHeight <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
36 _getFontAtlas <- :path _size renderer {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
37 sizes <- _atlasDict get: path else: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
38 new <- dict hash
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
39 _atlasDict set: path new
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
40 new
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
41 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
42 _getTexture <- :width height {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
43 _currentTex value: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
44 if: _texX + width > 2048 {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
45 _texY <- _texY + _texRowHeight
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
46 _texRowHeight <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
47 _texX <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
48 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
49 if: _texY + height > 2048 {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
50 _currentTex <- renderer createTexture: ((sdl pixelFormats) bgra8888) access: ((sdl textureAccess) streaming) width: 2048 height: 2048
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
51 _currentTex value: :tex {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
52 tex blendMode!: ((sdl blendModes) blend)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
53 } none: {}
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
54 _texX <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
55 _texY <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
56 _texRowHeight <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
57 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
58 if: _texRowHeight < height {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
59 _texRowHeight <- height
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
60 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
61 } none: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
62 _currentTex <- renderer createTexture: ((sdl pixelFormats) bgra8888) access: ((sdl textureAccess) streaming) width: 2048 height: 2048
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
63 _currentTex value: :tex {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
64 tex blendMode!: ((sdl blendModes) blend)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
65 } none: {}
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
66 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
67 _currentTex
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
68 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
69 atlas <- sizes get: _size else: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
70 //TODO: Only load face once for all sizes
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
71 a <- ((_getFT: ) faceFromPath: path index: 0) value: :_face {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
72 _textures <- #[]
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
73
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
74 _charmap <- _face charmap
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
75 //TODO: Feed in window system DPI or at least make this configurable
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
76 dpi <- 96
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
77 _face setCharSize: _size res: dpi
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
78 _pixelFactor <- ((_face unitsPerEm) f64) * 72.0 / (_size * (dpi f64))
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
79 _emUnits <- _face unitsPerEm
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
80 _glyphs <- dict hash
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
81 _getGlyph <- :codePoint face {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
82 _glyphs get: codePoint else: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
83 flags <- freetype loadFlags
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
84 _charmap ifget: codePoint :_glyphIndex {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
85 slot <- face glyphSlot
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
86 face loadGlyph: _glyphIndex flags: ((flags render) or (flags linearDesign) or (flags noHinting))
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
87 w <- slot bitmapWidth
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
88 h <- slot bitmapRows
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
89 _advX <- slot linearHoriAdvance
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
90 _advY <- slot linearVertAdvance
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
91 _texture <- _getTexture: w h
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
92 _rect <- sdl rect: _texX _texY size: w h
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
93 _texture value: :tex {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
94 srcPitch <- slot bitmapPitch
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
95 srcBuffer <- slot bitmapData
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
96 tex lockRect: (sdl rect: _texX _texY size: w h ) with: :dstBuffer dstPitch {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
97 srcY <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
98 dstY <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
99 while: { srcY < h } do: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
100 srcX <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
101 srcIdx <- srcY * srcPitch
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
102 dstIdx <- dstY * dstPitch
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
103 while: { srcX < w } do: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
104 //Set destination pixel to white with the source pixel as alpha
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
105 //This allows text color to be set using colorMod! on the texture
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
106 dstBuffer set: dstIdx (srcBuffer get: srcIdx)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
107 dstBuffer set: (dstIdx + 1) 255u8
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
108 dstBuffer set: (dstIdx + 2) 255u8
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
109 dstBuffer set: (dstIdx + 3) 255u8
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
110
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
111 srcX <- srcX + 1
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
112 srcIdx <- srcIdx + 1
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
113 dstIdx <- dstIdx + 4
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
114 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
115
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
116 srcY <- srcY + 1
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
117 dstY <- dstY + 1
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
118 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
119 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
120 } none: {}
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
121 _texX <- _texX + w
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
122 _leftOff <- slot bitmapLeft
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
123 _topOff <- slot bitmapTop
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
124 g <- #{
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
125 advanceX <- { _advX }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
126 advanceY <- { _advY }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
127 atlasRect <- { _rect }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
128
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
129 glyphIndex <- { _glyphIndex }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
130 drawAt:color <- :x y :color {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
131 _texture value: :tex {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
132 tex colorMod!: color
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
133 tex copyRect: _rect To: (sdl rect: x+_leftOff y-_topOff size: (_rect w) (_rect h))
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
134 } none: {}
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
135 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
136 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
137 _glyphs set: codePoint g
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
138 g
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
139 } else: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
140 //get fallback
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
141 _getGlyph: 0u32 face
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
142 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
143 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
144 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
145
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
146 _getGlyphs <- :str face {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
147 i <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
148 lines <- #[]
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
149 curline <- #[]
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
150 nl <- ("\n" byte: 0) uint32
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
151 while: { i < (str byte_length) } do: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
152 //TODO: Unicode
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
153 codePoint <- (str byte: i) uint32
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
154 if: codePoint = nl {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
155 lines append: curline
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
156 curline <- #[]
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
157 } else: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
158 glyph <- _getGlyph: codePoint face
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
159 curline append: glyph
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
160 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
161 i <- i + 1
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
162 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
163 lines append: curline
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
164 lines
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
165 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
166 _iterateGlyphs:leading:using <- :str :leading :fun {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
167 lines <- _getGlyphs: str _face
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
168 y <- 0.0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
169 maxX <- 0.0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
170 print: "Leading: " . leading . ", EM units: " . _emUnits . "\n"
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
171 leading <- leading * (_emUnits f64) / _size
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
172 foreach: lines :_ glyphs {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
173 x <- 0.0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
174 baseline <- y + (_emUnits f64)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
175 foreach: glyphs :_ glyph {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
176 fun: glyph x baseline
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
177 x <- x + ((glyph advanceX) f64)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
178 //baseline <- baseline + ((glyph advanceY) f64)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
179 //TODO: kerning
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
180 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
181 y <- y + leading
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
182 if: x > maxX {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
183 maxX <- x
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
184 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
185 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
186 //convert font units into pixels
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
187 x <- (maxX / _pixelFactor + 0.5) truncInt32
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
188 y <- (y / _pixelFactor + 0.5) truncInt32
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
189 vec x: x y: y
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
190 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
191 option value: #{
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
192 stringSize:leading <- :str :leading {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
193 _iterateGlyphs: str leading: leading using: :glyph x y {}
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
194 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
195
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
196 drawString:at:color:leading <- :str :xpos ypos :color :lead {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
197 _iterateGlyphs: str leading: lead using: :glyph x y {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
198 x <- ((x / _pixelFactor + 0.5) truncInt32) + xpos
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
199 y <- ((y / _pixelFactor + 0.5) truncInt32) + ypos
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
200 glyph drawAt: x y color: color
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
201 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
202 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
203 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
204 } none: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
205 option none
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
206 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
207 sizes set: _size a
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
208 a
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
209 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
210 }
332
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
211 #{
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
212 import: [
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
213 r:g:b
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
214 r:g:b:a
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
215 ] from: sdl
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
216 _styles <- []
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
217 window <- :properties {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
218 _wind <- option none
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
219 _renderer <- option none
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
220
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
221
332
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
222 base <- #{
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
223 title <- "Window"
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
224 width <- 640
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
225 height <- 480
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
226 x <- 0
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
227 y <- 0
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
228 color <- (ui r: 255u8 g: 255u8 b: 255u8)
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
229 children <- #[]
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
230
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
231 show <- {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
232 if: (_checkInitSDL: ) {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
233 _wind <- sdl createWindow: title pos: x y size: width height flags: 0u32
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
234 _wind value: :window {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
235 _renderer <- window createRenderer: -1 flags: ((window renderOpts) accelerated)
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
236 layout:
332
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
237 draw:
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
238 } none: {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
239 false
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
240 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
241 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
242 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
243
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
244 layout <- {
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
245 _renderer value: :renderer {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
246 yPos <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
247 xPos <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
248 rowMaxHeight <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
249 foreach: children :_ child {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
250 softMax <- (width - xPos)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
251 child softMaxWidth: softMax maxWidth: width maxHeight: (height - yPos) renderer: renderer
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
252 if: (child width) > softMax {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
253 yPos <- yPos + rowMaxHeight
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
254 xPos <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
255 rowMaxHeight <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
256 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
257 child x!: xPos
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
258 child y!: yPos
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
259 xPos <- xPos + (child width)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
260 if: (child height) > rowMaxHeight {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
261 rowMaxHeight <- (child height)
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
262 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
263 if: xPos >= width {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
264 yPos <- yPos + rowMaxHeight
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
265 xPos <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
266 rowMaxHeight <- 0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
267 }
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
268 }
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
269 } none: {}
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
270 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
271
332
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
272 draw <- {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
273 print: "Draw!\n"
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
274 _renderer value: :renderer {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
275 print: "Rendering!\n"
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
276 renderer drawColor!: color
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
277 renderer clear
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
278
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
279 foreach: children :_ child {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
280 child draw: renderer
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
281 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
282 renderer present
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
283 true
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
284 } none: { false }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
285 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
286
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
287 styles <- { _styles }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
288
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
289 styles! <- :newstyles{
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
290 //TODO: apply styles
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
291 _styles <- newstyles
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
292 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
293 }
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
294 _applyProps: base properties
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
295 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
296
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
297 text <- :properties {
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
298 _fontSize <- 12.0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
299 _leading <- 15.0
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
300 _explicitLeading <- false
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
301 _applyProps: #{
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
302 text <- ""
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
303 //TODO: replace this with font family and style once fontconfig is hooked up
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
304 font <- "/usr/share/fonts/truetype/droid/DroidSans.ttf"
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
305 fontSize <- { _fontSize }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
306 fontSize! <- :newSize {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
307 _fontSize <- newSize
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
308 if: (not: _explicitLeading) {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
309 _leading <- newSize * 1.25
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
310 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
311 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
312 leading <- { _leading }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
313 leading! <- :newLeading {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
314 _explicitLeading <- true
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
315 _leading <- newLeading
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
316 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
317 color <- (ui r: 0u8 g: 0u8 b: 0u8)
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
318 width <- -1
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
319 height <- -1
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
320 x <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
321 y <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
322
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
323 softMaxWidth:maxWidth:maxHeight:renderer <- :softMax :maxWidth :maxHeight :renderer {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
324 (_getFontAtlas: font fontSize renderer) value: :atlas {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
325 //TODO: word wrap
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
326 bbox <- atlas stringSize: text leading: _leading
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
327 width <- bbox x
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
328 height <- bbox y
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
329 print: "Text: " . text . " has size: " . width . ", " . height . "\n"
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
330 } none: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
331 }
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
332 }
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
333
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
334 draw <- :renderer {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
335 (_getFontAtlas: font fontSize renderer) value: :atlas {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
336 print: "Drawing: " . text . " at " . x . ", " . y . "\n"
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
337 atlas drawString: text at: x y color: color leading: _leading
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
338 } none: {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
339 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
340 }
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
341 } properties
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
342 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
343
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
344 image <- :properties {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
345 _texture <- option none
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
346 _applyProps: #{
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
347 source <- ""
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
348 width <- -1
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
349 height <- -1
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
350 x <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
351 y <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
352
337
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
353 softMaxWidth:maxWidth:maxHeight:renderer <- :softMax :maxWidth :maxHeight :renderer {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
354 _texture value: :_ {
b8f721bde066 Add support for text elements in ui module
Michael Pavone <pavone@retrodev.com>
parents: 335
diff changeset
355 } none: {
335
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
356 (sdl loadBMP: source) value: :surface {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
357 (surface asTexture: renderer) value: :texture {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
358 _texture <- option value: texture
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
359 width <- texture width
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
360 height <- texture height
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
361 if: (width > maxWidth) {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
362 width <- maxWidth
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
363 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
364 if: (height > maxHeight) {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
365 height <- maxHeight
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
366 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
367 } none: {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
368 width <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
369 height <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
370 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
371 } none: {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
372 print: "Failed to load " . source . "\n"
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
373 //Should this have some kind of placeholder as a fallback?
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
374 width <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
375 height <- 0
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
376 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
377 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
378 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
379
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
380 draw <- :_ {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
381 _texture value: :texture {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
382 print: "Rendering bitmap to " . x . ", " . y . ", size: " . width . ", " . height . "\n"
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
383 texture copyTo: (sdl rect: x y size: width height)
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
384 } none: {
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
385 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
386 }
79a14e41b79a Add image element and placeholder text element to ui module
Michael Pavone <pavone@retrodev.com>
parents: 332
diff changeset
387 } properties
332
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
388 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
389
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
390 enterEventLoop <- {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
391 continue? <- true
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
392
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
393 _handlers <- dict hash
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
394 _handlers set: ((sdl eventTypes) quit) :event {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
395 continue? <- false
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
396 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
397 _handlers set: ((sdl eventTypes) window) :event {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
398 if: (event event) = ((sdl windowEventTypes) exposed) {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
399 foreach: _visibleWindows :_ wind {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
400 print: "Redrawing window\n"
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
401 wind draw
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
402 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
403 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
404 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
405 while: { continue? } do: {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
406 (sdl waitEvent) value: :event {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
407 _handlers ifget: (event type) :handler {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
408 handler: event
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
409 } else: {
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
410 print: "Unhandled event type: " . (event type) . "\n"
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
411 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
412 } none: {}
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
413 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
414 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
415 }
ead24192ed45 Initial work on a UI module
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
416 }