RType
ButtonComponent.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 "../../ClientCore.hpp"
10 
11 #include <utility>
12 
13 /**
14  * @brief ButtonComponent constructor
15  * @param core
16  * @param socket
17  */
18 ButtonComponent::ButtonComponent(ClientCore *core, std::shared_ptr<ClientSocket> socket) :
19  AComponent(core)
20 {
21  _type = ComponentType::BUTTON;
22  _texture.loadFromFile("../src/client/assets/button.png");
23  _sprite.setTexture(_texture);
24  _position = sf::Vector2f(150, 100);
25  _sprite.setPosition(_position);
26  _size = sf::Vector2f(0.75, 0.75);
27  _sprite.setScale(_size);
28  _rect = sf::IntRect(0, 0, 701, 301);
29  _sprite.setTextureRect(_rect);
30  _attribute = "";
31  _socket = std::move(socket);
32  std::function<void()> default_handle_click = std::bind(&ButtonComponent::defaultCallback, this);
33  setCallback(default_handle_click);
34 }
35 
36 /**
37  * @brief action, call the callback function
38  */
40 {
41  _callback();
42 }
43 
44 /**
45  * @brief setTexture set the texture of the button
46  * @param texture
47  */
48 void ButtonComponent::setTexture(const sf::Texture &texture)
49 {
50  _texture = texture;
51  _sprite.setTexture(_texture);
52 }
53 
54 /**
55  * @brief setPosition set the position of the button
56  * @param position
57  */
58 void ButtonComponent::setPosition(sf::Vector2f position)
59 {
60  _position = position;
61  _sprite.setPosition(_position);
62 }
63 
64 /**
65  * @brief setSize set the size of the button
66  * @param size
67  */
68 void ButtonComponent::setSize(sf::Vector2f size)
69 {
70  _size = size;
71  _sprite.setScale(_size);
72 }
73 
74 /**
75  * @brief setRect set the rect of the button
76  * @param rect
77  */
78 void ButtonComponent::setRect(sf::IntRect rect)
79 {
80  _rect = rect;
81  _sprite.setTextureRect(_rect);
82 }
83 
84 /**
85  * @brief setAttribute set the attribute of the button
86  * @param attribute
87  */
88 void ButtonComponent::setCallback(std::function<void()> callback)
89 {
90  _callback = std::move(callback);
91 }
92 
93 /**
94  * @brief setAttribute set the attribute of the button
95  * @param window
96  */
97 void ButtonComponent::display(sf::RenderWindow &window)
98 {
99  window.draw(_sprite);
100 }
101 
102 /**
103  * @brief getRect get the texture of the button
104  * @return rect of the button
105  */
107 {
108  return _rect;
109 }
110 
111 /**
112  * @brief handleEvent handle the event of the button
113  * @param event to handle
114  * @param window
115  */
116 void ButtonComponent::handleEvent(const sf::Event &event, sf::RenderWindow &window)
117 {
118  if (event.type == sf::Event::MouseButtonPressed) {
119  if (event.mouseButton.button == sf::Mouse::Left) {
120  sf::Vector2i mousePos = sf::Mouse::getPosition(window);
121  sf::Vector2f mousePosF(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
122  if (_sprite.getGlobalBounds().contains(mousePosF)) {
123  action();
124  }
125  }
126  }
127 }
128 
129 /**
130  * @brief handleClickInitServer handle the click of the button
131  */
133 {
134  std::string ip;
135  std::string port;
136  for (auto &component : action_target) {
137  if (component->getType() == ComponentType::INPUT) {
138  if (component->getAttribute() == "address") {
139  ip = dynamic_cast<InputComponent *>(component.get())->getText();
140  } else if (component->getAttribute() == "port") {
141  port = dynamic_cast<InputComponent *>(component.get())->getText();
142  }
143  } else if (component->getType() == ComponentType::SOUND) {
144  dynamic_cast<SoundComponent *>(component.get())->action();
145  }
146  }
147  if (ip.empty() || port.empty()) {
148  for (auto &component : action_target) {
149  if (component->getType() == ComponentType::TEXT) {
150  if (component->getAttribute() == "text add serv") {
151  dynamic_cast<TextComponent *>(component.get())->setText("Please fill all fields");
152  }
153  }
154  }
155  } else {
156  for (auto &component : action_target) {
157  if (component->getType() == ComponentType::TEXT) {
158  if (component->getAttribute() == "text add serv") {
159  try {
160  std::stoi(port);
161  } catch (std::exception &e) {
162  dynamic_cast<TextComponent *>(component.get())->setText("Port must be a number");
163  return;
164  }
165  if (_clientCore->init_socket(ip, std::stoi(port))) {
166  if (!_socket->isInit())
167  dynamic_cast<TextComponent *>(component.get())->setText("Finding server...");
168  } else
169  dynamic_cast<TextComponent *>(component.get())->setText("Server not found");
170  }
171  }
172  }
173  }
174 }
175 
176 /**
177  * @brief handleClickMainScene handle the click of the button on the main scene
178  */
180 {
181  for (auto &component : action_target) {
182  if (component->getType() == ComponentType::SOUND) {
183  dynamic_cast<SoundComponent *>(component.get())->action();
184  }
185  if (component->getType() == ComponentType::MUSIC) {
186  auto music = dynamic_cast<MusicComponent *>(component.get());
187  if (music->getPersistant())
188  continue;
189  music->stop();
190  }
191  }
193 }
194 
195 /**
196  * @brief handleClickAccessGame handle the click of the button for access the game scene
197  */
199 {
200  for (auto &component : action_target) {
201  if (component->getType() == ComponentType::SOUND) {
202  dynamic_cast<SoundComponent *>(component.get())->action();
203  }
204  }
205  if (!_socket->isInit()) {
206  for (auto &component : action_target) {
207  if (component->getType() == ComponentType::TEXT) {
208  if (component->getAttribute() == "text error not init") {
209  dynamic_cast<TextComponent *>(component.get())->setText("Please init server connection");
210  }
211  }
212  }
213  return;
214  }
215  for (auto &component : action_target) {
216  if (component->getType() == ComponentType::MUSIC) {
217  auto music = dynamic_cast<MusicComponent *>(component.get());
218  music->stop();
219  }
220  }
222  Packet packet{};
223  packet.code = CODE::MESSAGE;
224  packet.data_size = 10;
225  packet.data = malloc(packet.data_size);
226  memcpy(packet.data, "enter game", packet.data_size);
227  _socket->send(&packet, _socket->serv_addr);
228  free(packet.data);
229 }
230 
232 {
233 }