34 std::shared_ptr<
TextComponent> text = std::make_shared<TextComponent>(_clientCore, _socket);
35 std::shared_ptr<
ButtonComponent> button = std::make_shared<ButtonComponent>(_clientCore, _socket);
36 std::shared_ptr<
TextComponent> text_button = std::make_shared<TextComponent>(_clientCore, _socket);
37 std::shared_ptr<
InputComponent> address_input = std::make_shared<InputComponent>(_clientCore, _socket);
38 std::shared_ptr<
InputComponent> port_input = std::make_shared<InputComponent>(_clientCore, _socket);
39 std::shared_ptr<
SoundComponent> sound = std::make_shared<SoundComponent>(_clientCore, _socket);
40 std::shared_ptr<
TextComponent> text_ping = std::make_shared<TextComponent>(_clientCore, _socket);
44 button->addActionTarget(text);
45 button->addActionTarget(address_input);
46 button->addActionTarget(port_input);
47 button->addActionTarget(sound);
48 std::function<
void()> handleClick = std::bind(&ButtonComponent::handleClickInitServer, button);
49 button->setCallback(handleClick);
51 text_button->setText(
"Init Server Connection");
52 text_button->setPosition(sf::Vector2f(275, 190));
54 address_input->setPosition(sf::Vector2f(150, 380));
55 port_input->setPosition(sf::Vector2f(150, 475));
57 address_input->addActionTarget(port_input);
58 port_input->addActionTarget(address_input);
60 address_input->setAttribute(
"address");
61 address_input->_textEntry =
"127.0.0.1";
62 address_input->setText();
63 port_input->setAttribute(
"port");
64 port_input->_textEntry =
"4242";
65 port_input->setText();
66 text->setAttribute(
"text add serv");
68 text_ping->setAttribute(
"text ping");
69 text_ping->setText(
"Ping: 0ms");
70 text_ping->setPosition(sf::Vector2f(0, 550));
74 addComponent(text_button);
75 addComponent(address_input);
76 addComponent(port_input);
78 addComponent(text_ping);
88 while (window.pollEvent(
const_cast<sf::Event &>(event))) {
89 if (event.type == sf::Event::Closed) {
93 if (event.type == sf::Event::KeyPressed) {
94 if (event.key.code == sf::Keyboard::Escape) {
99 for (
auto &component : _components) {
100 component->handleEvent(event, window);
110 std::tuple<std::unique_ptr<Packet>,
int> packet = _socket->receive();
111 std::unique_ptr<Packet> p = std::move(std::get<0>(packet));
114 if (p->code == MESSAGE) {
115 std::string message =
static_cast<
char *>(p->data);
116 if (message ==
"connection accepted") {
117 _socket->setInit(
true);
119 for (
auto &component : _components) {
120 if (component->getType() == ComponentType::TEXT) {
121 if (component->getAttribute() ==
"text add serv") {
122 dynamic_cast<TextComponent *>(component.get())->setText(
"Connection accepted");
127 std::cout <<
"Message: " << message << std::endl;
130 if (p->code == HEARTBEAT) {
133 gettimeofday(&now,
nullptr);
134 timerecv = *
static_cast<timeval *>(p->data);
137 for (
auto &component : _components) {
138 if (component->getType() == ComponentType::TEXT) {
139 if (component->getAttribute() ==
"text ping") {
140 dynamic_cast<TextComponent *>(component.get())
141 ->setText(
"Ping: " + std::to_string(_pingTime.tv_sec * 1000 + _pingTime.tv_usec / 1000) +
142 ", " + std::to_string(_pingTime.tv_usec % 1000) +
"ms");