forked from ceu-lang/ceu-sdl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui-grid.ceu
More file actions
370 lines (318 loc) · 11 KB
/
ui-grid.ceu
File metadata and controls
370 lines (318 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifndef _UI_GRID_CEU
#define _UI_GRID_CEU
#define UI_GRID_MAX_LINS 20
#define UI_GRID_MAX_COLS 20
#define UI_GRID_MAX_UIS 400
#include "c.ceu"
#include "sdl.ceu"
#include "sdl-gfx.ceu"
#include "ui.ceu"
input void SDL_REDRAW;
input _SDL_MouseButtonEvent* SDL_MOUSEBUTTONUP;
// TODO:
// * go_pause
// * remaining space from division
class UIGridItem with
var UI* ui;
var int idx;
do
watching ui do
await FOREVER;
end
end
class UIGrid with
interface UI;
var _SDL_Renderer& ren;
var bool should_redim = true;
var SDL_Color? bg_clr;
var int align_x = _UI_ALIGN_LEFT;
var int align_y = _UI_ALIGN_TOP;
var int align_bg_x = _UI_ALIGN_CENTER;
var int align_bg_y = _UI_ALIGN_MIDDLE;
event int ok_uiclicked;
// TODO: clr_bg vs bg_clr
var SDL_Color? clr_bg ; // whole UI background
var SDL_Color? clr_fr ; // whole UI frame
var SDL_Color? clr_cell_bg ; // each cell background
var SDL_Color? clr_cell_fr ; // each cell frame
var SDL_Color? clr_ui_bg ; // each UI background (if existent)
var SDL_Color? clr_ui_fr ; // each UI frame (if existent)
//var int one_x=0, one_y=0;
var int spc_x=0, spc_y=0;
var int pad_x=0, pad_y=0;
var int lay_lins, lay_cols;
var int& lay;
// TODO: bg after pool
pool UIGridItem[] uis;
var int uis_n = 0;
do
var SDL_Rect&? cell_rects;
finalize
this.cell_rects = _malloc(uis_n * sizeof(SDL_Rect));
with
_free(&this.cell_rects);
end
var SDL_Rect&? ui_rects;
finalize
this.ui_rects = _malloc(uis_n * sizeof(SDL_Rect));
with
_free(&this.ui_rects);
end
/* METHODS */
function @rec (SDL_Rect* r)=>void go do
if r != null then
this.rect.x = _UI_align(r:x, r:w, this.align_x);
this.rect.y = _UI_align(r:y, r:h, this.align_y);
if this.should_redim then
this.rect.w = r:w;
this.rect.h = r:h;
end
end
// TODO: save all cur in a vector and do this loop once, after
// calculating them all
loop item in this.uis do
if item:idx == -1 then
call/rec item:ui:go_bg(&this.rect, true); // -1 = BG item
end
end
var int x0 = this.rect.x;
var int y0 = this.rect.y;
var int one_x = (this.rect.w-this.spc_x)/this.lay_cols
- this.spc_x - 2*this.pad_x;
var int one_y = (this.rect.h-this.spc_y)/this.lay_lins
- this.spc_y - 2*this.pad_y;
var int cur = -1;
var int x, y;
loop/UI_GRID_MAX_LINS l in this.lay_lins do
x = x0 + this.spc_x; // starts from spc_x
if (l == 0) then
y = y0 + this.spc_y; // starts from spc_y
else
y = y + this.pad_y + one_y + this.pad_y + this.spc_y;
end
loop/UI_GRID_MAX_COLS c in this.lay_cols do
if (c != 0) then
x = x + this.pad_x + one_x
+ this.pad_x + this.spc_x;
end
var int i1 = l*this.lay_cols + c;
var int lay1 = (&this.lay)[i1];
if (lay1 > cur) then // always increasing items
cur = lay1;
_assert(cur < this.uis_n);
var SDL_Rect* cell_rect = (SDL_Rect*)&(&this.cell_rects)[cur];
cell_rect:x = x;
cell_rect:y = y;
cell_rect:w = one_x + 2*this.pad_x;
cell_rect:h = one_y + 2*this.pad_y;
var SDL_Rect* ui_rect = (SDL_Rect*)&(&this.ui_rects)[cur];
ui_rect:x = x+this.pad_x;
ui_rect:y = y+this.pad_y;
ui_rect:w = one_x;
ui_rect:h = one_y;
// colspan
loop/UI_GRID_MAX_COLS j in this.lay_cols-c-1 do
var int lay2 = (&this.lay)[i1 + j+1];
if (lay1 == lay2) then
cell_rect:w = cell_rect:w + this.spc_x
+ one_x + 2*this.pad_x;
ui_rect:w = ui_rect:w + this.spc_x
+ one_x + 2*this.pad_x;
else
break;
end
end
// linspan
loop/UI_GRID_MAX_LINS j in this.lay_lins-l-1 do
var int lay2 = (&this.lay)[i1 + (j+1)*this.lay_cols];
if (lay1 == lay2) then
cell_rect:h = cell_rect:h + this.spc_y +
one_y + 2*this.pad_y;
ui_rect:h = ui_rect:h + this.spc_y +
one_y + 2*this.pad_y;
else
break;
end
end
// TODO: save all cur in a vector and do this loop once,
// after calculating them all
loop item in this.uis do
if item:idx == cur then
call/rec item:ui:go_bg(ui_rect, true);
end
end
end
end
end
end
function @rec (SDL_Rect* bg, bool should_redim_)=>void go_bg do
//ui_go_bg(this, bg);
_assert(bg != null);
if should_redim_ then
this.rect.w = bg:w;
this.rect.h = bg:h;
end
this.rect.x = _UI_align_bg(bg:x, bg:w, this.rect.w, this.align_bg_x);
this.rect.y = _UI_align_bg(bg:y, bg:h, this.rect.h, this.align_bg_y);
call/rec this.go(null);
end
/* BODY */
par do
// OK_UICLICKED
// OK_CLICKED
loop do
var _SDL_MouseButtonEvent* but = await SDL_MOUSEBUTTONUP;
loop/UI_GRID_MAX_UIS i in uis_n do
if _SDL_Rect_vs_Mouse((_SDL_Rect*)&(&ui_rects)[i], but) then
emit ok_uiclicked => i;
end
end
if _SDL_Rect_vs_Mouse((_SDL_Rect*)&this.rect, but) then
emit ok_clicked;
end
end
with
every SDL_REDRAW do
// TODO: clr_bg vs bg_clr
if bg_clr? then
_SDL_SetRenderDrawColor(&ren,
bg_clr.r,bg_clr.g,bg_clr.b,bg_clr.a);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&this.rect);
end
if clr_bg? then
_SDL_SetRenderDrawColor(&ren,
clr_bg.r,clr_bg.g,clr_bg.b,clr_bg.a);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&rect);
end
loop/UI_GRID_MAX_UIS i in uis_n do
if clr_cell_bg? then
_SDL_SetRenderDrawColor(&ren,
clr_cell_bg.r,clr_cell_bg.g,clr_cell_bg.b,clr_cell_bg.a);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&(&cell_rects)[i]);
end
if clr_ui_bg? then
_SDL_SetRenderDrawColor(&ren,
clr_ui_bg.r,clr_ui_bg.g,clr_ui_bg.b,clr_ui_bg.a);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&(&ui_rects)[i]);
end
if clr_cell_fr? then
_SDL_SetRenderDrawColor(&ren,
clr_cell_fr.r,clr_cell_fr.g,clr_cell_fr.b,clr_cell_fr.a);
_SDL_RenderDrawRect(&ren, (_SDL_Rect*)&(&cell_rects)[i]);
end
if clr_ui_fr? then
_SDL_SetRenderDrawColor(&ren,
clr_ui_fr.r,clr_ui_fr.g,clr_ui_fr.b,clr_ui_fr.a);
_SDL_RenderDrawRect(&ren, (_SDL_Rect*)&(&ui_rects)[i]);
end
end
if clr_fr? then
_SDL_SetRenderDrawColor(&ren,
clr_fr.r,clr_fr.g,clr_fr.b,clr_fr.a);
_SDL_RenderDrawRect(&ren, (_SDL_Rect*)&rect);
end
end
end
end
#ifdef __UI_GRID_CEU
#include "sdl-colors.ceu"
input void SDL_QUIT;
var _SDL_Window&? win;
finalize
win = _SDL_CreateWindow("UI - Grid",
500, 1300, 800, 480, _SDL_WINDOW_SHOWN);
with
_SDL_DestroyWindow(win);
end
var int win_w;
var int win_h;
_SDL_GetWindowSize(&win, &win_w, &win_h);
var _SDL_Renderer&? ren;
finalize
ren = _SDL_CreateRenderer(&win, -1, 0);
with
_SDL_DestroyRenderer(&ren);
end
native _lay1, _lay2;
native do
int lay1[16] = {
0, -1, -1, -1,
-1, 1, 1, -1,
-1, 1, 1, -1,
-1, -1, -1, 2
};
int lay2[3] = {
0, 1, 2
};
end
par/or do
await SDL_QUIT;
with
var UIGrid g1 with
this.ren = ren;
this.spc_x = 10;
this.spc_y = 10;
this.lay_lins = 4;
this.lay_cols = 4;
this.lay = _lay1;
this.uis_n = 3;
this.clr_bg = SDL_COLOR_GRAY;
this.clr_fr = SDL_COLOR_RED;
this.clr_cell_bg = SDL_COLOR_WHITE;
this.clr_cell_fr = SDL_COLOR_BLACK;
end;
var UIGrid g2 with
this.ren = ren;
this.pad_x = 5;
this.pad_y = 5;
this.lay_lins = 1;
this.lay_cols = 3;
this.lay = _lay2;
this.uis_n = 3;
this.clr_cell_fr = SDL_COLOR_BLACK;
this.clr_ui_bg = SDL_COLOR_YELLOW;
this.clr_ui_fr = SDL_COLOR_RED;
end;
var SDL_Rect r = SDL_Rect(win_w/4, win_h/4,
win_w/2, win_h/2);
spawn UIGridItem in g1.uis with
this.ui = &g2;
this.idx = 1;
end;
call/rec g1.go_bg(&r, true);
par do
loop do
var int opt = await g1.ok_uiclicked;
_fprintf(_stderr, "G1: %d\n", opt);
end
with
loop do
var int opt = await g2.ok_uiclicked;
_fprintf(_stderr, "G2: %d\n", opt);
end
with
native _rand();
every 100ms do
r.w = r.w + _rand() % 3 - 1;
if r.w < 100 then r.w = 100; end
r.h = r.h + _rand() % 3 - 1;
if r.h < 100 then r.h = 100; end
g1.spc_x = g1.spc_x + _rand() % 3 - 1;
if g1.spc_x < 0 then g1.spc_x = 0; end
g1.spc_y = g1.spc_y + _rand() % 3 - 1;
if g1.spc_y < 0 then g1.spc_y = 0; end
g2.pad_x = g2.pad_x + _rand() % 3 - 1;
if g2.pad_x < 0 then g2.pad_x = 0; end
g2.pad_y = g2.pad_y + _rand() % 3 - 1;
if g2.pad_y < 0 then g2.pad_y = 0; end
call/rec g1.go_bg(&r, true);
end
end
with
every SDL_REDRAW do
_SDL_RenderPresent(&ren);
end
end
escape 0;
#endif
#endif