RType
InputComponent.cpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-type
4 ** File description:
5 ** R-type
6 */
7 
9 #include <iostream>
10 #include <utility>
11 
12 /**
13  * @brief InputComponent constructor
14  * @param core
15  * @param socket
16  */
17 InputComponent::InputComponent(ClientCore *core, std::shared_ptr<ClientSocket> socket) :
18  AComponent(core)
19 {
20  _type = ComponentType::INPUT;
21  _texture.loadFromFile("../src/client/assets/button.png");
22  _sprite.setTexture(_texture);
23  _position = sf::Vector2f(100, 400);
24  _sprite.setPosition(_position);
25  _size = sf::Vector2f(0.75, 0.25);
26  _sprite.setScale(_size);
27  _rect = sf::IntRect(0, 0, 701, 301);
28  _sprite.setTextureRect(_rect);
29  _textEntry = "";
30  _text.setString(_textEntry);
31  _font.loadFromFile("../src/client/assets/fonts/DecemberShow.ttf");
32  _text.setFont(_font);
33  _position = sf::Vector2f(130, 420);
34  _text.setPosition(_position);
35  _text.setFillColor(sf::Color::Black);
36  _text.setCharacterSize(30);
37  _isClicked = false;
38  _attribute = "";
39  _socket = std::move(socket);
40 }
41 
42 /**
43  * @brief action, call the callback function
44  */
46 {
47  _callback();
48 }
49 
50 /**
51  * @brief setTexture set the texture of the component
52  * @param texture
53  */
54 void InputComponent::setTexture(const sf::Texture &texture)
55 {
56  _texture = texture;
57  _sprite.setTexture(_texture);
58 }
59 
60 /**
61  * @brief setPosition set the position of the component
62  * @param position
63  */
64 void InputComponent::setPosition(sf::Vector2f position)
65 {
66  _position = position;
67  _sprite.setPosition(_position);
68  sf::Vector2f textPosition = _position;
69  textPosition.x += 30;
70  textPosition.y += 20;
71  _text.setPosition(textPosition);
72 }
73 
74 /**
75  * @brief setSize set the size of the component
76  * @param size
77  */
78 void InputComponent::setSize(sf::Vector2f size)
79 {
80  _size = size;
81  _sprite.setScale(_size);
82 }
83 
84 /**
85  * @brief setRect set the rect of the component
86  * @param rect
87  */
88 void InputComponent::setRect(sf::IntRect rect)
89 {
90  _rect = rect;
91  _sprite.setTextureRect(_rect);
92 }
93 
94 /**
95  * @brief display display the component
96  * @param window
97  */
98 void InputComponent::display(sf::RenderWindow &window)
99 {
100  window.draw(_sprite);
101  window.draw(_text);
102 }
103 
104 /**
105  * @brief getRect get the texture of the component
106  * @return rect of the component
107  */
109 {
110  return _rect;
111 }
112 
113 /**
114  * @brief setText set the text of the component
115  */
117 {
118  _text.setString(_textEntry);
119 }
120 
121 /**
122  * @brief getText get the text of the component
123  */
124 std::string InputComponent::getText()
125 {
126  return _textEntry;
127 }
128 
129 /**
130  * @brief SetIsClicked set the isClicked of the component
131  * @param isClicked
132  */
133 void InputComponent::setIsClicked(bool isClicked)
134 {
135  _isClicked = isClicked;
136 }
137 
138 /**
139  * @brief handleEvent handle the event of the component
140  * @param event
141  * @param window
142  */
143 void InputComponent::handleEvent(const sf::Event &event, sf::RenderWindow &window)
144 {
145  if (event.type == sf::Event::TextEntered) {
146  if (_isClicked) {
147  if (event.text.unicode < 128) {
148  if (event.text.unicode == 8) {
149  if (!_textEntry.empty()) {
150  _textEntry.erase(_textEntry.size() - 1);
151  setText();
152  }
153  } else {
154  _textEntry += static_cast<char>(event.text.unicode);
155  setText();
156  }
157  }
158  }
159  } else if (event.type == sf::Event::MouseButtonPressed) {
160  if (event.mouseButton.button == sf::Mouse::Left) {
161  sf::Vector2i mousePos = sf::Mouse::getPosition(window);
162  sf::Vector2f mousePosF(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
163  if (_sprite.getGlobalBounds().contains(mousePosF)) {
164  _isClicked = true;
165  for (auto &component : action_target) {
166  if (component->getType() == ComponentType::INPUT) {
167  auto input = std::dynamic_pointer_cast<InputComponent>(component);
168  input->setIsClicked(false);
169  }
170  }
171  } else {
172  _isClicked = false;
173  }
174  }
175  }
176 }