Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** AdvancedButton |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#ifndef ADVANCEDBUTTON_HPP_ |
9 |
|
|
#define ADVANCEDBUTTON_HPP_ |
10 |
|
|
|
11 |
|
|
#include <SFML/Graphics.hpp> |
12 |
|
|
#include <memory> |
13 |
|
|
|
14 |
|
|
class AdvancedButton { |
15 |
|
|
public: |
16 |
|
✗ |
AdvancedButton(std::string path, sf::Vector2f pos, sf::Vector2f size, float scale = 1) |
17 |
|
✗ |
{ |
18 |
|
✗ |
_pos = pos; |
19 |
|
✗ |
_size = size; |
20 |
|
✗ |
_texture.loadFromFile(path); |
21 |
|
✗ |
_sprite.setTexture(_texture); |
22 |
|
✗ |
_sprite.setPosition(pos); |
23 |
|
✗ |
_sprite.setScale(scale, scale); |
24 |
|
✗ |
} |
25 |
|
✗ |
~AdvancedButton() {}; |
26 |
|
|
|
27 |
|
✗ |
bool update(sf::Event event, sf::RenderWindow &window) |
28 |
|
|
{ |
29 |
|
✗ |
if (_sprite.getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) { |
30 |
|
✗ |
if (event.type == sf::Event::MouseButtonPressed) { |
31 |
|
✗ |
_state = CLICKED; |
32 |
|
✗ |
return true; |
33 |
|
|
} |
34 |
|
✗ |
_state = HOVER; |
35 |
|
✗ |
} else if (_state != CLICKED) |
36 |
|
✗ |
_state = NORMAL; |
37 |
|
|
return false; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
✗ |
void draw(sf::RenderWindow &window) |
41 |
|
|
{ |
42 |
|
✗ |
_sprite.setTextureRect(sf::IntRect(_state * _size.x, 0, _size.x, _size.y)); |
43 |
|
✗ |
window.draw(_sprite); |
44 |
|
✗ |
} |
45 |
|
|
|
46 |
|
|
protected: |
47 |
|
|
private: |
48 |
|
|
enum ButtonState { |
49 |
|
|
NORMAL, |
50 |
|
|
HOVER, |
51 |
|
|
CLICKED |
52 |
|
|
}; |
53 |
|
|
ButtonState _state = NORMAL; |
54 |
|
|
sf::Sprite _sprite; |
55 |
|
|
sf::Texture _texture; |
56 |
|
|
sf::Vector2f _pos; |
57 |
|
|
sf::Vector2f _size; |
58 |
|
|
}; |
59 |
|
|
|
60 |
|
|
#endif /* !ADVANCEDBUTTON_HPP_ */ |
61 |
|
|
|