forked from ceu-lang/ceu-sdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui-scroll.ceu
280 lines (242 loc) · 7.86 KB
/
ui-scroll.ceu
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
#ifndef _UI_SCROLL_CEU
#define _UI_SCROLL_CEU
#include "sdl.ceu"
#include "ui.ceu"
input void SDL_REDRAW;
input _SDL_MouseButtonEvent* SDL_MOUSEBUTTONDOWN;
class UIScroll with
interface UI;
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;
var UI& ui;
var SDL_Color& clr_scroll;
do
/* METHODS */
function @rec (SDL_Rect* r)=>void go do
if r != null then
this.rect.w = r:w;
this.rect.h = r:h;
this.rect.x = _UI_align(r:x, rect.w, this.align_x);
this.rect.y = _UI_align(r:y, rect.h, this.align_y);
end
call/rec this.ui.go(r);
end
function @rec (SDL_Rect* bg, bool should_redim_)=>void go_bg do
_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 */
event void go_x, go_y;
var bool should_go_x=false, should_go_y=false;
par do
loop do
var _SDL_MouseButtonEvent* but = await SDL_MOUSEBUTTONDOWN;
var SDL_Point pt1 = SDL_Point(but:x,but:y);
var SDL_Point pt2;
every 50ms do
var int s = _SDL_GetMouseState(&pt2.x, &pt2.y);
if not s then
break;
end
pt1.x = pt2.x - pt1.x;
pt1.y = pt2.y - pt1.y;
// LIMITS
if ui.rect.w > rect.w then
if pt1.x > 0 then
var int max_x = rect.x;
var int max_dx = max_x - ui.rect.x;
if pt1.x > max_dx then
pt1.x = max_dx;
end
else/if pt1.x < 0 then
var int min_x = rect.x - (ui.rect.w-rect.w);
var int min_dx = min_x - ui.rect.x;
if pt1.x < min_dx then
pt1.x = min_dx;
end
end
emit go_x;
else
pt1.x = 0;
end
if ui.rect.h > rect.h then
if pt1.y > 0 then
var int max_y = rect.y;
var int max_dy = max_y - ui.rect.y;
if pt1.y > max_dy then
pt1.y = max_dy;
end
else/if pt1.y < 0 then
var int min_y = rect.y - (ui.rect.h-rect.h);
var int min_dy = min_y - ui.rect.y;
if pt1.y < min_dy then
pt1.y = min_dy;
end
end
emit go_y;
else
pt1.y = 0;
end
if pt1.x!=0 or pt1.y!=0 then
ui.rect.x = ui.rect.x + pt1.x;
ui.rect.y = ui.rect.y + pt1.y;
call/rec ui.go(null);
end
//ui.rect.x = ui.rect.x + pt1.x;
//ui.rect.y = ui.rect.y + pt1.y;
pt1 = pt2;
end
end
with
par do
await go_x;
loop do
should_go_x = true;
par/or do
await 1s;
should_go_x = false;
await FOREVER;
with
await go_x;
end
end
with
await go_y;
loop do
should_go_y = true;
par/or do
await 1s;
should_go_y = false;
await FOREVER;
with
await go_y;
end
end
end
with
loop do
await SDL_REDRAW;
/*
// TODO!!!
_glEnable(_GL_SCISSOR_TEST);
_glScissor(rect.x,global:win_rect.h-rect.y-rect.h,rect.w,rect.h);
emit ui:go_redraw;
_glDisable(_GL_SCISSOR_TEST);
*/
if should_go_x then
var SDL_Rect rs = SDL_Rect(rect.x + (rect.x-ui.rect.x) * rect.w / ui.rect.w,
rect.y + rect.h - 6,
rect.w*rect.w / ui.rect.w,
5);
_SDL_SetRenderDrawBlendMode(&ren, _SDL_BLENDMODE_BLEND);
_SDL_SetRenderDrawColor(&ren, clr_scroll.r,
clr_scroll.g,
clr_scroll.b,
clr_scroll.a);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&rs);
end
if should_go_y then
var SDL_Rect rs = SDL_Rect(rect.x + rect.w - 6,
rect.y + (rect.y-ui.rect.y) * rect.h / ui.rect.h,
5,
rect.h*rect.h / ui.rect.h);
_SDL_SetRenderDrawBlendMode(&ren, _SDL_BLENDMODE_BLEND);
_SDL_SetRenderDrawColor(&ren, clr_scroll.r,
clr_scroll.g,
clr_scroll.b,
clr_scroll.a);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&rs);
end
_SDL_SetRenderDrawBlendMode(&ren, _SDL_BLENDMODE_NONE);
end
end
end
#ifdef __UI_SCROLL_CEU
#include "sdl-colors.ceu"
#include "ui-grid.ceu"
input void SDL_QUIT;
var int win_w;
var int win_h;
var _SDL_Window&? win;
finalize
win = _SDL_CreateWindow("UI - Scroll",
500, 1300, 400, 200, _SDL_WINDOW_SHOWN);
with
_SDL_DestroyWindow(win);
end
_SDL_GetWindowSize(&win, &win_w, &win_h);
var SDL_Rect win_r = SDL_Rect(0,0, win_w,win_h);
var _SDL_Renderer&? ren;
finalize
ren = _SDL_CreateRenderer(&win, -1, 0);
with
_SDL_DestroyRenderer(&ren);
end
par/or do
await SDL_QUIT;
with
every SDL_REDRAW do
_SDL_SetRenderDrawColor(&ren, 0x00,0x00,0x00, 0);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&win_r);
end
with
var UIGrid grid with
this.ren = ren;
this.lay_lins = 4;
this.lay_cols = 4;
this.lay = _UI_SCROLL_grid;
this.uis_n = 16;
native do
int UI_SCROLL_grid[] = {
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15
};
end
this.spc_x = 10;
this.spc_y = 10;
this.pad_x = 10;
this.pad_y = 10;
this.clr_bg = SDL_COLOR_WHITE;
this.clr_ui_bg = SDL_COLOR_GRAY;
end;
var SDL_Rect r = SDL_Rect(0,0, 2*win_r.w,2*win_r.h);
call/rec grid.go(&r);
grid.should_redim = false;
par do
loop do
var int opt = await grid.ok_uiclicked;
_fprintf(_stderr, "G: %d\n", opt);
end
with
await 5s;
_printf("oi\n");
var SDL_Color c = SDL_COLOR_BLUE;
c.a = 0x77;
var UIScroll scroll with
this.ren = ren;
this.ui = grid;
this.clr_scroll = c;
end;
call/rec scroll.go(&win_r);
await FOREVER;
end
with
every SDL_REDRAW do
_SDL_RenderPresent(&ren);
end
end
escape 0;
#endif
#endif