aboutsummaryrefslogtreecommitdiff
path: root/src/mgui/button.c
blob: d77750d36ee64eca7533b0ec7a0acb9b5e3020cf (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "../../include/mgui/button.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>

static int min_int(int a, int b) {
    return a <= b ? a : b;
}

mgui_button* mgui_button_create(const char *str, size_t size, unsigned char character_size) {
    /* TODO: Make this work for character size >= 100 */
    if(character_size >= 100)
        character_size = 99;

    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 };
    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;
}

mgui_button* mgui_widget_to_button(mgui_widget *widget) {
    assert(widget->type == MGUI_WIDGET_BUTTON);
    return (mgui_button*)widget;
}

void mgui_button_set_position(mgui_button *self, mgl_vec2i position) {
    const mgl_vec2f text_bounds = mgl_text_get_bounds(&self->text);
    self->background.position = (mgl_vec2f){ position.x, position.y };
    mgl_text_set_position(&self->text, (mgl_vec2f){
        (int)(self->background.position.x + self->background.size.x * 0.5f - text_bounds.x * 0.5f),
        (int)(self->background.position.y + self->background.size.y * 0.5f - text_bounds.y * 0.5f)
    });
}

void mgui_button_calculate_size(mgui_button *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->background.size = (mgl_vec2f){ min_int(text_bounds_int.x, max_size.x), min_int(text_bounds_int.y, max_size.y) };
    mgui_button_set_position(self, (mgl_vec2i){ self->background.position.x, self->background.position.y });
    self->widget.size = (mgl_vec2i){ self->background.size.x, self->background.size.y };
}

void mgui_button_on_event(mgui_button *self, mgl_window *window, mgl_event *event) {
    (void)window;
    if(event->type == MGL_EVENT_MOUSE_MOVED) {
        if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_move.x, event->mouse_move.y })) {
            self->background.color = (mgl_color){ 70, 70, 70, 255 };
        } else {
            self->background.color = (mgl_color){ 45, 45, 45, 255 };
        }
    } else if(event->type == MGL_EVENT_MOUSE_BUTTON_PRESSED && event->mouse_button.button == MGL_BUTTON_LEFT) {
        if(mgui_rectangle_contains(self->background.position, self->background.size, (mgl_vec2f){ event->mouse_button.x, event->mouse_button.y })) {
            
        }
    }
}

void mgui_button_draw(mgui_button *self, mgl_window *window) {
    const mgl_vec2i position = (mgl_vec2i){ self->background.position.x, self->background.position.y };
    const mgl_vec2i size = (mgl_vec2i){ self->background.size.x, self->background.size.y };
    if(mgui_rectangle_intersects_with_scissor(position, size, window)) {
        mgl_rectangle_draw(mgl_get_context(), &self->background);
        mgl_text_draw(mgl_get_context(), &self->text);
    }
}