diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-12-02 16:48:23 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-12-02 16:48:23 +0100 |
commit | 9644f3c05b808a3cac3892aae36ffca2cce9357d (patch) | |
tree | 6f4b575c3e7fc21bea91a9721291aa8f97aa1c9c /src/mgui/button.c | |
parent | df3eb74930491458f97a3328a68bf8526fef3caf (diff) |
Resize list widgets to the list width for vertical lists
Diffstat (limited to 'src/mgui/button.c')
-rw-r--r-- | src/mgui/button.c | 21 |
1 files changed, 17 insertions, 4 deletions
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 }; |