-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.cpp
466 lines (386 loc) · 13.7 KB
/
GUI.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "GUI.h"
std::vector<Button*>* GUI::buttons = new std::vector<Button*>();
std::vector<Textbox*>* GUI::textboxes = new std::vector<Textbox*>();
std::vector<Label*>* GUI::labels = new std::vector<Label*>();
std::vector<Checkbox*>* GUI::checkboxes = new std::vector<Checkbox*>();
SDL_Renderer* GUI::targetRenderer;
Uint32 GUI::delta, GUI::textboxCursorDelta;
Textbox* GUI::activeTextbox = NULL;
char GUI::lastPressedKey;
bool GUI::leftMouseButtonPressedState = false, GUI::leftMouseButtonPressedLastState = false,
GUI::drawTextBoxCursor = true, GUI::capsLockEnabled = false, GUI::rerender = false;
// LIBRARY SETUP METHODS
void GUI::Init() {
TTF_Init(); // Initializes the SDL font library
delta = SDL_GetTicks(); // Init milliseconds to be used for textbox input
textboxCursorDelta = SDL_GetTicks(); // Init milliseconds to be used for textbox cursor blinking
}
void GUI::SetRenderTarget(SDL_Renderer* renderer) {
targetRenderer = renderer;
}
// ELEMENT CREATION METHODS
Label* GUI::CreateLabel(std::string text, int x, int y, SDL_Color color, int fontSize, std::string fontPath) {
Label* lbl = new Label(text, x, y, color, fontSize, fontPath);
labels->push_back(lbl);
return lbl;
}
Textbox* GUI::CreateTextbox(std::string placeholder, int width, int height, int x, int y, int fontSize, int limit, std::string fontPath) {
Textbox* tb = new Textbox(placeholder, width, height, x, y, fontSize, limit, fontPath);
textboxes->push_back(tb);
return tb;
}
Checkbox* GUI::CreateCheckbox(int x, int y, int size, bool defaultState) {
Checkbox* cb = new Checkbox(x, y, size, defaultState);
checkboxes->push_back(cb);
return cb;
}
Button* GUI::CreateButton(std::string label, int width, int height, int x, int y, int fontSize, std::string fontPath) {
Button* b = new Button(label, width, height, x, y, fontSize, fontPath);
buttons->push_back(b);
return b;
}
// ELEMENT RENDERING METHODS
void GUI::RenderLabels() {
for (int i = 0; i < labels->size(); i++) {
Label* curr = (*labels)[i];
if (!curr->GetDisplayState()) continue;
RenderLabel(curr->GetText(), curr->GetX(), curr->GetY(), curr->GetColor(), curr->GetFont(), curr->GetFontSize());
}
}
void GUI::RenderButtons() {
// Loop through all buttons
for (int i = 0; i < buttons->size(); i++) {
Button* curr = (*buttons)[i];
if (!curr->GetDisplayState()) continue;
// Get necessary data from current object
int height = curr->GetHeight();
int width = curr->GetWidth();
int x = curr->GetX();
int y = curr->GetY();
SDL_Color color = curr->GetColor();
SDL_Color hoverColor = curr->GetHoverColor();
TTF_Font* font = curr->GetFont();
std::string label = curr->GetLabel();
// Create button rectangle data
SDL_Rect rect;
rect.w = width;
rect.h = height;
rect.x = x;
rect.y = y;
bool mHover = OnMouseHover(x, y, width, height);
// If mouse doesn't hover over button, default idle state
SDL_SetRenderDrawColor(targetRenderer, color.r, color.g, color.b, color.a);
curr->SetPressedState(false);
// If mouse hovers over button and activates
if (mHover && leftMouseButtonPressedState) {
curr->SetPressedState(true);
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a - 75);
activeTextbox = NULL;
}
// If mouse hovers over
else if (mHover) {
if (!leftMouseButtonPressedLastState)
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a);
else
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a - 75);
}
// Draw button rectangle
SDL_RenderFillRect(targetRenderer, &rect);
SDL_Color c = { 255, 255, 255 };
std::tuple<int, int> mesDim = GetTextDimensions(label, font);
// Display button label
RenderLabel(label, x + width / 2 - std::get<0>(mesDim) / 2, y + height / 2 - std::get<1>(mesDim) / 2, c, font, curr->GetFontSize());
}
}
void GUI::RenderTextboxes() {
// Loop through all textboxes
for (int i = 0; i < textboxes->size(); i++) {
Textbox* curr = (*textboxes)[i];
if (!curr->GetDisplayState()) continue;
// Get necessary data from current object
int height = curr->GetHeight();
int width = curr->GetWidth();
int x = curr->GetX();
int y = curr->GetY();
SDL_Color color = curr->GetColor();
SDL_Color hoverColor = curr->GetHoverColor();
TTF_Font* font = curr->GetFont();
int fontSize = curr->GetFontSize();
std::string value = curr->GetValue();
std::string placeholder = curr->GetPlaceholder();
// Create textbox rectangle data
SDL_Rect rect;
rect.w = width;
rect.h = height;
rect.x = x;
rect.y = y;
bool mHover = OnMouseHover(x, y, width, height);
// If mouse doesn't hover over textbox, default idle state
SDL_SetRenderDrawColor(targetRenderer, color.r, color.g, color.b, color.a);
// If mouse hovers over textbox and activates
if (mHover && leftMouseButtonPressedState) {
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a);
activeTextbox = curr;
}
// If mouse hovers over
else if (mHover) {
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a);
}
// Draw textbox rectangle
SDL_RenderFillRect(targetRenderer, &rect);
std::tuple<int, int> txtDim;
int lblX, lblY;
// If no value, show placeholder
if (value.empty()) {
SDL_Color c = { 255, 255, 255, 150 };
txtDim = GetTextDimensions(placeholder, font);
lblX = x + width / 2 - std::get<0>(txtDim) / 2;
lblY = y + height / 2 - std::get<1>(txtDim) / 2;
// Display textbox placeholder text
RenderLabel(placeholder, lblX, lblY, c, font, fontSize);
}
// If textbox has a user entered value, show that value in textbox
else {
SDL_Color c = { 255, 255, 255 };
txtDim = GetTextDimensions(value, font);
lblX = x + width / 2 - std::get<0>(txtDim) / 2;
lblY = y + height / 2 - std::get<1>(txtDim) / 2;
// Display textbox label
RenderLabel(value, lblX, lblY, c, font, fontSize);
}
// If there's an active textbox, toggle textbox cursor every 750 millisecond
if (activeTextbox) {
Uint32 now = SDL_GetTicks();
Uint32 cursorDelta = now - textboxCursorDelta;
if (cursorDelta > 750) {
drawTextBoxCursor = !drawTextBoxCursor;
textboxCursorDelta = now;
}
}
// If active textbox is the current textbox and timing is right, draw cursor on textbox
if (activeTextbox == curr && drawTextBoxCursor)
{
rect;
rect.w = 2;
rect.h = fontSize;
rect.x = lblX + std::get<0>(txtDim);
rect.y = y + height / 2 - fontSize / 2;
SDL_SetRenderDrawColor(targetRenderer, 255, 255, 255, 255);
SDL_RenderFillRect(targetRenderer, &rect);
}
CaptureInputText();
}
}
void GUI::RenderCheckboxes() { // TODO: Draw v-mark inside checkbox (if selected) to show its state
// Loop through all checkboxes
for (int i = 0; i < checkboxes->size(); i++) {
Checkbox* curr = (*checkboxes)[i];
if (!curr->GetDisplayState()) continue;
// Get necessary data from current object
int size = curr->GetSize();
int x = curr->GetX();
int y = curr->GetY();
bool checked = curr->IsChecked();
SDL_Color color = curr->GetColor();
SDL_Color checkmarkColor = curr->GetCheckmarkColor();
SDL_Color hoverColor = curr->GetHoverColor();
// Create checkbox rectangle data
SDL_Rect rect;
rect.w = size;
rect.h = size;
rect.x = x;
rect.y = y;
bool mHover = OnMouseHover(x, y, size, size);
// If mouse doesn't hover over checkbox, default idle state
SDL_SetRenderDrawColor(targetRenderer, color.r, color.g, color.b, color.a);
// If mouse hovers over button and activates
if (mHover && leftMouseButtonPressedState) {
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a - 75);
curr->SetState(!checked);
activeTextbox = NULL;
}
// If mouse hovers over
else if (mHover) {
if (!leftMouseButtonPressedLastState)
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a);
else
SDL_SetRenderDrawColor(targetRenderer, hoverColor.r, hoverColor.g, hoverColor.b, hoverColor.a - 75);
}
// Draw checkbox rectangle
SDL_RenderFillRect(targetRenderer, &rect);
// Draw checkmark if checked
if (checked) {
rect.w = size - size * 0.6;
rect.h = size - size * 0.6;
rect.x = x + size * 0.6 / 2;
rect.y = y + size * 0.6 / 2;
SDL_SetRenderDrawColor(targetRenderer, checkmarkColor.r, checkmarkColor.g, checkmarkColor.b, checkmarkColor.a);
SDL_RenderFillRect(targetRenderer, &rect);
}
}
}
void GUI::RenderLabel(std::string text, int x, int y, SDL_Color color, TTF_Font* font, int fontSize) {
// Create surface to render text on
SDL_Surface* surfaceMessage =
TTF_RenderText_Blended(font, text.c_str(), color);
// Convert to texture
SDL_Texture* message = SDL_CreateTextureFromSurface(targetRenderer, surfaceMessage);
// Create a rectangle/shape of the message texture
SDL_Rect message_rect;
message_rect.x = x;
message_rect.y = y;
message_rect.w = surfaceMessage->w;
message_rect.h = surfaceMessage->h;
// FIX THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/*if (!SDL_SetRenderTarget(targetRenderer, previousFrame)) {
std::cout << SDL_GetError() << std::endl;
}*/
//SDL_RenderClear(targetRenderer);
SDL_RenderCopy(targetRenderer, message, NULL, &message_rect);
//SDL_SetRenderTarget(targetRenderer, NULL);
//SDL_RenderCopy(targetRenderer, message, NULL, &message_rect);
// Frees resources
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(message);
}
void GUI::Render() {
UpdateMouseButtonState();
RenderLabels();
RenderButtons();
RenderTextboxes();
RenderCheckboxes();
}
// UTILITY METHODS
void GUI::DrawCircle(int32_t centreX, int32_t centreY, int32_t radius)
{
const int32_t diameter = (radius * 2);
int32_t x = (radius - 1);
int32_t y = 0;
int32_t tx = 1;
int32_t ty = 1;
int32_t error = (tx - diameter);
SDL_SetRenderDrawColor(targetRenderer, 0, 0, 0, 255);
while (x >= y)
{
// Each of the following renders an octant of the circle
SDL_RenderDrawPoint(targetRenderer, centreX + x, centreY - y);
SDL_RenderDrawPoint(targetRenderer, centreX + x, centreY + y);
SDL_RenderDrawPoint(targetRenderer, centreX - x, centreY - y);
SDL_RenderDrawPoint(targetRenderer, centreX - x, centreY + y);
SDL_RenderDrawPoint(targetRenderer, centreX + y, centreY - x);
SDL_RenderDrawPoint(targetRenderer, centreX + y, centreY + x);
SDL_RenderDrawPoint(targetRenderer, centreX - y, centreY - x);
SDL_RenderDrawPoint(targetRenderer, centreX - y, centreY + x);
if (error <= 0)
{
++y;
error += ty;
ty += 2;
}
if (error > 0)
{
--x;
tx += 2;
error += (tx - diameter);
}
}
}
std::tuple<int, int> GUI::GetTextDimensions(std::string text, TTF_Font* font) {
// Text color
SDL_Color color = { 0, 0, 0 };
// Create surface to render text onto
SDL_Surface* surfaceMessage =
TTF_RenderText_Blended(font, text.c_str(), color);
std::tuple<int, int> dim(surfaceMessage->w, surfaceMessage->h);
SDL_FreeSurface(surfaceMessage);
return dim;
}
void GUI::CaptureInputText() {
if (!activeTextbox) return;
int nK;
char key = NULL;
Uint32 now = SDL_GetTicks();
const Uint8* keys = SDL_GetKeyboardState(&nK);
std::string value = activeTextbox->GetValue();
// Enable capital letters if capslock is pressed
if (keys[SDL_SCANCODE_CAPSLOCK] && now - delta >= 300) {
capsLockEnabled = !capsLockEnabled;
delta = now;
}
// Delete last character from input string if backspace is pressed
if (keys[SDL_SCANCODE_BACKSPACE] && now - delta >= 110) {
activeTextbox->SetValue(value.substr(0, value.size() - 1));
delta = now;
// Reset textbox cursor on key input
textboxCursorDelta = now;
drawTextBoxCursor = true;
return;
}
// If character limit has been reached, jump out
if (value.length() >= activeTextbox->GetCharLimit()) return;
for (int i = 0; i < nK; i++) {
if (keys[i] && ValidKey(i)) {
if (keys[SDL_SCANCODE_LSHIFT] || capsLockEnabled) {
key = toupper(SDL_GetKeyFromScancode(SDL_Scancode(i)));
}
else {
key = SDL_GetKeyFromScancode(SDL_Scancode(i));
}
// Reset textbox cursor on key input
textboxCursorDelta = now;
drawTextBoxCursor = true;
}
}
// If pressed key is not the same as last key, then just print immediately
if (key != lastPressedKey && key) {
activeTextbox->SetValue(value += key);
lastPressedKey = key;
delta = now;
}
// If same key, check if enough time has passed since last key press.
// If enough time has passed, print pressed key
else if (now - delta >= 200 && key) {
activeTextbox->SetValue(value += key);
lastPressedKey = key;
delta = now;
}
}
bool GUI::ValidKey(int key) {
// Determines what keys are considered valid (valid as in printable)
if (key >= 4 && key <= 39 || key >= 44 && key <= 49 || key >= 54 && key <= 56) {
return true;
}
return false;
}
bool GUI::OnMouseHover(int x, int y, int width, int height) {
int mX = 0, mY = 0;
SDL_GetMouseState(&mX, &mY);
// If mouse hovers over button
if (mX >= x && mX <= x + width && mY >= y && mY <= y + height) {
return true;
}
return false;
}
void GUI::UpdateMouseButtonState() {
Uint32 mb = SDL_GetMouseState(NULL, NULL);
if (mb == SDL_BUTTON(1) && !leftMouseButtonPressedLastState) {
leftMouseButtonPressedLastState = true;
leftMouseButtonPressedState = true;
}
else if (mb == SDL_BUTTON(1) && leftMouseButtonPressedLastState) {
leftMouseButtonPressedState = false;
}
else if (mb != SDL_BUTTON(1)) {
leftMouseButtonPressedLastState = false;
leftMouseButtonPressedState = false;
}
}
TTF_Font* GUI::OpenFont(std::string fontUrl, int size) {
TTF_Font* font = TTF_OpenFont(fontUrl.c_str(), size);
if (!font) {
char t[] = "Font error";
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, t, TTF_GetError(), NULL);
exit(0);
}
return font;
}