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.c29
1 files changed, 11 insertions, 18 deletions
diff --git a/src/mgui/label.c b/src/mgui/label.c
index 767297c..e380aac 100644
--- a/src/mgui/label.c
+++ b/src/mgui/label.c
@@ -9,6 +9,10 @@
/* 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);
@@ -16,8 +20,6 @@ mgui_label* mgui_label_create(const char *str, size_t size, unsigned char charac
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;
}
@@ -37,22 +39,14 @@ mgui_label* mgui_widget_to_label(mgui_widget *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
- });
+ mgl_text_set_position(&self->text, (mgl_vec2f){ position.x, 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) {
+void mgui_label_calculate_size(mgui_label *self, mgl_vec2i max_size) {
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);
+ 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) {
@@ -62,10 +56,9 @@ void mgui_label_on_event(mgui_label *self, mgl_window *window, mgl_event *event)
(void)event;
}
-mgl_vec2i mgui_label_draw(mgui_label *self, mgl_window *window) {
+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(self->position, text_bounds_int, window))
+ 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);
- return text_bounds_int;
}