#include "../../include/mgui/button.h" #include "../../include/resource_loader.h" #include "../../include/common.h" #include "../../include/alloc.h" #include #include #include mgui_button* mgui_button_create() { mgui_button *button = mgui_alloc(sizeof(mgui_button)); mgui_widget_init(&button->widget, MGUI_WIDGET_BUTTON); button->background.position = (mgl_vec2f){ 0.0f, 0.0f }; button->background.size = (mgl_vec2f){ 0.0f, 0.0f }; button->background.color = (mgl_color){ 45, 45, 45, 255 }; mgl_text_init(&button->label, mgui_get_font(MGUI_FONT_LATIN, 32), "Label", 5); return button; } mgui_widget* mgui_button_to_widget(mgui_button *list) { return &list->widget; } mgui_button* mgui_widget_to_button(mgui_widget *widget) { assert(widget->type == MGUI_WIDGET_BUTTON); return (mgui_button*)widget; } void mgui_button_set_position(mgui_button *self, mgl_vec2i position) { const mgl_vec2f position_f = (mgl_vec2f){ position.x, position.y }; self->background.position = position_f; mgl_text_set_position(&self->label, position_f); } void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *event) { // TODO: Implement (void)window; if(event->type == MGL_EVENT_MOUSE_MOVED) { if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_move.x, event->mouse_move.y })) { self->background.color = (mgl_color){ 70, 70, 70, 255 }; } else { self->background.color = (mgl_color){ 45, 45, 45, 255 }; } } else if(event->type == MGL_EVENT_MOUSE_BUTTON_PRESSED && event->mouse_button.button == MGL_BUTTON_LEFT) { if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_button.x, event->mouse_button.y })) { } } } mgl_vec2i mgui_button_draw(mgui_button *self, mgl_window *window) { (void)window; self->background.size = mgl_text_get_bounds(&self->label); mgl_rectangle_draw(mgl_get_context(), &self->background); mgl_text_draw(mgl_get_context(), &self->label); return (mgl_vec2i){ self->background.size.x, self->background.size.y }; }