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
|
#include "../../include/gui/Button.hpp"
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <mglpp/system/FloatRect.hpp>
namespace gsr {
Button::Button(mgl::vec2f size) : size(size) {
}
bool Button::on_event(mgl::Event &event, mgl::Window&) {
if(event.type == mgl::Event::MouseMoved) {
const bool inside = mgl::FloatRect(position, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y });
if(mouse_inside && !inside) {
mouse_inside = false;
} else if(!mouse_inside && inside) {
mouse_inside = true;
}
} else if(event.type == mgl::Event::MouseButtonPressed) {
pressed_inside = mouse_inside;
} else if(event.type == mgl::Event::MouseButtonReleased) {
const bool clicked_inside = pressed_inside && mouse_inside;
pressed_inside = false;
if(clicked_inside && on_click)
on_click();
}
return true;
}
void Button::draw(mgl::Window &window) {
if(mouse_inside) {
// Background
{
mgl::Rectangle rect(size);
rect.set_position(position);
rect.set_color(mgl::Color(0, 0, 0, 255));
window.draw(rect);
}
const int border_size = 5;
const mgl::Color border_color(118, 185, 0);
// Green line at top
{
mgl::Rectangle rect({ size.x, border_size });
rect.set_position(position);
rect.set_color(border_color);
window.draw(rect);
}
// Green line at bottom
{
mgl::Rectangle rect({ size.x, border_size });
rect.set_position(position + mgl::vec2f(0.0f, size.y - border_size));
rect.set_color(border_color);
window.draw(rect);
}
// Green line at left
{
mgl::Rectangle rect({ border_size, size.y - border_size * 2 });
rect.set_position(position + mgl::vec2f(0, border_size));
rect.set_color(border_color);
window.draw(rect);
}
// Green line at right
{
mgl::Rectangle rect({ border_size, size.y - border_size * 2 });
rect.set_position(position + mgl::vec2f(size.x - border_size, border_size));
rect.set_color(border_color);
window.draw(rect);
}
} else {
// Background
mgl::Rectangle rect(size);
rect.set_position(position);
rect.set_color(mgl::Color(0, 0, 0, 250));
window.draw(rect);
}
}
}
|