-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassMenu.hpp
556 lines (438 loc) · 14 KB
/
ClassMenu.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* ClassMenu.hpp
*
* Copyright 2015 Joaquín Monteagudo Gómez <kindos7@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
/*
FDX_Fighter - ClassMenu.hpp
Little spaceship game made with C++ and SFML
Menu and windows - header file
*/
/*
Version 0.1 (dd/mm/yy, 24/11/2015 ->)
*/
/*
Preprocessor
*/
/*Header guard*/
#ifndef _FDX_CLASSMENU_H_
#define _FDX_CLASSMENU_H_
/* Includes */
//SFML
#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"
//STD
#include <string>
#include <vector>
/* Defines */
/*Constants*/
/*Macros*/
/*
Definitions
*/
/*Classes*/
/*BUTTONS*/
/*Button*/
namespace fdx { namespace menu
{
/*
Class definitions
*/
//Button class
class Button;
//Button with an image on it
class ButtonImage;
//Button with text on it
class ButtonText;
//Dictionary of colours
class DictionaryColors;
//Window menu
class Menu;
/*
Function prototypes
*/
/*
Data types
*/
/* Typedefs */
/* Classes */
//Button class
class Button : public sf::Drawable
{
/*Attributes*/
private:
//Button's shape
sf::RectangleShape front_rect;
//Button's shadow
sf::RectangleShape back_rect;
//Button pressed
bool pressed;
//Button available
bool available;
//Play once
bool played;
//Sound: mouse over button
sf::Sound over_sound;
//Sound: button pressed
sf::Sound pressed_sound;
/*Constructors, copy control*/
protected:
//Complete constructor
Button (
int ix, int iy, int iwidth, int iheight,
int idif_x, int idif_y,
const sf::Color& ifront_color, const sf::Color& iback_color,
bool iavailable,
const sf::Sound& iover, const sf::Sound& ipress
)
:front_rect(sf::Vector2f(iwidth, iheight)),back_rect(sf::Vector2f(iwidth+idif_x*2, iheight+idif_y*2))
,pressed(false),available(iavailable),over_sound(iover),pressed_sound(ipress)
{
//Front rect
front_rect.setPosition(ix, iy);
front_rect.setFillColor(ifront_color);
//Back rect
back_rect.setPosition(ix - idif_x, iy - idif_y);
back_rect.setFillColor(iback_color);
}
public:
//Virtual destructor
virtual ~Button(){}
/*Button methods*/
public:
//Test if the button is pressed by the given mouse
bool isPressed(int mos_x, int mos_y, bool but);
//Return the last pressed status of the button
bool lastPressed()
{
return pressed;
}
//Check if the button is available
bool isAvailable()
{
return available;
}
//Lock/unlock the button
virtual void unlock()
{
available=true;
}
virtual void lock()
{
available=false;
}
//Change the front color of the button
void change_color_button(const sf::Color& color)
{
front_rect.setFillColor(color);
}
/*Draw*/
public:
//Draw the button
virtual void draw (sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(back_rect,states);
if (!pressed) target.draw(front_rect,states);
}
};
//Button with text on it
class ButtonText : public Button
{
/*Attributes*/
private:
//Button's text
sf::Text button_text;
//Text's colors
//Locked text color
sf::Color lock_color;
//Unlocked text color
sf::Color unlock_color;
/*Constructors, copy control*/
public:
//Complete constructor
ButtonText (
int ix, int iy, int iwidth, int iheight,
int idif_x, int idif_y,
const sf::Color& ifront_color,const sf::Color& iback_color,
bool iavailable,
const sf::Sound& iover, const sf::Sound& ipress,
std::string itext, const sf::Font &ifont, int ifont_size,
const sf::Color& iunlock_color,const sf::Color& ilock_color
)
:Button(ix, iy, iwidth, iheight, idif_x, idif_y, ifront_color, iback_color, iavailable, iover, ipress), button_text(itext, ifont, ifont_size),lock_color(ilock_color),unlock_color(iunlock_color)
{
//Set the text's color
change_color_text();
//Center the text
sf::FloatRect textRect = button_text.getLocalBounds();
button_text.setOrigin(textRect.left + textRect.width/2.0f, textRect.top + textRect.height/2.0f);
//Position
button_text.setPosition(ix+iwidth/2,iy+iheight/2);
}
/*Locking/unlocking*/
private:
//Update the text's color
void change_color_text()
{
if (isAvailable())
button_text.setColor(unlock_color);
else
button_text.setColor(lock_color);
}
public:
//Lock/unlock the button
virtual void unlock()
{
Button::unlock();
change_color_text();
}
virtual void lock()
{
Button::lock();
change_color_text();
}
/*Set text*/
public:
void set_text(const std::string & str)
{
button_text.setString(str);
//Center the text
sf::FloatRect textRect = button_text.getLocalBounds();
button_text.setOrigin(textRect.left + textRect.width/2.0f, textRect.top + textRect.height/2.0f);
}
/*Draw*/
//Draw
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
};
/*ButtonImage*/
class ButtonImage : public Button
{
private:
//Image
sf::Sprite image;
public:
//Constructor
ButtonImage(int ix, int iy, float iwidth, float iheight, int idif_x, int idif_y, const sf::Color& ifront_color,const sf::Color& iback_color, bool iavailable, const sf::Sound& iover, const sf::Sound& ipress, const sf::Texture& ibackground)
:Button(ix, iy, iwidth, iheight, idif_x, idif_y, ifront_color, iback_color, iavailable, iover, ipress), image()
{
//Create the sprite
image.setTexture(ibackground);
//Center the image
sf::FloatRect imgRect = image.getLocalBounds();
image.setOrigin(imgRect.left + imgRect.width/2.0f, imgRect.top + imgRect.height/2.0f);
//Position
image.setPosition(ix+iwidth/2,iy+iheight/2);
}
void change_color_image(const sf::Color& color)
{
image.setColor(color);
}
virtual void unlock()
{
Button::unlock();
}
virtual void lock()
{
Button::lock();
}
//Draw function
void draw (sf::RenderTarget &target, sf::RenderStates states) const
{
Button::draw(target,states);
target.draw(image,states);
}
};
/*LANGUAGE*/
class Language
{
/*Settings*/
public:
//Type of enums
typedef unsigned char en_type;
//Enums
//Enum of words
enum Texts : en_type
{
SINGLEPLAYER=0,
MULTIPLAYER,
STATISTICS,
OPTIONS,
PERSONALIZE,
COLOR,
LANGUAGE,
COLORS,
LANGUAGES,
CONFIRM,
RESTART,
VSYNC_ON,
VSYNC_OFF,
YOU_DIED,
ENEMY_DIED,
CONTROLS,
CONFIGURE,
MOUSE,
XBOX,
WII,
SHOOT,
ACCELERATE,
BRAKE,
STICK,
WON,
LOSE,
MAX_TEXT//Limit
};
//Enum of languages
enum Langs : en_type
{
ENGLISH=0,
SPANISH,
MAX_LANG//Limit
};
private:
//Default config
static constexpr en_type DEF_LANG=ENGLISH;
/*Attributes*/
//Languages
private:
//Language matrix
static const std::string language_text[MAX_LANG][MAX_TEXT];
//Language selected
static en_type lang;
/*Constructors, copy control*/
private:
//Default constructor
Language() = delete;//Static class, not to be used
/*Language methods*/
public:
static void setLang(en_type nl)
{
lang=nl;
}
static en_type getLang()
{
return lang;
}
static std::string getText(en_type ntext)
{
if (lang<MAX_LANG&&ntext<MAX_TEXT)
return language_text[lang][ntext];
return std::string();
}
};
class DictionaryColors
{
/*Settings*/
public:
//Type of enums
typedef unsigned char en_type;
/*Attributes*/
private:
//Array of colors
static const sf::Color colors[];
public:
/*Constructors, copy control*/
//Default constructor
DictionaryColors() = delete;//Static class, not to be used
//Get a color from the dictionary
static const sf::Color& getColor(unsigned int pos)
{
return colors[pos];
}
//Get a color from the dictionary with a modified alpha (half transparent)
static sf::Color getSemiColor(unsigned int pos)
{
sf::Color rv(getColor(pos));
rv.a=127;
return rv;
}
};
/*MENU*/
class Menu : public sf::Drawable
{
/*Atributtes*/
private:
//Menu's background
sf::Sprite background;
//Menu's tittle
sf::Text title;
//Menu's bottons
std::vector<Button*> menu_buttons;
/*Constructors, copy control*/
public:
//Complete constructor
Menu(const sf::Texture& iimage,std::string imenu_title, int ititle_size, int itittle_x, int itittle_y, const sf::Font &ifont, const sf::Color &ititle_color)
:background(iimage),title(imenu_title, ifont, ititle_size),menu_buttons()
{
//Title
title.setPosition(sf::Vector2f(itittle_x, itittle_y));
title.setColor(ititle_color);
}
//Destructor
~Menu()
{
for (Button *b : menu_buttons)
delete b;
}
/*Button methods*/
public:
//Add a button
void add_button(Button* ptr_b)
{
if (ptr_b)
menu_buttons.push_back(ptr_b);
}
//Returns the position on the vector of the first pressed button
int button_pressed(int x, int y, bool but);
//Unlocks a button
void unlock_buttons(unsigned int but)
{
menu_buttons.at(but)->unlock();
}
//Locks button
void lock_buttons(int but)
{
menu_buttons.at(but)->lock();
}
//Changes the color of the front rect
void change_color_rect(int but, const sf::Color& color)
{
menu_buttons.at(but)->change_color_button(color);
}
//Change the color of an image
void change_color_image(int but, const sf::Color& color)
{
ButtonImage* bi;
if ((bi=dynamic_cast<ButtonImage*>(menu_buttons.at(but))))
bi->change_color_image(color);
}
//Change the text of a button
void change_text(int but, const std::string &str)
{
ButtonText *bt;
if ((bt=dynamic_cast<ButtonText*>(menu_buttons.at(but))))
bt->set_text(str);
}
/*Draw*/
public:
//Draw the menu
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
};
}}//End of namespace
#endif // _FDX_CLASSMENU_H_