-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuiServerList.cpp
169 lines (149 loc) · 3.81 KB
/
GuiServerList.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "GuiServerList.h"
GuiServerList::GuiServerList(TTF_Font* font, SDL_Color fontColor, SDL_Renderer* rendererIn, std::string ipIn, std::string nameIn, std::string descriptionIn, int yIn)
{
renderer = rendererIn;
edgeTexture = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/edge.png").c_str());
texture = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/border.png").c_str());
textureHover = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/borderHover.png").c_str());
textureClick = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/borderClick.png").c_str());
ipGuiText = new GuiText(font, fontColor, renderer, ipIn, 100, yIn);
nameGuiText = new GuiText(font, fontColor, renderer, nameIn, 300, yIn);
descGuiText = new GuiText(font, fontColor, renderer, descriptionIn, 500, yIn);
playerCountGuiText = new GuiText(font, fontColor, renderer, "NaN", 900, yIn);
pingGuiText = new GuiText(font, fontColor, renderer, "NaN", 1100, yIn);
int x = 20;
int y = yIn;
int height = 24;
int width = Settings::Graphics::screenWidth-40;
leftEdge.x = x;
leftEdge.y = y;
leftEdge.h = height;
leftEdge.w = 2;
rightEdge.x = x+width-2;
rightEdge.y = y;
rightEdge.h = height;
rightEdge.w = 2;
middle.x = x+2;
middle.y = y;
middle.h = height;
middle.w = width-4;
rect.x = x;
rect.y = y;
rect.h = height;
rect.w = width;
state = NORMAL;
onClick = STATE::GAME;
clicked = false;
ip = ipIn;
}
void GuiServerList::setState(int stateIn)
{
state = stateIn;
}
int GuiServerList::getState(void)
{
return state;
}
std::string GuiServerList::getIp(void)
{
return ip;
}
STATE GuiServerList::getOnClick(void)
{
return onClick;
}
void GuiServerList::render(void)
{
SDL_RenderCopy(renderer, edgeTexture, NULL, &leftEdge);
SDL_RenderCopyEx(renderer, edgeTexture, NULL, &rightEdge, 0.0, NULL, SDL_FLIP_HORIZONTAL);
switch (state)
{
case BUTTONSTATE::NORMAL:
SDL_RenderCopy(renderer, texture, NULL, &middle);
break;
case BUTTONSTATE::HOVER:
SDL_RenderCopy(renderer, textureHover, NULL, &middle);
break;
case BUTTONSTATE::CLICK:
SDL_RenderCopy(renderer, textureClick, NULL, &middle);
break;
default:
break;
}
ipGuiText->render();
nameGuiText->render();
descGuiText->render();
playerCountGuiText->render();
pingGuiText->render();
}
void GuiServerList::onEvent(SDL_Event* ev, const Uint8*)
{
switch (ev->type)
{
case SDL_MOUSEMOTION:
{
SDL_Rect mouse;
mouse.x = ev->button.x;
mouse.y = ev->button.y;
mouse.w = 1;
mouse.h = 1;
if(state != GuiServerList::BUTTONSTATE::CLICK)
{
if(SDL_HasIntersection(&mouse, &rect))
state = GuiServerList::BUTTONSTATE::HOVER;
else
state = GuiServerList::BUTTONSTATE::NORMAL;
}
break;
}
case SDL_MOUSEBUTTONDOWN:
{
if(ev->button.button == SDL_BUTTON_LEFT)
{
SDL_Rect mouse;
mouse.x = ev->button.x;
mouse.y = ev->button.y;
mouse.w = 1;
mouse.h = 1;
if(SDL_HasIntersection(&mouse, &rect))
state = GuiServerList::BUTTONSTATE::CLICK;
else
state = GuiServerList::BUTTONSTATE::NORMAL;
}
break;
}
case SDL_MOUSEBUTTONUP:
{
if(ev->button.button == SDL_BUTTON_LEFT)
{
SDL_Rect mouse;
mouse.x = ev->button.x;
mouse.y = ev->button.y;
mouse.w = 1;
mouse.h = 1;
if(state == GuiServerList::BUTTONSTATE::CLICK)
{
if(SDL_HasIntersection(&mouse, &rect))
{
clicked = true;
}
else
state = GuiServerList::BUTTONSTATE::NORMAL;
}
}
break;
}
}
}
bool GuiServerList::hasClicked(void)
{
return clicked;
}
void GuiServerList::setPing(int pingIn)
{
pingGuiText->setText(std::to_string(pingIn));
}
void GuiServerList::setPlayerCount(int curr, int max)
{
playerCountGuiText->setText(std::to_string(curr) + "/" + std::to_string(max));
}