#include "../../include/mgui/widget.h" #include "../../include/mgui/list.h" #include "../../include/mgui/scrollview.h" #include "../../include/mgui/button.h" #include "../../include/mgui/label.h" #include "../../include/mgui/richtext.h" #include "../../include/mgui/image.h" #include "../../include/alloc.h" #include #include /* TODO: Use margin */ /* TODO: Use alignment */ /* TODO: Use visible flag */ static void mgui_widget_set_flag(mgui_widget *self, uint8_t flag) { self->flags |= flag; } static void mgui_widget_unset_flag(mgui_widget *self, uint8_t flag) { self->flags &= ~flag; } static void mgui_widget_set_flag_state(mgui_widget *self, uint8_t flag, bool set) { if(set) mgui_widget_set_flag(self, flag); else mgui_widget_unset_flag(self, flag); } void mgui_widget_init(mgui_widget *self, mgui_widget_type type) { self->type = type; self->flags = MGUI_WIDGET_VISIBLE; self->alignment = MGUI_WIDGET_ALIGN_TOP_LEFT; mgui_widget_set_margin(self, (mgui_margin){ 0, 0, 0, 0 }); self->size = (mgl_vec2i){ 0, 0 }; } void mgui_widget_destroy(mgui_widget *widget) { switch(widget->type) { case MGUI_WIDGET_LIST: mgui_list_destroy(mgui_widget_to_list(widget)); break; case MGUI_WIDGET_SCROLLVIEW: mgui_scrollview_destroy(mgui_widget_to_scrollview(widget)); break; case MGUI_WIDGET_BUTTON: mgui_button_destroy(mgui_widget_to_button(widget)); break; case MGUI_WIDGET_LABEL: mgui_label_destroy(mgui_widget_to_label(widget)); break; case MGUI_WIDGET_RICHTEXT: mgui_richtext_destroy(mgui_widget_to_richtext(widget)); break; case MGUI_WIDGET_IMAGE: mgui_image_destroy(mgui_widget_to_image(widget)); break; } } void mgui_widget_set_visible(mgui_widget *self, bool visible) { mgui_widget_set_flag_state(self, MGUI_WIDGET_VISIBLE, visible); } void mgui_widget_set_expand(mgui_widget *self, bool horizontal, bool vertical) { mgui_widget_set_flag_state(self, MGUI_WIDGET_EXPAND_HORIZONTAL, horizontal); mgui_widget_set_flag_state(self, MGUI_WIDGET_EXPAND_VERTICAL, vertical); } void mgui_widget_set_alignment(mgui_widget *self, mgui_alignment alignment) { self->alignment = alignment; } void mgui_widget_set_margin(mgui_widget *self, mgui_margin margin) { self->margin = margin; } void mgui_widget_set_size(mgui_widget *self, mgl_vec2i size) { self->size = size; } void mgui_widget_set_position(mgui_widget *self, mgl_vec2i position) { switch(self->type) { case MGUI_WIDGET_LIST: mgui_list_set_position(mgui_widget_to_list(self), position); break; case MGUI_WIDGET_SCROLLVIEW: mgui_scrollview_set_position(mgui_widget_to_scrollview(self), position); break; case MGUI_WIDGET_BUTTON: mgui_button_set_position(mgui_widget_to_button(self), position); break; case MGUI_WIDGET_LABEL: mgui_label_set_position(mgui_widget_to_label(self), position); break; case MGUI_WIDGET_RICHTEXT: mgui_richtext_set_position(mgui_widget_to_richtext(self), position); break; case MGUI_WIDGET_IMAGE: mgui_image_set_position(mgui_widget_to_image(self), position); break; } } void mgui_widget_calculate_size(mgui_widget *self, mgl_vec2i max_size) { switch(self->type) { case MGUI_WIDGET_LIST: mgui_list_calculate_size(mgui_widget_to_list(self), max_size); break; case MGUI_WIDGET_SCROLLVIEW: mgui_scrollview_calculate_size(mgui_widget_to_scrollview(self), max_size); break; case MGUI_WIDGET_BUTTON: mgui_button_calculate_size(mgui_widget_to_button(self), max_size); break; case MGUI_WIDGET_LABEL: mgui_label_calculate_size(mgui_widget_to_label(self), max_size); break; case MGUI_WIDGET_RICHTEXT: mgui_richtext_calculate_size(mgui_widget_to_richtext(self), max_size); break; case MGUI_WIDGET_IMAGE: mgui_image_calculate_size(mgui_widget_to_image(self), max_size); break; } } void mgui_widget_set_has_parent(mgui_widget *self) { if(self->flags & MGUI_WIDGET_DEBUG_HAS_PARENT) { fprintf(stderr, "mglgui error: widget %p was added to a list/scrollview twice. One widget can only belong to one list/scrollview.\n", (void*)self); abort(); } self->flags |= MGUI_WIDGET_DEBUG_HAS_PARENT; } void mgui_widget_on_event(mgui_widget *self, mgl_window *window, mgl_event *event) { switch(self->type) { case MGUI_WIDGET_LIST: mgui_list_on_event(mgui_widget_to_list(self), window, event); break; case MGUI_WIDGET_SCROLLVIEW: mgui_scrollview_on_event(mgui_widget_to_scrollview(self), window, event); break; case MGUI_WIDGET_BUTTON: mgui_button_on_event(mgui_widget_to_button(self), window, event); break; case MGUI_WIDGET_LABEL: mgui_label_on_event(mgui_widget_to_label(self), window, event); break; case MGUI_WIDGET_RICHTEXT: mgui_richtext_on_event(mgui_widget_to_richtext(self), window, event); break; case MGUI_WIDGET_IMAGE: mgui_image_on_event(mgui_widget_to_image(self), window, event); break; } } void mgui_widget_draw(mgui_widget *self, mgl_window *window) { switch(self->type) { case MGUI_WIDGET_LIST: mgui_list_draw(mgui_widget_to_list(self), window); break; case MGUI_WIDGET_SCROLLVIEW: mgui_scrollview_draw(mgui_widget_to_scrollview(self), window); break; case MGUI_WIDGET_BUTTON: mgui_button_draw(mgui_widget_to_button(self), window); break; case MGUI_WIDGET_LABEL: mgui_label_draw(mgui_widget_to_label(self), window); break; case MGUI_WIDGET_RICHTEXT: mgui_richtext_draw(mgui_widget_to_richtext(self), window); break; case MGUI_WIDGET_IMAGE: mgui_image_draw(mgui_widget_to_image(self), window); break; } }