-
Notifications
You must be signed in to change notification settings - Fork 1
/
MetaServerObject.hpp
111 lines (98 loc) · 2.89 KB
/
MetaServerObject.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once
#include "Transformation.hpp"
#include "ShaderUniform.hpp"
#include "ShaderProgram.hpp"
#include "MetaServer.hpp"
#include "Button.hpp"
#include "StrConst.hpp"
struct MetaServerObject {
MetaServerClient &mclient;
C_STRING(font_name, "assets/Verdana.ttf");
C_STRING(hosttex_name, "assets/button.png");
C_STRING(exittex_name, "assets/button.png");
ui::Button<exittex_name, font_name> host_button;
ui::Button<hosttex_name, font_name> exit_button;
C_STRING(texture_name, "assets/button.png");
ui::Button<texture_name, font_name> button;
MetaServerObject(MetaServerClient &mclient, const std::string &dir):
mclient(mclient),
host_button(dir),
exit_button(dir),
button(dir)
{}
bool is_active() {
return !mclient.should_stop() && !mclient.has_hosted() && !mclient.has_joined() && !mclient.has_quit();
}
void init() {
button.init();
exit_button.setx(-1, -.7);
exit_button.sety(.9, 1);
exit_button.init();
exit_button.label.set_text("Exit");
host_button.setx(.7, 1);
host_button.sety(.9, 1);
host_button.init();
host_button.label.set_text("Host");
}
void mouse(float m_x, float m_y) {
cursorPosition.x = m_x;
cursorPosition.y = m_y;
}
glm::vec2 cursorPosition = {0.f, 0.f};
bool clicked = true;
int click_button;
int click_action;
void mouse_click(int button, int action) {
clicked = true;
click_button = button;
click_action = action;
}
template <typename ButtonT, typename F>
void button_display(ButtonT &btn, F &&func) {
btn.mouse(cursorPosition.x, cursorPosition.y);
if(clicked && (btn.region.contains(cursorPosition) || btn.state != ButtonT::DEFAULT_STATE)) {
btn.mouse_click(click_button, click_action);
clicked = false;
}
btn.action_on_click(func);
btn.display();
}
void display() {
if(!is_active())return;
button_display(host_button, [&]() mutable {
mclient.action_host("the game");
});
button_display(exit_button, [&]() mutable {
mclient.set_state(MetaServerClient::State::QUIT);
});
GameList glist;
{
// remove duplicates from multiple metaservers
std::lock_guard<std::recursive_mutex> guard(mclient.mservers_mtx);
for(auto &m : mclient.metaservers) {
for(auto &game : mclient.gamelists[m].games) {
auto &host = game.first;
if(glist.games.find(host) == std::end(glist.games)) {
glist.games[host] = game.second;
}
}
}
}
button.setx(-.9, .9);
button.sety(-1, -1+.1);
for(auto &g : glist.games) {
auto &host = g.first;
auto &name = g.second;
button.label.set_text(host.to_str() + " : " + name);
button_display(button, [&]() mutable {
mclient.action_join(g.first);
});
button.region.ys += .1;
}
}
void clear() {
button.clear();
exit_button.clear();
host_button.clear();
}
};