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
|
#ifndef ENGINE_UI_H
#define ENGINE_UI_H
#include <engine/core/types.h>
#include <engine/utils/list.h>
#include <engine/utils/vector.h>
#define DIRECTION_HORIZONTAL true
#define DIRECTION_VERTICAL false
/* Ui positioning constraints */
typedef enum {
ui_size_pixel,
ui_size_percent,
} ui_size_t;
typedef union {
ui_size_t type;
struct {
ui_size_t type;
i32 value;
} pixel;
struct {
ui_size_t type;
f32 value;
} percent;
} ui_size;
typedef enum {
ui_constraint_width,
ui_constraint_height,
ui_constraint_horizontal_align,
ui_constraint_vertical_align,
ui_constraint_left,
ui_constraint_right,
ui_constraint_top,
ui_constraint_bottom,
} ui_constraint_t;
typedef enum {
ui_constraint_vertical_align_top,
ui_constraint_vertical_align_center,
ui_constraint_vertical_align_bottom,
} ui_constraint_vertical_align_t;
typedef enum {
ui_constraint_horizontal_align_left,
ui_constraint_horizontal_align_center,
ui_constraint_horizontal_align_right,
} ui_constraint_horizontal_align_t;
typedef union {
struct {
ui_size size;
} width;
struct {
ui_size size;
} height;
/* spacing is the padding between the aligned edge and the container itself */
struct {
ui_constraint_horizontal_align_t align;
ui_size spacing;
} horizontal_align;
struct {
ui_constraint_vertical_align_t align;
ui_size spacing;
} vertical_align;
struct {
ui_size pos;
} left;
struct {
ui_size pos;
} top;
struct {
ui_size pos;
} right;
struct {
ui_size pos;
} bottom;
} constraintval_t;
#define CONSTRAINT_POSITION(axis, alignment_sub, margin, cnext) \
{ \
.value = \
{ \
.type = ui_constraint_##axis##_align, \
.constraint.axis##_align = \
{ \
.align = ui_constraint_##axis##_align_##alignment_sub, \
.spacing = margin, \
}, \
}, \
.next = cnext, \
}
#define CONSTRAINT_SIZE(c_type, csize, cnext) \
{ \
.value = \
{ \
.type = ui_constraint_##c_type, \
.constraint.c_type = \
{ \
.size = csize, \
}, \
}, \
.next = cnext, \
}
typedef struct {
ui_constraint_t type;
constraintval_t constraint;
} ui_constraint;
typedef DEFINE_LLIST(ui_constraint);
enum uitype {
uitype_container,
uitype_button,
uitype_title,
uitype_text,
uitype_MAX,
};
typedef struct UITree_container {
enum uitype type;
bool visible;
bool direction;
i32 x, y, w, h;
i32 padding;
i32 margin;
Engine_color fg;
Engine_color bg;
Engine_color bordercolor;
usize children_len; /* Number of children */
usize children_size; /* Memory size */
union UITree** children;
struct List_ui_constraint* constraints;
} UITree_container;
typedef struct UITree_button {
enum uitype type;
bool enabled;
u64 id;
i32 x, y, w, h;
i32 padding;
i32 margin;
Engine_color fg;
Engine_color bg;
i32 font_id;
const char* text_original;
u64 text_texture_index;
v2_i32 texture_size;
struct List_ui_constraint* constraints;
} UITree_button;
typedef struct UITree_title {
enum uitype type;
i32 x, y, w, h;
i32 padding;
i32 margin;
Engine_color fg;
i32 font_id;
const char* text_original;
u64 text_texture_index;
v2_i32 texture_size;
struct List_ui_constraint* constraints;
} UITree_title;
typedef struct UITree_text {
enum uitype type;
i32 x, y, w, h;
i32 padding;
i32 margin;
Engine_color fg;
i32 font_id;
const char* text_original;
u64 text_texture_index;
v2_i32 texture_size;
struct List_ui_constraint* constraints;
} UITree_text;
typedef union UITree {
enum uitype type;
UITree_container container;
UITree_button button;
UITree_title title;
UITree_text text;
struct List_ui_constraint constraints;
} UITree;
/* Sizes */
f32 ui_size_pixel_to_percent_width(i32 pixels);
f32 ui_size_pixel_to_percent_height(i32 pixels);
f32 ui_size_pixel_to_percent(i32 pixels);
i32 ui_size_percent_width_to_pixel(f32 percent);
i32 ui_size_percent_height_to_pixel(f32 percent);
i32 ui_size_percent_to_pixel(f32 percent);
i32 ui_size_to_pixel(ui_size s);
#define ui_size_percent_new(p) \
{ \
.percent = {.type = ui_size_percent, .value = p } \
}
#define ui_size_pixel_new(p) \
{ \
.pixel = {.type = ui_size_pixel, .value = p } \
}
/* Constructors */
UITree* ui_container(bool direction, struct List_ui_constraint* constraints);
UITree* ui_button(u64 id, i32 font_id, char* text,
struct List_ui_constraint* constraints);
UITree* ui_title(i32 font_id, const char* text,
struct List_ui_constraint* constraints);
UITree* ui_text(i32 font_id, const char* text,
struct List_ui_constraint* constraints);
/* Extended Constructors */
UITree* ui_container_new_ex(Engine_color fg, Engine_color bg,
Engine_color border, bool direction,
struct List_ui_constraint* constraints);
/* Destructors */
void clear_ui(void);
void uitree_free(UITree* t);
/* Manipulation */
void ui_container_attach(UITree* container, UITree* child);
void ui_resolve_constraints(void);
void ui_button_enable(UITree_button* b);
void ui_button_disable(UITree_button* b);
/* Tweaking default colors */
void ui_set_default_colors(Engine_color fg, Engine_color bg);
void ui_set_default_padding(i32 padding);
Engine_color ui_get_default_fg(void);
Engine_color ui_get_default_bg(void);
#define ELEM_NOT_FOUND (-1)
#define NO_CLICK (-2)
/* Returns -1 if not found */
u64 ui_check_click(UITree* root);
#endif
|