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/list.c | |
parent | df3eb74930491458f97a3328a68bf8526fef3caf (diff) |
Resize list widgets to the list width for vertical lists
Diffstat (limited to 'src/mgui/list.c')
-rw-r--r-- | src/mgui/list.c | 33 |
1 files changed, 31 insertions, 2 deletions
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 <mgl/window/window.h> #include <assert.h> -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; } |