-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
134 lines (115 loc) · 3.8 KB
/
Button.h
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
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
#include <SFML/Graphics.hpp>
#include "ButtonState.h"
class Button
{
public:
sf::RectangleShape rect;
sf::Text text;
sf::Font font;
ButtonState state;
int ticker = 30;
bool interactable;
Button()
{
ticker = 30;
this->rect = sf::RectangleShape();
this->text = sf::Text();
this->font.loadFromFile("resources/fonts/JetBrainsMono-Regular.ttf");
this->text.setFont(font);
this->text.setString("textToDisplay");
this->text.setCharacterSize(15);
this->text.setFillColor(sf::Color::Black);
this->text.setStyle(sf::Text::Bold);
this->rect.setSize(sf::Vector2f(text.getLocalBounds().width + 10, text.getLocalBounds().height + 10));
this->rect.setFillColor(sf::Color(150,150,150));
this->rect.setOutlineThickness(2);
this->rect.setOutlineColor(sf::Color::Black);
this->text.setOrigin(text.getLocalBounds().left + text.getLocalBounds().width / 2.0f, text.getLocalBounds().top + text.getLocalBounds().height / 2.0f);
this->text.setPosition(sf::Vector2f(0,0));
this->rect.setOrigin(rect.getLocalBounds().left + rect.getLocalBounds().width / 2.0f, rect.getLocalBounds().top + rect.getLocalBounds().height / 2.0f);
this->rect.setPosition(sf::Vector2f(0,0));
this->state = DEFAULT;
this->interactable = true;
}
Button(std::string textToDisplay, sf::Vector2f position)
{
ticker = 30;
this->rect = sf::RectangleShape();
this->text = sf::Text();
font.loadFromFile("resources/fonts/JetBrainsMono-Regular.ttf");
text.setFont(font);
text.setString(textToDisplay);
text.setCharacterSize(15);
text.setFillColor(sf::Color::Black);
text.setStyle(sf::Text::Bold);
text.setOrigin(text.getLocalBounds().left + text.getLocalBounds().width / 2.0f, text.getLocalBounds().top + text.getLocalBounds().height / 2.0f);
text.setPosition(position);
rect.setSize(sf::Vector2f(text.getLocalBounds().width + 10, text.getLocalBounds().height + 10));
rect.setOrigin(rect.getLocalBounds().left + rect.getLocalBounds().width / 2.0f, rect.getLocalBounds().top + rect.getLocalBounds().height / 2.0f);
rect.setPosition(position);
rect.setFillColor(sf::Color(200,200,200));
rect.setOutlineThickness(2);
rect.setOutlineColor(sf::Color::Black);
state = DEFAULT;
this->interactable = true;
}
bool isClicked(sf::RenderWindow& window)
{
if (!interactable)
return false;
if (ticker == 0)
{
return state == PRESSED;
}
return false;
}
void setUse(bool use)
{
interactable = use;
}
void update(sf::Vector2i mousePosition)
{
if (!interactable)
return;
if (ticker > 0)
ticker--;
if (state == HOVER)
{
rect.setFillColor(sf::Color(0, 241, 241));
}
else if (state == PRESSED)
{
rect.setFillColor(sf::Color(0, 214, 214));
}
else
{
rect.setFillColor(sf::Color(0, 180, 180));
}
if (rect.getGlobalBounds().contains(mousePosition.x,mousePosition.y))
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
state = PRESSED;
else
state = HOVER;
}
else
{
state = DEFAULT;
}
}
void draw(sf::RenderWindow& window)
{
window.draw(rect);
window.draw(text);
}
void setSize(sf::Vector2f size)
{
this->rect.setSize(size);
rect.setOrigin(rect.getLocalBounds().left + rect.getLocalBounds().width / 2.0f, rect.getLocalBounds().top + rect.getLocalBounds().height / 2.0f);
}
sf::Vector2f getSize()
{
return rect.getSize();
}
};