-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
32 lines (25 loc) · 1.07 KB
/
Copy pathbutton.cpp
File metadata and controls
32 lines (25 loc) · 1.07 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
#include <SFML/Graphics.hpp>
#include "headers/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);
text.setString(s);
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
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::Yellow : sf::Color::White);
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);
}