-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuButton.cpp
302 lines (257 loc) · 7.95 KB
/
menuButton.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
#include "menuButton.h"
//============================================================================================//
// Constructors
//============================================================================================//
menuButton::menuButton()
{
setPosition(300, 300);
mRect.h = BUTTON_HEIGHT;
mRect.w = BUTTON_WIDTH;
buttonText = "NEW STAGE";
}
menuButton::menuButton(int x, int y, std::string text)
{
setPosition(x, y);
mRect.h = BUTTON_HEIGHT;
mRect.w = BUTTON_WIDTH;
buttonText = text;
displayRect = Rectangle(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, 10, 10, 10);
DefaultColor = { 120,120,120,0 };
HoldColor = { 100,190,100,0 };
HoverColor = { 240,240,40,0 };
hoverBox = Rectangle(x - 2, y - 2, BUTTON_WIDTH +4, BUTTON_HEIGHT +4);
}
menuButton::menuButton(Rectangle oRect, std::string text, SDL_Renderer* Renderer)
{
mRect = oRect.getRect();
setPosition(mRect.x, mRect.y);
setDimensions(mRect.w, mRect.h);
displayRect = oRect;
DefaultColor = { 120,120,120,0 };
HoldColor = { 100,190,100,0 };
HoverColor = { 240,240,40,0 };
hoverBox = Rectangle(mPosition.x - 2, mPosition.y - 2, BUTTON_WIDTH + 4, BUTTON_HEIGHT + 4);
loadText(Renderer);
textRect.w = mRect.w = oRect.getDimensions().x;
textRect.h = mRect.h = oRect.getDimensions().y;
}
//============================================================================================//
// setters
//============================================================================================//
void menuButton::setPosition(int x, int y)
{
mPosition.x = mRect.x = x;
mPosition.y = mRect.y = y;
}
void menuButton::setDimensions(int w, int h)
{
mRect.w = BUTTON_WIDTH = w;
mRect.h = BUTTON_HEIGHT = h;
displayRect.setDimensions(w, h);
}
void menuButton::setDimensions(int w, int h, SDL_Renderer* Renderer)
{
mRect.w = BUTTON_WIDTH = w;
mRect.h = BUTTON_HEIGHT = h;
displayRect.setDimensions(w, h);
loadText(Renderer);
}
//============================================================================================//
// event loop
//============================================================================================//
void menuButton::handleEvent(SDL_Event* e)
{
//If mouse event happened
if (e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP || e->type == SDL_KEYDOWN)
{
//Check collision of mouse and button
//Get mouse position
int x, y;
SDL_GetMouseState(&x, &y);
//Check if mouse is in button
bool inside = checkMouse(x, y);
if (inside)
{
hovering = true;
//BUTTON EVENT CODE
switch (e->type)
{
case SDL_MOUSEBUTTONDOWN:
displayRect.setColor(100, 190, 100);
held = true;
break;
case SDL_MOUSEBUTTONUP:
held = false;
pressed = true;
break;
}
}
else hovering = false;
}
}
bool menuButton::checkMouse(int x, int y)
{
//Check if mouse is in button
bool inside = true;
//Mouse is left of the button
if (x < mPosition.x)
{
held = inside = false;
}
//Mouse is right of the button
else if (x > mPosition.x + displayRect.getDimensions().x)
{
held = inside = false;
}
//Mouse above the button
else if (y < mPosition.y)
{
held = inside = false;
}
//Mouse below the button
else if (y > mPosition.y + displayRect.getDimensions().y)
{
held = inside = false;
}
return inside;
}
//============================================================================================//
// rendering
//============================================================================================//
bool menuButton::render(SDL_Renderer* Renderer)
{
bool success = true;
SDL_Point dimensions = displayRect.getDimensions();
SDL_Rect oRect = { 0,0,dimensions.x,dimensions.y };
SDL_Rect oTextRect = { 0,0,textRect.w,textRect.h };
SDL_Rect oHoverRect = { 0,0,dimensions.x + 4, dimensions.y + 4 };
SDL_Rect screen = { 0,0,WINDOW_WIDTH,WINDOW_HEIGHT };
SDL_RenderSetViewport(Renderer, &hoverBox.getRect());
if (hovering)
{
SDL_SetRenderDrawColor(Renderer, HoverColor.r, HoverColor.g, HoverColor.b, HoverColor.a);
SDL_RenderFillRect(Renderer, &oHoverRect);
}
SDL_RenderSetViewport(Renderer, &displayRect.getRect());
if (held)
{
SDL_Color tempcol = displayRect.getColor();
SDL_SetRenderDrawColor(Renderer, tempcol.r, tempcol.b, tempcol.g, 0xFF);
}
else
SDL_SetRenderDrawColor(Renderer, 0x8F, 0x99, 0xAA, 0xFF);
SDL_RenderFillRect(Renderer, &oRect);
SDL_SetRenderDrawColor(Renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderDrawRect(Renderer, &oRect);
SDL_RenderCopyEx(Renderer, ttfTexture, 0, &oTextRect, 0, 0, SDL_FLIP_NONE);
SDL_RenderSetViewport(Renderer, &screen);
return success;
}
bool menuButton::render(SDL_Renderer* Renderer, SDL_Color buttonColor)
{
bool success = true;
SDL_SetRenderDrawColor(Renderer, buttonColor.r, buttonColor.b, buttonColor.g, buttonColor.a);
SDL_RenderFillRect(Renderer, &mRect);
SDL_SetRenderDrawColor(Renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderDrawRect(Renderer, &mRect);
SDL_RenderCopyEx(Renderer, ttfTexture, 0, &textRect, 0, 0, SDL_FLIP_NONE);
return success;
}
//============================================================================================//
// font loading
//============================================================================================//
bool menuButton::loadText(SDL_Renderer* Renderer)
{
bool success = true;
//Init Font
if (TTF_Init() < 0) {
// Error handling code
printf("SDL_TTF could not initialize! SDL_TTF Error: %s\n", TTF_GetError());
success = false;
}
Font = TTF_OpenFont("stages/fonts/VT323.ttf", 60);
if (Font == NULL)
{
printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError());
success = false;
}
SDL_Color Stagecolor;
Stagecolor.a = 0;
Stagecolor.r = 160;
Stagecolor.b = 0;
Stagecolor.g = 0;
std::string txt;
if (buttonText[0] == 's' && buttonText[6] == '/')
{
for (int i = 7; i < int(buttonText.size()) - 4; i++)
{
txt += buttonText[i];
}
}
else txt = buttonText;
SDL_Surface* surface = TTF_RenderText_Solid(Font, txt.c_str(), Stagecolor);
if (surface == NULL)
{
printf("Failed to create surface! SDL_ttf Error: %s\n", TTF_GetError());
success = false;
}
ttfTexture = SDL_CreateTextureFromSurface(Renderer, surface);
SDL_QueryTexture(ttfTexture, NULL, NULL, &textWidth, &textHeight);
textRect.x = displayRect.getPosition().x;
textRect.y = displayRect.getPosition().y;
textRect.w = textWidth;
textRect.h = displayRect.getDimensions().y;
mRect.w = textWidth;
SDL_FreeSurface(surface);
return success;
}
bool menuButton::loadText(SDL_Renderer* Renderer, SDL_Color stagecolor)\
{
bool success = true;
//Init Font
if (TTF_Init() < 0) {
// Error handling code
printf("SDL_TTF could not initialize! SDL_TTF Error: %s\n", TTF_GetError());
success = false;
}
Font = TTF_OpenFont("stages/fonts/VT323.ttf", 60);
if (Font == NULL)
{
printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError());
success = false;
}
std::string txt;
if (buttonText.size() > 6 && buttonText[0] == 's' && buttonText[1] == 't' && buttonText[2] == 'a' && buttonText[3] == 'g' && buttonText[4] == 'e' && buttonText[5] == 's' && buttonText[6] == '/')
{
for (int i = 7; i < int(buttonText.size()) - 4; i++)
{
txt += buttonText[i];
}
}
else txt = buttonText;
SDL_Surface* surface = TTF_RenderText_Solid(Font, txt.c_str(), stagecolor);
if (surface == NULL)
{
printf("Failed to create surface! SDL_ttf Error: %s\n", TTF_GetError());
success = false;
}
ttfTexture = SDL_CreateTextureFromSurface(Renderer, surface);
SDL_QueryTexture(ttfTexture, NULL, NULL, &textWidth, &textHeight);
textRect.x = mRect.x;
textRect.y = mRect.y;
textRect.w = textWidth;
textRect.h = mRect.h;
mRect.w = textWidth;
SDL_FreeSurface(surface);
return success;
}
//============================================================================================//
// newStageButton
//============================================================================================//
newStageButton::newStageButton()
{
mRect.h = BUTTON_HEIGHT;
mRect.w = BUTTON_WIDTH;
setPosition(100, 150);
buttonText = "NEW STAGE";
}