forked from ceu-lang/ceu-sdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui-texture.ceu
307 lines (262 loc) · 8 KB
/
ui-texture.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
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
#ifndef _UI_TEXTURE_CEU
#define _UI_TEXTURE_CEU
#include "sdl.ceu"
#include "sdl-gfx.ceu"
#include "ui.ceu"
input void SDL_REDRAW;
input _SDL_MouseButtonEvent* SDL_MOUSEBUTTONUP;
class UITexture with
interface UI;
var _SDL_Renderer& ren;
var bool should_redim = false;
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 float rect_mul = 1; // for ok_clicked
var _SDL_Texture& tex;
var int w, h; // original dimensions
var int pad_x = 0;
var int pad_y = 0;
function (SDL_Rect* r)=>void go;
function (SDL_Rect* bg, bool should_redim_)=>void go_bg;
do
/* METHODS */
function (SDL_Rect* r)=>void go do
_assert(r != null);
_assert(not this.should_redim);
// ignoring r:w / r:h (no redim)
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
function @rec (SDL_Rect* bg, bool should_redim_)=>void go_bg do
//ui_go_bg(this, bg);
_assert(bg != null);
//_assert(not should_redim?);
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);
end
/* BODY */
_SDL_QueryTexture(&tex, null, null, &this.w, &this.h);
this.rect.w = this.w;
this.rect.h = this.h;
par do
// OK_CLICKED
loop do
var _SDL_MouseButtonEvent* but = await SDL_MOUSEBUTTONUP;
var SDL_Rect r = this.rect;
if this.rect_mul != 1 then
var int w = rect.w * this.rect_mul;
var int h = rect.h * this.rect_mul;
r = SDL_Rect(rect.x - (r.w-w)/2,
rect.y - (r.h-h)/2,
w, h);
end
if _SDL_Rect_vs_Mouse((_SDL_Rect*)&r, but) then
emit ok_clicked;
end
end
with
every SDL_REDRAW do
if bg_clr? then
/*
_SDL_SetRenderDrawColor(ren,
bg_clr:r,bg_clr:g,bg_clr:b,bg_clr:a);
_SDL_RenderFillRect(ren, &this.bg_rect);
*/
_boxColor(&ren, rect.x-pad_x/2,rect.y-pad_y/2,
rect.x+rect.w+pad_x/2,rect.y+rect.h+pad_y/2,
bg_clr);
end
//emit ok_redraw_pre;
_SDL_RenderCopy(&this.ren, &this.tex, null, (_SDL_Rect*)&rect);
//emit ok_redraw_pos;
end
end
end
native @nohold _SDL_text2texture();
native do
//#include "SDL2/SDL_ttf.h"
SDL_Texture* SDL_text2texture (SDL_Renderer* ren,
TTF_Font* font, char* text,
SDL_Color* fg, SDL_Color* bg)
{
// Text => Surface
SDL_Surface* sfc;
if (bg == NULL)
sfc = TTF_RenderText_Blended(font, text, *fg);
else
sfc = TTF_RenderText_Shaded(font, text, *fg, *bg);
assert(sfc != NULL);
// Surface => Texture
SDL_Texture* tex = SDL_CreateTextureFromSurface(ren, sfc);
SDL_FreeSurface(sfc);
return tex;
}
end
#ifdef __UI_TEXTURE_CEU
#include "c.ceu"
#include "sdl-colors.ceu"
input void SDL_QUIT;
var int win_w;
var int win_h;
var _SDL_Window&? win;
finalize
win = _SDL_CreateWindow("UI - Texture",
500, 1300, 800, 480, _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
_TTF_Init();
finalize with
_TTF_Quit();
end
var _TTF_Font&? font;
finalize
font = _TTF_OpenFont("fnts/monofonto.ttf", 20);
with
_TTF_CloseFont(&font);
end
var _SDL_Texture&? tex_txt;
var SDL_Color black = SDL_COLOR_BLACK;
var SDL_Color white = SDL_COLOR_WHITE;
finalize
tex_txt = _SDL_text2texture(&ren, &font, "ola mundo!",
(_SDL_Color*)&black, (_SDL_Color*)&white);
with
_SDL_DestroyTexture(&tex_txt);
end
var SDL_Rect r1, r2;
par/or do
await SDL_QUIT;
with
every SDL_REDRAW do
_SDL_SetRenderDrawColor(&ren, 0x00,0x00,0x00, 0xFF);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&win_r);
_SDL_SetRenderDrawColor(&ren, 0xFF,0xFF,0xFF, 0xFF);
_SDL_RenderFillRect(&ren, (_SDL_Rect*)&r2);
_circleRGBA(&ren, win_r.w/2, win_r.h/2, 2, 0xFF,0xFF,0xFF,0xFF);
end
with
var _SDL_Texture&? tex;
finalize
tex = _IMG_LoadTexture(&ren, "samples/img.png");
with
_SDL_DestroyTexture(tex);
end
var UITexture ui_txt with
this.ren = ren;
this.tex = tex_txt;
this.align_x = _UI_ALIGN_LEFT;
this.align_y = _UI_ALIGN_TOP;
end;
var SDL_Rect r_txt = SDL_Rect(0,0,0,0);
ui_txt.go(&r_txt);
await 1s;
ui_txt.align_bg_x = _UI_ALIGN_RIGHT;
ui_txt.align_bg_y = _UI_ALIGN_BOTTOM;
ui_txt.go_bg(&win_r, false);
var UITexture ui1 with
this.ren = ren;
this.tex = tex;
this.bg_clr = SDL_COLOR_BLUE;
end;
var UITexture ui2 with
this.ren = ren;
this.tex = tex;
end;
par do
r1 = SDL_Rect(10,10, 150,150);
r2 = SDL_Rect(200,100, 100,100);
loop do
native _rand();
loop i in 500 do
r1.x = r1.x + 1;
r1.w = r1.w + _rand() % 3 - 1;
if r1.w < 5 then r1.w = 5; end
r1.h = r1.h + _rand() % 3 - 1;
if r1.h < 5 then r1.h = 5; end
ui1.go_bg(&r1, false);
await 10ms;
end
loop i in 300 do
r1.y = r1.y + 1;
r1.w = r1.w + _rand() % 3 - 1;
if r1.w < 5 then r1.w = 5; end
r1.h = r1.h + _rand() % 3 - 1;
if r1.h < 5 then r1.h = 5; end
ui1.go_bg(&r1, false);
await 10ms;
end
loop i in 500 do
r1.x = r1.x - 1;
r1.w = r1.w + _rand() % 3 - 1;
if r1.w < 5 then r1.w = 5; end
r1.h = r1.h + _rand() % 3 - 1;
if r1.h < 5 then r1.h = 5; end
ui1.go_bg(&r1, false);
await 10ms;
end
loop i in 300 do
r1.y = r1.y - 1;
r1.w = r1.w + _rand() % 3 - 1;
if r1.w < 5 then r1.w = 5; end
r1.h = r1.h + _rand() % 3 - 1;
if r1.h < 5 then r1.h = 5; end
ui1.go_bg(&r1, false);
await 10ms;
end
end
with
loop do
ui2.align_x = _UI_ALIGN_CENTER;
ui2.align_y = _UI_ALIGN_MIDDLE;
loop i in 3 do
loop j in 3 do
ui1.align_bg_x = i;
ui1.align_bg_y = j;
ui1.go_bg(&r1, false);
ui2.align_bg_x = i;
ui2.align_bg_y = j;
ui2.go_bg(&r2, false);
await 1s;
end
end
var SDL_Rect r = SDL_Rect(win_r.w / 2,
win_r.h / 2,
0,0);
loop i in 3 do
loop j in 3 do
ui2.align_x = i;
ui2.align_y = j;
ui2.go(&r);
await 1s;
end
end
end
with
every ui1.ok_clicked do
_printf("clicked 1\n");
end
with
every ui2.ok_clicked do
_printf("clicked 2\n");
end
end
with
every SDL_REDRAW do
_SDL_RenderPresent(&ren);
end
end
escape 0;
#endif
#endif