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
|
#include "serialization-static-ui.hpp"
#include "ecs.hpp"
#include "font.hpp"
#include "pk.h"
#include "static-ui.hpp"
pk_handle pke_serialize_ui_box_internal(srlztn_serialize_helper *h, pke_ui_box_type_data::pke_ui_box_type_data_text *data) {
char *s;
pke_kve kve{};
pke_kve_container kvec{};
kvec.srlztn_handle = h->handle_head;
kvec.type_code = cstring_to_pk_cstr(SRLZTN_OBJ_UI_BOX_TYPE_DATA);
kvec.bkt = h->bkt;
kvec.arr.bkt = h->bkt;
kvec.children.bkt = h->bkt;
kvec.child_handles.bkt = h->bkt;
h->handle_head.itemIndex++;
{
kve.key = SRLZTN_UI_BOX_DATA_TEXT_FONT_RENDER_UUID;
s = pk_new<char>(37, h->bkt);
FontRender *fr = FontType_GetFontRender(data->font_render_handle);
sprintf(s, pk_uuid_printf_format, pk_uuid_printf_var(fr->uuid));
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
pk_arr_append_t(&h->kvp_containers, kvec);
kvec.arr.data = nullptr;
kvec.children.data = nullptr;
kvec.child_handles.data = nullptr;
return kvec.srlztn_handle;
}
void pke_deserialize_ui_box_internal(srlztn_deserialize_helper *h, pke_kve_container *kvec, pke_ui_box_type_data::pke_ui_box_type_data_text *data) {
(void)h;
assert(kvec != nullptr);
assert(data != nullptr);
assert(kvec->arr.next == 1);
pk_uuid font_render_uuid = pk_uuid_zed;
if (strstr(SRLZTN_UI_BOX_DATA_TEXT_FONT_RENDER_UUID, kvec->arr[0].key)) {
kvec->arr[0].val >> font_render_uuid;
}
assert(font_render_uuid != pk_uuid_zed);
FontRender *fr = static_cast<FontRender *>(ECS_GetEntityByUUID(font_render_uuid));
data->font_render_handle = fr->fr_handle;
}
pk_handle pke_serialize_ui_box(srlztn_serialize_helper *h, pke_ui_box *box) {
char *s;
int len;
pk_handle internal_type_handle = PK_HANDLE_MAX;
pke_kve kve{};
pke_kve_container kvec{};
if (box->type == PKE_UI_BOX_TYPE_TEXT) {
internal_type_handle = pke_serialize_ui_box_internal(h, &box->type_data->text);
}
kvec.srlztn_handle = h->handle_head;
kvec.type_code = cstring_to_pk_cstr(SRLZTN_OBJ_UI_BOX);
kvec.bkt = h->bkt;
kvec.arr.bkt = h->bkt;
kvec.children.bkt = h->bkt;
kvec.child_handles.bkt = h->bkt;
h->handle_head.itemIndex++;
if (internal_type_handle != PK_HANDLE_MAX) {
pk_arr_append_t(&kvec.child_handles, internal_type_handle);
}
if (box->uuid != pk_uuid_zed && box->uuid != pk_uuid_max) {
kve.key = SRLZTN_UI_BOX_UUID;
s = pk_new<char>(37, h->bkt);
sprintf(s, pk_uuid_printf_format, pk_uuid_printf_var(box->uuid));
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
if (box->parentHandle != EntityHandle_MAX) {
Entity_Base *e = ECS_GetEntity(box->parentHandle);
kve.key = SRLZTN_UI_BOX_PARENT_UUID;
s = pk_new<char>(37, h->bkt);
sprintf(s, pk_uuid_printf_format, pk_uuid_printf_var(e->uuid));
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
if (box->flags != PKE_UI_BOX_FLAG_NONE) {
kve.key = SRLZTN_UI_BOX_FLAGS;
s = pk_new<char>(19, h->bkt);
sprintf(s, "0x%.2lX", static_cast<PKE_UI_BOX_FLAG_T>(box->flags));
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
{
kve.key = SRLZTN_UI_BOX_POS_TOP_LEFT;
len = snprintf(NULL, 0, "%f;%f", box->pos_top_left.x, box->pos_top_left.y);
s = pk_new<char>(len+1, h->bkt);
sprintf(s, "%f;%f", box->pos_top_left.x, box->pos_top_left.y);
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
if (box->min_size != glm::vec2(0,0)) {
kve.key = SRLZTN_UI_BOX_MIN_SIZE;
len = snprintf(NULL, 0, "%f;%f", box->min_size.x, box->min_size.y);
s = pk_new<char>(len+1, h->bkt);
sprintf(s, "%f%f", box->min_size.x, box->min_size.y);
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
if (box->max_size != glm::vec2(0,0)) {
kve.key = SRLZTN_UI_BOX_MAX_SIZE;
len = snprintf(NULL, 0, "%f;%f", box->max_size[0], box->max_size[1]);
s = pk_new<char>(len+1, h->bkt);
sprintf(s, "%f;%f", box->max_size[0], box->max_size[1]);
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
if (box->flex_weight != 0.f) {
kve.key = SRLZTN_UI_BOX_FLEX_WEIGHT;
len = snprintf(NULL, 0, "%f", box->flex_weight);
s = pk_new<char>(len+1, h->bkt);
sprintf(s, "%f", box->flex_weight);
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
// only serialize if we're using flex
kve.key = SRLZTN_UI_BOX_FLEX_DIRECTION;
s = pk_new<char>(5, h->bkt);
sprintf(s, "0x%.2X", box->flex_direction);
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
{
kve.key = SRLZTN_UI_BOX_TYPE;
s = pk_new<char>(5, h->bkt);
sprintf(s, "0x%.2X", static_cast<PKE_UI_BOX_TYPE_T>(box->type));
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
{
kve.key = SRLZTN_UI_BOX_LAYER;
s = pk_new<char>(5, h->bkt);
sprintf(s, "0x%.2X", box->layer);
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
}
pk_arr_append_t(&h->kvp_containers, kvec);
kvec.arr.data = nullptr;
kvec.children.data = nullptr;
kvec.child_handles.data = nullptr;
return kvec.srlztn_handle;
}
void pke_deserialize_ui_box(srlztn_deserialize_helper *h, pke_kve_container *kvec) {
/* How this works, a concrete example
*
* wrapper box > type_text box > { type_text data + FRSettings }
*
* 1. wrapper box parse & create
* 2. box_type text starts parse
* a. parse type_data (one child only) (by reference)
* 1. parse font-render settings (by reference)
*
*/
uint32_t i, index;
PK_STN_RES res;
const char *starting_char;
char *stn_end;
pke_kve *kve;
pke_ui_box bx{};
pke_ui_box *parent_box = nullptr;
bx.type_data = nullptr;
for (i = 0; i < kvec->arr.next; ++i) {
kve = &kvec->arr[i];
if (strstr(SRLZTN_UI_BOX_UUID, kve->key)) {
kve->val >> bx.uuid;
continue;
}
if (strstr(SRLZTN_UI_BOX_PARENT_UUID, kve->key)) {
uint32_t target_instance_index = -1;
pk_uuid id;
kve->val >> id;
target_instance_index = pk_arr_find_first_index(&h->mapping, &id, srlztn_mapping_find_first_handle_by_uuid);
if (target_instance_index != uint32_t(-1)) {
parent_box = static_cast<pke_ui_box*>(h->mapping[target_instance_index].created_entity);
}
continue;
}
if (strstr(SRLZTN_UI_BOX_FLAGS, kve->key)) {
PKE_UI_BOX_FLAG_T flags;
res = pk_stn(&flags, kve->val, &stn_end, 16);
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i\n", SRLZTN_UI_BOX_FLAGS, res);
continue;
}
bx.flags = PKE_UI_BOX_FLAG(flags);
continue;
}
if (strstr(SRLZTN_UI_BOX_POS_TOP_LEFT, kve->key)) {
starting_char = kve->val;
index = 0;
do {
assert(index < 2);
res = pk_stn(&bx.pos_top_left[index], starting_char, &stn_end);
if (res != PK_STN_RES_SUCCESS) break;
starting_char = stn_end + 1;
++index;
} while (*stn_end != '\0');
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i\n", SRLZTN_UI_BOX_POS_TOP_LEFT, res);
}
continue;
}
if (strstr(SRLZTN_UI_BOX_MIN_SIZE, kve->key)) {
starting_char = kve->val;
index = 0;
do {
assert(index < 2);
res = pk_stn(&bx.min_size[index], starting_char, &stn_end);
if (res != PK_STN_RES_SUCCESS) break;
starting_char = stn_end + 1;
++index;
} while (*stn_end != '\0');
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i\n", SRLZTN_UI_BOX_MIN_SIZE, res);
}
continue;
}
if (strstr(SRLZTN_UI_BOX_MAX_SIZE, kve->key)) {
starting_char = kve->val;
index = 0;
do {
assert(index < 2);
res = pk_stn(&bx.max_size[index], starting_char, &stn_end);
if (res != PK_STN_RES_SUCCESS) break;
starting_char = stn_end + 1;
++index;
} while (*stn_end != '\0');
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i\n", SRLZTN_UI_BOX_MAX_SIZE, res);
}
continue;
}
if (strstr(SRLZTN_UI_BOX_FLEX_WEIGHT, kve->key)) {
res = pk_stn(&bx.flex_weight, kve->val, &stn_end);
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i\n", SRLZTN_UI_BOX_FLEX_WEIGHT, res);
continue;
}
continue;
}
if (strstr(SRLZTN_UI_BOX_FLEX_DIRECTION, kve->key)) {
res = pk_stn(&bx.flex_direction, kve->val, &stn_end);
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i\n", SRLZTN_UI_BOX_FLEX_DIRECTION, res);
continue;
}
continue;
}
if (strstr(SRLZTN_UI_BOX_LAYER, kve->key)) {
res = pk_stn(&bx.layer, kve->val, &stn_end);
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i\n", SRLZTN_UI_BOX_LAYER, res);
continue;
}
continue;
}
if (strstr(SRLZTN_UI_BOX_TYPE, kve->key)) {
uint8_t v;
res = pk_stn<uint8_t>(&v, kve->val, &stn_end);
if (res != PK_STN_RES_SUCCESS) {
fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse '%s' from '%s', err: %i\n", SRLZTN_UI_BOX_TYPE, kve->val, res);
continue;
}
// log warning for unknown
bx.type = PKE_UI_BOX_TYPE{v};
switch (bx.type) {
case PKE_UI_BOX_TYPE_STANDARD:
case PKE_UI_BOX_TYPE_TEXT:
case PKE_UI_BOX_TYPE_INPUT_TEXT:
break;
default:
fprintf(stderr, "[pke_deserialize_ui_box] Parsed unknown ui box data type from: '%s'\n", kve->val);
continue;
}
continue;
}
}
// deserialize data
// TODO specific bucket?
if (bx.type != PKE_UI_BOX_TYPE_STANDARD) {
bx.type_data = pk_new<pke_ui_box_type_data>();
switch (bx.type) {
case PKE_UI_BOX_TYPE_TEXT:
pke_deserialize_ui_box_internal(h, kvec->children[0], &bx.type_data->text);
break;
default:
break;
}
}
pke_ui_box *box;
if (parent_box == nullptr) {
box = pke_ui_box_new_root(bx.type, bx.uuid);
} else {
box = pke_ui_box_new_child(parent_box, bx.type, bx.uuid);
}
box->flags = bx.flags;
box->pos_top_left = bx.pos_top_left;
box->min_size = bx.min_size;
box->max_size = bx.max_size;
box->flex_weight = bx.flex_weight;
box->type = bx.type;
box->flex_direction = bx.flex_direction;
box->layer = bx.layer;
// TODO consider type-specific clone functions
if (box->type_data != nullptr && bx.type_data != nullptr) {
*box->type_data = *box->type_data;
pk_delete<pke_ui_box_type_data>(bx.type_data);
bx.type_data = nullptr;
}
srlztn_ecs_mapping map{};
map.serialized_uuid = bx.uuid;
map.created_entity = box;
pk_arr_append_t(&h->mapping, map);
// recursively deserialize children
for (i = 0; i < kvec->children.next; ++i) {
pke_kve_container *child_kvec = kvec->children[i];
if (strcmp(child_kvec->type_code.val, SRLZTN_OBJ_UI_BOX) == 0) {
pke_deserialize_ui_box(h, child_kvec);
}
}
return;
}
|