aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-12-08 04:16:34 +0100
committerdec05eba <dec05eba@protonmail.com>2021-12-08 04:16:34 +0100
commit396686a09ef471499c11256b8516c2702f761060 (patch)
tree9215d37141ae1d387fb38dd69e5dd36466012531 /src
parent319accba370d3f0872501de6386833fea140fbac (diff)
Destroy widgets
Diffstat (limited to 'src')
-rw-r--r--src/mgui/button.c14
-rw-r--r--src/mgui/image.c4
-rw-r--r--src/mgui/label.c6
-rw-r--r--src/mgui/list.c8
-rw-r--r--src/mgui/richtext.c8
-rw-r--r--src/mgui/scrollview.c8
-rw-r--r--src/mgui/widget.c24
7 files changed, 68 insertions, 4 deletions
diff --git a/src/mgui/button.c b/src/mgui/button.c
index 4abe8a0..02070c3 100644
--- a/src/mgui/button.c
+++ b/src/mgui/button.c
@@ -4,19 +4,29 @@
#include "../../include/alloc.h"
#include <mgl/mgl.h>
#include <mgl/window/event.h>
+#include <string.h>
#include <assert.h>
-mgui_button* mgui_button_create() {
+mgui_button* mgui_button_create(const char *str, size_t size, unsigned char character_size) {
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->text, mgui_get_font(MGUI_FONT_LATIN, 32), "Label", 5);
+ button->str = mgui_alloc(size);
+ button->str_size = size;
+ memcpy(button->str, str, size);
+ mgl_text_init(&button->text, mgui_get_font(MGUI_FONT_LATIN, character_size), button->str, button->str_size);
button->background.size = mgl_text_get_bounds(&button->text);
return button;
}
+void mgui_button_destroy(mgui_button *button) {
+ mgl_text_deinit(&button->text);
+ mgui_free(button->str);
+ mgui_free(button);
+}
+
mgui_widget* mgui_button_to_widget(mgui_button *list) {
return &list->widget;
}
diff --git a/src/mgui/image.c b/src/mgui/image.c
index a3b163e..c2ff12f 100644
--- a/src/mgui/image.c
+++ b/src/mgui/image.c
@@ -18,6 +18,10 @@ mgui_image* mgui_image_create(const char *filepath) {
return image;
}
+void mgui_image_destroy(mgui_image *image) {
+ mgui_free(image);
+}
+
mgui_widget* mgui_image_to_widget(mgui_image *list) {
return &list->widget;
}
diff --git a/src/mgui/label.c b/src/mgui/label.c
index e67821d..767297c 100644
--- a/src/mgui/label.c
+++ b/src/mgui/label.c
@@ -21,6 +21,12 @@ mgui_label* mgui_label_create(const char *str, size_t size, unsigned char charac
return label;
}
+void mgui_label_destroy(mgui_label *label) {
+ mgl_text_deinit(&label->text);
+ mgui_free(label->str);
+ mgui_free(label);
+}
+
mgui_widget* mgui_label_to_widget(mgui_label *list) {
return &list->widget;
}
diff --git a/src/mgui/list.c b/src/mgui/list.c
index cf39b02..58fc964 100644
--- a/src/mgui/list.c
+++ b/src/mgui/list.c
@@ -20,6 +20,14 @@ mgui_list* mgui_list_create(mgui_list_direction direction) {
return list;
}
+void mgui_list_destroy(mgui_list *list) {
+ for(size_t i = 0; i < list->num_items; ++i) {
+ mgui_widget_destroy(list->items[i]);
+ }
+ mgui_free(list->items);
+ mgui_free(list);
+}
+
mgui_widget* mgui_list_to_widget(mgui_list *list) {
return &list->widget;
}
diff --git a/src/mgui/richtext.c b/src/mgui/richtext.c
index 2f7c163..c70967f 100644
--- a/src/mgui/richtext.c
+++ b/src/mgui/richtext.c
@@ -112,6 +112,14 @@ mgui_richtext* mgui_richtext_create(const char *str, size_t size, unsigned char
return richtext;
}
+void mgui_richtext_destroy(mgui_richtext *richtext) {
+ for(size_t i = 0; i < NUM_VERTEX_DATA; ++i) {
+ mgui_free(richtext->vertex_data[i].vertices);
+ }
+ mgui_free(richtext->str);
+ mgui_free(richtext);
+}
+
mgui_widget* mgui_richtext_to_widget(mgui_richtext *list) {
return &list->widget;
}
diff --git a/src/mgui/scrollview.c b/src/mgui/scrollview.c
index e5b8bb8..3c5850c 100644
--- a/src/mgui/scrollview.c
+++ b/src/mgui/scrollview.c
@@ -9,8 +9,6 @@
#define SCROLL_ACCEL 20.0f
#define SCROLL_DEACCEL 10.0f
-/* TODO: free child */
-
mgui_scrollview* mgui_scrollview_create() {
mgui_scrollview *scrollview = mgui_alloc(sizeof(mgui_scrollview));
mgui_widget_init(&scrollview->widget, MGUI_WIDGET_SCROLLVIEW);
@@ -23,6 +21,12 @@ mgui_scrollview* mgui_scrollview_create() {
return scrollview;
}
+void mgui_scrollview_destroy(mgui_scrollview *scrollview) {
+ if(scrollview->child)
+ mgui_widget_destroy(scrollview->child);
+ mgui_free(scrollview);
+}
+
mgui_widget* mgui_scrollview_to_widget(mgui_scrollview *list) {
return &list->widget;
}
diff --git a/src/mgui/widget.c b/src/mgui/widget.c
index 8a49715..b9865ca 100644
--- a/src/mgui/widget.c
+++ b/src/mgui/widget.c
@@ -5,6 +5,7 @@
#include "../../include/mgui/label.h"
#include "../../include/mgui/richtext.h"
#include "../../include/mgui/image.h"
+#include "../../include/alloc.h"
/* TODO: Use margin */
@@ -13,6 +14,29 @@ void mgui_widget_init(mgui_widget *self, mgui_widget_type type) {
mgui_widget_set_margin(self, 0, 0, 0, 0);
}
+void mgui_widget_destroy(mgui_widget *widget) {
+ switch(widget->type) {
+ case MGUI_WIDGET_LIST:
+ mgui_list_destroy(mgui_widget_to_list(widget));
+ break;
+ case MGUI_WIDGET_SCROLLVIEW:
+ mgui_scrollview_destroy(mgui_widget_to_scrollview(widget));
+ break;
+ case MGUI_WIDGET_BUTTON:
+ mgui_button_destroy(mgui_widget_to_button(widget));
+ break;
+ case MGUI_WIDGET_LABEL:
+ mgui_label_destroy(mgui_widget_to_label(widget));
+ break;
+ case MGUI_WIDGET_RICHTEXT:
+ mgui_richtext_destroy(mgui_widget_to_richtext(widget));
+ break;
+ case MGUI_WIDGET_IMAGE:
+ mgui_image_destroy(mgui_widget_to_image(widget));
+ break;
+ }
+}
+
void mgui_widget_set_margin(mgui_widget *self, int left, int top, int right, int bottom) {
self->margin.left = left;
self->margin.top = top;