#include "../../include/mgui/list.h" #include "../../include/alloc.h" #include #include mgui_list* mgui_list_create(mgui_list_direction direction) { 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->items = NULL; list->num_items = 0; list->items_capacity = 0; return list; } mgui_widget* mgui_list_to_widget(mgui_list *list) { return &list->widget; } mgui_list* mgui_widget_to_list(mgui_widget *widget) { assert(widget->type == MGUI_WIDGET_LIST); return (mgui_list*)widget; } void mgui_list_set_position(mgui_list *self, mgl_vec2i position) { self->position = position; } static void mgui_list_ensure_capacity(mgui_list *self, size_t new_capacity) { if(self->items_capacity >= new_capacity) return; size_t capacity = self->items_capacity; if(capacity == 0) capacity = 8; while(capacity < new_capacity) { capacity = capacity + (capacity >> 1); /* capacity *= 1.5 */ } self->items = mgui_realloc(self->items, capacity); self->items_capacity = capacity; } void mgui_list_append(mgui_list *self, mgui_widget *widget) { mgui_list_ensure_capacity(self, (self->num_items + 1) * sizeof(mgui_widget*)); self->items[self->num_items] = widget; ++self->num_items; } void mgui_list_on_event(mgui_list *self, mgl_window *window, mgl_event *event) { for(size_t i = 0; i < self->num_items; ++i) { mgui_widget_on_event(self->items[i], window, 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); } context->gl.glLoadIdentity(); /* TODO: Remove, but what about glRotatef above */ }