-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_ugui.c
412 lines (323 loc) · 14.5 KB
/
c_ugui.c
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
//
// Created by https://github.com/Aurumaker72/
//
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <stdio.h>
#include "c_ugui.h"
t_vector2 primary_mouse_down_position = {0};
t_input input = {0}, last_input = {0};
t_renderer *renderer;
int32_t focus_uid = -1;
void gui_begin_frame(t_input _input, t_renderer *_renderer) {
last_input = input;
input = _input;
renderer = _renderer;
if (input.is_primary_mouse_button_down && !last_input.is_primary_mouse_button_down) {
primary_mouse_down_position = input.mouse_position;
}
}
e_visual_state get_visual_state(t_control control) {
if (!control.is_enabled) return e_visual_state_disabled;
if (is_vector2_inside(input.mouse_position, control.rectangle)) {
if (input.is_primary_mouse_button_down && is_vector2_inside(primary_mouse_down_position, control.rectangle)) {
return e_visual_state_active;
}
return e_visual_state_hovered;
}
return e_visual_state_normal;
}
void strslice(const char *str, char *result, size_t start, size_t end) {
strncpy(result, str + start, end - start);
}
void strremoveregion(char *str, size_t start, size_t end) {
size_t length = strlen(str);
if (start >= end || end >= length)
return;
memmove(str + start, str + end + 1, length - end);
str[length - (end - start + 1)] = '\0';
}
void strinsert(char *dst, const char *src, size_t index) {
size_t src_length = strlen(src);
memmove(dst + index + src_length, dst + index, strlen(dst) - index + 1);
memcpy(dst + index, src, src_length);
}
void strremch(char *str, size_t index) {
memmove(&str[index], &str[index + 1], strlen(str) - index);
}
void strremslice(char *str, size_t from, size_t to) {
size_t len = strlen(str);
if (from >= len || from >= to || to > len) {
return;
}
memmove(str + from, str + to, len - to + 1);
}
float fclamp(float value, float min, float max) {
if (value > max) return max;
if (value < min) return min;
return value;
}
int32_t iclamp(int32_t value, int32_t min, int32_t max) {
if (value > max) return max;
if (value < min) return min;
return value;
}
int32_t imin(int32_t a, int32_t b) {
if (a > b) return b;
return a;
}
int32_t imax(int32_t a, int32_t b) {
if (a > b) return a;
return b;
}
int32_t get_caret_index_for_relative_position(const char *text, float target) {
int32_t matching_caret_index = 0;
float lowest_distance = 9999999.0f;
for (int i = 0; i < strlen(text) + 1; ++i) {
char *sliced_str = (char *) calloc(strlen(text), sizeof(char));
strslice(text, sliced_str, 0, i);
float x = renderer->measure_text(sliced_str).x;
if (fabsf(target - x) < lowest_distance) {
matching_caret_index = i;
lowest_distance = fabsf(target - x);
}
free(sliced_str);
}
return matching_caret_index;
}
int32_t is_primary_interacting(t_control control) {
return (input.is_primary_mouse_button_down && !last_input.is_primary_mouse_button_down) &&
is_vector2_inside(primary_mouse_down_position, control
.rectangle) && control.is_enabled;
}
int32_t gui_get_focus_uid() {
return focus_uid;
}
int32_t gui_button(t_control control, t_button button) {
renderer->draw_button(control, get_visual_state(control), button);
if (is_primary_interacting(control)) {
focus_uid = control.uid;
return 1;
}
return 0;
}
int32_t gui_togglebutton(t_control control, t_togglebutton togglebutton) {
e_visual_state visual_state = get_visual_state(control);
if (togglebutton.is_checked && control.is_enabled) {
visual_state = e_visual_state_active;
}
if (is_primary_interacting(control)) {
togglebutton.is_checked ^= 1;
focus_uid = control.uid;
}
renderer->draw_togglebutton(control, visual_state, togglebutton);
return togglebutton.is_checked;
}
t_textbox gui_textbox(t_control control, t_textbox textbox) {
e_visual_state visual_state = get_visual_state(control);
// if we capture the focus, the control has to remain in the active visual state (emulates commctl behaviour)
if (focus_uid == control.uid) {
visual_state = e_visual_state_active;
}
renderer->draw_textbox(control, visual_state, textbox);
// after this point, only editing and selection operations are performed
// we can return early here if the control is disabled
if (!control.is_enabled)
return textbox;
if (is_primary_interacting(control)) {
focus_uid = control.uid;
// we need to find a caret index from the mouse's X position
float target = input.mouse_position.x - control.rectangle.x;
int32_t caret_index = get_caret_index_for_relative_position(textbox.text, target);
textbox.caret_index = textbox.selection_start_index = textbox.selection_end_index = caret_index;
}
// extending the selection after selection start was initially created
if (is_vector2_inside(primary_mouse_down_position, control.rectangle) && input.is_primary_mouse_button_down) {
float target = input.mouse_position.x - control.rectangle.x;
textbox.selection_end_index = get_caret_index_for_relative_position(textbox.text, target);
}
// insert all pressed chars
for (int i = 0; i < input.pressed_chars_length; ++i) {
// if we dont have a selection, replace the selected text with the new char, otherwise just insert it at the caret
if (textbox.selection_start_index == textbox.selection_end_index) {
strinsert(textbox.text, (const char[]) {input.pressed_chars[i], '\0'}, textbox.caret_index);
textbox.caret_index++;
} else {
int32_t lower_index = imin(textbox.selection_start_index, textbox.selection_end_index);
int32_t higher_index = imax(textbox.selection_start_index, textbox.selection_end_index);
strremslice(textbox.text, lower_index, higher_index);
strinsert(textbox.text, (const char[]) {input.pressed_chars[i], '\0'}, lower_index);
textbox.caret_index = textbox.selection_start_index = textbox.selection_end_index = higher_index;
}
}
// process special inputs
for (int i = 0; i < input.pressed_keycodes_length; ++i) {
if (input.pressed_keycodes[i] == e_keycodes_backspace) {
// either remove one character behind the caret, or lift out the entire selection
if (textbox.selection_start_index == textbox.selection_end_index) {
// caret-deletion at index 0 is not possible
if (textbox.caret_index > 0) {
strremch(textbox.text, textbox.caret_index - 1);
textbox.caret_index--;
}
} else {
int32_t lower_index = imin(textbox.selection_start_index, textbox.selection_end_index);
int32_t higher_index = imax(textbox.selection_start_index, textbox.selection_end_index);
strremslice(textbox.text, lower_index, higher_index);
textbox.caret_index = textbox.selection_start_index = textbox.selection_end_index = lower_index;
}
} else if (input.pressed_keycodes[i] == e_keycodes_left) {
// if there's a selection, we nuke it and put the caret at the selection's lower index
if (textbox.selection_start_index == textbox.selection_end_index) {
textbox.caret_index--;
} else {
textbox.caret_index = imin(textbox.selection_start_index, textbox.selection_end_index);
textbox.selection_start_index = textbox.selection_end_index = textbox.caret_index;
}
} else if (input.pressed_keycodes[i] == e_keycodes_right) {
// if there's a selection, we nuke it and put the caret at the selection's higher index
if (textbox.selection_start_index == textbox.selection_end_index) {
textbox.caret_index++;
} else {
textbox.caret_index = imax(textbox.selection_start_index, textbox.selection_end_index);
textbox.selection_start_index = textbox.selection_end_index = textbox.caret_index;
}
}
}
textbox.caret_index = iclamp(textbox.caret_index, 0, strlen(textbox.text));
return textbox;
}
float gui_slider(t_control control, t_slider slider) {
e_visual_state visual_state = get_visual_state(control);
// if we capture the focus, the control has to remain in the active visual state (emulates commctl behaviour)
if (focus_uid == control.uid) {
visual_state = e_visual_state_active;
}
if (focus_uid == control.uid && !input.is_primary_mouse_button_down) {
focus_uid = -1;
}
if (is_primary_interacting(control)) {
focus_uid = control.uid;
}
if (focus_uid == control.uid) {
// if width>height, we have a horizontal slider, otherwise a vertical one
if (control.rectangle.width > control.rectangle.height) {
slider.value = (input.mouse_position.x - control.rectangle.x) / control.rectangle.width;
} else {
slider.value = (input.mouse_position.y - control.rectangle.y) / control.rectangle.height;
}
slider.value = fclamp(slider.value, 0.0f, 1.0f);
}
renderer->draw_slider(control, visual_state, slider);
return slider.value;
}
size_t gui_get_selected_index_in_collection_control(t_control control, size_t items_length, float translation) {
float list_height = renderer->listbox_get_item_height() * items_length;
float rel_mouse_y = input.mouse_position.y - control.rectangle.y;
// if the mouse is outside of the control's bounds, we don't allow the selection to change
if (rel_mouse_y > 0.0f && rel_mouse_y < control.rectangle.height) {
// compute the selection index based off of the bounds, mouse position, and translation
size_t i = floor((rel_mouse_y + (translation * (list_height - control.rectangle.height))) /
renderer->listbox_get_item_height());
return iclamp(i, 0, items_length - 1);
}
return SIZE_MAX;
}
t_listbox gui_listbox(t_control control, t_listbox listbox) {
if (focus_uid == control.uid && !input.is_primary_mouse_button_down) {
focus_uid = -1;
}
if (is_primary_interacting(control)) {
focus_uid = control.uid;
}
if (focus_uid == control.uid) {
size_t selected_index = gui_get_selected_index_in_collection_control(control, listbox.items_length,
listbox.translation);
if (selected_index != SIZE_MAX) {
listbox.selected_index = selected_index;
}
}
if (control.is_enabled && is_vector2_inside(input.mouse_position, control.rectangle) &&
input.mouse_wheel_delta.y != 0) {
// we dont allow scrolling if the content doesnt overflow
float list_height = listbox.items_length * renderer->listbox_get_item_height();
if (list_height > control.rectangle.height) {
float sign = input.mouse_wheel_delta.y > 0.0f ? -1.0f : 1.0f;
listbox.translation += (sign != 0.0f ? (renderer->listbox_get_item_height() /
(renderer->listbox_get_item_height() * listbox.items_length)) : 0) *
sign;
}
}
listbox.translation = fclamp(listbox.translation, 0.0f, 1.0f);
renderer->draw_listbox(control, e_visual_state_normal, listbox);
return listbox;
}
void gui_progressbar(t_control control, t_progressbar progressbar) {
renderer->draw_progressbar(control, control.is_enabled ? e_visual_state_normal : e_visual_state_disabled,
progressbar);
}
void gui_draw_node(t_control control, e_visual_state visual_state, t_node *node, size_t index, size_t subdepth,
int32_t *was_handled) {
renderer->draw_treeview_node(control, visual_state, *node, index, subdepth);
if (node->children_length == 0) return;
float x = control.rectangle.x + (renderer->listbox_get_item_height() * (subdepth - 1));
float y = control.rectangle.y + (renderer->listbox_get_item_height() * index);
if (gui_button((t_control) {
.uid = control.uid * 1024 * index * subdepth,
.rectangle = (t_rectangle) {
.x = x,
.y = y,
.width = renderer->listbox_get_item_height(),
.height = renderer->listbox_get_item_height()
},
.is_enabled = control.is_enabled
}, (t_button) {
.text = node->is_expanded ? "-" : "+"
})) {
*was_handled = 1;
node->is_expanded ^= 1;
}
}
void gui_walk_tree(t_control control, e_visual_state visual_state, t_node *node, size_t accumulator, size_t subdepth,
size_t *length, int32_t *was_handled) {
subdepth++;
*length = *length + 1;
gui_draw_node(control, visual_state, node, accumulator, subdepth, was_handled);
if (!node->is_expanded)
return;
for (size_t i = 0; i < node->children_length; i++) {
gui_walk_tree(control, visual_state, &node->children[i], ++accumulator, subdepth, length, was_handled);
}
}
void gui_select_node_at_index(t_node *node, size_t accumulator, size_t subdepth, size_t index) {
subdepth++;
node->is_selected = accumulator == index;
if (!node->is_expanded)
return;
for (size_t i = 0; i < node->children_length; i++) {
gui_select_node_at_index(&node->children[i], ++accumulator, subdepth, index);
}
}
t_treeview gui_treeview(t_control control, t_treeview treeview) {
e_visual_state visual_state = control.is_enabled ? e_visual_state_normal : e_visual_state_disabled;
renderer->draw_treeview(control, visual_state, treeview);
size_t length = 0;
int32_t handled = 0;
gui_walk_tree(control, visual_state, &treeview.root_node, 0, 0, &length, &handled);
if (handled) return treeview;
if (focus_uid == control.uid && !input.is_primary_mouse_button_down) {
focus_uid = -1;
}
if (is_primary_interacting(control)) {
focus_uid = control.uid;
}
if (focus_uid == control.uid) {
size_t selected_index = gui_get_selected_index_in_collection_control(control, length,
0.0f);
if (selected_index != SIZE_MAX) {
gui_select_node_at_index(&treeview.root_node, 0, 0, selected_index);
}
}
return treeview;
}