-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscr_inputbox.gml
443 lines (387 loc) · 11.5 KB
/
scr_inputbox.gml
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
global.IBDATA = { focused_ib: undefined, focus_reset: true, arrow_switch_t: -1 };
function inputbox(text = "", template_ib = undefined) constructor {
// customization
ib_next = undefined;
ib_prev = undefined;
highlight_color = c_aqua;
highlight_alpha = 0.3;
line_color = merge_color(c_dkgray, c_white, 0.15);
line_alpha = 1;
bkg_color = merge_color(c_dkgray, c_black, 0.5);
bkg_alpha = 1;
bkg_color_focused = merge_color(c_dkgray, c_black, 0.25);
bkg_alpha_focused = 1;
font = -1;
width = 200;
height = 20;
lcase = false;
ucase = false;
char_limit = infinity;
width_limit = true;
// touch these and your pc explodes
slen = 0;
str = "";
cstr = str;
cursor_pos = 0;
has_focus = false;
spam_time = room_speed * 0.2;
spam_t = spam_time;
spam_speed = room_speed * 0.025;
spam_s = spam_speed;
cursor_time = room_speed * 0.5;
cursor_t = cursor_time;
cursor_vis = true;
sel1 = cursor_pos;
sel2 = cursor_pos;
sel1x = 0;
sel2x = 0;
cursx = 0;
double_click_speed = room_speed * 0.2;
dc_t = double_click_speed;
double_click_hold = false;
if text != "" set_text(text);
if !is_undefined(template_ib) copy_look(template_ib);
static focus = function() {
global.IBDATA.focused_ib = self;
}
static copy_look = function(from) {
if !is_undefined(from) {
highlight_color = from.highlight_color;
highlight_alpha = from.highlight_alpha;
line_color = from.line_color;
line_alpha = from.line_alpha;
bkg_color = from.bkg_color;
bkg_alpha = from.bkg_alpha;
bkg_color_focused = from.bkg_color_focused;
bkg_alpha_focused = from.bkg_alpha_focused;
font = from.font;
width = from.width;
height = from.height;
lcase = from.lcase;
ucase = from.ucase;
}
}
static draw = function(x, y, gui = true) {
var mx = gui ? device_mouse_x_to_gui(0) : device_mouse_x(0);
var my = gui ? device_mouse_y_to_gui(0) : device_mouse_y(0);
var prev_c = draw_get_color();
var prev_a = draw_get_alpha();
var prev_f = draw_get_font();
var slen = string_length(str);
draw_set_font(font);
var mouse_in_rect = point_in_rectangle(mx, my, x-8, y, x+width+8, y+height);
dc_t = max(--dc_t, -1);
if device_mouse_check_button_released(0, mb_left) {
global.IBDATA.focus_reset = true;
global.IBDATA.arrow_switch_t = -1;
keyboard_string = "";
}
else if device_mouse_check_button_pressed(0, mb_left) {
if global.IBDATA.focus_reset {
global.IBDATA.focus_reset = false;
global.IBDATA.focused_ib = undefined;
global.IBDATA.arrow_switch_t = room_speed * 0.1;
}
if mouse_in_rect global.IBDATA.focused_ib = self;
}
has_focus = global.IBDATA.focused_ib == self;
if !has_focus {
if ucase cstr = string_upper(str);
else if lcase cstr = string_lower(str);
else cstr = str;
}
else {
if keyboard_check_pressed(vk_anykey)
or keyboard_check_released(vk_anykey) {
spam_t = spam_time;
spam_s = spam_speed;
}
if keyboard_check_released(vk_backspace) keyboard_string = "";
var can_spam = false;
if keyboard_check(vk_anykey) {
if --spam_t < 0 {
spam_t = spam_s;
can_spam = true;
}
cursor_t = cursor_time;
cursor_vis = true;
}
if --cursor_t < 0 {
cursor_t = cursor_time;
cursor_vis = !cursor_vis;
}
global.IBDATA.arrow_switch_t = max(-1, global.IBDATA.arrow_switch_t-1);
if keyboard_check_pressed(vk_anykey) or can_spam {
switch(keyboard_key) {
case vk_up:
if global.IBDATA.arrow_switch_t < 0 and !is_undefined(ib_prev) {
global.IBDATA.arrow_switch_t = room_speed * 0.1;
global.IBDATA.focused_ib = ib_prev;
ib_prev.cursor_pos = min(cursor_pos, ib_prev.slen);
ib_prev.sel1 = 0;
ib_prev.sel2 = 0;
}
break;
case vk_down:
if global.IBDATA.arrow_switch_t < 0 and !is_undefined(ib_next) {
global.IBDATA.arrow_switch_t = room_speed * 0.1;
global.IBDATA.focused_ib = ib_next;
ib_next.cursor_pos = min(cursor_pos, ib_next.slen);
ib_next.sel1 = 0;
ib_next.sel2 = 0;
}
break;
case vk_right:
if keyboard_check(vk_control) {
for(var i = cursor_pos+1; i < slen; i++) {
var ch = string_char_at(str, i+1);
if ch == " " or ch == "," or ch == "." break;
cursor_pos = i+1;
}
}
else cursor_pos++;
if sel1 != sel2 {
cursor_pos = sel2;
sel1 = sel2;
}
break;
case vk_left:
if keyboard_check(vk_control) {
for(var i = cursor_pos-1; i > -1; i--) {
var ch = string_char_at(str, i);
if ch == " " or ch == "," or ch == "." break;
cursor_pos = i-1;
}
}
else cursor_pos--;
if sel1 != sel2 {
cursor_pos = sel2;
sel1 = sel2;
}
break;
case vk_backspace:
if sel1 != sel2 {
var from = min(sel1, sel2);
var to = max(sel1, sel2);
str = string_delete(str, from+1, to-from);
cursor_pos = from;
sel1 = cursor_pos;
sel2 = cursor_pos;
}
else str = string_delete(str, cursor_pos--, 1);
if keyboard_check(vk_control) {
for(var i = cursor_pos; i > -1; i--) {
var ch = string_char_at(str, i);
if ch == " " or ch == "," or ch == "." break;
str = string_delete(str, cursor_pos--, 1);
}
}
break;
case vk_delete:
if sel1 != sel2 {
var from = min(sel1, sel2);
var to = max(sel1, sel2);
str = string_delete(str, from+1, to-from);
cursor_pos = from;
sel1 = cursor_pos;
sel2 = cursor_pos;
}
else str = string_delete(str, cursor_pos+1, 1);
break;
default:
if keyboard_string != "" {
if sel1 != sel2 {
var from = min(sel1, sel2);
var to = max(sel1, sel2);
str = string_delete(str, from+1, to-from);
cursor_pos = from;
sel1 = cursor_pos;
sel2 = cursor_pos;
}
var sl = string_length(keyboard_string);
for(var i = 0; i <= sl; i++) {
var ch = string_char_at(keyboard_string, i+1);
if ucase cstr = string_upper(str);
else if lcase cstr = string_lower(str);
else cstr = str;
if width_limit and string_width(cstr+ch) > width break;
if char_limit and string_length(cstr) >= char_limit break;
str = string_insert(ch, str, cursor_pos+1);
if i < sl cursor_pos++;
}
}
keyboard_string = "";
break;
}
switch(keyboard_key) {
case ord("C"):
if keyboard_check(vk_control) and sel1 != sel2 {
var from = min(sel1, sel2);
var to = max(sel1, sel2);
clipboard_set_text(string_copy(str, from+1, to-from));
}
break;
case ord("V"):
if keyboard_check(vk_control) and clipboard_has_text() {
if sel1 != sel2 {
var from = min(sel1, sel2);
var to = max(sel1, sel2);
str = string_delete(str, from+1, to-from);
cursor_pos = from;
sel1 = cursor_pos;
sel2 = cursor_pos;
}
var ct = clipboard_get_text();
var sl = string_length(ct);
for(var i = 0; i <= sl; i++) {
var ch = string_char_at(ct, i+1);
if ucase cstr = string_upper(str);
else if lcase cstr = string_lower(str);
else cstr = str;
if width_limit and string_width(cstr+ch) > width break;
if char_limit and string_length(cstr) >= char_limit break;
str = string_insert(ch, str, cursor_pos+1);
if i < sl cursor_pos++;
}
}
break;
case ord("A"):
if keyboard_check(vk_control) and slen != 0 {
sel1 = 0;
sel2 = slen;
sel1x = string_width(string_copy(cstr, 1, sel1));
sel2x = string_width(string_copy(cstr, 1, sel2));
}
break;
case ord("X"):
if keyboard_check(vk_control) and sel1 != sel2 {
var from = min(sel1, sel2);
var to = max(sel1, sel2);
clipboard_set_text(string_copy(str, from+1, to-from));
str = string_delete(str, from+1, to-from);
cursor_pos = from;
sel1 = cursor_pos;
sel2 = cursor_pos;
}
break;
}
}
slen = string_length(str);
cursor_pos = clamp(cursor_pos, 0, slen);
if ucase cstr = string_upper(str);
else if lcase cstr = string_lower(str);
else cstr = str;
if device_mouse_check_button(0, mb_left) and !double_click_hold {
cursor_t = cursor_time;
cursor_vis = true;
var press = device_mouse_check_button_pressed(0, mb_left);
var closest = infinity;
for(var i = 0; i <= slen; i++) {
var sw = string_width(string_copy(cstr, 1, i));
var dist = abs(x+sw-mx);
if dist < closest {
closest = dist;
cursor_pos = i;
if press {
sel1 = i;
sel2 = i;
}
else sel2 = i;
}
else break;
}
if sel1 != sel2 {
sel1x = string_width(string_copy(cstr, 1, sel1));
sel2x = string_width(string_copy(cstr, 1, sel2));
cursx = sel2x;
}
else cursx = string_width(string_copy(cstr, 1, cursor_pos));
}
var double_clicked = false;
if device_mouse_check_button_pressed(0, mb_left) {
if dc_t < 0 {
dc_t = double_click_speed;
sel1x = cursor_pos;
sel2x = cursor_pos;
}
else {
double_clicked = true;
}
}
if !device_mouse_check_button(0, mb_left) double_click_hold = false;
if double_clicked {
double_click_hold = true;
sel1 = 0;
sel2 = slen;
for(var i = cursor_pos+1; i < slen; i++) {
var ch = string_char_at(cstr, i);
if ch == " " or ch == "," or ch == "." {
sel2 = i-1;
break;
}
}
for(var i = cursor_pos+1; i > -1; i--) {
var ch = string_char_at(cstr, i);
if ch == " " or ch == "," or ch == "." {
sel1 = i;
break;
}
}
if sel1 != sel2 {
sel1x = string_width(string_copy(cstr, 1, sel1));
sel2x = string_width(string_copy(cstr, 1, sel2));
cursx = sel2x;
}
}
}
draw_set_color(has_focus ? bkg_color_focused : bkg_color);
draw_set_alpha(has_focus ? bkg_alpha_focused : bkg_alpha);
draw_rectangle(x, y, x+width, y+height, false);
draw_set_color(line_color);
draw_set_alpha(line_alpha);
draw_line(x, y+height, x+width, y+height);
draw_set_color(prev_c);
draw_set_alpha(prev_a);
if has_focus {
if keyboard_check(vk_anykey) {
cursx = string_width(string_copy(cstr, 1, cursor_pos));
}
if sel1 != sel2 {
draw_set_color(highlight_color);
draw_set_alpha(min(1, highlight_alpha + 0.3));
draw_rectangle(x+min(sel1x, sel2x), y, x+max(sel1x, sel2x), y+height, true);
draw_set_alpha(highlight_alpha);
draw_rectangle(x+min(sel1x, sel2x), y, x+max(sel1x, sel2x), y+height, false);
draw_set_color(prev_c);
draw_set_alpha(prev_a);
}
else if cursor_vis {
draw_line(x+cursx, y, x+cursx, y+height);
}
}
draw_text(x, y, cstr);
draw_set_font(prev_f);
}
static get_text = function(with_case = false) {
return with_case ? cstr : str;
}
static set_text = function(text) {
draw_set_font(font);
text = string(text);
str = "";
var sl = string_length(text);
for(var i = 0; i <= sl; i++) {
var ch = string_char_at(text, i+1);
if ucase cstr = string_upper(str);
else if lcase cstr = string_lower(str);
else cstr = str;
if width_limit and string_width(cstr+ch) > width break;
if char_limit and string_length(cstr) >= char_limit break;
str += ch;
cursor_pos++;
}
slen = string_length(str);
cursor_pos = max(slen, 0);
cursx = string_width(string_copy(cstr, 1, cursor_pos));
}
}