aboutsummaryrefslogtreecommitdiff
path: root/src/mgui/label.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mgui/label.c')
-rw-r--r--src/mgui/label.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/mgui/label.c b/src/mgui/label.c
new file mode 100644
index 0000000..5ab1bc0
--- /dev/null
+++ b/src/mgui/label.c
@@ -0,0 +1,64 @@
+#include "../../include/mgui/label.h"
+#include "../../include/resource_loader.h"
+#include "../../include/common.h"
+#include "../../include/alloc.h"
+#include <mgl/mgl.h>
+#include <mgl/window/event.h>
+#include <string.h>
+#include <assert.h>
+
+/* TODO: Scale label by scale setting */
+
+mgui_label* mgui_label_create(const char *str, size_t size, unsigned char character_size) {
+ mgui_label *label = mgui_alloc(sizeof(mgui_label));
+ mgui_widget_init(&label->widget, MGUI_WIDGET_LABEL);
+ label->str = mgui_alloc(size);
+ label->str_size = size;
+ memcpy(label->str, str, size);
+ mgl_text_init(&label->text, mgui_get_font(MGUI_FONT_LATIN, character_size), label->str, label->str_size);
+ label->position = (mgl_vec2i){ 0, 0 };
+ label->width = mgl_text_get_bounds(&label->text).x;
+ return label;
+}
+
+mgui_widget* mgui_label_to_widget(mgui_label *list) {
+ return &list->widget;
+}
+
+mgui_label* mgui_widget_to_label(mgui_widget *widget) {
+ assert(widget->type == MGUI_WIDGET_LABEL);
+ return (mgui_label*)widget;
+}
+
+void mgui_label_set_position(mgui_label *self, mgl_vec2i position) {
+ const mgl_vec2f text_bounds = mgl_text_get_bounds(&self->text);
+ self->position = position;
+ mgl_text_set_position(&self->text, (mgl_vec2f){
+ (int)(position.x + self->width * 0.5f - text_bounds.x * 0.5f),
+ position.y
+ });
+}
+
+static int max_int(int a, int b) {
+ return a >= b ? a : b;
+}
+
+void mgui_label_set_width(mgui_label *self, int width) {
+ const mgl_vec2f text_bounds = mgl_text_get_bounds(&self->text);
+ self->width = max_int(text_bounds.x, width);
+ mgui_label_set_position(self, self->position);
+}
+
+void mgui_label_on_event(mgui_label *self, mgl_window *window, mgl_event *event) {
+ /* TODO: Implement */
+ (void)self;
+ (void)window;
+ (void)event;
+}
+
+mgl_vec2i mgui_label_draw(mgui_label *self, mgl_window *window) {
+ (void)window;
+ const mgl_vec2f text_bounds = mgl_text_get_bounds(&self->text);
+ mgl_text_draw(mgl_get_context(), &self->text);
+ return (mgl_vec2i){ text_bounds.x, text_bounds.y };
+}