86 lines
3.4 KiB
C
86 lines
3.4 KiB
C
#include <pgpl.h>
|
|
#include <stdlib.h>
|
|
|
|
static void get_content_size(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme,
|
|
double *width, double *height, double max_width,
|
|
double max_height) {
|
|
PGPL_GuiButtonWidget *button_widget = (PGPL_GuiButtonWidget *)widget;
|
|
PGPL_Rectangle rect;
|
|
|
|
(void)max_width;
|
|
(void)max_height;
|
|
|
|
pgpl_font_string_dimensions(theme->font, button_widget->text,
|
|
theme->font_size[widget->font_size], &rect);
|
|
|
|
*width = rect.right - rect.left;
|
|
*height = rect.bottom - rect.top;
|
|
}
|
|
|
|
static void render_content(PGPL_GuiWidget *widget, PGPL_Renderer *renderer,
|
|
PGPL_GuiTheme *theme, double x, double y,
|
|
double max_width, double max_height) {
|
|
PGPL_GuiButtonWidget *button_widget = (PGPL_GuiButtonWidget *)widget;
|
|
|
|
(void)max_width;
|
|
(void)max_height;
|
|
|
|
pgpl_font_render_string(renderer, theme->font, button_widget->text,
|
|
theme->text_color[widget->gui_color], x, y,
|
|
theme->font_size[widget->font_size]);
|
|
}
|
|
|
|
static void event(PGPL_GuiWidget *widget, PGPL_GuiTheme *theme, PGPL_Gui *gui,
|
|
PGPL_WindowEventType event_type,
|
|
PGPL_WindowEventData *event_data, double x, double y,
|
|
double max_width, double max_height) {
|
|
PGPL_GuiButtonWidget *button_widget = (PGPL_GuiButtonWidget *)widget;
|
|
|
|
if (event_type == PGPL_WINDOW_EVENT_MOUSE_PRESS ||
|
|
event_type == PGPL_WINDOW_EVENT_MOUSE_RELEASE ||
|
|
event_type == PGPL_WINDOW_EVENT_MOUSE_MOVE) {
|
|
if (pgpl_gui_widget_within_bounds(widget, theme, x, y, max_width,
|
|
max_height, event_data->input_event.x,
|
|
event_data->input_event.y)) {
|
|
if (event_type == PGPL_WINDOW_EVENT_MOUSE_PRESS &&
|
|
event_data->input_event.key == PGPL_WINDOW_MOUSE_BUTTON_LEFT) {
|
|
widget->gui_color = PGPL_GUI_STATUS_COLOR_ACTIVE;
|
|
} else if (event_type == PGPL_WINDOW_EVENT_MOUSE_RELEASE &&
|
|
event_data->input_event.key == PGPL_WINDOW_MOUSE_BUTTON_LEFT) {
|
|
if (widget->gui_color == PGPL_GUI_STATUS_COLOR_ACTIVE) {
|
|
button_widget->click_callback(gui->app_data);
|
|
}
|
|
widget->gui_color = PGPL_GUI_STATUS_COLOR_HOVER;
|
|
} else {
|
|
if (widget->gui_color != PGPL_GUI_STATUS_COLOR_ACTIVE) {
|
|
widget->gui_color = PGPL_GUI_STATUS_COLOR_HOVER;
|
|
}
|
|
}
|
|
} else {
|
|
widget->gui_color = PGPL_GUI_STATUS_COLOR_NORMAL;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void destroy(PGPL_GuiWidget *widget) {
|
|
pgpl_gui_button_widget_destroy((PGPL_GuiButtonWidget *)widget);
|
|
}
|
|
|
|
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;
|
|
button_widget->parent.event = event;
|
|
button_widget->parent.destroy = destroy;
|
|
button_widget->text = text;
|
|
button_widget->click_callback = click_callback;
|
|
return button_widget;
|
|
}
|
|
|
|
void pgpl_gui_button_widget_destroy(PGPL_GuiButtonWidget *button_widget) {
|
|
free(button_widget);
|
|
}
|