From 9644f3c05b808a3cac3892aae36ffca2cce9357d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 2 Dec 2021 16:48:23 +0100 Subject: Resize list widgets to the list width for vertical lists --- src/mgui/button.c | 21 +++++++++++++++++---- src/mgui/list.c | 33 +++++++++++++++++++++++++++++++-- src/mgui/widget.c | 11 +++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) (limited to 'src/mgui') diff --git a/src/mgui/button.c b/src/mgui/button.c index c6b65d5..b548140 100644 --- a/src/mgui/button.c +++ b/src/mgui/button.c @@ -13,6 +13,7 @@ mgui_button* mgui_button_create() { 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); + button->background.size = mgl_text_get_bounds(&button->label); return button; } @@ -26,9 +27,22 @@ mgui_button* mgui_widget_to_button(mgui_widget *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); + const mgl_vec2f label_bounds = mgl_text_get_bounds(&self->label); + self->background.position = (mgl_vec2f){ position.x, position.y }; + mgl_text_set_position(&self->label, (mgl_vec2f){ + (int)(self->background.position.x + self->background.size.x * 0.5f - label_bounds.x * 0.5f), + (int)(self->background.position.y + self->background.size.y * 0.5f - label_bounds.y * 0.5f) + }); +} + +static int max_int(int a, int b) { + return a >= b ? a : b; +} + +void mgui_button_set_width(mgui_button *self, int width) { + mgl_vec2f label_bounds = mgl_text_get_bounds(&self->label); + self->background.size.x = max_int(label_bounds.x, width); + mgui_button_set_position(self, (mgl_vec2i){ self->background.position.x, self->background.position.y }); } void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *event) { @@ -49,7 +63,6 @@ void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *even 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 }; diff --git a/src/mgui/list.c b/src/mgui/list.c index 320b448..9923c4f 100644 --- a/src/mgui/list.c +++ b/src/mgui/list.c @@ -1,12 +1,14 @@ #include "../../include/mgui/list.h" #include "../../include/alloc.h" +#include #include -mgui_list* mgui_list_create(mgui_list_direction direction) { +mgui_list* mgui_list_create(mgui_list_direction direction, mgl_vec2i size) { mgui_list *list = mgui_alloc(sizeof(mgui_list)); mgui_widget_init(&list->widget, MGUI_WIDGET_LIST); list->direction = direction; list->position = (mgl_vec2i){ 0, 0 }; + list->size = size; list->items = NULL; list->num_items = 0; list->items_capacity = 0; @@ -26,6 +28,22 @@ void mgui_list_set_position(mgui_list *self, mgl_vec2i position) { self->position = position; } +void mgui_list_set_size(mgui_list *self, mgl_vec2i size) { + self->size = size; + /* TODO: if direction is horizontal then put the widget in the center of its area */ + /* TODO: put the widget in the center for vertical as well, for items with a max size */ + /* TODO: support max size for widgets */ + if(self->direction == MGUI_LIST_VERTICAL) { + for(size_t i = 0; i < self->num_items; ++i) { + mgui_widget_set_width(self->items[i], size.x); + } + } +} + +void mgui_list_set_width(mgui_list *self, int width) { + mgui_list_set_size(self, (mgl_vec2i){ width, self->size.y }); +} + static void mgui_list_ensure_capacity(mgui_list *self, size_t new_capacity) { if(self->items_capacity >= new_capacity) return; @@ -59,9 +77,19 @@ static int max_int(int a, int b) { } mgl_vec2i mgui_list_draw(mgui_list *self, mgl_window *window) { - mgl_vec2i position = self->position; + mgl_vec2i position = (mgl_vec2i){ 0, 0 }; + /* TODO: */ mgl_vec2i size = (mgl_vec2i){ 0, 0 }; + mgl_view prev_view; + mgl_window_get_view(window, &prev_view); + + mgl_view new_view = { + .position = { self->position.x, self->position.y }, + .size = self->size + }; + mgl_window_set_view(window, &new_view); + switch(self->direction) { case MGUI_LIST_HORIZONITAL: { for(size_t i = 0; i < self->num_items; ++i) { @@ -85,5 +113,6 @@ mgl_vec2i mgui_list_draw(mgui_list *self, mgl_window *window) { } } + mgl_window_set_view(window, &prev_view); /* TODO: Remove */ return size; } diff --git a/src/mgui/widget.c b/src/mgui/widget.c index f35ff05..195d253 100644 --- a/src/mgui/widget.c +++ b/src/mgui/widget.c @@ -25,6 +25,17 @@ void mgui_widget_set_position(mgui_widget *self, mgl_vec2i position) { } } +void mgui_widget_set_width(mgui_widget *self, int width) { + switch(self->type) { + case MGUI_WIDGET_LIST: + mgui_list_set_width(mgui_widget_to_list(self), width); + break; + case MGUI_WIDGET_BUTTON: + mgui_button_set_width(mgui_widget_to_button(self), width); + break; + } +} + void mgui_widget_on_event(mgui_widget *self, mgl_window *window, mgl_event *event) { switch(self->type) { case MGUI_WIDGET_LIST: -- cgit v1.2.3