aboutsummaryrefslogtreecommitdiff
path: root/src/mgui
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-12-02 14:20:48 +0100
committerdec05eba <dec05eba@protonmail.com>2021-12-02 14:29:13 +0100
commit14770f42df291753a240def91fb488a904a909c1 (patch)
treec1ef52c408cb365890da4aeb5536256040bb96fd /src/mgui
Initial commit, set up skeleton with list and button
Diffstat (limited to 'src/mgui')
-rw-r--r--src/mgui/button.c55
-rw-r--r--src/mgui/list.c65
-rw-r--r--src/mgui/widget.c48
3 files changed, 168 insertions, 0 deletions
diff --git a/src/mgui/button.c b/src/mgui/button.c
new file mode 100644
index 0000000..d8d0c58
--- /dev/null
+++ b/src/mgui/button.c
@@ -0,0 +1,55 @@
+#include "../../include/mgui/button.h"
+#include "../../include/resource_loader.h"
+#include "../../include/common.h"
+#include "../../include/alloc.h"
+#include <mgl/mgl.h>
+#include <mgl/window/event.h>
+#include <assert.h>
+
+mgui_button* mgui_button_create() {
+ mgui_button *button = mgui_alloc(sizeof(mgui_button));
+ mgui_widget_init(&button->widget, MGUI_WIDGET_BUTTON);
+ button->background.position = (mgl_vec2f){ 0.0f, 0.0f };
+ 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);
+ return button;
+}
+
+mgui_widget* mgui_button_to_widget(mgui_button *list) {
+ return &list->widget;
+}
+
+mgui_button* mgui_widget_to_button(mgui_widget *widget) {
+ assert(widget->type == MGUI_WIDGET_BUTTON);
+ return (mgui_button*)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);
+}
+
+void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *event) {
+ // TODO: Implement
+ (void)window;
+ if(event->type == MGL_EVENT_MOUSE_MOVED) {
+ if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_move.x, event->mouse_move.y })) {
+ self->background.color = (mgl_color){ 70, 70, 70, 255 };
+ } else {
+ self->background.color = (mgl_color){ 45, 45, 45, 255 };
+ }
+ } else if(event->type == MGL_EVENT_MOUSE_BUTTON_PRESSED && event->mouse_button.button == MGL_BUTTON_LEFT) {
+ if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_button.x, event->mouse_button.y })) {
+
+ }
+ }
+}
+
+void 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);
+}
diff --git a/src/mgui/list.c b/src/mgui/list.c
new file mode 100644
index 0000000..8dc4283
--- /dev/null
+++ b/src/mgui/list.c
@@ -0,0 +1,65 @@
+#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) {
+ 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 */
+}
diff --git a/src/mgui/widget.c b/src/mgui/widget.c
new file mode 100644
index 0000000..242174d
--- /dev/null
+++ b/src/mgui/widget.c
@@ -0,0 +1,48 @@
+#include "../../include/mgui/widget.h"
+#include "../../include/mgui/list.h"
+#include "../../include/mgui/button.h"
+
+void mgui_widget_init(mgui_widget *self, mgui_widget_type type) {
+ self->type = type;
+ mgui_widget_set_margin(self, 0, 0, 0, 0);
+}
+
+void mgui_widget_set_margin(mgui_widget *self, int left, int top, int right, int bottom) {
+ self->margin.left = left;
+ self->margin.top = top;
+ self->margin.right = right;
+ self->margin.bottom = bottom;
+}
+
+void mgui_widget_set_position(mgui_widget *self, mgl_vec2i position) {
+ switch(self->type) {
+ case MGUI_WIDGET_LIST:
+ mgui_list_set_position(mgui_widget_to_list(self), position);
+ break;
+ case MGUI_WIDGET_BUTTON:
+ mgui_button_set_position(mgui_widget_to_button(self), position);
+ break;
+ }
+}
+
+void mgui_widget_on_event(mgui_widget *self, mgl_window *window, mgl_event *event) {
+ switch(self->type) {
+ case MGUI_WIDGET_LIST:
+ mgui_list_on_event(mgui_widget_to_list(self), window, event);
+ break;
+ case MGUI_WIDGET_BUTTON:
+ mgui_button_on_event(mgui_widget_to_button(self), window, event);
+ break;
+ }
+}
+
+void 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;
+ case MGUI_WIDGET_BUTTON:
+ mgui_button_draw(mgui_widget_to_button(self), window);
+ break;
+ }
+}