diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-12-02 14:44:54 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-12-02 14:44:54 +0100 |
commit | df3eb74930491458f97a3328a68bf8526fef3caf (patch) | |
tree | 129dbc6e6d1c7c65af25d13427f92c45271f433e | |
parent | 14770f42df291753a240def91fb488a904a909c1 (diff) |
list set items position with rendering
-rw-r--r-- | include/mgui/button.h | 3 | ||||
-rw-r--r-- | include/mgui/list.h | 3 | ||||
-rw-r--r-- | include/mgui/widget.h | 3 | ||||
-rw-r--r-- | src/mgui/button.c | 3 | ||||
-rw-r--r-- | src/mgui/list.c | 38 | ||||
-rw-r--r-- | src/mgui/widget.c | 9 | ||||
-rw-r--r-- | tests/main.c | 4 |
7 files changed, 45 insertions, 18 deletions
diff --git a/include/mgui/button.h b/include/mgui/button.h index 8f9a2db..864d3c6 100644 --- a/include/mgui/button.h +++ b/include/mgui/button.h @@ -17,6 +17,7 @@ mgui_button* mgui_widget_to_button(mgui_widget *widget); void mgui_button_set_position(mgui_button *self, mgl_vec2i position); void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *event); -void mgui_button_draw(mgui_button *self, mgl_window *window); +/* Returns the size of the widget */ +mgl_vec2i mgui_button_draw(mgui_button *self, mgl_window *window); #endif /* MGUI_BUTTON_H */ diff --git a/include/mgui/list.h b/include/mgui/list.h index 1da326d..82efaf8 100644 --- a/include/mgui/list.h +++ b/include/mgui/list.h @@ -28,6 +28,7 @@ mgui_list* mgui_widget_to_list(mgui_widget *widget); void mgui_list_set_position(mgui_list *self, mgl_vec2i position); void mgui_list_append(mgui_list *self, mgui_widget *widget); void mgui_list_on_event(mgui_list *self, mgl_window *window, mgl_event *event); -void mgui_list_draw(mgui_list *self, mgl_window *window); +/* Returns the size of the widget */ +mgl_vec2i mgui_list_draw(mgui_list *self, mgl_window *window); #endif /* MGUI_LIST_H */ diff --git a/include/mgui/widget.h b/include/mgui/widget.h index 60f850c..4ff394c 100644 --- a/include/mgui/widget.h +++ b/include/mgui/widget.h @@ -28,6 +28,7 @@ void mgui_widget_init(mgui_widget *self, mgui_widget_type type); void mgui_widget_set_margin(mgui_widget *self, int left, int top, int right, int bottom); void mgui_widget_set_position(mgui_widget *self, mgl_vec2i position); void mgui_widget_on_event(mgui_widget *self, mgl_window *window, mgl_event *event); -void mgui_widget_draw(mgui_widget *self, mgl_window *window); +/* Returns the size of the widget */ +mgl_vec2i mgui_widget_draw(mgui_widget *self, mgl_window *window); #endif /* MGUI_WIDGET_H */ diff --git a/src/mgui/button.c b/src/mgui/button.c index d8d0c58..c6b65d5 100644 --- a/src/mgui/button.c +++ b/src/mgui/button.c @@ -47,9 +47,10 @@ void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *even } } -void mgui_button_draw(mgui_button *self, mgl_window *window) { +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 8dc4283..320b448 100644 --- a/src/mgui/list.c +++ b/src/mgui/list.c @@ -1,6 +1,5 @@ #include "../../include/mgui/list.h" #include "../../include/alloc.h" -#include <mgl/mgl.h> #include <assert.h> mgui_list* mgui_list_create(mgui_list_direction direction) { @@ -55,11 +54,36 @@ void mgui_list_on_event(mgui_list *self, mgl_window *window, mgl_event *event) { } } -void mgui_list_draw(mgui_list *self, mgl_window *window) { - mgl_context *context = mgl_get_context(); - context->gl.glTranslatef(self->position.x, self->position.y, 0.0f); - for(size_t i = 0; i < self->num_items; ++i) { - mgui_widget_draw(self->items[i], window); +static int max_int(int a, int b) { + return a >= b ? a : b; +} + +mgl_vec2i mgui_list_draw(mgui_list *self, mgl_window *window) { + mgl_vec2i position = self->position; + mgl_vec2i size = (mgl_vec2i){ 0, 0 }; + + switch(self->direction) { + case MGUI_LIST_HORIZONITAL: { + for(size_t i = 0; i < self->num_items; ++i) { + mgui_widget_set_position(self->items[i], position); + const mgl_vec2i widget_size = mgui_widget_draw(self->items[i], window); + size.x += widget_size.x; + size.y = max_int(size.y, widget_size.y); + position.x += widget_size.x; + } + break; + } + case MGUI_LIST_VERTICAL: { + for(size_t i = 0; i < self->num_items; ++i) { + mgui_widget_set_position(self->items[i], position); + const mgl_vec2i widget_size = mgui_widget_draw(self->items[i], window); + size.x = max_int(size.x, widget_size.x); + size.y += widget_size.y; + position.y += widget_size.y; + } + break; + } } - context->gl.glLoadIdentity(); /* TODO: Remove, but what about glRotatef above */ + + return size; } diff --git a/src/mgui/widget.c b/src/mgui/widget.c index 242174d..f35ff05 100644 --- a/src/mgui/widget.c +++ b/src/mgui/widget.c @@ -36,13 +36,12 @@ void mgui_widget_on_event(mgui_widget *self, mgl_window *window, mgl_event *even } } -void mgui_widget_draw(mgui_widget *self, mgl_window *window) { +mgl_vec2i 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; + return mgui_list_draw(mgui_widget_to_list(self), window); case MGUI_WIDGET_BUTTON: - mgui_button_draw(mgui_widget_to_button(self), window); - break; + return mgui_button_draw(mgui_widget_to_button(self), window); } + return (mgl_vec2i){ 0, 0 }; } diff --git a/tests/main.c b/tests/main.c index 6a94414..79afbea 100644 --- a/tests/main.c +++ b/tests/main.c @@ -13,8 +13,8 @@ int main() { return 1; mgui_list *list = mgui_list_create(MGUI_LIST_VERTICAL); - mgui_button *button = mgui_button_create(); - mgui_list_append(list, mgui_button_to_widget(button)); + mgui_list_append(list, mgui_button_to_widget(mgui_button_create())); + mgui_list_append(list, mgui_button_to_widget(mgui_button_create())); mgl_event event; while(mgl_window_is_open(&window)) { |