aboutsummaryrefslogtreecommitdiff
path: root/src/gui/Button.cpp
blob: ebbb8bb55aeb9273d033761080e3395cd53a5e1a (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
#include "../../include/gui/Button.hpp"
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Window/Event.hpp>
#include <cmath>

namespace QuickMedia {
    static const float PADDING_Y = 10.0f;

    Button::Button(const std::string &label, sf::Font *font, unsigned int character_size, float width, sf::Shader *rounded_rectangle_shader, float scale) :
        label(label, *font, character_size * scale),
        background(sf::Vector2f(1.0f, 1.0f), 10.0f, sf::Color(33, 37, 44), rounded_rectangle_shader),
        scale(scale)
    {
        background.set_size(sf::Vector2f(std::floor(width * scale), get_height()));
        set_position(sf::Vector2f(0.0f, 0.0f));
    }

    ButtonEvent Button::on_event(sf::Event &event) {
        ButtonEvent performed_event = BUTTON_EVENT_NONE;
        if(event.type == sf::Event::MouseMoved) {
            if(sf::FloatRect(background.get_position(), background.get_size()).contains(event.mouseMove.x, event.mouseMove.y)) {
                const int inc = 20;
                background.set_color(sf::Color(
                    std::min(255, (int)background_color.r + inc),
                    std::min(255, (int)background_color.g + inc),
                    std::min(255, (int)background_color.b + inc)));
            } else {
                background.set_color(background_color);
            }
        } else if(event.type == sf::Event::MouseButtonPressed) {
            if(event.mouseButton.button == sf::Mouse::Left && sf::FloatRect(background.get_position(), background.get_size()).contains(event.mouseButton.x, event.mouseButton.y)) {
                clicked_inside = true;
            } else {
                clicked_inside = false;
            }
        } else if(event.type == sf::Event::MouseButtonReleased) {
            if(clicked_inside && event.mouseButton.button == sf::Mouse::Left && sf::FloatRect(background.get_position(), background.get_size()).contains(event.mouseButton.x, event.mouseButton.y)) {
                performed_event = BUTTON_EVENT_CLICKED;
            }
            clicked_inside = false;
        }
        return performed_event;
    }

    void Button::draw(sf::RenderTarget &target) {
        background.draw(target);
        target.draw(label);
    }

    void Button::set_background_color(sf::Color color) {
        background_color = color;
        background.set_color(background_color);
    }

    void Button::set_position(sf::Vector2f pos) {
        background.set_position(pos);

        const auto label_bounds = label.getLocalBounds();
        sf::Vector2f label_pos(pos + background.get_size() * 0.5f - sf::Vector2f(label_bounds.width * 0.5f, label_bounds.height * 0.5f) - sf::Vector2f(0.0f, 5.0f * scale));
        label_pos.x = std::floor(label_pos.x);
        label_pos.y = std::floor(label_pos.y);
        label.setPosition(label_pos);
    }

    sf::Vector2f Button::get_position() const {
        return background.get_position();
    }

    float Button::get_width() const {
        return background.get_size().x;
    }

    float Button::get_height() const {
        return std::floor((PADDING_Y * 2.0f) * scale + label.getLocalBounds().height);
    }
}