From 099b4cd13cf13635849b0fc6eb36b70cc22dd699 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 3 Dec 2024 21:33:51 -0700 Subject: [PATCH] Changed UI logic to pass container pointer to callbacks and keep track of active container pointer instead of ID --- client/include/ui.h | 63 ++++++++++++--- client/src/editor.c | 184 ++++++++++---------------------------------- client/src/engine.c | 12 ++- 3 files changed, 105 insertions(+), 154 deletions(-) diff --git a/client/include/ui.h b/client/include/ui.h index 609be4d..76e6dac 100644 --- a/client/include/ui.h +++ b/client/include/ui.h @@ -144,13 +144,56 @@ typedef struct GPUContainerStruct { } GPUContainer; typedef struct UIContextStruct UIContext; +typedef struct ContainerStruct Container; -typedef bool (*ui_text_callback)(void* data, UIContext* ui, RenderContext* gpu, unsigned int codepoint); -typedef bool (*ui_key_callback)(void* data, UIContext* ui, RenderContext* gpu, int key, int action, int mods); -typedef bool (*ui_button_callback)(void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods); -typedef bool (*ui_scroll_callback)(void* data, UIContext* ui, RenderContext* gpu, double x, double y); -typedef bool (*ui_cursor_callback)(void* data, UIContext* ui, RenderContext* gpu, float x, float y); -typedef void (*ui_deselect_callback)(void* data, UIContext* ui, RenderContext* gpu); +typedef bool (*ui_text_callback)( + Container* container, + void* data, + UIContext* ui, + RenderContext* gpu, + unsigned int codepoint); + +typedef bool (*ui_key_callback)( + Container* container, + void* data, + UIContext* ui, + RenderContext* gpu, + int key, + int action, + int mods); + +typedef bool (*ui_button_callback)( + Container* container, + void* data, + UIContext* ui, + RenderContext* gpu, + float x, + float y, + int button, + int action, + int mods); + +typedef bool (*ui_scroll_callback)( + Container* container, + void* data, + UIContext* ui, + RenderContext* gpu, + double x, + double y); + +typedef bool (*ui_cursor_callback)( + Container* container, + void* data, + UIContext* ui, + RenderContext* gpu, + float x, + float y); + +typedef void (*ui_deselect_callback)( + Container* container, + void* data, + UIContext* ui, + RenderContext* gpu); typedef struct UICallbacksStruct { uint32_t layer; @@ -165,7 +208,7 @@ typedef struct UICallbacksStruct { ui_deselect_callback deselect; } UICallbacks; -typedef struct ContainerStruct { +struct ContainerStruct { VkBuffer container[MAX_FRAMES_IN_FLIGHT]; VmaAllocation container_memory[MAX_FRAMES_IN_FLIGHT]; @@ -180,7 +223,7 @@ typedef struct ContainerStruct { uint32_t callback_count; UICallbacks* callbacks; -} Container; +}; typedef struct ContainerInputStruct { uint32_t id; @@ -238,7 +281,7 @@ struct UIContextStruct { FT_Library freetype; UICallbacks* active_callbacks; - uint32_t active_container; + Container* active_container; uint32_t active_layer; uint32_t active_element; @@ -295,7 +338,7 @@ VkResult update_ui_context_resolution( VkResult update_ui_string( const char* string, - uint32_t container_id, + Container* container, uint32_t layer_index, uint32_t string_index, UIContext* ui, diff --git a/client/src/editor.c b/client/src/editor.c index 79672f9..00e7497 100644 --- a/client/src/editor.c +++ b/client/src/editor.c @@ -127,17 +127,17 @@ uint32_t add_hex_region(ClientContext* context) { return i; } -void update_hex_string(UIContext* ui, RenderContext* gpu, EditorData* data) { +void update_hex_string(Container* container, UIContext* ui, RenderContext* gpu, EditorData* data) { snprintf(data->string, 10, "#%02X%02X%02X%02X", (uint)rintf(data->current[0]*255), (uint)rintf(data->current[1]*255), (uint)rintf(data->current[2]*255), (uint)rintf(data->current[3]*255)); data->string_len = 8; - update_ui_string(data->string, COLOR_PICK_CONTAINER_ID, 0, 0, ui, gpu); + update_ui_string(data->string, container, 0, 0, ui, gpu); } -void sv_square_pick(UIContext* ui, RenderContext* gpu, EditorData* data, float s, float v) { +void sv_square_pick(Container* container, UIContext* ui, RenderContext* gpu, EditorData* data, float s, float v) { if(s < 0) s = 0; if(s > 1) s = 1; @@ -147,7 +147,6 @@ void sv_square_pick(UIContext* ui, RenderContext* gpu, EditorData* data, float s data->current_hsv[1] = s; data->current_hsv[2] = v; - Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui); Layer* layer = &container->layers[0]; GPUDrawable* select_outline = &layer->drawables_buffer[3]; GPUDrawable* select = &layer->drawables_buffer[4]; @@ -192,16 +191,16 @@ void sv_square_pick(UIContext* ui, RenderContext* gpu, EditorData* data, float s gpu); hsv_to_rgb(data->current_hsv, data->current); - update_hex_string(ui, gpu, data); + update_hex_string(container, ui, gpu, data); } -bool sv_square_button_callback(void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { +bool sv_square_button_callback(Container* container, void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { (void)mods; (void)x; if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) { set_active_element(COLOR_PICK_CONTAINER_ID, 0, 1, ui); - sv_square_pick(ui, gpu, data, x, 1-y); + sv_square_pick(container, ui, gpu, data, x, 1-y); } else if(action == GLFW_RELEASE && button == GLFW_MOUSE_BUTTON_LEFT) { clear_active_element(ui, gpu); } @@ -209,23 +208,22 @@ bool sv_square_button_callback(void* data, UIContext* ui, RenderContext* gpu, fl return true; } -bool sv_square_cursor_callback(void* data, UIContext* ui, RenderContext* gpu, float x, float y) { +bool sv_square_cursor_callback(Container* container, void* data, UIContext* ui, RenderContext* gpu, float x, float y) { if(ui->active_element == 1 && ui->active_layer == 0 - && ui->active_container == COLOR_PICK_CONTAINER_ID) { - sv_square_pick(ui, gpu, data, x, 1-y); + && ui->active_container == container) { + sv_square_pick(container, ui, gpu, data, x, 1-y); } return true; } -void hue_bar_set(UIContext* ui, RenderContext* gpu, EditorData* data, float y) { +void hue_bar_set(Container* container, UIContext* ui, RenderContext* gpu, EditorData* data, float y) { if(y < 0) y = 0; if(y > 1) y = 1; data->current_hsv[0] = y; - Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui); Layer* layer = &container->layers[0]; GPUDrawable* sv_square = &layer->drawables_buffer[1]; GPUDrawable* sv_select = &layer->drawables_buffer[4]; @@ -265,37 +263,36 @@ void hue_bar_set(UIContext* ui, RenderContext* gpu, EditorData* data, float y) { gpu); hsv_to_rgb(data->current_hsv, data->current); - update_hex_string(ui, gpu, data); + update_hex_string(container, ui, gpu, data); } -bool hue_bar_scroll_callback(void* data, UIContext* ui, RenderContext* gpu, double x, double y) { +bool hue_bar_scroll_callback(Container* container, void* data, UIContext* ui, RenderContext* gpu, double x, double y) { (void)x; - Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui); - hue_bar_set(ui, gpu, data, y*0.01 + container->layers[0].drawables_buffer[1].color[0][0]); + hue_bar_set(container, ui, gpu, data, y*0.01 + container->layers[0].drawables_buffer[1].color[0][0]); return true; } -bool hue_bar_cursor_callback(void*data, UIContext* ui, RenderContext* gpu, float x, float y) { +bool hue_bar_cursor_callback(Container* container, void*data, UIContext* ui, RenderContext* gpu, float x, float y) { (void)x; if(ui->active_element == 2 && ui->active_layer == 0 - && ui->active_container == COLOR_PICK_CONTAINER_ID) { - hue_bar_set(ui, gpu, data, y); + && ui->active_container == container) { + hue_bar_set(container, ui, gpu, data, y); } return true; } -bool hue_bar_button_callback(void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { +bool hue_bar_button_callback(Container* container, void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { (void)mods; (void)x; if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) { set_active_element(COLOR_PICK_CONTAINER_ID, 0, 2, ui); - hue_bar_set(ui, gpu, data, y); + hue_bar_set(container, ui, gpu, data, y); } else if(action == GLFW_RELEASE && button == GLFW_MOUSE_BUTTON_LEFT) { clear_active_element(ui, gpu); } @@ -303,8 +300,7 @@ bool hue_bar_button_callback(void* data, UIContext* ui, RenderContext* gpu, floa return true; } -void hex_string_set_color(UIContext* ui, RenderContext* gpu, float color) { - Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui); +void hex_string_set_color(Container* container, RenderContext* gpu, float color) { container->layers[0].drawables_buffer[5].color[0][2] = color; container->layers[0].drawables_buffer[5].color[0][3] = color; @@ -325,7 +321,7 @@ void hex_string_set_color(UIContext* ui, RenderContext* gpu, float color) { gpu); } -bool hex_string_text_callback(void* ptr, UIContext* ui, RenderContext* gpu, unsigned int codepoint) { +bool hex_string_text_callback(Container* container, void* ptr, UIContext* ui, RenderContext* gpu, unsigned int codepoint) { EditorData* data = ptr; if(codepoint >= 'a' && codepoint <= 'f') { @@ -337,13 +333,13 @@ bool hex_string_text_callback(void* ptr, UIContext* ui, RenderContext* gpu, unsi if(data->string_len < 8) { data->string_len += 1; data->string[data->string_len] = codepoint; - update_ui_string(data->string, COLOR_PICK_CONTAINER_ID, 0, 0, ui, gpu); + update_ui_string(data->string, container, 0, 0, ui, gpu); } return true; } -bool hex_string_key_callback(void* ptr, UIContext* ui, RenderContext* gpu, int key, int action, int mods) { +bool hex_string_key_callback(Container* container, void* ptr, UIContext* ui, RenderContext* gpu, int key, int action, int mods) { (void)mods; EditorData* data = ptr; char tmp[3]; @@ -352,7 +348,7 @@ bool hex_string_key_callback(void* ptr, UIContext* ui, RenderContext* gpu, int k switch(key) { case GLFW_KEY_ESCAPE: hsv_to_rgb(data->current_hsv, data->current); - update_hex_string(ui, gpu, data); + update_hex_string(container, ui, gpu, data); clear_active_element(ui, gpu); break; case GLFW_KEY_ENTER: @@ -378,8 +374,8 @@ bool hex_string_key_callback(void* ptr, UIContext* ui, RenderContext* gpu, int k data->current[3] = strtol(tmp, NULL, 16)/255.0; rgb_to_hsv(data->current, data->current_hsv); - hue_bar_set(ui, gpu, data, data->current_hsv[0]); - sv_square_pick(ui, gpu, data, data->current_hsv[1], data->current_hsv[2]); + hue_bar_set(container, ui, gpu, data, data->current_hsv[0]); + sv_square_pick(container, ui, gpu, data, data->current_hsv[1], data->current_hsv[2]); clear_active_element(ui, gpu); break; @@ -387,7 +383,7 @@ bool hex_string_key_callback(void* ptr, UIContext* ui, RenderContext* gpu, int k if(data->string_len > 0) { data->string[data->string_len] = '\0'; data->string_len -= 1; - update_ui_string(data->string, COLOR_PICK_CONTAINER_ID, 0, 0, ui, gpu); + update_ui_string(data->string, container, 0, 0, ui, gpu); } break; } @@ -396,7 +392,7 @@ bool hex_string_key_callback(void* ptr, UIContext* ui, RenderContext* gpu, int k return true; } -bool hex_string_button_callback(void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { +bool hex_string_button_callback(Container* container, void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { (void)data; (void)mods; (void)x; @@ -404,24 +400,24 @@ bool hex_string_button_callback(void* data, UIContext* ui, RenderContext* gpu, f if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) { set_active_element(COLOR_PICK_CONTAINER_ID, 0, 5, ui); - hex_string_set_color(ui, gpu, 1); + hex_string_set_color(container, gpu, 1); } return true; } -void hex_string_deselect_callback(void* data, UIContext* ui, RenderContext* gpu) { +void hex_string_deselect_callback(Container* container, void* data, UIContext* ui, RenderContext* gpu) { (void)data; - hex_string_set_color(ui, gpu, 0); + (void)ui; + hex_string_set_color(container, gpu, 0); } -void set_saved_color(UIContext* ui, RenderContext* gpu, EditorData* data, uint32_t index, vec4 color) { +void set_saved_color(Container* container, RenderContext* gpu, EditorData* data, uint32_t index, vec4 color) { data->saved[index][0] = color[0]; data->saved[index][1] = color[1]; data->saved[index][2] = color[2]; data->saved[index][3] = color[3]; - Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui); Layer* layer = &container->layers[0]; GPUDrawable* saved_color = &layer->drawables_buffer[7 + index]; @@ -450,6 +446,7 @@ void set_saved_color(UIContext* ui, RenderContext* gpu, EditorData* data, uint32 } bool saved_color_button_callback( + Container* container, void* ptr, UIContext* ui, RenderContext* gpu, @@ -469,14 +466,14 @@ bool saved_color_button_callback( data->current[2] = data->saved[index][2]; data->current[3] = data->saved[index][3]; rgb_to_hsv(data->current, data->current_hsv); - hue_bar_set(ui, gpu, data, data->current_hsv[0]); - sv_square_pick(ui, gpu, data, data->current_hsv[1], data->current_hsv[2]); - update_hex_string(ui, gpu, data); + hue_bar_set(container, ui, gpu, data, data->current_hsv[0]); + sv_square_pick(container, ui, gpu, data, data->current_hsv[1], data->current_hsv[2]); + update_hex_string(container, ui, gpu, data); } else if(button == GLFW_MOUSE_BUTTON_RIGHT) { - set_saved_color(ui, gpu, data, index, data->current); + set_saved_color(container, gpu, data, index, data->current); } else if (button == GLFW_MOUSE_BUTTON_MIDDLE) { vec4 clear = {0, 0, 0, 0}; - set_saved_color(ui, gpu, data, index, clear); + set_saved_color(container, gpu, data, index, clear); } } @@ -484,8 +481,8 @@ bool saved_color_button_callback( } #define SAVED_COLOR_BUTTON_CALLBACK(n) \ -bool saved_color_button_callback_##n(void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { \ - return saved_color_button_callback(data, ui, gpu, x, y, button, action, mods, n); \ +bool saved_color_button_callback_##n(Container* container, void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) { \ + return saved_color_button_callback(container, data, ui, gpu, x, y, button, action, mods, n); \ } SAVED_COLOR_BUTTON_CALLBACK( 0); @@ -729,104 +726,6 @@ VkResult color_ui(ClientContext* context) { return load_container(&container, &context->render, &context->ui); } -VkResult hex_info_ui(ClientContext* context) { - GPUString strings[] = { - { - .pos = {0, 33}, - .color = {1, 1, 1, 1}, - .size = 32, - .offset = 0, - .length = 0, - .font = 0, - }, - { - .pos = {2, 50}, - .color = {1, 1, 1, 1}, - .size = 16, - .offset = 20, - .length = 0, - .font = 0, - }, - { - .pos = {2, 73}, - .color = {1, 1, 1, 1}, - .size = 16, - .offset = 40, - .length = 0, - .font = 0, - }, - { - .pos = {2, 93}, - .color = {1, 1, 1, 1}, - .size = 16, - .offset = 60, - .length = 0, - .font = 0, - }, - { - .pos = {2, 113}, - .color = {1, 1, 1, 1}, - .size = 16, - .offset = 80, - .length = 0, - .font = 0, - }, - { - .pos = {2, 133}, - .color = {1, 1, 1, 1}, - .size = 16, - .offset = 100, - .length = 0, - .font = 0, - }, - { - .pos = {2, 153}, - .color = {1, 1, 1, 1}, - .size = 16, - .offset = 120, - .length = 0, - .font = 0, - }, - { - .pos = {2, 173}, - .color = {1, 1, 1, 1}, - .size = 16, - .offset = 140, - .length = 0, - .font = 0, - }, - - }; - - GPUDrawable drawables[] = { - { - .pos = {0, 0}, - .size = {150, 175}, - .color = {{0.4, 0.4, 0.4, 0.8}, {0.4, 0.4, 0.4, 0.8}, {0.4, 0.4, 0.4, 0.8}, {0.4, 0.4, 0.4, 0.8}}, - }, - }; - - LayerInput layer = { - .strings = strings, - .num_strings = sizeof(strings)/sizeof(GPUString), - .max_strings = sizeof(strings)/sizeof(GPUString) + 4, - .max_codes = 200, - .drawables = drawables, - .num_drawables = sizeof(drawables)/sizeof(GPUDrawable), - }; - - ContainerInput container = { - .id = 0x02, - .size = {150, 175}, - .offset = {0, 0}, - .anchor = ANCHOR_BOTTOM_RIGHT, - .layers = &layer, - .layer_count = 1, - }; - - return load_container(&container, &context->render, &context->ui); -} - VkResult mode_string_ui(ClientContext* context) { VkResult result; @@ -861,7 +760,8 @@ VkResult mode_string_ui(ClientContext* context) { } VkResult update_mode_string(ClientContext* context, EditorData* data) { - return update_ui_string(ModeStrings[data->mode], MODE_STRING_CONTAINER_ID, 0, 0, &context->ui, &context->render); + Container* container = context_container(MODE_STRING_CONTAINER_ID, &context->ui); + return update_ui_string(ModeStrings[data->mode], container, 0, 0, &context->ui, &context->render); } void editor_key_callback(ClientContext* context, int key, int action, int mods) { diff --git a/client/src/engine.c b/client/src/engine.c index eb3206e..f1a9094 100644 --- a/client/src/engine.c +++ b/client/src/engine.c @@ -7,6 +7,7 @@ void text_callback(GLFWwindow* window, unsigned int codepoint) { if(context->ui.active_callbacks != NULL && context->ui.active_callbacks->text != NULL && context->ui.active_callbacks->text( + context->ui.active_container, context->ui.active_callbacks->data, &context->ui, &context->render, @@ -24,6 +25,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod if(context->ui.active_callbacks != NULL && context->ui.active_callbacks->key != NULL && context->ui.active_callbacks->key( + context->ui.active_container, context->ui.active_callbacks->data, &context->ui, &context->render, @@ -44,7 +46,7 @@ void button_callback(GLFWwindow* window, int button, int action, int mods) { glfwGetCursorPos(window, &cursor[0], &cursor[1]); if(context->ui.active_callbacks != NULL && context->ui.active_callbacks->button != NULL) { - Container* container_ptr = context_container(context->ui.active_container, &context->ui); + Container* container_ptr = context->ui.active_container; GPUDrawable* drawable_ptr = &container_ptr->layers[context->ui.active_layer].drawables_buffer[context->ui.active_element]; vec2 element_pos = { drawable_ptr->pos[0] + container_ptr->data.offset[0], @@ -63,6 +65,7 @@ void button_callback(GLFWwindow* window, int button, int action, int mods) { if((point[0] <= 1 && point[0] >= 0 && point[1] <= 1 && point[1] >= 0) || action == GLFW_RELEASE) { context->ui.active_callbacks->button( + context->ui.active_container, context->ui.active_callbacks->data, &context->ui, &context->render, @@ -83,6 +86,7 @@ void button_callback(GLFWwindow* window, int button, int action, int mods) { if(container_ptr->callbacks[c].element == element) { if(container_ptr->callbacks[c].button != NULL) { if(container_ptr->callbacks[c].button( + container_ptr, container_ptr->callbacks[c].data, &context->ui, &context->render, @@ -111,6 +115,7 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { if(context->ui.active_callbacks != NULL && context->ui.active_callbacks->scroll != NULL && context->ui.active_callbacks->scroll( + context->ui.active_container, context->ui.active_callbacks->data, &context->ui, &context->render, @@ -130,6 +135,7 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { if(container_ptr->callbacks[c].element == element) { if(container_ptr->callbacks[c].scroll != NULL) { container_ptr->callbacks[c].scroll( + container_ptr, container_ptr->callbacks[c].data, &context->ui, &context->render, @@ -155,7 +161,7 @@ void cursor_callback(GLFWwindow* window, double xpos, double ypos) { context->ui.cursor[1] = ypos; if(context->ui.active_callbacks != NULL && context->ui.active_callbacks->cursor != NULL) { - Container* container_ptr = context_container(context->ui.active_container, &context->ui); + Container* container_ptr = context->ui.active_container; GPUDrawable* drawable_ptr = &container_ptr->layers[context->ui.active_layer].drawables_buffer[context->ui.active_element]; vec2 element_pos = { drawable_ptr->pos[0] + container_ptr->data.offset[0], @@ -167,6 +173,7 @@ void cursor_callback(GLFWwindow* window, double xpos, double ypos) { drawable_ptr->size[1], }; if(context->ui.active_callbacks->cursor( + context->ui.active_container, context->ui.active_callbacks->data, &context->ui, &context->render, @@ -180,6 +187,7 @@ void cursor_callback(GLFWwindow* window, double xpos, double ypos) { if(container_ptr->callbacks[c].element == element) { if(container_ptr->callbacks[c].cursor != NULL) { container_ptr->callbacks[c].cursor( + container_ptr, container_ptr->callbacks[c].data, &context->ui, &context->render,