better layout management

This commit is contained in:
Patrick 2025-10-07 23:39:22 +02:00
commit a315d72a57
11 changed files with 86 additions and 79 deletions

View file

@ -23,8 +23,8 @@ static bool pgpl_gui_internal_event_loop(PGPL_Window *window,
*(PGPL_GuiWidget **)pgpl_vector_get_index(gui->widgets, i);
PGPL_GuiTheme *theme = &gui->theme;
if (widget->use_theme_override) {
theme = &widget->theme_override;
if (widget->theme_override) {
theme = widget->theme_override;
}
pgpl_gui_widget_render_full(widget, renderer, theme, 0, 0, width, height);

View file

@ -128,8 +128,8 @@ void pgpl_gui_widget_render_full(PGPL_GuiWidget *widget,
double max_height) {
double width, height;
if (widget->use_theme_override) {
theme = &widget->theme_override;
if (widget->theme_override) {
theme = widget->theme_override;
}
x += widget->offset_x;
@ -233,25 +233,17 @@ bool pgpl_gui_widget_within_bounds(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme,
max_height);
}
void pgpl_gui_widget_configure(PGPL_GuiWidget *widget, double offset_x,
double offset_y, bool expand_x, bool expand_y,
bool border, bool background,
PGPL_GuiFontSize font_size,
PGPL_GuiTheme *theme_override,
bool use_theme_override, bool ignore_margin,
bool ignore_border, bool ignore_padding) {
widget->offset_x = offset_x;
widget->offset_y = offset_y;
widget->expand_x = expand_x;
widget->expand_y = expand_y;
widget->border = border;
widget->background = background;
widget->font_size = font_size;
if (theme_override != NULL) {
widget->theme_override = *theme_override;
}
widget->use_theme_override = use_theme_override;
widget->ignore_margin = ignore_margin;
widget->ignore_border = ignore_border;
widget->ignore_padding = ignore_padding;
void pgpl_gui_widget_default_config(PGPL_GuiWidget *widget) {
widget->offset_x = 0;
widget->offset_y = 0;
widget->expand_x = true;
widget->expand_y = true;
widget->border = false;
widget->background = false;
widget->font_size = PGPL_GUI_FONT_SIZE_CONTENT;
widget->expansion_fraction = 1;
widget->theme_override = NULL;
widget->ignore_margin = false;
widget->ignore_border = false;
widget->ignore_padding = false;
}

View file

@ -70,6 +70,7 @@ PGPL_GuiButtonWidget *
pgpl_gui_button_widget_create(const char *text,
void (*click_callback)(void *app_data)) {
PGPL_GuiButtonWidget *button_widget = calloc(1, sizeof(*button_widget));
pgpl_gui_widget_default_config(&button_widget->parent);
button_widget->parent.id = "PGPL_GuiButtonWidget";
button_widget->parent.get_content_size = get_content_size;
button_widget->parent.render_content = render_content;

View file

@ -16,13 +16,21 @@ static void render_content(PGPL_GuiWidget *widget, PGPL_Renderer *renderer,
double max_width, double max_height) {
PGPL_GuiContainerLayout *container_layout = (PGPL_GuiContainerLayout *)widget;
uint32_t length = pgpl_vector_get_length(container_layout->widgets);
uint32_t fraction_total = 0;
for (uint32_t i = 0; i < length; i++) {
PGPL_GuiWidget *child_widget =
*(PGPL_GuiWidget **)pgpl_vector_get_index(container_layout->widgets, i);
fraction_total += child_widget->expansion_fraction;
}
switch (container_layout->direction) {
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_VERTICAL:
max_height /= length;
max_height /= fraction_total;
break;
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_HORIZONTAL:
max_width /= length;
max_width /= fraction_total;
break;
}
@ -30,15 +38,23 @@ static void render_content(PGPL_GuiWidget *widget, PGPL_Renderer *renderer,
PGPL_GuiWidget *child_widget =
*(PGPL_GuiWidget **)pgpl_vector_get_index(container_layout->widgets, i);
pgpl_gui_widget_render_full(child_widget, renderer, theme, x, y, max_width,
max_height);
pgpl_gui_widget_render_full(
child_widget, renderer, theme, x, y,
container_layout->direction ==
PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_HORIZONTAL
? max_width * child_widget->expansion_fraction
: max_width,
container_layout->direction ==
PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_VERTICAL
? max_height * child_widget->expansion_fraction
: max_height);
switch (container_layout->direction) {
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_VERTICAL:
y += max_height;
y += max_height * child_widget->expansion_fraction;
break;
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_HORIZONTAL:
x += max_width;
x += max_width * child_widget->expansion_fraction;
break;
}
}
@ -50,16 +66,24 @@ static void event(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme, PGPL_Gui *gui,
double max_width, double max_height) {
PGPL_GuiContainerLayout *container_layout = (PGPL_GuiContainerLayout *)widget;
uint32_t length = pgpl_vector_get_length(container_layout->widgets);
uint32_t fraction_total = 0;
for (uint32_t i = 0; i < length; i++) {
PGPL_GuiWidget *child_widget =
*(PGPL_GuiWidget **)pgpl_vector_get_index(container_layout->widgets, i);
fraction_total += child_widget->expansion_fraction;
}
pgpl_gui_widget_max_content_size(widget, theme, &max_width, &max_height,
max_width, max_height);
switch (container_layout->direction) {
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_VERTICAL:
max_height /= length;
max_height /= fraction_total;
break;
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_HORIZONTAL:
max_width /= length;
max_width /= fraction_total;
break;
}
@ -83,22 +107,29 @@ static void event(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme, PGPL_Gui *gui,
*(PGPL_GuiWidget **)pgpl_vector_get_index(container_layout->widgets, i);
PGPL_GuiTheme *child_theme;
if (child_widget->use_theme_override) {
child_theme = &child_widget->theme_override;
if (child_widget->theme_override) {
child_theme = child_widget->theme_override;
} else {
child_theme = theme;
}
child_widget->event(child_widget, child_theme, gui, event_type, event_data,
x + child_widget->offset_x, y + child_widget->offset_y,
max_width, max_height);
container_layout->direction ==
PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_HORIZONTAL
? max_width * child_widget->expansion_fraction
: max_width,
container_layout->direction ==
PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_VERTICAL
? max_height * child_widget->expansion_fraction
: max_height);
switch (container_layout->direction) {
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_VERTICAL:
y += max_height;
y += max_height * child_widget->expansion_fraction;
break;
case PGPL_GUI_CONTAINER_LAYOUT_DIRECTION_HORIZONTAL:
x += max_width;
x += max_width * child_widget->expansion_fraction;
break;
}
}
@ -112,6 +143,7 @@ PGPL_GuiContainerLayout *
pgpl_gui_container_layout_create(PGPL_GuiContainerLayoutDirection direction) {
PGPL_GuiContainerLayout *container_layout =
calloc(1, sizeof(*container_layout));
pgpl_gui_widget_default_config(&container_layout->parent);
container_layout->parent.id = "PGPL_GuiContainerLayout";
container_layout->parent.get_content_size = get_content_size;
container_layout->parent.render_content = render_content;

View file

@ -46,6 +46,7 @@ static void destroy(PGPL_GuiWidget *widget) {
PGPL_GuiWidget *pgpl_gui_empty_widget_create(void) {
PGPL_GuiWidget *empty_widget = calloc(1, sizeof(*empty_widget));
pgpl_gui_widget_default_config(empty_widget);
empty_widget->id = "PGPL_GuiWidget";
empty_widget->get_content_size = get_content_size;
empty_widget->render_content = render_content;

View file

@ -183,6 +183,7 @@ PGPL_GuiSliderWidget *pgpl_gui_slider_widget_create(
uint32_t bar_height, 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);
slider_widget->parent.id = "PGPL_GuiSliderWidget";
slider_widget->parent.get_content_size = get_content_size;
slider_widget->parent.render_content = render_content;

View file

@ -51,6 +51,7 @@ static void destroy(PGPL_GuiWidget *widget) {
PGPL_GuiTextWidget *pgpl_gui_text_widget_create(const char *text) {
PGPL_GuiTextWidget *text_widget = calloc(1, sizeof(*text_widget));
pgpl_gui_widget_default_config(&text_widget->parent);
text_widget->parent.id = "PGPL_GuiTextWidget";
text_widget->parent.get_content_size = get_content_size;
text_widget->parent.render_content = render_content;