aboutsummaryrefslogtreecommitdiff
path: root/src/mgui/label.c
blob: 5ab1bc091f2bd74a4409aefb5b8185b57f20d71b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 };
}