From 8cff4fb8a69cca3d1569f2e1192602a114f54301 Mon Sep 17 00:00:00 2001 From: Patrick Cleavelin Date: Sat, 20 Apr 2024 19:25:29 -0500 Subject: [PATCH] make janky titlebar --- src/main.c | 28 ++++++++++++++++++++++++++++ src/ui.h | 49 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/main.c b/src/main.c index 94bb8d8..3553fbb 100644 --- a/src/main.c +++ b/src/main.c @@ -145,6 +145,34 @@ void ed_frame(int mouse_x, int mouse_y, bool mouse_left_down, .mouse_right_down = mouse_right_down, }); + // TODO: make having custom title bar configurable + { + size_t title_bar_elm = + ui_element(&state.ui_cx, _String("chat - [slack sux]"), + UI_AXIS_HORIZONTAL, ui_make_size(ui_fill, ui_exact(32)), + UI_FLAG_DRAW_BACKGROUND | UI_FLAG_DRAW_TEXT | + UI_FLAG_CENTERED_TEXT); + + ui_interaction title_bar_interaction = + _ui_test_interaction(&state.ui_cx, title_bar_elm); + + if (title_bar_interaction.dragging) { + // TODO: drag the window around + } + + ui_push_parent(&state.ui_cx); + { + ui_element(&state.ui_cx, _String("title bar spacer"), + UI_AXIS_HORIZONTAL, ui_make_size(ui_fill, ui_exact(32)), + 0); + + if (ui_button_centered(&state.ui_cx, _String("X")).clicked) { + keep_running = false; + } + } + ui_pop_parent(&state.ui_cx); + } + ui_element(&state.ui_cx, _String("channel sidebar"), UI_AXIS_VERTICAL, ui_make_size(ui_children_sum, ui_fill), UI_FLAG_DRAW_BACKGROUND); ui_push_parent(&state.ui_cx); diff --git a/src/ui.h b/src/ui.h index 23d7a21..76bbc3d 100644 --- a/src/ui.h +++ b/src/ui.h @@ -61,6 +61,9 @@ typedef struct { ((ui_semantic_size){.type = UI_SEMANTIC_SIZE_CHILDREN_SUM}) #define ui_exact(value) \ ((ui_semantic_size){.type = UI_SEMANTIC_SIZE_EXACT, .integer = value}) +#define ui_percent_of_parent(value) \ + ((ui_semantic_size){.type = UI_SEMANTIC_SIZE_PERCENT_OF_PARENT, \ + .integer = value}) typedef struct { ui_axis axis; @@ -83,15 +86,16 @@ typedef struct { } ui_element_cache_data; typedef enum { - UI_FLAG_CLICKABLE = 0b000000001, - UI_FLAG_HOVERABLE = 0b000000010, - UI_FLAG_SCROLLABLE = 0b000000100, - UI_FLAG_DRAW_TEXT = 0b000001000, - UI_FLAG_DRAW_BORDER = 0b000010000, - UI_FLAG_DRAW_BACKGROUND = 0b000100000, - UI_FLAG_ROUNDED_BORDER = 0b001000000, - UI_FLAG_FLOATING = 0b010000000, - UI_FLAG_CUSTOM_DRAW_FUNC = 0b100000000, + UI_FLAG_CLICKABLE = 0b0000000001, + UI_FLAG_HOVERABLE = 0b0000000010, + UI_FLAG_SCROLLABLE = 0b0000000100, + UI_FLAG_DRAW_TEXT = 0b0000001000, + UI_FLAG_CENTERED_TEXT = 0b1000000000, + UI_FLAG_DRAW_BORDER = 0b0000010000, + UI_FLAG_DRAW_BACKGROUND = 0b0000100000, + UI_FLAG_ROUNDED_BORDER = 0b0001000000, + UI_FLAG_FLOATING = 0b0010000000, + UI_FLAG_CUSTOM_DRAW_FUNC = 0b0100000000, } ui_flags; // Ephemeral frame only UI Element data @@ -161,7 +165,8 @@ ui_context ui_init_context() { .prev = -1, .parent = -1, .size = { - .axis = UI_AXIS_HORIZONTAL, + // TODO: make this configurable + .axis = UI_AXIS_VERTICAL, .computed_size = {640, 480}, }}; pushArray(ui_element_frame_data, &frame_elements, frame_data); @@ -267,6 +272,15 @@ ui_interaction ui_button(ui_context *cx, string label) { return _ui_test_interaction(cx, id); } +ui_interaction ui_button_centered(ui_context *cx, string label) { + size_t id = ui_element( + cx, label, UI_AXIS_HORIZONTAL, ui_make_size(ui_fit_text, ui_fit_text), + UI_FLAG_DRAW_BACKGROUND | UI_FLAG_DRAW_TEXT | UI_FLAG_CENTERED_TEXT | + UI_FLAG_HOVERABLE | UI_FLAG_CLICKABLE); + + return _ui_test_interaction(cx, id); +} + static uint32_t _ui_ancestor_size(ui_context *cx, size_t element_index, ui_axis axis) { if (element_index == SIZE_MAX || _parent(element_index) == SIZE_MAX) { @@ -293,9 +307,9 @@ static void _ui_compute_simple_layout(ui_context *cx, switch (elm->size.semantic_size[axis].type) { case UI_SEMANTIC_SIZE_FIT_TEXT: if (axis == UI_AXIS_HORIZONTAL) { - elm->size.computed_size[axis] = elm->label.len * _FONT_WIDTH; + elm->size.computed_size[axis] = (elm->label.len + 2) * _FONT_WIDTH; } else if (axis == UI_AXIS_VERTICAL) { - elm->size.computed_size[axis] = _FONT_HEIGHT; + elm->size.computed_size[axis] = _FONT_HEIGHT + (_FONT_HEIGHT / 4); } break; @@ -504,7 +518,16 @@ void ui_render(ui_context *cx, _ui_render_text_func text_func, ui_element_frame_data *elm = &cx->frame_elements.data[i]; if (_flags(i, UI_FLAG_DRAW_TEXT)) { - text_func(text, (float[]){(float)elm->size.computed_pos[0], + uint32_t x_offset = 0; + + if (_flags(i, UI_FLAG_CENTERED_TEXT)) { + uint32_t half_width = elm->size.computed_size[0] / 2; + uint32_t half_text_width = (text.len / 2) * _FONT_WIDTH; + x_offset = half_width - half_text_width; + } + + text_func(text, (float[]){(float)elm->size.computed_pos[0] + + (float)x_offset, (float)elm->size.computed_pos[1]}); }