summaryrefslogtreecommitdiff
path: root/src/serialization-static-ui.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-04-21 15:46:46 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-04-21 15:46:46 -0400
commitef37d054dfe5812efa9eefb4b9b18621fdabac25 (patch)
tree713042c875004da0058bf5813c4b2b736b6c4ed3 /src/serialization-static-ui.cpp
parenta066448effaa9a56c049136067a73ba0156f65d3 (diff)
pke: first-pass serializing ui and font renders
Diffstat (limited to 'src/serialization-static-ui.cpp')
-rw-r--r--src/serialization-static-ui.cpp245
1 files changed, 245 insertions, 0 deletions
diff --git a/src/serialization-static-ui.cpp b/src/serialization-static-ui.cpp
new file mode 100644
index 0000000..ca17c10
--- /dev/null
+++ b/src/serialization-static-ui.cpp
@@ -0,0 +1,245 @@
+
+#include "serialization-static-ui.hpp"
+#include "ecs.hpp"
+#include "pk.h"
+#include "serialization-font.hpp"
+#include "static-ui.hpp"
+
+bool pke_serialize_ui_box_internal(srlztn_serialize_helper *h, pke_ui_box_type_data_text *data) {
+ if (data->font_render_uuid != pk_uuid_zed) {
+ h->o << SRLZTN_UI_BOX_DATA_TEXT_FONT_RENDER_UUID << data->font_render_uuid << std::endl;
+ }
+ pke_serialize_font_render_settings(h, &data->font_render_settings);
+ return true;
+}
+
+bool pke_deserialize_ui_box_internal(srlztn_deserialize_helper *h, pke_ui_box_type_data_text *data) {
+ uint64_t prefix_len;
+ if (strstr(SRLZTN_UI_BOX_DATA_TEXT_FONT_RENDER_UUID, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_UUID);
+ (h->read_line + prefix_len) >> data->font_render_uuid;
+ return true;
+ }
+ return pke_deserialize_font_render_settings(h, &data->font_render_settings);
+ return false;
+}
+
+bool pke_serialize_ui_box(srlztn_serialize_helper *h, pke_ui_box *box) {
+ if (box->uuid != pk_uuid_zed && box->uuid != pk_uuid_max) {
+ h->o << SRLZTN_UI_BOX_UUID << box->uuid << std::endl;
+ }
+ if (box->parentHandle != EntityHandle_MAX) {
+ Entity_Base *e = ECS_GetEntity(box->parentHandle);
+ h->o << SRLZTN_UI_BOX_PARENT_UUID << e->uuid << std::endl;
+ }
+ if (box->flags != PKE_UI_BOX_FLAG_NONE) {
+ h->o << SRLZTN_UI_BOX_FLAGS << (PKE_UI_BOX_FLAG_T)box->flags << std::endl;
+ }
+ if (box->pos_top_left_x != 0.0) {
+ h->o << SRLZTN_UI_BOX_POS_TOP_LEFT_X << box->pos_top_left_x << std::endl;
+ }
+ if (box->pos_top_left_y != 0.0) {
+ h->o << SRLZTN_UI_BOX_POS_TOP_LEFT_Y << box->pos_top_left_y << std::endl;
+ }
+ if (box->min_width != 0.0) {
+ h->o << SRLZTN_UI_BOX_MIN_WIDTH << box->min_width << std::endl;
+ }
+ if (box->min_height != 0.0) {
+ h->o << SRLZTN_UI_BOX_MIN_HEIGHT << box->min_height << std::endl;
+ }
+ if (box->max_width != 0.0) {
+ h->o << SRLZTN_UI_BOX_MAX_WIDTH << box->max_width << std::endl;
+ }
+ if (box->max_height != 0.0) {
+ h->o << SRLZTN_UI_BOX_MAX_HEIGHT << box->max_height << std::endl;
+ }
+ if (box->flex_weight != 0.0) {
+ h->o << SRLZTN_UI_BOX_FLEX_WEIGHT << box->flex_weight << std::endl;
+ }
+ if (box->type != PKE_UI_BOX_TYPE_STANDARD) {
+ h->o << SRLZTN_UI_BOX_TYPE << (PKE_UI_BOX_TYPE_T)box->type << std::endl;
+ }
+ if (box->flex_direction != 0) {
+ h->o << SRLZTN_UI_BOX_FLEX_DIRECTION << box->flex_direction << std::endl;
+ }
+ if (box->layer != 0) {
+ h->o << SRLZTN_UI_BOX_LAYER << box->layer << std::endl;
+ }
+ if (box->type == PKE_UI_BOX_TYPE_TEXT) {
+ pke_ui_box_type_data_text *d = reinterpret_cast<pke_ui_box_type_data_text *>(box->type_data);
+ pke_serialize_ui_box_internal(h, d);
+ }
+ return true;
+}
+
+bool pke_deserialize_ui_box(srlztn_deserialize_helper *h) {
+ uint64_t prefix_len;
+ PK_STN_RES res;
+ char *stn_end;
+ pke_ui_box bx{};
+ pke_ui_box *parent_box = nullptr;
+ void *type_data = nullptr;
+ memset(&bx, 0, sizeof(pke_ui_box));
+ bx.type_data = nullptr;
+ while (h->i->getline(h->read_line, h->read_line_len)) {
+ if (strstr(SRLZTN_OBJ_END, h->read_line)) {
+ 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_x = bx.pos_top_left_x;
+ box->pos_top_left_y = bx.pos_top_left_y;
+ box->min_width = bx.min_width;
+ box->min_height = bx.min_height;
+ box->max_width = bx.max_width;
+ box->max_height = bx.max_height;
+ box->flex_weight = bx.flex_weight;
+ box->type = bx.type;
+ box->flex_direction = bx.flex_direction;
+ box->layer = bx.layer;
+ return true;
+ }
+ if (strstr(SRLZTN_UI_BOX_UUID, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_UUID);
+ (h->read_line + prefix_len) >> bx.uuid;
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_PARENT_UUID, h->read_line)) {
+ uint32_t target_instance_index = -1;
+ pk_uuid id;
+ prefix_len = strlen(SRLZTN_UI_BOX_PARENT_UUID);
+ (h->read_line + prefix_len) >> 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, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_FLAGS);
+ PKE_UI_BOX_FLAG_T flags;
+ res = pk_stn(&flags, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_FLAGS, res);
+ continue;
+ }
+ bx.flags = PKE_UI_BOX_FLAG(flags);
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_POS_TOP_LEFT_X, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_POS_TOP_LEFT_X);
+ res = pk_stn(&bx.pos_top_left_x, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_POS_TOP_LEFT_X, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_POS_TOP_LEFT_Y, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_POS_TOP_LEFT_Y);
+ res = pk_stn(&bx.pos_top_left_y, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_POS_TOP_LEFT_Y, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_MIN_WIDTH, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_MIN_WIDTH);
+ res = pk_stn(&bx.min_width, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_MIN_WIDTH, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_MIN_HEIGHT, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_MIN_HEIGHT);
+ res = pk_stn(&bx.min_width, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_MIN_HEIGHT, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_MAX_WIDTH, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_MAX_WIDTH);
+ res = pk_stn(&bx.max_width, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_MAX_WIDTH, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_MAX_HEIGHT, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_MAX_HEIGHT);
+ res = pk_stn(&bx.max_width, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_MAX_HEIGHT, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_FLEX_WEIGHT, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_FLEX_WEIGHT);
+ res = pk_stn(&bx.flex_weight, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_FLEX_WEIGHT, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_FLEX_DIRECTION, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_FLEX_DIRECTION);
+ res = pk_stn(&bx.flex_direction, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_FLEX_DIRECTION, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_LAYER, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_LAYER);
+ res = pk_stn(&bx.layer, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_LAYER, res);
+ continue;
+ }
+ continue;
+ }
+ if (strstr(SRLZTN_UI_BOX_TYPE, h->read_line)) {
+ prefix_len = strlen(SRLZTN_UI_BOX_TYPE);
+ res = pk_stn(&bx.type, h->read_line + prefix_len, &stn_end);
+ if (res != PK_STN_RES_SUCCESS) {
+ fprintf(stderr, "[pke_deserialize_ui_box] Failed to parse value for: '%s', err: %i", SRLZTN_UI_BOX_TYPE, res);
+ continue;
+ }
+ // TODO specific bucket?
+ switch (bx.type) {
+ case PKE_UI_BOX_TYPE_TEXT:
+ type_data = pk_new<pke_ui_box_type_data_text>();
+ break;
+ default:
+ fprintf(stderr, "[pke_deserialize_ui_box] Parsed unknown ui box data type from: '%s'", h->read_line);
+ continue;
+ }
+ continue;
+ }
+ // TODO something isn't right here, too much room for parsing mistakes
+ if (bx.type != PKE_UI_BOX_TYPE_STANDARD && type_data != nullptr) {
+ switch (bx.type) {
+ case PKE_UI_BOX_TYPE_TEXT:
+ if (pke_deserialize_ui_box_internal(h, reinterpret_cast<pke_ui_box_type_data_text *>(bx.type_data))) {
+ continue;
+ }
+ break;
+ default:
+ continue;
+ }
+ }
+ }
+ return false;
+}