From 320dabc5b1006c9c1a090c71c4bddcbef058dede Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Sat, 12 Jul 2025 20:52:52 +0200 Subject: [PATCH] Widget style changes --- game/src/gl/program.c | 18 +++---- game/src/gl/program.h | 18 +++---- game/src/ui/uiman.c | 8 +--- game/src/ui/widget_composites.c | 29 ++---------- game/src/ui/widget_core.c | 55 +--------------------- game/src/ui/widget_core.h | 7 ++- game/src/ui/widget_render.c | 16 +++---- game/src/ui/widget_style.h | 83 ++++++++++++++++++++++++++++++--- 8 files changed, 112 insertions(+), 122 deletions(-) diff --git a/game/src/gl/program.c b/game/src/gl/program.c index 75d071c..e9ab755 100644 --- a/game/src/gl/program.c +++ b/game/src/gl/program.c @@ -79,55 +79,55 @@ program_create (const char *v_src, const char *f_src) } void -program_set_vec2 (int program, const char *name, vec2 value) +program_set_vec2 (int program, const char *name, const vec2 value) { glUniform2f (glGetUniformLocation (program, name), value[0], value[1]); } void -program_set_vec3 (int program, const char *name, vec3 value) +program_set_vec3 (int program, const char *name, const vec3 value) { glUniform3f (glGetUniformLocation (program, name), value[0], value[1], value[2]); } void -program_set_vec4 (int program, const char *name, vec4 value) +program_set_vec4 (int program, const char *name, const vec4 value) { glUniform4f (glGetUniformLocation (program, name), value[0], value[1], value[2], value[3]); } void -program_set_vec3_ind (int program, const char *name, float x, float y, float z) +program_set_vec3_ind (int program, const char *name, const float x, const float y, const float z) { glUniform3f (glGetUniformLocation (program, name), x, y, z); } void -program_set_int (int program, const char *name, int value) +program_set_int (int program, const char *name, const int value) { glUniform1i (glGetUniformLocation (program, name), value); } void -program_set_float (int program, const char *name, float value) +program_set_float (int program, const char *name, const float value) { glUniform1f (glGetUniformLocation (program, name), value); } void -program_set_int_array (int program, const char *name, size_t count, int *value) +program_set_int_array (int program, const char *name, const size_t count, const int *value) { glUniform1iv (glGetUniformLocation (program, name), count, value); } void -program_set_mat3 (int program, const char *name, mat3 value) +program_set_mat3 (int program, const char *name, const mat3 value) { glUniformMatrix3fv (glGetUniformLocation (program, name), 1, GL_FALSE, (float *) value); } void -program_set_mat4 (int program, const char *name, mat4 value) +program_set_mat4 (int program, const char *name, const mat4 value) { glUniformMatrix4fv (glGetUniformLocation (program, name), 1, GL_FALSE, (float *) value); } diff --git a/game/src/gl/program.h b/game/src/gl/program.h index 0e3d7b8..e6771e0 100644 --- a/game/src/gl/program.h +++ b/game/src/gl/program.h @@ -8,13 +8,13 @@ unsigned int program_use (int program); void shader_fail (int shader); void program_fail (int program); unsigned int program_create (const char *v_src, const char *f_src); -void program_set_int (int program, const char *name, int value); -void program_set_float (int program, const char *name, float value); -void program_set_int_array (int program, const char *name, size_t count, int *value); -void program_set_vec2 (int program, const char *name, vec2 value); -void program_set_vec3 (int program, const char *name, vec3 value); -void program_set_vec3_ind (int program, const char *name, float x, float y, float z); -void program_set_vec4 (int program, const char *name, vec4 value); -void program_set_mat3 (int program, const char *name, mat3 value); -void program_set_mat4 (int program, const char *name, mat4 value); +void program_set_int (int program, const char *name, const int value); +void program_set_float (int program, const char *name, const float value); +void program_set_int_array (int program, const char *name, const size_t count, const int *value); +void program_set_vec2 (int program, const char *name, const vec2 value); +void program_set_vec3 (int program, const char *name, const vec3 value); +void program_set_vec3_ind (int program, const char *name, const float x, const float y, const float z); +void program_set_vec4 (int program, const char *name, const vec4 value); +void program_set_mat3 (int program, const char *name, const mat3 value); +void program_set_mat4 (int program, const char *name, const mat4 value); void program_print (int program); diff --git a/game/src/ui/uiman.c b/game/src/ui/uiman.c index 3750f0d..d8cb604 100644 --- a/game/src/ui/uiman.c +++ b/game/src/ui/uiman.c @@ -34,7 +34,7 @@ uiman_init () tooltip_widget->size[0] = 200; tooltip_widget->layout_padding = 4; tooltip_widget->layout_spacing = 8; - tooltip_widget->style->background_image = RESTEX ("terrain4"); + tooltip_widget->style = WSTYLE (STYLE_TOOLTIP); tooltip_text = Widget_multiline_text_new (NULL, 20, TEXT_ALIGN_CENTER); Widget_add_child_flowbox (tooltip_widget, tooltip_text, 1, 0); @@ -45,15 +45,11 @@ uiman_init () debug_window->size[0] = 350; debug_window->window.resizable_v = FALSE; debug_window->layout_padding = 8; - debug_window->style->border_width = 2; - VEC4_SETI (debug_window->style->border_color, 0.4, 0.4, 0.4, 1); - Widget_set_all_backgrounds (debug_window, (vec4) { 0.1, 0.1, 0.1, 1.0 }); - VEC4_SETI (debug_window->style->background_color_clicked, 0.2, 0.2, 0.2, 1); + debug_window->style = WSTYLE (STYLE_WINDOW); Widget *debug_menu = Widget_flowbox_new (TRUE); debug_menu->layout_padding = 8; debug_menu->layout_spacing = 8; - Widget_set_all_backgrounds (debug_menu, (vec4) { 0.3, 0.3, 0.3, 1.0 }); Widget_add_child_flowbox (debug_menu, Widget_label_make_float ("Frame time: %.2f", &delta_time, TEXT_ALIGN_LEFT), 1, 20); Widget_add_child_flowbox (debug_menu, Widget_label_make_int ("Chunks: %d", &n_frame_chunks, TEXT_ALIGN_LEFT), 1./2., 20); diff --git a/game/src/ui/widget_composites.c b/game/src/ui/widget_composites.c index 9d4cb8f..9ffd143 100644 --- a/game/src/ui/widget_composites.c +++ b/game/src/ui/widget_composites.c @@ -1,28 +1,5 @@ #include "widget_composites.h" -WidgetStyle widget_style_icon_button = - { - .background_color = { 0.0, 0.0, 0.0, 0.0 }, - .background_color_hovered = { 0.0, 0.0, 0.0, 0.0 }, - .background_color_clicked = { 0.0, 0.0, 0.0, 0.0 }, - - .text_color = { 1.0, 1.0, 1.0, 1.0 }, - .text_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, - .text_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, - - .icon_color = { 1.0, 1.0, 1.0, 1.0 }, - .icon_color_hovered = { 1.0, 1.0, 0.0, 1.0 }, - .icon_color_clicked = { 1.0, 0.0, 0.0, 1.0 }, - - .border_color = { 1.0, 1.0, 1.0, 1.0 }, - .progress_color = { 1.0, 1.0, 1.0, 1.0 }, - - .border_width = 0, - - .background_image = 0, - .font_bitmap = 0 - }; - Widget * Widget_label_make_int (const char *fmt, int *ptr, TextAlignment alignment) { @@ -47,8 +24,8 @@ Widget_composite_spinner_make_int (const char *fmt, int *ptr) Widget *label = Widget_label_make_int (fmt, ptr, TEXT_ALIGN_CENTER); Widget *right = Widget_plain_new (); - Widget_set_style (left, widget_style_icon_button); - Widget_set_style (right, widget_style_icon_button); + left->style = WSTYLE (STYLE_ICON_BUTTON); + right->style = WSTYLE (STYLE_ICON_BUTTON); Widget_set_on_event (left, EVENT_LEFT_UP, uicb_widget_op, WidgetOpData_new (OP_MODIFY_INT, ptr, -1.0)); left->icon = ICON_MINUS; @@ -73,7 +50,7 @@ Widget_composite_checkbox_make (const char *label, char *flag) Widget *cont = Widget_container_new (TRUE); Widget *box = Widget_plain_new (); - Widget_set_style (box, widget_style_icon_button); + box->style = WSTYLE (STYLE_ICON_BUTTON); box->icon = ICON_CHECKBOX_UNCHECKED; box->aspect_ratio = 1.0; diff --git a/game/src/ui/widget_core.c b/game/src/ui/widget_core.c index 5622cc4..fe6da52 100644 --- a/game/src/ui/widget_core.c +++ b/game/src/ui/widget_core.c @@ -11,35 +11,6 @@ static const Widget DEFAULT_WIDGET = .icon = ICON_NONE }; -WidgetStyle widget_style_default = - { - .background_color = { 0.0, 0.0, 0.0, 0.0 }, - .background_color_hovered = { 0.0, 0.0, 0.0, 0.0 }, - .background_color_clicked = { 0.0, 0.0, 0.0, 0.0 }, - - .text_color = { 1.0, 1.0, 1.0, 1.0 }, - .text_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, - .text_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, - .ghost_text_color = { 1.0, 1.0, 1.0, 0.5 }, - - .icon_color = { 1.0, 1.0, 1.0, 1.0 }, - .icon_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, - .icon_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, - - .scrollbar_color = { 1.0, 1.0, 1.0, 1.0 }, - .scrollbar_color_hovered = { 1.0, 1.0, 1.0, 1.0 }, - .scrollbar_color_clicked = { 1.0, 1.0, 1.0, 1.0 }, - - .border_color = { 1.0, 1.0, 1.0, 1.0 }, - .progress_color = { 1.0, 1.0, 1.0, 1.0 }, - .resize_handle_color = { 0.6, 0.6, 0.6, 1.0 }, - - .border_width = 0, - - .background_image = 0, - .font_bitmap = 0 - }; - static inline Widget * _uiel_new (WidgetKind kind) { @@ -49,8 +20,7 @@ _uiel_new (WidgetKind kind) *e = DEFAULT_WIDGET; e->kind = kind; e->children = g_array_new (false, false, sizeof (Widget *)); - Widget_set_style (e, widget_style_default); - e->style->font_bitmap = RESTEX ("font0"); + e->style = WSTYLE (STYLE_DEFAULT); return e; } @@ -60,15 +30,6 @@ Widget_plain_new () return _uiel_new (WIDGET_PLAIN); } -Widget * -Widget_test_new (float size) -{ - Widget *e = Widget_plain_new (); - IVEC2_SETI (e->size, size, size); - VEC4_SETI (e->style->background_color, 1, 0, 0, 1); - return e; -} - Widget * Widget_label_new (const char *text, TextAlignment alignment) { @@ -230,20 +191,6 @@ Widget_set_on_key (Widget *widget, SDL_Scancode key, widget->key_data[key] = userdata; } -void -Widget_set_style (Widget *widget, WidgetStyle style) -{ - widget->style = g_memdup2 (&style, sizeof (WidgetStyle)); -} - -void -Widget_set_all_backgrounds (Widget *widget, vec4 color) -{ - glm_vec4_copy (color, widget->style->background_color); - glm_vec4_copy (color, widget->style->background_color_hovered); - glm_vec4_copy (color, widget->style->background_color_clicked); -} - void Widget_label_set_text (Widget *widget, const char *text) { diff --git a/game/src/ui/widget_core.h b/game/src/ui/widget_core.h index 80c3354..3a751b5 100644 --- a/game/src/ui/widget_core.h +++ b/game/src/ui/widget_core.h @@ -19,6 +19,8 @@ for (unsigned int i = 0; i < widget->children->len \ && ((child = g_array_index (widget->children, Widget *, i)), 1); ++i) +#define WIDGET_STYLE(widget, id) ((widget)->style = WSTYLE (id)) + typedef enum WidgetKind { WIDGET_PLAIN, @@ -171,7 +173,7 @@ typedef struct Widget int layout_padding, layout_spacing; float layout_weight, aspect_ratio; - WidgetStyle *style; + const WidgetStyle *style; WidgetIcon icon; GString *tooltip; @@ -187,7 +189,6 @@ typedef struct Widget } Widget; Widget *Widget_plain_new (); -Widget *Widget_test_new (float size); Widget *Widget_label_new (const char *text, TextAlignment alignment); Widget *Widget_scrollbar_new (Widget *target, gboolean horizontal); Widget *Widget_progress_new (gboolean horizontal, float min, float max, float *ptr); @@ -206,8 +207,6 @@ void Widget_add_child_flowbox (Widget *parent, Widget *child, void Widget_set_on_event (Widget *widget, WidgetEvent event, void (*on_event) (Widget *, SDL_Event *, void *), void *userdata); void Widget_set_on_key (Widget *widget, SDL_Scancode key, void (*on_key) (Widget *, SDL_Event *, void *), void *userdata); -void Widget_set_style (Widget *widget, WidgetStyle style); -void Widget_set_all_backgrounds (Widget *widget, vec4 color); void Widget_label_set_text (Widget *widget, const char *text); void Widget_label_printf (Widget *widget, const char *format, ...); diff --git a/game/src/ui/widget_render.c b/game/src/ui/widget_render.c index 800280a..97d37ae 100644 --- a/game/src/ui/widget_render.c +++ b/game/src/ui/widget_render.c @@ -17,7 +17,7 @@ _Widget_get_aabb (Widget *self, vec2 aabb[2]) static inline void _Widget_render_text_line(const char *text, TextAlignment alignment, ivec2 position, - ivec2 size, int max_width, vec4 color, mat4 projection) + ivec2 size, int max_width, const vec4 color, mat4 projection) { program_use (RESPRG ("uitext")); program_set_mat4 (RESPRG ("uitext"), "projection", projection); @@ -72,7 +72,7 @@ Widget_render_base (Widget *self, mat4 projection, int window_height) SimpleMesh_bind (&quad); } - WidgetStyle *style = self->style; + const WidgetStyle *style = self->style; program_use (RESPRG ("uiwidget")); @@ -144,8 +144,8 @@ Widget_render_label (Widget *self, mat4 projection, int window_height) int char_w = self->size[1] * (9.0f / 20.0f); ivec2 pos = { self->position[0], self->position[1] }; - WidgetStyle *style = self->style; - texture_use (style->font_bitmap, 0); + const WidgetStyle *style = self->style; + texture_use (RESTEX ("font0"), 0); _Widget_render_text_line (self->label.text->str, self->label.text_alignment, pos, (ivec2) { char_w, self->size[1] }, self->size[0], self->clicked ? style->text_color_clicked : self->hovered @@ -158,7 +158,7 @@ Widget_render_scrollbar (Widget *self, mat4 projection, int window_height) program_use (RESPRG ("uiwidget")); program_set_int (RESPRG ("uiwidget"), "has_scrollbar", TRUE); - WidgetStyle *style = self->style; + const WidgetStyle *style = self->style; program_set_vec4 (RESPRG ("uiwidget"), "scrollbar_color", style->scrollbar_color); @@ -218,7 +218,7 @@ Widget_render_multiline_text (Widget *self, mat4 projection, int window_height) { Widget_render_base (self, projection, window_height); - texture_use (self->style->font_bitmap, 0); + texture_use (RESTEX ("font0"), 0); int pad = self->layout_padding; int row_h = self->multiline_text.line_height; @@ -240,7 +240,7 @@ Widget_render_entry (Widget *self, mat4 projection, int window_height) { Widget_render_base (self, projection, window_height); - texture_use (self->style->font_bitmap, 0); + texture_use (RESTEX ("font0"), 0); int char_w = self->size[1] * (9.0f / 20.0f); int cursor_in_px = self->entry.cursor_position * char_w; @@ -292,7 +292,7 @@ Widget_render_window (Widget *self, mat4 projection, int window_height) UNUSED (self, projection, window_height); Widget_render_base (self, projection, window_height); - texture_use (self->style->font_bitmap, 0); + texture_use (RESTEX ("font0"), 0); ivec2 pos = { self->position[0] + self->layout_padding, diff --git a/game/src/ui/widget_style.h b/game/src/ui/widget_style.h index 6b63cb7..a9b9c36 100644 --- a/game/src/ui/widget_style.h +++ b/game/src/ui/widget_style.h @@ -1,7 +1,10 @@ #pragma once -#include "widget_fw.h" -#include "widget_core.h" +#include + +#define COLOR_TRANSPARENT { 0.0, 0.0, 0.0, 0.0 } +#define COLOR_BLACK { 0.0, 0.0, 0.0, 1.0 } +#define COLOR_WHITE { 1.0, 1.0, 1.0, 1.0 } typedef struct WidgetStyle { @@ -29,9 +32,77 @@ typedef struct WidgetStyle float border_width; unsigned int background_image; - unsigned int font_bitmap; - - /* Unused for now. */ - unsigned int font_char_w, font_char_h; } WidgetStyle; +typedef enum WidgetStyleId + { + STYLE_DEFAULT, + STYLE_TOOLTIP, + STYLE_ICON_BUTTON, + STYLE_WINDOW, + NSTYLES + } WidgetStyleId; + +static const WidgetStyle widget_styles[NSTYLES] = { + [STYLE_DEFAULT] = { + .text_color = COLOR_WHITE, + .text_color_hovered = COLOR_WHITE, + .text_color_clicked = COLOR_WHITE, + .ghost_text_color = { 1.0, 1.0, 1.0, 0.5 }, + + .icon_color = COLOR_WHITE, + .icon_color_hovered = COLOR_WHITE, + .icon_color_clicked = COLOR_WHITE, + + .scrollbar_color = COLOR_WHITE, + .scrollbar_color_hovered = COLOR_WHITE, + .scrollbar_color_clicked = COLOR_WHITE, + + .border_color = COLOR_WHITE, + .progress_color = COLOR_WHITE, + .resize_handle_color = { 0.6, 0.6, 0.6, 1.0 }, + + .border_width = 0, + }, + + [STYLE_TOOLTIP] = { + .background_color = COLOR_BLACK, + .background_color_hovered = COLOR_BLACK, + .background_color_clicked = COLOR_BLACK, + + .text_color = COLOR_WHITE, + .text_color_hovered = COLOR_WHITE, + .text_color_clicked = COLOR_WHITE, + + .border_width = 0, + }, + + [STYLE_ICON_BUTTON] = { + .text_color = { 0.0, 0.0, 0.0, 1.0 }, + .text_color_hovered = { 0.0, 0.0, 0.0, 1.0 }, + .text_color_clicked = { 0.0, 0.0, 0.0, 1.0 }, + + .icon_color = COLOR_WHITE, + .icon_color_hovered = { 1.0, 1.0, 0.0, 1.0 }, + .icon_color_clicked = { 1.0, 0.0, 0.0, 1.0 }, + + .border_width = 0, + }, + + [STYLE_WINDOW] = { + .background_color = { 0.2, 0.2, 0.2, 1.0 }, + .background_color_hovered = { 0.2, 0.2, 0.2, 1.0 }, + .background_color_clicked = { 0.3, 0.3, 0.3, 1.0 }, + + .text_color = COLOR_WHITE, + .text_color_hovered = COLOR_WHITE, + .text_color_clicked = COLOR_WHITE, + + .border_color = { 0.5, 0.5, 0.5, 1.0 }, + .resize_handle_color = COLOR_WHITE, + + .border_width = 3, + }, +}; + +#define WSTYLE(id) (&widget_styles[id])