-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
49 lines (38 loc) · 1.08 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
#ifndef BUTTON_H_
#define BUTTON_H_
#include <SFML/Graphics.hpp>
class Button
{
public:
Button(sf::Font& font, std::string str, sf::Vector2f pos,
sf::Vector2f size)
{
sprite.setSize(size);
sprite.setOrigin(size.x/2, size.y/2);
sprite.setPosition(pos);
sprite.setFillColor(sf::Color(160, 82, 45));
sprite.setOutlineColor(sf::Color::Black);
sprite.setOutlineThickness(2);
highlight = sprite;
highlight.setFillColor(sf::Color(255, 255, 0, 100));
text.setFont(font);
text.setString(str);
text.setOrigin(text.getLocalBounds().width/2, text.getLocalBounds().height);
text.setPosition(pos);
}
bool update(sf::RenderWindow& window, sf::View view)
{
sf::Vector2f mpos = window.mapPixelToCoords(sf::Mouse::getPosition(window), view);
bool hovered = sprite.getGlobalBounds().contains(mpos);
bool clicked = sf::Mouse::isButtonPressed(sf::Mouse::Left);
window.draw(sprite);
window.draw(text);
if(hovered)
window.draw(highlight);
return hovered && clicked;
}
sf::RectangleShape highlight;
sf::RectangleShape sprite;
sf::Text text;
};
#endif /* BUTTON_H_ */