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 */
static int min_int(int a, int b) {
return a <= b ? a : b;
}
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);
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;
}
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) {
mgl_text_set_position(&self->text, (mgl_vec2f){ position.x, position.y });
}
void mgui_label_calculate_size(mgui_label *self, mgl_vec2i max_size) {
const mgl_vec2f text_bounds = mgl_text_get_bounds(&self->text);
const mgl_vec2i text_bounds_int = (mgl_vec2i){ text_bounds.x, text_bounds.y };
self->widget.size.x = min_int(text_bounds_int.x, max_size.x);
self->widget.size.y = min_int(text_bounds_int.y, max_size.y);
}
void mgui_label_on_event(mgui_label *self, mgl_window *window, mgl_event *event) {
/* TODO: Implement */
(void)self;
(void)window;
(void)event;
}
void mgui_label_draw(mgui_label *self, mgl_window *window) {
const mgl_vec2f text_bounds = mgl_text_get_bounds(&self->text);
const mgl_vec2i text_bounds_int = (mgl_vec2i){ text_bounds.x, text_bounds.y };
if(mgui_rectangle_intersects_with_scissor((mgl_vec2i){ self->text.position.x, self->text.position.y }, text_bounds_int, window))
mgl_text_draw(mgl_get_context(), &self->text);
}
|