Updated ui callbacks

main
noah metz 2024-11-13 12:19:06 -07:00
parent 89bd65f649
commit 9b5b65a5a8
3 changed files with 364 additions and 297 deletions

@ -23,7 +23,6 @@
#define UI_EVENT_CURSOR (1 << 0) #define UI_EVENT_CURSOR (1 << 0)
#define UI_EVENT_SCROLL (1 << 1) #define UI_EVENT_SCROLL (1 << 1)
#define UI_EVENT_BUTTON (1 << 2) #define UI_EVENT_BUTTON (1 << 2)
#define UI_EVENT_KEY (1 << 3)
typedef struct UIPushConstantStruct { typedef struct UIPushConstantStruct {
VkDeviceAddress layer; VkDeviceAddress layer;
@ -144,10 +143,13 @@ typedef struct GPUContainerStruct {
VkDeviceAddress context; VkDeviceAddress context;
} GPUContainer; } GPUContainer;
typedef void (*ui_key_callback)(void* ptr, int key, int action, int mods); typedef struct UIContextStruct UIContext;
typedef void (*ui_button_callback)(void* ptr, float x, float y, int button, int action, int mods);
typedef void (*ui_scroll_callback)(void* ptr, double x, double y); typedef bool (*ui_key_callback)(UIContext* ui, RenderContext* gpu, int key, int action, int mods);
typedef void (*ui_cursor_callback)(void* ptr, float x, float y); typedef void (*ui_button_callback)(UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods);
typedef void (*ui_scroll_callback)(UIContext* ui, RenderContext* gpu, double x, double y);
typedef void (*ui_cursor_callback)(UIContext* ui, RenderContext* gpu, float x, float y);
typedef void (*ui_deselect_callback)(UIContext* ui, RenderContext* gpu);
typedef struct UICallbacksStruct { typedef struct UICallbacksStruct {
uint32_t layer; uint32_t layer;
@ -156,6 +158,7 @@ typedef struct UICallbacksStruct {
ui_button_callback button; ui_button_callback button;
ui_scroll_callback scroll; ui_scroll_callback scroll;
ui_cursor_callback cursor; ui_cursor_callback cursor;
ui_deselect_callback deselect;
} UICallbacks; } UICallbacks;
typedef struct ContainerStruct { typedef struct ContainerStruct {
@ -195,7 +198,7 @@ typedef struct GPUUIContextStruct {
vec2 scale; vec2 scale;
} GPUUIContext; } GPUUIContext;
typedef struct UIContextStruct { struct UIContextStruct {
VkDeviceAddress address; VkDeviceAddress address;
VkBuffer context; VkBuffer context;
@ -236,7 +239,7 @@ typedef struct UIContextStruct {
uint32_t active_element; uint32_t active_element;
double cursor[2]; double cursor[2];
} UIContext; };
VkResult create_ui_context( VkResult create_ui_context(
uint32_t max_fonts, uint32_t max_fonts,
@ -303,7 +306,7 @@ bool ui_intersect(
void set_active_element(uint32_t container, uint32_t layer, uint32_t element, UIContext* ui); void set_active_element(uint32_t container, uint32_t layer, uint32_t element, UIContext* ui);
void clear_active_element(UIContext* ui); void clear_active_element(UIContext* ui, RenderContext* gpu);
void anchor_offset(RenderContext* gpu, Container* container, vec2 offset); void anchor_offset(RenderContext* gpu, Container* container, vec2 offset);

@ -22,10 +22,6 @@ typedef struct EditorContextStruct {
uint32_t last_clicked_region; uint32_t last_clicked_region;
uint32_t last_clicked_hex; uint32_t last_clicked_hex;
uint32_t last_clicked_vertex; uint32_t last_clicked_vertex;
vec4 saved_colors[12];
vec4 current_color;
vec4 color_string;
} EditorContext; } EditorContext;
typedef struct ClientContextStruct { typedef struct ClientContextStruct {
@ -128,7 +124,7 @@ void hsv_to_rgb(vec3 hsv, vec3 rgb) {
rgb[2] = temp[2] + m; rgb[2] = temp[2] + m;
} }
void rgb_string_set(ClientContext* context, vec3 hsv) { void rgb_string_set(UIContext* ui, RenderContext* gpu, vec3 hsv) {
vec3 rgb; vec3 rgb;
hsv_to_rgb(hsv, rgb); hsv_to_rgb(hsv, rgb);
char temp[10]; char temp[10];
@ -136,17 +132,17 @@ void rgb_string_set(ClientContext* context, vec3 hsv) {
(uint)(rgb[0]*255), (uint)(rgb[0]*255),
(uint)(rgb[1]*255), (uint)(rgb[1]*255),
(uint)(rgb[2]*255)); (uint)(rgb[2]*255));
update_ui_string(temp, COLOR_PICK_CONTAINER_ID, 0, 0, context->ui, context->render); update_ui_string(temp, COLOR_PICK_CONTAINER_ID, 0, 0, ui, gpu);
} }
void sv_square_pick(ClientContext* context, float s, float v) { void sv_square_pick(UIContext* ui, RenderContext* gpu, float s, float v) {
if(s < 0) s = 0; if(s < 0) s = 0;
if(s > 1) s = 1; if(s > 1) s = 1;
if(v < 0) v = 0; if(v < 0) v = 0;
if(v > 1) v = 1; if(v > 1) v = 1;
Container* container = context_container(COLOR_PICK_CONTAINER_ID, context->ui); Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui);
Layer* layer = &container->layers[0]; Layer* layer = &container->layers[0];
GPUDrawable* select_outline = &layer->drawables_buffer[3]; GPUDrawable* select_outline = &layer->drawables_buffer[3];
GPUDrawable* select = &layer->drawables_buffer[4]; GPUDrawable* select = &layer->drawables_buffer[4];
@ -174,53 +170,50 @@ void sv_square_pick(ClientContext* context, float s, float v) {
layer->drawables, layer->drawables,
3*sizeof(GPUDrawable) + offsetof(GPUDrawable, pos), 3*sizeof(GPUDrawable) + offsetof(GPUDrawable, pos),
1*sizeof(vec2), 1*sizeof(vec2),
context->render); gpu);
add_transfers( add_transfers(
&select->pos[0], &select->pos[0],
layer->drawables, layer->drawables,
4*sizeof(GPUDrawable) + offsetof(GPUDrawable, pos), 4*sizeof(GPUDrawable) + offsetof(GPUDrawable, pos),
1*sizeof(vec2), 1*sizeof(vec2),
context->render); gpu);
add_transfers( add_transfers(
&select->color[0], &select->color[0],
layer->drawables, layer->drawables,
4*sizeof(GPUDrawable) + offsetof(GPUDrawable, color), 4*sizeof(GPUDrawable) + offsetof(GPUDrawable, color),
4*sizeof(vec4), 4*sizeof(vec4),
context->render); gpu);
rgb_string_set(context, select->color[0]); rgb_string_set(ui, gpu, select->color[0]);
} }
void sv_square_button_callback(void* ptr, float x, float y, int button, int action, int mods) { void sv_square_button_callback(UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) {
(void)mods; (void)mods;
(void)x; (void)x;
ClientContext* context = (ClientContext*)ptr;
if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) { if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) {
set_active_element(COLOR_PICK_CONTAINER_ID, 0, 1, context->ui); set_active_element(COLOR_PICK_CONTAINER_ID, 0, 1, ui);
sv_square_pick(context, x, y); sv_square_pick(ui, gpu, x, y);
} else if(action == GLFW_RELEASE && button == GLFW_MOUSE_BUTTON_LEFT) { } else if(action == GLFW_RELEASE && button == GLFW_MOUSE_BUTTON_LEFT) {
clear_active_element(context->ui); clear_active_element(ui, gpu);
} }
} }
void sv_square_cursor_callback(void* ptr, float x, float y) { void sv_square_cursor_callback(UIContext* ui, RenderContext* gpu, float x, float y) {
ClientContext* context = (ClientContext*)ptr; if(ui->active_element == 1
&& ui->active_layer == 0
if(context->ui->active_element == 1 && ui->active_container == COLOR_PICK_CONTAINER_ID) {
&& context->ui->active_layer == 0 sv_square_pick(ui, gpu, x, y);
&& context->ui->active_container == COLOR_PICK_CONTAINER_ID) {
sv_square_pick(context, x, y);
} }
} }
void hue_bar_set(ClientContext* context, float y) { void hue_bar_set(UIContext* ui, RenderContext* gpu, float y) {
if(y < 0) y = 0; if(y < 0) y = 0;
if(y > 1) y = 1; if(y > 1) y = 1;
Container* container = context_container(COLOR_PICK_CONTAINER_ID, context->ui); Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui);
Layer* layer = &container->layers[0]; Layer* layer = &container->layers[0];
GPUDrawable* sv_square = &layer->drawables_buffer[1]; GPUDrawable* sv_square = &layer->drawables_buffer[1];
GPUDrawable* select = &layer->drawables_buffer[4]; GPUDrawable* select = &layer->drawables_buffer[4];
@ -240,52 +233,95 @@ void hue_bar_set(ClientContext* context, float y) {
layer->drawables, layer->drawables,
1*sizeof(GPUDrawable) + offsetof(GPUDrawable, color), 1*sizeof(GPUDrawable) + offsetof(GPUDrawable, color),
4*sizeof(vec4), 4*sizeof(vec4),
context->render); gpu);
add_transfers( add_transfers(
&select->color[0], &select->color[0],
layer->drawables, layer->drawables,
4*sizeof(GPUDrawable) + offsetof(GPUDrawable, color), 4*sizeof(GPUDrawable) + offsetof(GPUDrawable, color),
4*sizeof(vec4), 4*sizeof(vec4),
context->render); gpu);
rgb_string_set(context, select->color[0]); rgb_string_set(ui, gpu, select->color[0]);
} }
void hue_bar_scroll_callback(void* ptr, double x, double y) { void hue_bar_scroll_callback(UIContext* ui, RenderContext* gpu, double x, double y) {
(void)x; (void)x;
ClientContext* context = (ClientContext*)ptr; Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui);
hue_bar_set(ui, gpu, y*0.01 + container->layers[0].drawables_buffer[1].color[0][0]);
}
void hue_bar_cursor_callback(UIContext* ui, RenderContext* gpu, float x, float y) {
(void)x;
Container* container = context_container(COLOR_PICK_CONTAINER_ID, context->ui); if(ui->active_element == 2
hue_bar_set(context, y*0.01 + container->layers[0].drawables_buffer[1].color[0][0]); && ui->active_layer == 0
&& ui->active_container == COLOR_PICK_CONTAINER_ID) {
hue_bar_set(ui, gpu, y);
}
} }
void hue_bar_cursor_callback(void* ptr, float x, float y) { void hue_bar_button_callback(UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) {
(void)mods;
(void)x; (void)x;
ClientContext* context = (ClientContext*)ptr; if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) {
set_active_element(COLOR_PICK_CONTAINER_ID, 0, 2, ui);
hue_bar_set(ui, gpu, y);
} else if(action == GLFW_RELEASE && button == GLFW_MOUSE_BUTTON_LEFT) {
clear_active_element(ui, gpu);
}
}
void hex_string_set_color(UIContext* ui, RenderContext* gpu, float color) {
Container* container = context_container(COLOR_PICK_CONTAINER_ID, ui);
container->layers[0].drawables_buffer[5].color[0][2] = color;
container->layers[0].drawables_buffer[5].color[0][3] = color;
container->layers[0].drawables_buffer[5].color[1][2] = color;
container->layers[0].drawables_buffer[5].color[1][3] = color;
container->layers[0].drawables_buffer[5].color[2][2] = color;
container->layers[0].drawables_buffer[5].color[2][3] = color;
container->layers[0].drawables_buffer[5].color[3][2] = color;
container->layers[0].drawables_buffer[5].color[3][3] = color;
add_transfers(
&container->layers[0].drawables_buffer[5].color[0],
container->layers[0].drawables,
5*sizeof(GPUDrawable) + offsetof(GPUDrawable, color),
4*sizeof(vec4),
gpu);
}
if(context->ui->active_element == 2 bool hex_string_key_callback(UIContext* ui, RenderContext* gpu, int key, int action, int mods) {
&& context->ui->active_layer == 0 (void)mods;
&& context->ui->active_container == COLOR_PICK_CONTAINER_ID) {
hue_bar_set(context, y); if(action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) {
clear_active_element(ui, gpu);
return true;
} }
return false;
} }
void hue_bar_button_callback(void* ptr, float x, float y, int button, int action, int mods) { void hex_string_button_callback(UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods) {
(void)mods; (void)mods;
(void)x; (void)x;
(void)y;
ClientContext* context = (ClientContext*)ptr;
if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) { if(action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT) {
set_active_element(COLOR_PICK_CONTAINER_ID, 0, 2, context->ui); set_active_element(COLOR_PICK_CONTAINER_ID, 0, 5, ui);
hue_bar_set(context, y); hex_string_set_color(ui, gpu, 1);
} else if(action == GLFW_RELEASE && button == GLFW_MOUSE_BUTTON_LEFT) {
clear_active_element(context->ui);
} }
} }
void hex_string_deselect_callback(UIContext* ui, RenderContext* gpu) {
hex_string_set_color(ui, gpu, 0);
}
VkResult color_ui(ClientContext* context) { VkResult color_ui(ClientContext* context) {
GPUString strings[] = { GPUString strings[] = {
{ {
@ -332,6 +368,7 @@ VkResult color_ui(ClientContext* context) {
{ {
.pos = {20, 134}, .pos = {20, 134},
.size = {95, 15}, .size = {95, 15},
.events = UI_EVENT_BUTTON | UI_EVENT_CURSOR,
}, },
{ {
.pos = {146, 2}, .pos = {146, 2},
@ -422,6 +459,13 @@ VkResult color_ui(ClientContext* context) {
.cursor = hue_bar_cursor_callback, .cursor = hue_bar_cursor_callback,
.scroll = hue_bar_scroll_callback, .scroll = hue_bar_scroll_callback,
}, },
{
.layer = 0,
.element = 5,
.button = hex_string_button_callback,
.key = hex_string_key_callback,
.deselect = hex_string_deselect_callback,
},
}; };
ContainerInput container = { ContainerInput container = {
@ -893,8 +937,11 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window); ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window);
if(context->ui->active_callbacks != NULL && context->ui->active_callbacks->key != NULL) { if(context->ui->active_callbacks != NULL && context->ui->active_callbacks->key != NULL) {
context->ui->active_callbacks->key(context, key, action, mods); if(context->ui->active_callbacks->key(context->ui, context->render, key, action, mods)) {
} else { return;
}
}
switch(key) { switch(key) {
case GLFW_KEY_A: case GLFW_KEY_A:
if(action == GLFW_PRESS) { if(action == GLFW_PRESS) {
@ -1124,7 +1171,6 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
} }
break; break;
} }
}
} }
void button_callback(GLFWwindow* window, int button, int action, int mods) { void button_callback(GLFWwindow* window, int button, int action, int mods) {
@ -1150,24 +1196,40 @@ void button_callback(GLFWwindow* window, int button, int action, int mods) {
drawable_ptr->size[0], drawable_ptr->size[0],
drawable_ptr->size[1], drawable_ptr->size[1],
}; };
context->ui->active_callbacks->button(
context, vec2 point = {
(cursor[0] - element_pos[0])/element_size[0], (cursor[0] - element_pos[0])/element_size[0],
(cursor[1] - element_pos[1])/element_size[1], (cursor[1] - element_pos[1])/element_size[1],
};
if(point[0] <= 1 && point[0] >= 0 && point[1] <= 1 && point[1] >= 0) {
context->ui->active_callbacks->button(
context->ui,
context->render,
point[0],
point[1],
button, button,
action, action,
mods); mods);
} else if(ui_intersect(cursor, context->render, context->ui, UI_EVENT_BUTTON, &container, &layer, &element, position)) { return;
} else {
clear_active_element(context->ui, context->render);
}
}
if(ui_intersect(cursor, context->render, context->ui, UI_EVENT_BUTTON, &container, &layer, &element, position)) {
Container* container_ptr = context_container(container, context->ui); Container* container_ptr = context_container(container, context->ui);
for(uint32_t c = 0; c < container_ptr->callback_count; c++) { for(uint32_t c = 0; c < container_ptr->callback_count; c++) {
if(container_ptr->callbacks[c].element == element) { if(container_ptr->callbacks[c].element == element) {
if(container_ptr->callbacks[c].button != NULL) { if(container_ptr->callbacks[c].button != NULL) {
container_ptr->callbacks[c].button(context, position[0], position[1], button, action, mods); container_ptr->callbacks[c].button(context->ui, context->render, position[0], position[1], button, action, mods);
} }
break; break;
} }
} }
} else { } else {
}
update_hex_click(cursor, context->hex, context->render); update_hex_click(cursor, context->hex, context->render);
if(button == GLFW_MOUSE_BUTTON_MIDDLE) { if(button == GLFW_MOUSE_BUTTON_MIDDLE) {
if(action == GLFW_PRESS) { if(action == GLFW_PRESS) {
@ -1178,7 +1240,6 @@ void button_callback(GLFWwindow* window, int button, int action, int mods) {
context->camera_mode = false; context->camera_mode = false;
} }
} }
}
} }
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
@ -1190,7 +1251,7 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window); ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window);
if(context->ui->active_callbacks != NULL && context->ui->active_callbacks->scroll != NULL) { if(context->ui->active_callbacks != NULL && context->ui->active_callbacks->scroll != NULL) {
context->ui->active_callbacks->scroll(context, xoffset, yoffset); context->ui->active_callbacks->scroll(context->ui, context->render, xoffset, yoffset);
} else if(ui_intersect( } else if(ui_intersect(
context->ui->cursor, context->ui->cursor,
context->render, context->render,
@ -1204,7 +1265,7 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
for(uint32_t c = 0; c < container_ptr->callback_count; c++) { for(uint32_t c = 0; c < container_ptr->callback_count; c++) {
if(container_ptr->callbacks[c].element == element) { if(container_ptr->callbacks[c].element == element) {
if(container_ptr->callbacks[c].scroll != NULL) { if(container_ptr->callbacks[c].scroll != NULL) {
container_ptr->callbacks[c].scroll(context, xoffset, yoffset); container_ptr->callbacks[c].scroll(context->ui, context->render, xoffset, yoffset);
} }
break; break;
} }
@ -1245,16 +1306,16 @@ void cursor_callback(GLFWwindow* window, double xpos, double ypos) {
drawable_ptr->size[1], drawable_ptr->size[1],
}; };
context->ui->active_callbacks->cursor( context->ui->active_callbacks->cursor(
context, context->ui,
context->render,
(context->ui->cursor[0] - element_pos[0])/element_size[0], (context->ui->cursor[0] - element_pos[0])/element_size[0],
(context->ui->cursor[1] - element_pos[1])/element_size[1]); (context->ui->cursor[1] - element_pos[1])/element_size[1]);
} else if(ui_intersect(context->ui->cursor, context->render, context->ui, UI_EVENT_CURSOR, &container, &layer, &element, position)) { } else if(ui_intersect(context->ui->cursor, context->render, context->ui, UI_EVENT_CURSOR, &container, &layer, &element, position)) {
fprintf(stderr, "Intersect %d %d %d\n", container, layer, element);
Container* container_ptr = context_container(container, context->ui); Container* container_ptr = context_container(container, context->ui);
for(uint32_t c = 0; c < container_ptr->callback_count; c++) { for(uint32_t c = 0; c < container_ptr->callback_count; c++) {
if(container_ptr->callbacks[c].element == element) { if(container_ptr->callbacks[c].element == element) {
if(container_ptr->callbacks[c].button != NULL) { if(container_ptr->callbacks[c].cursor != NULL) {
container_ptr->callbacks[c].cursor(context, position[0], position[1]); container_ptr->callbacks[c].cursor(context->ui, context->render, position[0], position[1]);
} }
break; break;
} }

@ -1266,7 +1266,10 @@ bool ui_intersect(
return false; return false;
} }
void clear_active_element(UIContext* ui) { void clear_active_element(UIContext* ui, RenderContext* gpu) {
if(ui->active_callbacks != NULL && ui->active_callbacks->deselect != NULL) {
ui->active_callbacks->deselect(ui, gpu);
}
ui->active_callbacks = NULL; ui->active_callbacks = NULL;
ui->active_element = 0; ui->active_element = 0;
ui->active_container = 0; ui->active_container = 0;