-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeySetWindow.cpp
More file actions
262 lines (223 loc) · 7.53 KB
/
Copy pathKeySetWindow.cpp
File metadata and controls
262 lines (223 loc) · 7.53 KB
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
#include "KeySetWindow.h"
KeySetWindow::KeySetWindow(Key *key, SDL_Color color, Keyboard *keyboard) {
this->keyboard = keyboard;
isWindowEvent = false;
this->key = key;
window = SDL_CreateWindow("Key set window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED);
this->keyLabel.hasFocus = false;
this->fileTextbox.hasFocus = false;
this->needRedraw = false;
this->color = color;
SDL_GetWindowSize(this->window, &windowWidth, &windowHeight);
keyLabel.button.rectangle.x = 0;
keyLabel.button.rectangle.y = 0;
fileTextbox.button.rectangle.x = 0;
keyLabel.text = "Press this button with mouse. Then press key to change the key control. ID of this key is " + std::to_string(key->ID + 1);;
fileTextbox.text = "Click here on the bottom half of window and add file from which the sound should be taken.";
resizeWindow(windowWidth, windowHeight);
}
bool KeySetWindow::mainCycle() {
SDL_Event event;
bool quit = false;
drawWindow();
bool returnVal = false;
bool render;
while (!quit) {
isWindowEvent = false;
needRedraw = false;
render = false;
if (SDL_PollEvent(&event)) {
quit = checkEvents(event, &returnVal);
}
if (needRedraw) {
setFileTextbox(this->fileTextbox.textRectangle.w, this->fileTextbox.textRectangle.h);
setKeyLabel(this->keyLabel.textRectangle.w, this->keyLabel.textRectangle.h);
this->drawTextboxes();
render = true;
}
if (isWindowEvent) {
// Redraw the keyboard window
SDL_SetRenderDrawColor(this->keyboard->renderer, 0, 0, 100, 255);
// Clear window
SDL_RenderClear(this->keyboard->renderer);
this->keyboard->drawWindow();
this->keyboard->drawKeyLabels();
SDL_RenderPresent(this->keyboard->renderer);
render = true;
}
if (render) {
SDL_RenderPresent(this->renderer);
}
}
freeResources();
return returnVal;
}
bool KeySetWindow::checkEvents(const SDL_Event &event, bool *returnVal) {
Uint32 flags = SDL_GetWindowFlags(this->window);
bool enterPressed = false;
bool shouldResizeTextboxes = false;
switch (event.type) {
case SDL_TEXTINPUT:
if ((flags & SDL_WINDOW_INPUT_FOCUS) != 0) {
if (fileTextbox.hasFocus) {
needRedraw = this->fileTextbox.processKeyEvent(event, &enterPressed, &shouldResizeTextboxes);
}
}
break;
case SDL_KEYDOWN:
if ((flags & SDL_WINDOW_INPUT_FOCUS) != 0) {
if (keyLabel.hasFocus) {
this->key->keysym = event.key.keysym;
keyLabel.text = "Key " + std::to_string(this->key->ID + 1) + " changed to " + (std::string)SDL_GetScancodeName(event.key.keysym.scancode) + " with current mods.";
this->keyLabel.findFittingFont(&this->keyLabel.textRectangle.w, &this->keyLabel.textRectangle.h, this->windowWidth, this->windowHeight / 2);
this->setKeyLabel(this->keyLabel.textRectangle.w, this->keyLabel.textRectangle.h);
keyboard->drawKeyLabels();
keyLabel.hasFocus = false;
fileTextbox.hasFocus = false;
needRedraw = true;
isWindowEvent = true;
}
else if (fileTextbox.hasFocus) {
needRedraw = this->fileTextbox.processKeyEvent(event, &enterPressed, &shouldResizeTextboxes);
}
}
break;
case SDL_KEYUP:
break;
case SDL_MOUSEBUTTONDOWN:
break;
case SDL_MOUSEBUTTONUP:
if ((flags & SDL_WINDOW_MOUSE_FOCUS) != 0) {
if (event.button.button == SDL_BUTTON_LEFT) {
if (keyLabel.button.checkMouseClick(event.button))
{
keyLabel.hasFocus = !keyLabel.hasFocus;
fileTextbox.hasFocus = false;
SDL_StopTextInput();
}
else if (fileTextbox.button.checkMouseClick(event.button))
{
keyLabel.hasFocus = false;
fileTextbox.hasFocus = !fileTextbox.hasFocus;
if (fileTextbox.hasFocus) {
SDL_StartTextInput();
}
else {
SDL_StopTextInput();
}
}
else {
keyLabel.hasFocus = false;
fileTextbox.hasFocus = false;
SDL_StopTextInput();
}
needRedraw = true;
}
}
break;
case SDL_WINDOWEVENT:
if ((flags & SDL_WINDOW_INPUT_FOCUS) != 0) {
switch (event.window.event) {
case SDL_WINDOWEVENT_MINIMIZED:
break;
case SDL_WINDOWEVENT_HIDDEN:
break;
case SDL_WINDOWEVENT_MOVED:
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
break;
case SDL_WINDOWEVENT_RESIZED:
resizeWindow(event.window);
drawWindow();
break;
case SDL_WINDOWEVENT_CLOSE:
if (event.window.windowID != SDL_GetWindowID(this->window)) {
*returnVal = true;
}
return true;
default:
break;
}
}
else {
if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
*returnVal = true;
return true;
}
else {
if (event.window.event == SDL_WINDOWEVENT_MOVED || event.window.event == SDL_WINDOWEVENT_RESIZED) {
isWindowEvent = true;
keyboard->checkEvents(event);
}
}
}
break;
case SDL_QUIT:
return true;
default:
break;
}
if (enterPressed) {
this->key->setAudioBufferWithFile(&this->fileTextbox.text[0], &this->keyboard->audioSpec);
this->keyLabel.hasFocus = false;
this->fileTextbox.hasFocus = false;
}
if (shouldResizeTextboxes) {
setWindowButtons(this->fileTextbox.textRectangle.w, this->fileTextbox.textRectangle.h, this->keyLabel.textRectangle.w, this->keyLabel.textRectangle.h);
}
return false;
}
void KeySetWindow::resizeWindow(int w, int h) {
this->windowWidth = w;
this->windowHeight = h;
int w1;
int h1;
int w2;
int h2;
this->fileTextbox.findFittingFont(&w1, &h1, this->windowWidth, this->windowHeight / 2);
this->keyLabel.findFittingFont(&w2, &h2, this->windowWidth, this->windowHeight / 2);
setWindowButtons(w1, h1, w2, h2);
}
void KeySetWindow::resizeWindow(const SDL_WindowEvent &event) {
resizeWindow(event.data1, event.data2);
}
void KeySetWindow::setWindowButtons(int fileTextboxWidth, int fileTextboxHeight, int keyLabelWidth, int keyLabelHeight) {
setFileTextbox(fileTextboxWidth, fileTextboxHeight);
setKeyLabel(keyLabelWidth, keyLabelHeight);
}
constexpr void KeySetWindow::setKeyLabel(int w, int h) {
keyLabel.textRectangle.x = (this->windowWidth - w) / 2;
keyLabel.textRectangle.y = -this->windowHeight / 4 + (this->windowHeight - h) / 2;
keyLabel.textRectangle.w = w;
keyLabel.textRectangle.h = h;
keyLabel.button.rectangle.w = this->windowWidth;
keyLabel.button.rectangle.h = this->windowHeight / 2;
}
constexpr void KeySetWindow::setFileTextbox(int w, int h) {
this->fileTextbox.textRectangle.x = (this->windowWidth - w) / 2;
this->fileTextbox.textRectangle.y = this->windowHeight / 4 + (this->windowHeight - h) / 2;
this->fileTextbox.textRectangle.w = w;
this->fileTextbox.textRectangle.h = h;
fileTextbox.button.rectangle.y = this->windowHeight / 2;
fileTextbox.button.rectangle.w = this->windowWidth;
fileTextbox.button.rectangle.h = this->windowHeight;
}
void KeySetWindow::drawWindow() {
SDL_RenderClear(renderer);
drawTextboxes();
needRedraw = false;
SDL_RenderPresent(renderer);
}
void KeySetWindow::drawTextboxes() {
this->keyLabel.drawTextWithBackground(this->renderer, this->color, GlobalConstants::RED, GlobalConstants::BLACK);
this->fileTextbox.drawTextWithBackground(this->renderer, this->color, GlobalConstants::RED, GlobalConstants::BACKGROUND_BLUE);
}
void KeySetWindow::freeResources() {
// Destroy renderer
SDL_DestroyRenderer(renderer);
// Close and destroy the window
SDL_DestroyWindow(window);
}