From 74c5f7a004b146e1cddc9c84f327f1d071219383 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 8 Oct 2025 18:22:43 +0200 Subject: [PATCH] fixes more or less --- include/pgpl/gui/predef.h | 9 ++--- include/pgpl/gui/widgets.h | 6 ++-- include/pgpl/render/color.h | 6 ++-- src/gui/helpers.c | 30 +++++++++------- src/gui/widgets/slider.c | 69 ++++++++++++++++++------------------- src/window/window-win32.c | 4 +-- tests/program.c | 6 ++-- 7 files changed, 67 insertions(+), 63 deletions(-) diff --git a/include/pgpl/gui/predef.h b/include/pgpl/gui/predef.h index 4f44e63..a993af4 100644 --- a/include/pgpl/gui/predef.h +++ b/include/pgpl/gui/predef.h @@ -28,9 +28,10 @@ typedef enum PGPL_GuiStatusColor { /* This controls what font size from the theme should be used. There are three * different types. */ typedef enum PGPL_GuiFontSize { - PGPL_GUI_FONT_SIZE_TITLE, - PGPL_GUI_FONT_SIZE_HEADING, - PGPL_GUI_FONT_SIZE_CONTENT + PGPL_GUI_FONT_SIZE_CONTENT, + PGPL_GUI_FONT_SIZE_HEADING_SMALL, + PGPL_GUI_FONT_SIZE_HEADING_LARGE, + PGPL_GUI_FONT_SIZE_TITLE } PGPL_GuiFontSize; /* This is the struct for the themes. This controls the colors, font sizes, font @@ -54,7 +55,7 @@ typedef struct PGPL_GuiTheme { PGPL_Rectangle padding; /* Controls the font size of the widgets, this is an array of three so that it * can support all values from PGPL_GuiFontSize. */ - double font_size[3]; + double font_size[4]; /* The font of the theme. */ PGPL_Font *font; } PGPL_GuiTheme; diff --git a/include/pgpl/gui/widgets.h b/include/pgpl/gui/widgets.h index 74d7495..eb347b9 100644 --- a/include/pgpl/gui/widgets.h +++ b/include/pgpl/gui/widgets.h @@ -91,11 +91,9 @@ void pgpl_gui_empty_widget_destroy(PGPL_GuiWidget *empty_widget); /* This is the struct for a slider widget. The inversed option inverses its * slide direction, vertical determines if it's vertical or horizontal. The - * bar_height is the height of the slider bar. The slide_callback gets called - * whenever there is sliding activity. */ + * slide_callback gets called whenever there is sliding activity. */ typedef struct PGPL_GuiSliderWidget { PGPL_GuiWidget parent; - uint32_t bar_height; bool inversed; bool vertical; double current_value; @@ -107,7 +105,7 @@ typedef struct PGPL_GuiSliderWidget { /* This creates a new slider with the given properties. */ PGPL_GuiSliderWidget *pgpl_gui_slider_widget_create( - uint32_t bar_height, bool inversed, bool vertical, double value, + bool inversed, bool vertical, double value, void (*slide_callback)(void *app_data, double value)); /* This destorys a slider widget. */ diff --git a/include/pgpl/render/color.h b/include/pgpl/render/color.h index 5f68029..f243fe7 100644 --- a/include/pgpl/render/color.h +++ b/include/pgpl/render/color.h @@ -43,9 +43,9 @@ static inline uint8_t pgpl_color_get_alpha(PGPL_Color color) { /* Divides all color values by the given amount. */ static inline PGPL_Color pgpl_color_divide(PGPL_Color color, double divisor) { - return pgpl_color_create(pgpl_color_get_red(color) / divisor, - pgpl_color_get_green(color) / divisor, - pgpl_color_get_blue(color) / divisor, + return pgpl_color_create((uint8_t)(pgpl_color_get_red(color) / divisor), + (uint8_t)(pgpl_color_get_green(color) / divisor), + (uint8_t)(pgpl_color_get_blue(color) / divisor), pgpl_color_get_alpha(color)); } diff --git a/src/gui/helpers.c b/src/gui/helpers.c index 3f2f4b4..5a0a021 100644 --- a/src/gui/helpers.c +++ b/src/gui/helpers.c @@ -4,29 +4,35 @@ void pgpl_gui_theme_configure(PGPL_GuiTheme *theme, PGPL_Color base_color, double content_font_size, PGPL_Font *font) { theme->background_color = pgpl_color_divide(base_color, 8); - theme->widget_background_color[0] = pgpl_color_divide(base_color, 5); - theme->widget_background_color[1] = pgpl_color_divide(base_color, 4); - theme->widget_background_color[2] = pgpl_color_divide(base_color, 3); + theme->widget_background_color[PGPL_GUI_STATUS_COLOR_NORMAL] = + pgpl_color_divide(base_color, 5); + theme->widget_background_color[PGPL_GUI_STATUS_COLOR_HOVER] = + pgpl_color_divide(base_color, 4); + theme->widget_background_color[PGPL_GUI_STATUS_COLOR_ACTIVE] = + pgpl_color_divide(base_color, 3); - theme->text_color[0] = pgpl_color_divide(base_color, 1.5); - theme->text_color[1] = pgpl_color_divide(base_color, 1.25); - theme->text_color[2] = base_color; + theme->text_color[PGPL_GUI_STATUS_COLOR_NORMAL] = + pgpl_color_divide(base_color, 1.5); + theme->text_color[PGPL_GUI_STATUS_COLOR_HOVER] = + pgpl_color_divide(base_color, 1.25); + theme->text_color[PGPL_GUI_STATUS_COLOR_ACTIVE] = base_color; - theme->font_size[0] = content_font_size * 3; - theme->font_size[1] = content_font_size * 2; - theme->font_size[2] = content_font_size; + theme->font_size[PGPL_GUI_FONT_SIZE_TITLE] = content_font_size * 4; + theme->font_size[PGPL_GUI_FONT_SIZE_HEADING_LARGE] = content_font_size * 3; + theme->font_size[PGPL_GUI_FONT_SIZE_HEADING_SMALL] = content_font_size * 2; + theme->font_size[PGPL_GUI_FONT_SIZE_CONTENT] = content_font_size; - theme->margin.top = content_font_size / 6; + theme->margin.top = content_font_size / 3; theme->margin.left = theme->margin.top; theme->margin.bottom = theme->margin.top; theme->margin.right = theme->margin.top; - theme->border.top = content_font_size / 12; + theme->border.top = content_font_size / 6; theme->border.left = theme->border.top; theme->border.bottom = theme->border.top; theme->border.right = theme->border.top; - theme->padding.top = content_font_size / 6; + theme->padding.top = content_font_size / 3; theme->padding.left = theme->padding.top; theme->padding.bottom = theme->padding.top; theme->padding.right = theme->padding.top; diff --git a/src/gui/widgets/slider.c b/src/gui/widgets/slider.c index 2a06a55..36d312d 100644 --- a/src/gui/widgets/slider.c +++ b/src/gui/widgets/slider.c @@ -2,9 +2,11 @@ #include static void get_slider_size(PGPL_GuiSliderWidget *slider_widget, - double *slider_x, double *slider_y, double x, - double y, double max_width, double max_height) { + PGPL_GuiTheme *theme, double *slider_x, + double *slider_y, double x, double y, + double max_width, double max_height) { double offset; + double bar_height = theme->font_size[PGPL_GUI_FONT_SIZE_CONTENT] / 2; if (slider_widget->inversed) { offset = 1.0 - slider_widget->current_value; @@ -13,13 +15,11 @@ static void get_slider_size(PGPL_GuiSliderWidget *slider_widget, } if (slider_widget->vertical) { - *slider_x = x - slider_widget->bar_height / 2.0; - *slider_y = y + offset * (max_height - slider_widget->bar_height) - - slider_widget->bar_height / 2.0; + *slider_x = x - bar_height / 2.0; + *slider_y = y + offset * (max_height - bar_height) - bar_height / 2.0; } else { - *slider_x = x + offset * (max_width - slider_widget->bar_height) - - slider_widget->bar_height / 2.0; - *slider_y = y - slider_widget->bar_height / 2.0; + *slider_x = x + offset * (max_width - bar_height) - bar_height / 2.0; + *slider_y = y - bar_height / 2.0; } } @@ -27,15 +27,16 @@ static void get_content_size(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme, double *width, double *height, double max_width, double max_height) { PGPL_GuiSliderWidget *slider_widget = (PGPL_GuiSliderWidget *)widget; + double bar_height = theme->font_size[PGPL_GUI_FONT_SIZE_CONTENT] / 2; (void)theme; if (slider_widget->vertical) { - *width = slider_widget->bar_height; + *width = bar_height; *height = max_height; } else { *width = max_width; - *height = slider_widget->bar_height; + *height = bar_height; } } @@ -43,6 +44,7 @@ static void render_content(PGPL_GuiWidget *widget, PGPL_Renderer *renderer, PGPL_GuiTheme *theme, double x, double y, double max_width, double max_height) { PGPL_GuiSliderWidget *slider_widget = (PGPL_GuiSliderWidget *)widget; + double bar_height = theme->font_size[PGPL_GUI_FONT_SIZE_CONTENT] / 2; double slider_x; double slider_y; @@ -52,44 +54,42 @@ static void render_content(PGPL_GuiWidget *widget, PGPL_Renderer *renderer, double completion_width; double completion_height; - get_slider_size(slider_widget, &slider_x, &slider_y, x, y, max_width, + get_slider_size(slider_widget, theme, &slider_x, &slider_y, x, y, max_width, max_height); if (slider_widget->vertical) { - max_width = slider_widget->bar_height; + max_width = bar_height; } else { - max_height = slider_widget->bar_height; + max_height = bar_height; } pgpl_render_rectangle(renderer, theme->text_color[widget->gui_color], x, y, max_width, max_height); - pgpl_render_rectangle(renderer, theme->background_color, - x + slider_widget->bar_height / 4.0, - y + slider_widget->bar_height / 4.0, - max_width - slider_widget->bar_height / 2.0, - max_height - slider_widget->bar_height / 2.0); + pgpl_render_rectangle(renderer, theme->background_color, x + bar_height / 4.0, + y + bar_height / 4.0, max_width - bar_height / 2.0, + max_height - bar_height / 2.0); - max_width -= slider_widget->bar_height / 2.0; - max_height -= slider_widget->bar_height / 2.0; + max_width -= bar_height / 2.0; + max_height -= bar_height / 2.0; if (slider_widget->inversed) { if (slider_widget->vertical) { - completion_x = x + slider_widget->bar_height / 4.0; - completion_y = y + slider_widget->bar_height / 4.0 + + completion_x = x + bar_height / 4.0; + completion_y = y + bar_height / 4.0 + max_height * (1.0 - slider_widget->current_value); completion_width = max_width; completion_height = max_height * slider_widget->current_value; } else { - completion_x = x + slider_widget->bar_height / 4.0 + + completion_x = x + bar_height / 4.0 + max_width * (1.0 - slider_widget->current_value); - completion_y = y + slider_widget->bar_height / 4.0; + completion_y = y + bar_height / 4.0; completion_width = max_width * slider_widget->current_value; completion_height = max_height; } } else { - completion_x = x + slider_widget->bar_height / 4.0; - completion_y = y + slider_widget->bar_height / 4.0; + completion_x = x + bar_height / 4.0; + completion_y = y + bar_height / 4.0; if (slider_widget->vertical) { completion_width = max_width; @@ -105,7 +105,7 @@ static void render_content(PGPL_GuiWidget *widget, PGPL_Renderer *renderer, completion_y, completion_width, completion_height); pgpl_render_circle(renderer, theme->text_color[widget->gui_color], slider_x, - slider_y, slider_widget->bar_height, 32); + slider_y, bar_height, 32); } static void event(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme, PGPL_Gui *gui, @@ -116,20 +116,20 @@ static void event(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme, PGPL_Gui *gui, event_type == PGPL_WINDOW_EVENT_MOUSE_RELEASE || event_type == PGPL_WINDOW_EVENT_MOUSE_MOVE) { PGPL_GuiSliderWidget *slider_widget = (PGPL_GuiSliderWidget *)widget; + double bar_height = theme->font_size[PGPL_GUI_FONT_SIZE_CONTENT] / 2; double slider_x; double slider_y; pgpl_gui_widget_adjust_event_params(widget, theme, &x, &y, &max_width, &max_height); - get_slider_size(slider_widget, &slider_x, &slider_y, x, y, max_width, + get_slider_size(slider_widget, theme, &slider_x, &slider_y, x, y, max_width, max_height); if (event_type == PGPL_WINDOW_EVENT_MOUSE_PRESS) { - if (pgpl_gui_widget_within_area(event_data->input_event.x, - event_data->input_event.y, slider_x, - slider_y, slider_widget->bar_height * 2, - slider_widget->bar_height * 2)) { + if (pgpl_gui_widget_within_area( + event_data->input_event.x, event_data->input_event.y, slider_x, + slider_y, bar_height * 2, bar_height * 2)) { slider_widget->sliding = true; if (slider_widget->vertical) { slider_widget->last_pos = event_data->input_event.y; @@ -151,7 +151,7 @@ static void event(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme, PGPL_Gui *gui, axis_length = max_width; } - axis_length -= slider_widget->bar_height; + axis_length -= bar_height; if (slider_widget->inversed) { slider_widget->current_value -= @@ -180,7 +180,7 @@ static void destroy(PGPL_GuiWidget *widget) { } PGPL_GuiSliderWidget *pgpl_gui_slider_widget_create( - uint32_t bar_height, bool inversed, bool vertical, double value, + bool inversed, bool vertical, double value, void (*slide_callback)(void *app_data, double value)) { PGPL_GuiSliderWidget *slider_widget = calloc(1, sizeof(*slider_widget)); pgpl_gui_widget_default_config(&slider_widget->parent); @@ -189,7 +189,6 @@ PGPL_GuiSliderWidget *pgpl_gui_slider_widget_create( slider_widget->parent.render_content = render_content; slider_widget->parent.event = event; slider_widget->parent.destroy = destroy; - slider_widget->bar_height = bar_height; slider_widget->inversed = inversed; slider_widget->vertical = vertical; slider_widget->current_value = value; diff --git a/src/window/window-win32.c b/src/window/window-win32.c index 096f565..6e3eb2d 100644 --- a/src/window/window-win32.c +++ b/src/window/window-win32.c @@ -181,8 +181,8 @@ PGPL_Window *pgpl_window_create(const char *title, uint32_t width, 0, 0, 0, - 24, - 8, + 0, + 0, 0, PFD_MAIN_PLANE, 0, diff --git a/tests/program.c b/tests/program.c index a0539e2..7f18747 100644 --- a/tests/program.c +++ b/tests/program.c @@ -25,7 +25,7 @@ PGPL_GuiWidget *create_main_view(struct AppData *app) { layout->parent.ignore_margin = true; layout->parent.ignore_border = true; - slider = pgpl_gui_slider_widget_create(16, false, false, 0.0, slide_callback); + slider = pgpl_gui_slider_widget_create(false, false, 0.0, slide_callback); slider->parent.border = true; slider->parent.background = true; @@ -41,7 +41,7 @@ PGPL_GuiWidget *create_main_view(struct AppData *app) { return (PGPL_GuiWidget *)layout; } -int32_t main(void) { +int main(void) { PGPL_Gui *gui; PGPL_GuiTheme theme; PGPL_Font *font; @@ -55,7 +55,7 @@ int32_t main(void) { font = pgpl_font_create_from_file("../roboto.ttf", 256); - pgpl_gui_theme_configure(&theme, pgpl_color_create(255, 255, 255, 255), 64, + pgpl_gui_theme_configure(&theme, pgpl_color_create(0, 128, 255, 255), 32, font); gui = pgpl_gui_create("PGPL Test", 480, 320, 0, 0, &theme, &app);