-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
34 lines (28 loc) · 877 Bytes
/
Copy pathButton.cpp
File metadata and controls
34 lines (28 loc) · 877 Bytes
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 "Button.h"
#include "TextContainer.h"
#include "GameEngine.h"
#include <SFML/Graphics.hpp>
#include <string>
Button::Button(std::string label) {
this->label = new TextContainer("PressStart2P", label);
this->label->setPosition(getPosition().x, getPosition().y);
}
void Button::draw() {
label->setPosition(getPosition().x, getPosition().y);
label->draw();
}
sf::Vector2f Button::getSize() {
return this->label->getSize();
}
bool Button::isMouseOver() {
sf::RenderWindow& window = GameEngine::getInstance().getWindow();
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::FloatRect bounds(getPosition(), getSize());
return bounds.contains(static_cast<sf::Vector2f>(mousePos));
}
void Button::handleEvent() {
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && isMouseOver() && onClick) onClick();
}
void Button::update() {
handleEvent();
}