187 lines
6.1 KiB
C
187 lines
6.1 KiB
C
|
|
#include <pgpl.h>
|
||
|
|
|
||
|
|
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->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->font_size[0] = content_font_size * 3;
|
||
|
|
theme->font_size[1] = content_font_size * 2;
|
||
|
|
theme->font_size[2] = content_font_size;
|
||
|
|
|
||
|
|
theme->margin.top = content_font_size / 6;
|
||
|
|
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.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.left = theme->padding.top;
|
||
|
|
theme->padding.bottom = theme->padding.top;
|
||
|
|
theme->padding.right = theme->padding.top;
|
||
|
|
|
||
|
|
theme->font = font;
|
||
|
|
}
|
||
|
|
|
||
|
|
void pgpl_gui_widget_max_content_size(PGPL_GuiWidget *widget,
|
||
|
|
PGPL_GuiTheme *theme, double *width,
|
||
|
|
double *height, double max_width,
|
||
|
|
double max_height) {
|
||
|
|
*width = max_width;
|
||
|
|
*height = max_height;
|
||
|
|
|
||
|
|
if (!widget->ignore_margin) {
|
||
|
|
*width -= theme->margin.left + theme->margin.right;
|
||
|
|
*height -= theme->margin.top + theme->margin.bottom;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!widget->ignore_border) {
|
||
|
|
*width -= theme->border.left + theme->border.right;
|
||
|
|
*height -= theme->border.top + theme->border.bottom;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!widget->ignore_padding) {
|
||
|
|
*width -= theme->padding.left + theme->padding.right;
|
||
|
|
*height -= theme->padding.top + theme->padding.bottom;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void pgpl_gui_widget_render_full(PGPL_GuiWidget *widget,
|
||
|
|
PGPL_Renderer *renderer, PGPL_GuiTheme *theme,
|
||
|
|
double x, double y, double max_width,
|
||
|
|
double max_height) {
|
||
|
|
double width, height;
|
||
|
|
|
||
|
|
if (widget->use_theme_override) {
|
||
|
|
theme = &widget->theme_override;
|
||
|
|
}
|
||
|
|
|
||
|
|
x += +widget->offset_x;
|
||
|
|
y += +widget->offset_y;
|
||
|
|
|
||
|
|
widget->get_content_size(widget, theme, &width, &height, max_width,
|
||
|
|
max_height);
|
||
|
|
|
||
|
|
if (!widget->expand_x) {
|
||
|
|
max_width = width;
|
||
|
|
if (!widget->ignore_margin) {
|
||
|
|
max_width += theme->margin.left + theme->margin.right;
|
||
|
|
}
|
||
|
|
if (!widget->ignore_border) {
|
||
|
|
max_width += theme->border.left + theme->border.right;
|
||
|
|
}
|
||
|
|
if (!widget->ignore_padding) {
|
||
|
|
max_width += theme->padding.left + theme->padding.right;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!widget->expand_y) {
|
||
|
|
max_height = height;
|
||
|
|
if (!widget->ignore_margin) {
|
||
|
|
max_height += theme->margin.top + theme->margin.bottom;
|
||
|
|
}
|
||
|
|
if (!widget->ignore_border) {
|
||
|
|
max_height += theme->border.top + theme->border.bottom;
|
||
|
|
}
|
||
|
|
if (!widget->ignore_padding) {
|
||
|
|
max_height += theme->padding.top + theme->padding.bottom;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!widget->ignore_margin) {
|
||
|
|
x += theme->margin.left;
|
||
|
|
y += theme->margin.top;
|
||
|
|
|
||
|
|
max_width -= theme->margin.left + theme->margin.right;
|
||
|
|
max_height -= theme->margin.top + theme->margin.bottom;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (widget->border) {
|
||
|
|
pgpl_render_rectangle(renderer, theme->text_color[widget->gui_color], x, y,
|
||
|
|
max_width, max_height);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!widget->ignore_border) {
|
||
|
|
x += theme->border.left;
|
||
|
|
y += theme->border.top;
|
||
|
|
|
||
|
|
max_width -= theme->border.left + theme->border.right;
|
||
|
|
max_height -= theme->border.top + theme->border.bottom;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (widget->background) {
|
||
|
|
pgpl_render_rectangle(renderer,
|
||
|
|
theme->widget_background_color[widget->gui_color], x,
|
||
|
|
y, max_width, max_height);
|
||
|
|
} else {
|
||
|
|
pgpl_render_rectangle(renderer, theme->background_color, x, y, max_width,
|
||
|
|
max_height);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!widget->ignore_padding) {
|
||
|
|
x += theme->padding.left;
|
||
|
|
y += theme->padding.top;
|
||
|
|
|
||
|
|
max_width -= theme->padding.left + theme->padding.right;
|
||
|
|
max_height -= theme->padding.top + theme->padding.bottom;
|
||
|
|
}
|
||
|
|
|
||
|
|
x += (max_width - width) / 2;
|
||
|
|
y += (max_height - height) / 2;
|
||
|
|
|
||
|
|
widget->render_content(widget, renderer, theme, x, y, max_width, max_height);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool pgpl_gui_widget_within_bounds(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme,
|
||
|
|
double widget_x, double widget_y,
|
||
|
|
double max_width, double max_height,
|
||
|
|
double x, double y) {
|
||
|
|
(void)widget;
|
||
|
|
|
||
|
|
widget_x += theme->margin.left;
|
||
|
|
widget_y += theme->margin.top;
|
||
|
|
|
||
|
|
max_width -= theme->margin.left + theme->margin.right;
|
||
|
|
max_height -= theme->margin.top + theme->margin.bottom;
|
||
|
|
|
||
|
|
if ((x >= widget_x && x <= (widget_x + max_width)) &&
|
||
|
|
(y >= widget_y && y <= (widget_y + max_height))) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|