Line |
Branch |
Exec |
Source |
1 |
|
|
#include "Button.hpp" |
2 |
|
|
|
3 |
|
|
#include <iostream> |
4 |
|
|
|
5 |
|
✗ |
Button::Button(sf::Vector2f pos, [[maybe_unused]] sf::Vector2f size, std::string text, sf::Font &font) { |
6 |
|
✗ |
_text.setFont(font); |
7 |
|
✗ |
_text.setString(text); |
8 |
|
✗ |
_text.setPosition(pos); |
9 |
|
✗ |
_text.setCharacterSize(24); |
10 |
|
✗ |
_text.setFillColor(sf::Color::White); |
11 |
|
✗ |
} |
12 |
|
|
|
13 |
|
✗ |
bool Button::update(sf::Event event, sf::RenderWindow &window) { |
14 |
|
✗ |
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window)); |
15 |
|
|
|
16 |
|
✗ |
if (_text.getGlobalBounds().contains(mousePos.x, mousePos.y)) { |
17 |
|
✗ |
if (event.type == sf::Event::MouseButtonPressed) |
18 |
|
✗ |
_state = CLICKED; |
19 |
|
✗ |
else if (event.type == sf::Event::MouseButtonReleased) { |
20 |
|
✗ |
if (_state == CLICKED) { |
21 |
|
|
try { |
22 |
|
✗ |
_event(); |
23 |
|
✗ |
} catch (const std::bad_function_call &e) { |
24 |
|
✗ |
std::cerr << "No event set for button" << std::endl; |
25 |
|
✗ |
} |
26 |
|
✗ |
_state = HOVER; |
27 |
|
✗ |
return true; |
28 |
|
|
} |
29 |
|
|
} else |
30 |
|
✗ |
_state = HOVER; |
31 |
|
|
} else |
32 |
|
✗ |
_state = IDLE; |
33 |
|
|
return false; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
✗ |
void Button::draw(sf::RenderWindow &window) { |
37 |
|
✗ |
if (_state == HOVER) |
38 |
|
✗ |
_text.setFillColor(sf::Color::Red); |
39 |
|
|
else |
40 |
|
✗ |
_text.setFillColor(sf::Color::White); |
41 |
|
✗ |
window.draw(_text); |
42 |
|
✗ |
} |
43 |
|
|
|