forked from ceu-lang/ceu-sdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uitextfield.ceu
159 lines (145 loc) · 4.46 KB
/
uitextfield.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
/*{-{*/
input _SDL_TextInputEvent* SDL_TEXTINPUT;
input _SDL_TextEditingEvent* SDL_TEXTEDITING;
C nohold _strlen(), _strcpy();
C do
#include <string.h>
end
class UITextField with
interface UI;
var _SDL_Color clr_bg, clr_fg;
event int go_keyboard;
event _char* ok_keyboard;
event _char* ok_char;
var _TTF_Font* font;
var int font_own?;
do
this.font_own? = 0;
var int keyboard? = 0;
var _char[21] txt;
var UITexture* tex = null;
var int txt_len = 0;
txt[0] = '\0';
var int cursor? = 0;
await/0 go;
_assert(font != null);
finalize with
if font_own? then
_TTF_CloseFont(font);
end;
var _SDL_Rect* r = await/0 go_redim;
par do
loop do
var _SDL_MouseButtonEvent* but = await go_mousebuttondown;
if _SDL_Rect_vs_Mouse(&rect, but) then
if keyboard? then
emit ok_keyboard = txt;
else
emit go_keyboard;
end
end
end
with
loop do
await go_keyboard;
keyboard? = 1;
cursor? = 1;
_SDL_StartTextInput();
par/or do
await ok_keyboard;
keyboard? = 0;
cursor? = 0;
_SDL_StopTextInput();
with
loop do
await 500ms;
cursor? = not cursor?;
end
with
loop do
var _SDL_KeyboardEvent* key = await SDL_KEYDOWN;
if key:keysym.sym == _SDLK_BACKSPACE then
if txt_len > 0 then
txt_len = txt_len - 1;
txt[txt_len] = '\0';
end
emit go_redim=null;
emit ok_char = txt;
else/if key:keysym.sym == _SDLK_RETURN then
emit ok_keyboard = txt;
end
end
with
loop do
await SDL_TEXTEDITING;
//___android_log_print(4, "MTG_TRADER", "QUIT");
emit ok_keyboard = txt;
end
with
loop do
var _SDL_TextInputEvent* inp = await SDL_TEXTINPUT;
if txt_len<20 and _strlen(inp:text)==1 then
txt[txt_len] = inp:text[0];
txt_len = txt_len + 1;
end
emit go_redim=null;
emit ok_char = txt;
end
end
end
with
loop do
if r != null then
rect = *r;
end
free tex;
var _char* s = " "; // RenderText returns null for 0-len strings
if txt_len > 0 then
s = txt;
end
tex = new UITexture;
tex:align_x = _UI_ALIGN_LEFT;
tex:tex_own? = 1;
var _SDL_Surface* sfc;
finalize
sfc = _TTF_RenderText_Shaded(font, s, clr_fg, clr_bg);
with
_SDL_FreeSurface(sfc);
finalize
tex:tex = _SDL_CreateTextureFromSurface(global:ren, sfc);
with
nothing; // owned by UI
emit tex:go;
emit tex:go_redim=▭
r = await go_redim;
end
with
loop do
var _SDL_Point* pt = await go_move;
rect.x = rect.x + pt:x;
rect.y = rect.y + pt:y;
emit tex:go_move=pt;
end
with
var _SDL_Renderer* ren = global:ren;
loop do
await go_redraw;
// bg
_SDL_SetRenderDrawColor(ren, clr_bg.r, clr_bg.g, clr_bg.b, 0);
_SDL_RenderFillRect(ren, &rect);
// txt
emit tex:go_redraw;
// cursor
if cursor? then
var _SDL_Rect rc;
rc.x = rect.x + tex:rect.w;
rc.y = rect.y+2;
rc.w = 2;
rc.h = rect.h-4;
_SDL_SetRenderDrawColor(ren, clr_fg.r, clr_fg.g, clr_fg.b, 0);
_SDL_RenderDrawRect(ren, &rc); // TODO: leak (???)
end
end
end
end
/*}-}*/ dnl