aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TODO3
-rw-r--r--include/mgui/button.h6
-rw-r--r--include/mgui/image.h1
-rw-r--r--include/mgui/label.h1
-rw-r--r--include/mgui/list.h1
-rw-r--r--include/mgui/richtext.h1
-rw-r--r--include/mgui/scrollview.h1
-rw-r--r--include/mgui/widget.h1
-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
-rw-r--r--tests/main.c3
16 files changed, 82 insertions, 8 deletions
diff --git a/TODO b/TODO
index d5220a9..866aa89 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,5 @@
Widget alignment.
Widget horizontal/vertical expand.
-Destroy widgets when the container is destroyed.
Homogeneous sizing for list items.
Allow setting list direction. This is more efficient than prepending widgets to a list.
-To do not render widgets outside the current scissor (or window). Also stop rendering scroll view when reaching outside the scissor view. \ No newline at end of file
+Stop rendering scroll view when reaching outside the scissor view. \ No newline at end of file
diff --git a/include/mgui/button.h b/include/mgui/button.h
index d9e8783..986cb9c 100644
--- a/include/mgui/button.h
+++ b/include/mgui/button.h
@@ -4,14 +4,18 @@
#include "widget.h"
#include <mgl/graphics/rectangle.h>
#include <mgl/graphics/text.h>
+#include <stddef.h>
typedef struct {
mgui_widget widget;
+ char *str;
+ size_t str_size;
mgl_rectangle background;
mgl_text text;
} mgui_button;
-mgui_button* mgui_button_create();
+mgui_button* mgui_button_create(const char *str, size_t size, unsigned char character_size);
+void mgui_button_destroy(mgui_button *button);
mgui_widget* mgui_button_to_widget(mgui_button *list);
mgui_button* mgui_widget_to_button(mgui_widget *widget);
diff --git a/include/mgui/image.h b/include/mgui/image.h
index c1072d4..8d34a7a 100644
--- a/include/mgui/image.h
+++ b/include/mgui/image.h
@@ -10,6 +10,7 @@ typedef struct {
} mgui_image;
mgui_image* mgui_image_create(const char *filepath);
+void mgui_image_destroy(mgui_image *image);
mgui_widget* mgui_image_to_widget(mgui_image *list);
mgui_image* mgui_widget_to_image(mgui_widget *widget);
diff --git a/include/mgui/label.h b/include/mgui/label.h
index 920ecf9..4b66ef8 100644
--- a/include/mgui/label.h
+++ b/include/mgui/label.h
@@ -15,6 +15,7 @@ typedef struct {
} mgui_label;
mgui_label* mgui_label_create(const char *str, size_t size, unsigned char character_size);
+void mgui_label_destroy(mgui_label *label);
mgui_widget* mgui_label_to_widget(mgui_label *list);
mgui_label* mgui_widget_to_label(mgui_widget *widget);
diff --git a/include/mgui/list.h b/include/mgui/list.h
index f0080c8..af1ceae 100644
--- a/include/mgui/list.h
+++ b/include/mgui/list.h
@@ -24,6 +24,7 @@ typedef struct {
} mgui_list;
mgui_list* mgui_list_create(mgui_list_direction direction);
+void mgui_list_destroy(mgui_list *list);
mgui_widget* mgui_list_to_widget(mgui_list *list);
mgui_list* mgui_widget_to_list(mgui_widget *widget);
diff --git a/include/mgui/richtext.h b/include/mgui/richtext.h
index 52706f5..c0604c9 100644
--- a/include/mgui/richtext.h
+++ b/include/mgui/richtext.h
@@ -26,6 +26,7 @@ typedef struct {
} mgui_richtext;
mgui_richtext* mgui_richtext_create(const char *str, size_t size, unsigned char character_size);
+void mgui_richtext_destroy(mgui_richtext *richtext);
mgui_widget* mgui_richtext_to_widget(mgui_richtext *list);
mgui_richtext* mgui_widget_to_richtext(mgui_widget *widget);
diff --git a/include/mgui/scrollview.h b/include/mgui/scrollview.h
index d394e50..1b3d109 100644
--- a/include/mgui/scrollview.h
+++ b/include/mgui/scrollview.h
@@ -14,6 +14,7 @@ typedef struct {
} mgui_scrollview;
mgui_scrollview* mgui_scrollview_create();
+void mgui_scrollview_destroy(mgui_scrollview *scrollview);
mgui_widget* mgui_scrollview_to_widget(mgui_scrollview *list);
mgui_scrollview* mgui_widget_to_scrollview(mgui_widget *widget);
diff --git a/include/mgui/widget.h b/include/mgui/widget.h
index ee3a9f1..679e4ad 100644
--- a/include/mgui/widget.h
+++ b/include/mgui/widget.h
@@ -29,6 +29,7 @@ struct mgui_widget {
};
void mgui_widget_init(mgui_widget *self, mgui_widget_type type);
+void mgui_widget_destroy(mgui_widget *widget);
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);
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;
diff --git a/tests/main.c b/tests/main.c
index e1aa6f1..e02d4bb 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -13,7 +13,7 @@
static mgui_list* create_list_item(const char *title, const char *description) {
mgui_list *container = mgui_list_create(MGUI_LIST_HORIZONITAL);
mgui_list_set_background_color(container, (mgl_color){ 255, 0, 0, 255 });
- mgui_list_append(container, mgui_button_to_widget(mgui_button_create()));
+ mgui_list_append(container, mgui_button_to_widget(mgui_button_create("button", 6, 30)));
mgui_list *list = mgui_list_create(MGUI_LIST_VERTICAL);
mgui_list_append(container, mgui_list_to_widget(list));
@@ -54,6 +54,7 @@ int main() {
mgl_window_display(&window);
}
+ mgui_widget_destroy(mgui_scrollview_to_widget(scrollview));
mgl_deinit();
return 0;
}