|
| 1 | +from ./image import *; |
| 2 | + |
| 3 | +class UINode |
| 4 | +{ |
| 5 | + init(self) |
| 6 | + { |
| 7 | + // Position and size to be calculated |
| 8 | + self.x = nil; |
| 9 | + self.y = nil; |
| 10 | + self.width = nil; |
| 11 | + self.height = nil; |
| 12 | + |
| 13 | + // Margin in pixels |
| 14 | + self.margin = 0; |
| 15 | + |
| 16 | + // Background color |
| 17 | + self.bgcolor = nil; |
| 18 | + |
| 19 | + // TODO: border color? |
| 20 | + |
| 21 | + self.children = []; |
| 22 | + } |
| 23 | + |
| 24 | + add_child(self, node) |
| 25 | + { |
| 26 | + self.children.push(node); |
| 27 | + } |
| 28 | + |
| 29 | + compute_size(self) |
| 30 | + { |
| 31 | + for (let var i = 0; i < self.children.len; ++i) |
| 32 | + { |
| 33 | + self.children[i].compute_size(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + compute_pos(self) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + redraw(self, fb) |
| 42 | + { |
| 43 | + assert(self.x != nil && self.y != nil); |
| 44 | + |
| 45 | + if (self.bgcolor != nil) { |
| 46 | + assert(self.bgcolor instanceof Int64); |
| 47 | + fb.fill_rect(self.x, self.y, self.width, self.height, self.bgcolor); |
| 48 | + } |
| 49 | + |
| 50 | + // Draw the children on top of this node |
| 51 | + for (let var i = 0; i < self.children.len; ++i) |
| 52 | + { |
| 53 | + self.children[i].redraw(fb); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Box with a fixed size |
| 59 | +class Box extends UINode |
| 60 | +{ |
| 61 | + init(self, width, height) |
| 62 | + { |
| 63 | + UINode.init(self); |
| 64 | + self.width = width; |
| 65 | + self.height = height; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Row of elements |
| 70 | +class Row extends UINode |
| 71 | +{ |
| 72 | + init(self) |
| 73 | + { |
| 74 | + UINode.init(self); |
| 75 | + } |
| 76 | + |
| 77 | + compute_size(self) |
| 78 | + { |
| 79 | + let var height = 0; |
| 80 | + let var width = 0; |
| 81 | + let var prev_margin = 0; |
| 82 | + |
| 83 | + for (let var i = 0; i < self.children.len; ++i) |
| 84 | + { |
| 85 | + let child = self.children[i]; |
| 86 | + child.compute_size(); |
| 87 | + height = height.max(child.height + 2 * child.margin); |
| 88 | + |
| 89 | + let margin = child.margin.max(prev_margin); |
| 90 | + width += margin + child.width; |
| 91 | + prev_margin = child.margin; |
| 92 | + } |
| 93 | + |
| 94 | + width += prev_margin; |
| 95 | + self.width = width; |
| 96 | + self.height = height; |
| 97 | + } |
| 98 | + |
| 99 | + compute_pos(self) |
| 100 | + { |
| 101 | + assert(self.x instanceof Int64); |
| 102 | + assert(self.y instanceof Int64); |
| 103 | + |
| 104 | + let var prev_margin = 0; |
| 105 | + let var cur_x = self.x; |
| 106 | + |
| 107 | + for (let var i = 0; i < self.children.len; ++i) |
| 108 | + { |
| 109 | + let child = self.children[i]; |
| 110 | + |
| 111 | + let margin = child.margin.max(prev_margin); |
| 112 | + cur_x += margin; |
| 113 | + |
| 114 | + child.x = cur_x; |
| 115 | + child.y = self.y + (self.height - child.height) _/ 2; |
| 116 | + child.compute_pos(); |
| 117 | + |
| 118 | + cur_x += child.width; |
| 119 | + prev_margin = child.margin; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// Node that centers a single child node |
| 125 | +class Center extends UINode |
| 126 | +{ |
| 127 | + init(self, child) |
| 128 | + { |
| 129 | + UINode.init(self); |
| 130 | + self.children.push(child); |
| 131 | + } |
| 132 | + |
| 133 | + compute_pos(self) |
| 134 | + { |
| 135 | + assert(self.x instanceof Int64); |
| 136 | + assert(self.y instanceof Int64); |
| 137 | + assert(self.children.len == 1); |
| 138 | + |
| 139 | + let child = self.children[0]; |
| 140 | + child.x = self.x + (self.width - child.width) _/ 2; |
| 141 | + child.y = self.y + (self.height - child.height) _/ 2; |
| 142 | + child.compute_pos(); |
| 143 | + |
| 144 | + $println(child.x); |
| 145 | + $println(child.y); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +let fb = Image(600, 400); |
| 153 | +let window = $window_create(fb.width, fb.height, "Layout Engine Test", 0); |
| 154 | + |
| 155 | +let row = Row(); |
| 156 | +row.bgcolor = rgb(50, 50, 50); |
| 157 | + |
| 158 | +let center = Center(row); |
| 159 | +center.width = fb.width; |
| 160 | +center.height = fb.height; |
| 161 | +center.x = 0; |
| 162 | +center.y = 0; |
| 163 | +center.bgcolor = COLOR_BLACK; |
| 164 | + |
| 165 | +for (let var i = 0; i < 16; ++i) |
| 166 | +{ |
| 167 | + let box = Box(24, 24); |
| 168 | + box.bgcolor = COLOR_GREY; |
| 169 | + box.margin = 6; |
| 170 | + row.add_child(box); |
| 171 | +} |
| 172 | + |
| 173 | +center.compute_size(); |
| 174 | +center.compute_pos(); |
| 175 | +center.redraw(fb); |
| 176 | +$window_draw_frame(window, fb.bytes); |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +loop |
| 183 | +{ |
| 184 | + let msg = $actor_recv(); |
| 185 | + if (msg instanceof UIEvent) { |
| 186 | + if (msg.kind == 'CLOSE_WINDOW' || (msg.kind == 'KEY_DOWN' && msg.key == 'ESCAPE')) { |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | +} |
0 commit comments