| f3ef3495 |
sago007 |
2008-11-13 20:00 |
1 |
/* |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
2 |
=========================================================================== |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
3 |
blockattack - Block Attack - Rise of the Blocks |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
4 |
Copyright (C) 2005-2012 Poul Sander |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
5 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
6 |
This program is free software: you can redistribute it and/or modify |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
7 |
it under the terms of the GNU General Public License as published by |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
8 |
the Free Software Foundation, either version 2 of the License, or |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
9 |
(at your option) any later version. |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
10 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
11 |
This program is distributed in the hope that it will be useful, |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
14 |
GNU General Public License for more details. |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
15 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
16 |
You should have received a copy of the GNU General Public License |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
17 |
along with this program. If not, see http://www.gnu.org/licenses/ |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
18 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
19 |
Source information and contacts persons can be found at |
| 021de090 |
sago007 |
2015-12-30 18:56 |
20 |
http://blockattack.net |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
21 |
=========================================================================== |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
22 |
*/ |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
23 |
|
| b7590374 |
sago007 |
2011-05-19 16:15 |
24 |
|
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
25 |
#include "MenuSystem.h" |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
26 |
#include "common.h" |
| 7d604e02 |
sago007 |
2015-11-29 18:39 |
27 |
#include "global.hpp" |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
28 |
|
| 0c201300 |
sago007 |
2016-02-20 12:12 |
29 |
static int mousex; |
| 0c201300 |
sago007 |
2016-02-20 12:12 |
30 |
static int mousey; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
31 |
|
| 33f4e975 |
sago007 |
2015-11-14 21:49 |
32 |
using namespace std; |
| 33f4e975 |
sago007 |
2015-11-14 21:49 |
33 |
|
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
34 |
|
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
35 |
ButtonGfx standardButton; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
36 |
|
| 1383de53 |
sago007 |
2016-02-21 12:18 |
37 |
void ButtonGfx::setSurfaces(sago::SagoSprite* marked, sago::SagoSprite* unmarked) { |
| 78ab168e |
sago007 |
2015-11-14 22:02 |
38 |
this->marked = marked; |
| 78ab168e |
sago007 |
2015-11-14 22:02 |
39 |
this->unmarked = unmarked; |
| 1383de53 |
sago007 |
2016-02-21 12:18 |
40 |
xsize=(marked)->GetWidth(); |
| 1383de53 |
sago007 |
2016-02-21 12:18 |
41 |
ysize=(marked)->GetHeight(); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
42 |
if (verboseLevel) { |
| 56241205 |
sago007 |
2012-08-05 11:15 |
43 |
cout << "Surfaces set, size: " <<xsize << " , " << ysize << endl; |
| acd79baf |
sago007 |
2015-11-22 19:37 |
44 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
45 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
46 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
47 |
Button::Button() { |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
48 |
gfx = &standardButton; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
49 |
label = ""; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
50 |
marked = false; |
| acd79baf |
sago007 |
2015-11-22 19:37 |
51 |
action = nullptr; |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
52 |
popOnRun = false; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
53 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
54 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
55 |
Button::~Button() { |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
56 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
57 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
58 |
Button::Button(const Button& b) { |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
59 |
gfx = b.gfx; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
60 |
label = b.label; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
61 |
marked = b.marked; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
62 |
action = b.action; |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
63 |
popOnRun = false; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
64 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
65 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
66 |
void Button::setLabel(const string& text) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
67 |
label = text; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
68 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
69 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
70 |
void Button::setAction(void (*action2run)(Button*)) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
71 |
action = action2run; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
72 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
73 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
74 |
bool Button::isClicked(int x,int y) { |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
75 |
if ( x >= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
76 |
return true; |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
77 |
} |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
78 |
return false; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
79 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
80 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
81 |
void Button::doAction() { |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
82 |
if (action) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
83 |
action(this); |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
84 |
return; |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
85 |
} |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
86 |
cerr << "Warning: button \"" << label << "\" has no action assigned!"; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
87 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
88 |
|
| 0c1de203 |
sago007 |
2015-12-05 22:15 |
89 |
void Button::drawToScreen() { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
90 |
#if DEBUG |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
91 |
//cout << "Painting button: " << label << " at: " << x << "," << y << endl; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
92 |
#endif |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
93 |
if (marked) { |
| 1383de53 |
sago007 |
2016-02-21 12:18 |
94 |
gfx->marked->Draw(screen, SDL_GetTicks(), x, y); |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
95 |
} |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
96 |
else { |
| 1383de53 |
sago007 |
2016-02-21 12:18 |
97 |
gfx->unmarked->Draw(screen, SDL_GetTicks(), x, y); |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
98 |
} |
| 3a23aa5e |
sago007 |
2015-12-23 18:06 |
99 |
gfx->thefont->draw(screen, x+gfx->xsize/2,y+gfx->ysize/2-gfx->thefont->getHeight("%s", label.c_str())/2, NFont::CENTER, "%s", label.c_str()); |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
100 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
101 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
102 |
void Button::setPopOnRun(bool popOnRun) { |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
103 |
this->popOnRun = popOnRun; |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
104 |
} |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
105 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
106 |
bool Button::isPopOnRun() const { |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
107 |
return popOnRun; |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
108 |
} |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
109 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
110 |
void Button::setGfx(ButtonGfx* gfx) { |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
111 |
this->gfx = gfx; |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
112 |
} |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
113 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
114 |
int Button::getHeight() { |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
115 |
return this->gfx->ysize; |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
116 |
} |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
117 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
118 |
void Menu::drawSelf() { |
| b539639c |
sago007 |
2015-12-29 11:51 |
119 |
SDL_RenderClear(screen); |
| 1d2e2129 |
sago007 |
2015-12-23 15:41 |
120 |
backgroundImage.Draw(screen, SDL_GetTicks(), 0, 0); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
121 |
vector<Button*>::iterator it; |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
122 |
for (it = buttons.begin(); it < buttons.end(); it++) { |
| 0c1de203 |
sago007 |
2015-12-05 22:15 |
123 |
(*it)->drawToScreen(); |
| 6258a442 |
sago007 |
2015-11-14 22:11 |
124 |
} |
| 0c1de203 |
sago007 |
2015-12-05 22:15 |
125 |
exit.drawToScreen(); |
| 3a23aa5e |
sago007 |
2015-12-23 18:06 |
126 |
standardButton.thefont->draw(screen, 50, 50, "%s", title.c_str()); |
| 1d2e2129 |
sago007 |
2015-12-23 15:41 |
127 |
mouse.Draw(screen, SDL_GetTicks(), mousex, mousey); |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
128 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
129 |
|
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
130 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
131 |
void Menu::placeButtons() { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
132 |
int nextY = 100; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
133 |
const int X = 50; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
134 |
vector<Button*>::iterator it; |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
135 |
for (it = buttons.begin(); it < buttons.end(); it++) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
136 |
(*it)->x = X; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
137 |
(*it)->y = nextY; |
| b283e0f7 |
sago007 |
2012-07-29 14:01 |
138 |
nextY += (*it)->getHeight()+10; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
139 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
140 |
exit.x = X; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
141 |
exit.y = nextY; |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
142 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
143 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
144 |
void Menu::addButton(Button* b) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
145 |
buttons.push_back(b); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
146 |
b->marked = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
147 |
placeButtons(); |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
148 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
149 |
|
| 1d2e2129 |
sago007 |
2015-12-23 15:41 |
150 |
Menu::Menu(SDL_Renderer* screen) { |
| 0c1de203 |
sago007 |
2015-12-05 22:15 |
151 |
this->screen = screen; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
152 |
buttons = vector<Button*>(10); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
153 |
isSubmenu = true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
154 |
exit.setLabel( _("Back") ); |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
155 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
156 |
|
| 1d2e2129 |
sago007 |
2015-12-23 15:41 |
157 |
Menu::Menu(SDL_Renderer* screen,bool submenu) { |
| 0c1de203 |
sago007 |
2015-12-05 22:15 |
158 |
this->screen = screen; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
159 |
buttons = vector<Button*>(0); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
160 |
isSubmenu = submenu; |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
161 |
if (isSubmenu) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
162 |
exit.setLabel( _("Back") ); |
| acd79baf |
sago007 |
2015-11-22 19:37 |
163 |
} |
| acd79baf |
sago007 |
2015-11-22 19:37 |
164 |
else { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
165 |
exit.setLabel( _("Exit") ); |
| acd79baf |
sago007 |
2015-11-22 19:37 |
166 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
167 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
168 |
|
| 1d2e2129 |
sago007 |
2015-12-23 15:41 |
169 |
Menu::Menu(SDL_Renderer* screen, const string& title, bool submenu) { |
| 0c1de203 |
sago007 |
2015-12-05 22:15 |
170 |
this->screen = screen; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
171 |
buttons = vector<Button*>(0); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
172 |
isSubmenu = submenu; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
173 |
this->title = title; |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
174 |
if (isSubmenu) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
175 |
exit.setLabel(_("Back") ); |
| acd79baf |
sago007 |
2015-11-22 19:37 |
176 |
} |
| acd79baf |
sago007 |
2015-11-22 19:37 |
177 |
else { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
178 |
exit.setLabel(_("Exit") ); |
| acd79baf |
sago007 |
2015-11-22 19:37 |
179 |
} |
| cc3ef158 |
sago007 |
2011-07-03 16:13 |
180 |
} |
| cc3ef158 |
sago007 |
2011-07-03 16:13 |
181 |
|
| 826cf176 |
sago007 |
2016-03-12 10:53 |
182 |
bool isUpEvent(const SDL_Event& event) { |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
183 |
if ( event.type == SDL_KEYDOWN ) { |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
184 |
if (event.key.keysym.sym == SDLK_UP) { |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
185 |
return true; |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
186 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
187 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
188 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
189 |
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP ) { |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
190 |
return true; |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
191 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
192 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
193 |
return false; |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
194 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
195 |
|
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
196 |
bool isDownEvent(const SDL_Event& event) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
197 |
if ( event.type == SDL_KEYDOWN ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
198 |
if (event.key.keysym.sym == SDLK_DOWN) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
199 |
return true; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
200 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
201 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
202 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
203 |
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
204 |
return true; |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
205 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
206 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
207 |
return false; |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
208 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
209 |
|
| d21782d4 |
sago007 |
2016-03-13 11:17 |
210 |
bool isLeftEvent(const SDL_Event& event) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
211 |
if ( event.type == SDL_KEYDOWN ) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
212 |
if (event.key.keysym.sym == SDLK_LEFT) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
213 |
return true; |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
214 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
215 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
216 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
217 |
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
218 |
return true; |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
219 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
220 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
221 |
return false; |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
222 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
223 |
|
| d21782d4 |
sago007 |
2016-03-13 11:17 |
224 |
bool isRightEvent(const SDL_Event& event) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
225 |
if ( event.type == SDL_KEYDOWN ) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
226 |
if (event.key.keysym.sym == SDLK_DOWN) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
227 |
return true; |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
228 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
229 |
} |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
230 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| d21782d4 |
sago007 |
2016-03-13 11:17 |
231 |
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
232 |
return true; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
233 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
234 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
235 |
return false; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
236 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
237 |
|
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
238 |
bool isEscapeEvent(const SDL_Event& event) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
239 |
if ( event.type == SDL_KEYDOWN ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
240 |
if ( event.key.keysym.sym == SDLK_ESCAPE ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
241 |
return true; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
242 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
243 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
244 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
245 |
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_Y ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
246 |
return true; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
247 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
248 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
249 |
return false; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
250 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
251 |
|
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
252 |
bool isConfirmEvent(const SDL_Event& event) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
253 |
if ( event.type == SDL_KEYDOWN ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
254 |
if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
255 |
return true; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
256 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
257 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
258 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
259 |
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
260 |
return true; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
261 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
262 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
263 |
return false; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
264 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
265 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
266 |
void Menu::run() { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
267 |
running = true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
268 |
bool bMouseUp = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
269 |
long oldmousex = mousex; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
270 |
long oldmousey = mousey; |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
271 |
while (running && !Config::getInstance()->isShuttingDown()) { |
| acd79baf |
sago007 |
2015-11-22 19:37 |
272 |
if (!(highPriority)) { |
| acd79baf |
sago007 |
2015-11-22 19:37 |
273 |
SDL_Delay(10); |
| acd79baf |
sago007 |
2015-11-22 19:37 |
274 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
275 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
276 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
277 |
SDL_Event event; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
278 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
279 |
while ( SDL_PollEvent(&event) ) { |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
280 |
if ( event.type == SDL_QUIT ) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
281 |
Config::getInstance()->setShuttingDown(5); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
282 |
running = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
283 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
284 |
|
| 826cf176 |
sago007 |
2016-03-12 10:53 |
285 |
if (isUpEvent(event)) { |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
286 |
marked--; |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
287 |
if (marked<0) { |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
288 |
marked = buttons.size(); //not -1, since exit is after the last element in the list |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
289 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
290 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
291 |
|
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
292 |
if (isDownEvent(event)) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
293 |
marked++; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
294 |
if (marked> (int)buttons.size()) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
295 |
marked = 0; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
296 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
297 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
298 |
|
| d21782d4 |
sago007 |
2016-03-13 11:17 |
299 |
if (isEscapeEvent(event) && isSubmenu) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
300 |
running = false; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
301 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
302 |
|
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
303 |
if (isConfirmEvent(event)) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
304 |
if (marked < (int)buttons.size()) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
305 |
buttons.at(marked)->doAction(); |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
306 |
if (buttons.at(marked)->isPopOnRun()) { |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
307 |
running = false; |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
308 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
309 |
} |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
310 |
if (marked == (int)buttons.size()) { |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
311 |
running = false; |
| d5efd7d1 |
sago007 |
2016-03-12 11:32 |
312 |
} |
| 826cf176 |
sago007 |
2016-03-12 10:53 |
313 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
314 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
315 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
316 |
|
| daa8f1cf |
sago007 |
2016-01-23 10:57 |
317 |
for (int i=0; i<(int)buttons.size(); i++) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
318 |
buttons.at(i)->marked = (i == marked); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
319 |
} |
| daa8f1cf |
sago007 |
2016-01-23 10:57 |
320 |
exit.marked = (marked == (int)buttons.size()); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
321 |
Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
322 |
// If the mouse button is released, make bMouseUp equal true |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
323 |
if ( (buttonState&SDL_BUTTON(1))==0) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
324 |
bMouseUp=true; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
325 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
326 |
|
| ecef5838 |
sago007 |
2015-11-24 20:01 |
327 |
if (abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) { |
| daa8f1cf |
sago007 |
2016-01-23 10:57 |
328 |
for (int i=0; i< (int)buttons.size(); ++i) { |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
329 |
if (buttons.at(i)->isClicked(mousex,mousey)) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
330 |
marked = i; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
331 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
332 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
333 |
if (exit.isClicked(mousex,mousey)) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
334 |
marked = buttons.size(); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
335 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
336 |
oldmousex = mousex; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
337 |
oldmousey = mousey; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
338 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
339 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
340 |
//mouse clicked |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
341 |
if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
342 |
bMouseUp = false; |
| daa8f1cf |
sago007 |
2016-01-23 10:57 |
343 |
for (int i=0; i< (int)buttons.size(); ++i) { |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
344 |
if (buttons.at(i)->isClicked(mousex,mousey)) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
345 |
buttons.at(i)->doAction(); |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
346 |
if (buttons.at(i)->isPopOnRun()) { |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
347 |
running = false; |
| acd79baf |
sago007 |
2015-11-22 19:37 |
348 |
} |
| 67a32395 |
sago007 |
2012-04-19 18:40 |
349 |
mousex = 0; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
350 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
351 |
} |
| ecef5838 |
sago007 |
2015-11-24 20:01 |
352 |
if (exit.isClicked(mousex,mousey)) { |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
353 |
running = false; |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
354 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
355 |
} |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
356 |
|
| c53e6443 |
sago007 |
2012-04-17 11:04 |
357 |
drawSelf(); |
| 1d2e2129 |
sago007 |
2015-12-23 15:41 |
358 |
SDL_RenderPresent(screen); |
| c53e6443 |
sago007 |
2012-04-17 11:04 |
359 |
} |
| f3ef3495 |
sago007 |
2008-11-13 20:00 |
360 |
} |
|
|
1970-01-01 00:00 |
361 |
|