aboutsummaryrefslogtreecommitdiff
path: root/src/gui/Button.cpp
blob: 54d18544cff26254300864dd41011449440eb25d (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "../../include/gui/Button.hpp"
#include "../../include/gui/Utils.hpp"
#include "../../include/Theme.hpp"
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <mglpp/system/FloatRect.hpp>

namespace gsr {
    static const float padding_top_scale = 0.004629f;
    static const float padding_bottom_scale = 0.004629f;
    static const float padding_left_scale = 0.007f;
    static const float padding_right_scale = 0.007f;

    // These are relative to the button size
    static const float padding_top_icon_scale = 0.25f;
    static const float padding_bottom_icon_scale = 0.25f;
    static const float padding_left_icon_scale = 0.25f;
    static const float padding_right_icon_scale = 0.25f;

    Button::Button(mgl::Font *font, const char *text, mgl::vec2f size, mgl::Color bg_color) :
        size(size), bg_color(bg_color), bg_hover_color(bg_color), text(text, *font)
    {

    }

    bool Button::on_event(mgl::Event &event, mgl::Window&, mgl::vec2f offset) {
        if(!visible)
            return true;

        const mgl::vec2f item_size = get_size().floor();
        if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) {
            const bool clicked_inside = mgl::FloatRect(position + offset, item_size).contains({ (float)event.mouse_button.x, (float)event.mouse_button.y });
            if(clicked_inside) {
                if(on_click)
                    on_click();
                return false;
            }
        }
        return true;
    }

    void Button::draw(mgl::Window &window, mgl::vec2f offset) {
        if(!visible)
            return;

        const mgl::vec2f draw_pos = position + offset;
        const mgl::vec2f item_size = get_size().floor();
        const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(window.get_mouse_position().to_vec2f()) && !has_parent_with_selected_child_widget();

        mgl::Rectangle background(item_size);
        background.set_position(draw_pos.floor());
        background.set_color(mouse_inside ? bg_hover_color : bg_color);
        window.draw(background);

        text.set_position((draw_pos + item_size * 0.5f - text.get_bounds().size * 0.5f).floor());
        window.draw(text);

        if(sprite.get_texture() && sprite.get_texture()->is_valid()) {
            scale_sprite_to_button_size();
            sprite.set_position((background.get_position() + background.get_size() * 0.5f - sprite.get_size() * 0.5f).floor());
            window.draw(sprite);
        }

        if(mouse_inside) {
            const mgl::Color outline_color = (bg_color == get_color_theme().tint_color) ? mgl::Color(255, 255, 255) : get_color_theme().tint_color;
            draw_rectangle_outline(window, draw_pos, item_size, outline_color, std::max(1.0f, border_scale * get_theme().window_height));
        }
    }

    mgl::vec2f Button::get_size() {
        if(!visible)
            return {0.0f, 0.0f};

        const int padding_top = padding_top_scale * get_theme().window_height;
        const int padding_bottom = padding_bottom_scale * get_theme().window_height;
        const int padding_left = padding_left_scale * get_theme().window_height;
        const int padding_right = padding_right_scale * get_theme().window_height;

        const mgl::vec2f text_bounds = text.get_bounds().size;
        mgl::vec2f s = size;
        if(s.x < 0.0001f)
            s.x = padding_left + text_bounds.x + padding_right;
        if(s.y < 0.0001f)
            s.y = padding_top + text_bounds.y + padding_bottom;
        return s;
    }

    void Button::set_border_scale(float scale) {
        border_scale = scale;
    }

    void Button::set_bg_hover_color(mgl::Color color) {
        bg_hover_color = color;
    }

    void Button::set_icon(mgl::Texture *texture) {
        sprite.set_texture(texture);
    }

    const std::string& Button::get_text() const {
        return text.get_string();
    }

    void Button::set_text(std::string str) {
        text.set_string(std::move(str));
    }

    void Button::scale_sprite_to_button_size() {
        if(!sprite.get_texture() || !sprite.get_texture()->is_valid())
            return;

        const mgl::vec2f button_size = get_size();
        const int padding_icon_top = padding_top_icon_scale * button_size.y;
        const int padding_icon_bottom = padding_bottom_icon_scale * button_size.y;
        const int padding_icon_left = padding_left_icon_scale * button_size.y;
        const int padding_icon_right = padding_right_icon_scale * button_size.y;

        const mgl::vec2f desired_size = button_size - mgl::vec2f(padding_icon_left + padding_icon_right, padding_icon_top + padding_icon_bottom);
        sprite.set_size(scale_keep_aspect_ratio(sprite.get_texture()->get_size().to_vec2f(), desired_size).floor());
    }
}