-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
34 lines (27 loc) · 1.2 KB
/
button.cpp
File metadata and controls
34 lines (27 loc) · 1.2 KB
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
#include <SFML/Graphics.hpp>
#include "button.hpp"
extern const sf::Font font;
Button::Button(const sf::Vector2f &size, const sf::Vector2f &position, const std::string &s)
: button(size), text(font)
{
button.setPosition(position);
button.setFillColor({45, 50, 70});
text.setString(s);
text.setCharacterSize(24);
text.setFillColor({240, 240, 240});
float position_x = button.getPosition().x + (button.getSize().x / 2);
float position_y = button.getPosition().y + (button.getSize().y / 2);
text.setOrigin({text.getGlobalBounds().getCenter()});
text.setPosition({position_x, position_y});
}
//changes colour of rectangle while mouse inside button, returns wether it has been clicked or not
bool Button::clicked(sf::Vector2f point){
bool inside = button.getGlobalBounds().contains(point);
button.setFillColor(inside ? sf::Color(0, 153, 255) : sf::Color(45, 50, 70));
text.setFillColor(inside ? sf::Color(255, 255, 255) : sf::Color(240, 240, 240));
return inside && sf::Mouse::isButtonPressed(sf::Mouse::Button::Left);
}
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const {
target.draw(button, states);
target.draw(text, states);
}