Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** button |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "Input.hpp" |
9 |
|
|
|
10 |
|
✗ |
Input::Input(sf::Vector2f pos, [[maybe_unused]] sf::Vector2f size, std::string text, sf::Font &font, std::string accept) |
11 |
|
✗ |
: _accept(accept) |
12 |
|
|
{ |
13 |
|
|
_placeHolder = text; |
14 |
|
✗ |
_text.setFont(font); |
15 |
|
✗ |
_text.setString(text); |
16 |
|
✗ |
_text.setPosition(pos); |
17 |
|
✗ |
_text.setCharacterSize(24); |
18 |
|
✗ |
_text.setFillColor(sf::Color::White); |
19 |
|
✗ |
} |
20 |
|
|
|
21 |
|
✗ |
bool Input::update(sf::Event event, sf::RenderWindow &window) { |
22 |
|
✗ |
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window)); |
23 |
|
|
bool ret = false; |
24 |
|
|
|
25 |
|
✗ |
if (!_isFocused) |
26 |
|
✗ |
_hover = _text.getGlobalBounds().contains(mousePos.x, mousePos.y); |
27 |
|
✗ |
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) |
28 |
|
✗ |
_isFocused = _text.getGlobalBounds().contains(mousePos.x, mousePos.y); |
29 |
|
✗ |
if (_isFocused) { |
30 |
|
✗ |
if (event.type == sf::Event::TextEntered) { |
31 |
|
✗ |
if (event.text.unicode == 8) { |
32 |
|
✗ |
if (_input.size() > 0) |
33 |
|
✗ |
_input.pop_back(); |
34 |
|
✗ |
} else if (event.text.unicode == 13) { |
35 |
|
✗ |
_isFocused = false; |
36 |
|
|
ret = true; |
37 |
|
|
} else { |
38 |
|
✗ |
if (_accept.find(event.text.unicode) != std::string::npos) |
39 |
|
✗ |
_input += event.text.unicode; |
40 |
|
|
} |
41 |
|
|
} |
42 |
|
✗ |
if (event.type == sf::Event::KeyPressed && |
43 |
|
✗ |
sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && |
44 |
|
✗ |
sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) |
45 |
|
✗ |
_input = ""; |
46 |
|
|
} |
47 |
|
✗ |
return ret; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
✗ |
void Input::draw(sf::RenderWindow &window, float deltaTime) { |
51 |
|
✗ |
_time += deltaTime; |
52 |
|
✗ |
if (!_isFocused && _input.size() == 0) |
53 |
|
✗ |
_text.setString(_placeHolder + " " + _end); |
54 |
|
✗ |
else if (_isFocused) { |
55 |
|
✗ |
if (_time < 0.5) |
56 |
|
✗ |
_text.setString(_input + "_"); |
57 |
|
|
else |
58 |
|
✗ |
_text.setString(_input + " "); |
59 |
|
✗ |
if (_time > 1) |
60 |
|
✗ |
_time = 0; |
61 |
|
|
} else { |
62 |
|
✗ |
_text.setString(_input + " " + _end); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
✗ |
if (_isFocused || _hover) |
66 |
|
✗ |
_text.setFillColor(sf::Color::Red); |
67 |
|
|
else |
68 |
|
✗ |
_text.setFillColor(sf::Color::White); |
69 |
|
✗ |
window.draw(_text); |
70 |
|
✗ |
} |
71 |
|
|
|