aboutsummaryrefslogtreecommitdiff
path: root/include/gui/Button.hpp
blob: 7070457b1b4dbb639603f71780823e2f00252e13 (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
#pragma once

#include "Widget.hpp"
#include <functional>

#include <mglpp/graphics/Color.hpp>
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Sprite.hpp>

namespace gsr {
    class Button : public Widget {
    public:
        // If width is 0 then the width of the text is used instead (with padding).
        // If height is 0 then the height of the text is used instead (with padding).
        Button(mgl::Font *font, const char *text, mgl::vec2f size, mgl::Color bg_color);
        Button(const Button&) = delete;
        Button& operator=(const Button&) = delete;

        bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override;
        void draw(mgl::Window &window, mgl::vec2f offset) override;

        mgl::vec2f get_size() override;
        void set_border_scale(float scale);
        void set_bg_hover_color(mgl::Color color);
        void set_icon(mgl::Texture *texture);

        const std::string& get_text() const;
        void set_text(std::string str);

        std::function<void()> on_click;
    private:
        void scale_sprite_to_button_size();
        float get_button_height();
    private:
        mgl::vec2f size;
        mgl::Color bg_color;
        mgl::Color bg_hover_color;
        mgl::Text text;
        mgl::Sprite sprite;
        float border_scale = 0.0015f;
    };
}