git repos / blockattack-game

blame: source/code/MenuSystem.cpp

normal view · raw

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
a077dceb sago007 2016-03-19 19:19 20
http://www.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"
a077dceb sago007 2016-03-19 19:19 28
#include "gamecontroller.h"
f3ef3495 sago007 2008-11-13 20:00 29
0c201300 sago007 2016-02-20 12:12 30
static int mousex;
0c201300 sago007 2016-02-20 12:12 31
static int mousey;
f3ef3495 sago007 2008-11-13 20:00 32
96eb7a16 sago007 2016-04-29 17:01 33
using std::string;
96eb7a16 sago007 2016-04-29 17:01 34
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 35
using std::cout;
96eb7a16 sago007 2016-04-29 17:01 36
using std::vector;
33f4e975 sago007 2015-11-14 21:49 37
094b2dfe sago007 2016-06-12 10:50 38
const char* const menu_marked = "menu_marked";
094b2dfe sago007 2016-06-12 10:50 39
const char* const menu_unmarked = "menu_unmarked";
f3ef3495 sago007 2008-11-13 20:00 40
b283e0f7 sago007 2012-07-29 14:01 41
ButtonGfx standardButton;
f3ef3495 sago007 2008-11-13 20:00 42
094b2dfe sago007 2016-06-12 10:50 43
void ButtonGfx::setSurfaces() {
13476fc2 sago007 2016-09-28 18:34 44
	this->xsize = spriteHolder->GetSprite(menu_marked).GetWidth();
13476fc2 sago007 2016-09-28 18:34 45
	this->ysize = spriteHolder->GetSprite(menu_marked).GetHeight();
ecef5838 sago007 2015-11-24 20:01 46
	if (verboseLevel) {
13476fc2 sago007 2016-09-28 18:34 47
		cout << "Surfaces set, size: " << this->xsize << " , " << this->ysize << "\n";
acd79baf sago007 2015-11-22 19:37 48
	}
f3ef3495 sago007 2008-11-13 20:00 49
}
f3ef3495 sago007 2008-11-13 20:00 50
ecef5838 sago007 2015-11-24 20:01 51
Button::Button() {
b283e0f7 sago007 2012-07-29 14:01 52
	gfx = &standardButton;
c53e6443 sago007 2012-04-17 11:04 53
	label = "";
c53e6443 sago007 2012-04-17 11:04 54
	marked = false;
acd79baf sago007 2015-11-22 19:37 55
	action = nullptr;
67a32395 sago007 2012-04-19 18:40 56
	popOnRun = false;
f3ef3495 sago007 2008-11-13 20:00 57
}
f3ef3495 sago007 2008-11-13 20:00 58
ecef5838 sago007 2015-11-24 20:01 59
Button::~Button() {
f3ef3495 sago007 2008-11-13 20:00 60
}
f3ef3495 sago007 2008-11-13 20:00 61
ecef5838 sago007 2015-11-24 20:01 62
Button::Button(const Button& b) {
b283e0f7 sago007 2012-07-29 14:01 63
	gfx = b.gfx;
c53e6443 sago007 2012-04-17 11:04 64
	label = b.label;
c53e6443 sago007 2012-04-17 11:04 65
	marked = b.marked;
c53e6443 sago007 2012-04-17 11:04 66
	action = b.action;
67a32395 sago007 2012-04-19 18:40 67
	popOnRun = false;
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::setLabel(const string& text) {
c53e6443 sago007 2012-04-17 11:04 71
	label = text;
f3ef3495 sago007 2008-11-13 20:00 72
}
f3ef3495 sago007 2008-11-13 20:00 73
ecef5838 sago007 2015-11-24 20:01 74
void Button::setAction(void (*action2run)(Button*)) {
c53e6443 sago007 2012-04-17 11:04 75
	action = action2run;
f3ef3495 sago007 2008-11-13 20:00 76
}
f3ef3495 sago007 2008-11-13 20:00 77
ecef5838 sago007 2015-11-24 20:01 78
bool Button::isClicked(int x,int y) {
6258a442 sago007 2015-11-14 22:11 79
	if ( x >= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) {
c53e6443 sago007 2012-04-17 11:04 80
		return true;
6258a442 sago007 2015-11-14 22:11 81
	}
6258a442 sago007 2015-11-14 22:11 82
	return false;
f3ef3495 sago007 2008-11-13 20:00 83
}
f3ef3495 sago007 2008-11-13 20:00 84
ecef5838 sago007 2015-11-24 20:01 85
void Button::doAction() {
6258a442 sago007 2015-11-14 22:11 86
	if (action) {
c53e6443 sago007 2012-04-17 11:04 87
		action(this);
6258a442 sago007 2015-11-14 22:11 88
		return;
6258a442 sago007 2015-11-14 22:11 89
	}
6258a442 sago007 2015-11-14 22:11 90
	cerr << "Warning: button \"" << label << "\" has no action assigned!";
f3ef3495 sago007 2008-11-13 20:00 91
}
f3ef3495 sago007 2008-11-13 20:00 92
0c1de203 sago007 2015-12-05 22:15 93
void Button::drawToScreen() {
c53e6443 sago007 2012-04-17 11:04 94
#if DEBUG
161a010c sago007 2016-05-06 14:00 95
	//cout << "Painting button: " << label << " at: " << x << "," << y << "\n";
c53e6443 sago007 2012-04-17 11:04 96
#endif
6258a442 sago007 2015-11-14 22:11 97
	if (marked) {
92ec708e sago007 2016-06-13 16:50 98
		spriteHolder->GetSprite(menu_marked).Draw(screen, SDL_GetTicks(), x, y);
6258a442 sago007 2015-11-14 22:11 99
	}
6258a442 sago007 2015-11-14 22:11 100
	else {
92ec708e sago007 2016-06-13 16:50 101
		spriteHolder->GetSprite(menu_unmarked).Draw(screen, SDL_GetTicks(), x, y);
6258a442 sago007 2015-11-14 22:11 102
	}
92ec708e sago007 2016-06-13 16:50 103
	
3a23aa5e sago007 2015-12-23 18:06 104
	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 105
}
f3ef3495 sago007 2008-11-13 20:00 106
ecef5838 sago007 2015-11-24 20:01 107
void Button::setPopOnRun(bool popOnRun) {
67a32395 sago007 2012-04-19 18:40 108
	this->popOnRun = popOnRun;
67a32395 sago007 2012-04-19 18:40 109
}
67a32395 sago007 2012-04-19 18:40 110
ecef5838 sago007 2015-11-24 20:01 111
bool Button::isPopOnRun() const {
67a32395 sago007 2012-04-19 18:40 112
	return popOnRun;
67a32395 sago007 2012-04-19 18:40 113
}
67a32395 sago007 2012-04-19 18:40 114
ecef5838 sago007 2015-11-24 20:01 115
void Button::setGfx(ButtonGfx* gfx) {
b283e0f7 sago007 2012-07-29 14:01 116
	this->gfx = gfx;
b283e0f7 sago007 2012-07-29 14:01 117
}
b283e0f7 sago007 2012-07-29 14:01 118
ecef5838 sago007 2015-11-24 20:01 119
int Button::getHeight() {
b283e0f7 sago007 2012-07-29 14:01 120
	return this->gfx->ysize;
b283e0f7 sago007 2012-07-29 14:01 121
}
b283e0f7 sago007 2012-07-29 14:01 122
ecef5838 sago007 2015-11-24 20:01 123
void Menu::drawSelf() {
f1708e5e sago007 2016-09-28 17:09 124
	DrawBackground(screen);
c53e6443 sago007 2012-04-17 11:04 125
	vector<Button*>::iterator it;
6258a442 sago007 2015-11-14 22:11 126
	for (it = buttons.begin(); it < buttons.end(); it++) {
0c1de203 sago007 2015-12-05 22:15 127
		(*it)->drawToScreen();
6258a442 sago007 2015-11-14 22:11 128
	}
0c1de203 sago007 2015-12-05 22:15 129
	exit.drawToScreen();
3a23aa5e sago007 2015-12-23 18:06 130
	standardButton.thefont->draw(screen, 50, 50, "%s", title.c_str());
1d2e2129 sago007 2015-12-23 15:41 131
	mouse.Draw(screen, SDL_GetTicks(), mousex, mousey);
f3ef3495 sago007 2008-11-13 20:00 132
}
f3ef3495 sago007 2008-11-13 20:00 133
f3ef3495 sago007 2008-11-13 20:00 134
ecef5838 sago007 2015-11-24 20:01 135
void Menu::placeButtons() {
c53e6443 sago007 2012-04-17 11:04 136
	int nextY = 100;
c53e6443 sago007 2012-04-17 11:04 137
	const int X = 50;
c53e6443 sago007 2012-04-17 11:04 138
	vector<Button*>::iterator it;
ecef5838 sago007 2015-11-24 20:01 139
	for (it = buttons.begin(); it < buttons.end(); it++) {
c53e6443 sago007 2012-04-17 11:04 140
		(*it)->x = X;
c53e6443 sago007 2012-04-17 11:04 141
		(*it)->y = nextY;
b283e0f7 sago007 2012-07-29 14:01 142
		nextY += (*it)->getHeight()+10;
c53e6443 sago007 2012-04-17 11:04 143
	}
c53e6443 sago007 2012-04-17 11:04 144
	exit.x = X;
c53e6443 sago007 2012-04-17 11:04 145
	exit.y = nextY;
f3ef3495 sago007 2008-11-13 20:00 146
}
f3ef3495 sago007 2008-11-13 20:00 147
ecef5838 sago007 2015-11-24 20:01 148
void Menu::addButton(Button* b) {
c53e6443 sago007 2012-04-17 11:04 149
	buttons.push_back(b);
c53e6443 sago007 2012-04-17 11:04 150
	b->marked = false;
c53e6443 sago007 2012-04-17 11:04 151
	placeButtons();
f3ef3495 sago007 2008-11-13 20:00 152
}
f3ef3495 sago007 2008-11-13 20:00 153
1d2e2129 sago007 2015-12-23 15:41 154
Menu::Menu(SDL_Renderer* screen) {
0c1de203 sago007 2015-12-05 22:15 155
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 156
	buttons = vector<Button*>(10);
c53e6443 sago007 2012-04-17 11:04 157
	isSubmenu = true;
c53e6443 sago007 2012-04-17 11:04 158
	exit.setLabel( _("Back") );
f3ef3495 sago007 2008-11-13 20:00 159
}
f3ef3495 sago007 2008-11-13 20:00 160
1d2e2129 sago007 2015-12-23 15:41 161
Menu::Menu(SDL_Renderer* screen,bool submenu) {
0c1de203 sago007 2015-12-05 22:15 162
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 163
	buttons = vector<Button*>(0);
c53e6443 sago007 2012-04-17 11:04 164
	isSubmenu = submenu;
ecef5838 sago007 2015-11-24 20:01 165
	if (isSubmenu) {
c53e6443 sago007 2012-04-17 11:04 166
		exit.setLabel( _("Back") );
acd79baf sago007 2015-11-22 19:37 167
	}
acd79baf sago007 2015-11-22 19:37 168
	else {
c53e6443 sago007 2012-04-17 11:04 169
		exit.setLabel( _("Exit") );
acd79baf sago007 2015-11-22 19:37 170
	}
f3ef3495 sago007 2008-11-13 20:00 171
}
f3ef3495 sago007 2008-11-13 20:00 172
1d2e2129 sago007 2015-12-23 15:41 173
Menu::Menu(SDL_Renderer* screen, const string& title, bool submenu) {
0c1de203 sago007 2015-12-05 22:15 174
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 175
	buttons = vector<Button*>(0);
c53e6443 sago007 2012-04-17 11:04 176
	isSubmenu = submenu;
c53e6443 sago007 2012-04-17 11:04 177
	this->title = title;
ecef5838 sago007 2015-11-24 20:01 178
	if (isSubmenu) {
c53e6443 sago007 2012-04-17 11:04 179
		exit.setLabel(_("Back") );
acd79baf sago007 2015-11-22 19:37 180
	}
acd79baf sago007 2015-11-22 19:37 181
	else {
c53e6443 sago007 2012-04-17 11:04 182
		exit.setLabel(_("Exit") );
acd79baf sago007 2015-11-22 19:37 183
	}
cc3ef158 sago007 2011-07-03 16:13 184
}
cc3ef158 sago007 2011-07-03 16:13 185
826cf176 sago007 2016-03-12 10:53 186
bool isUpEvent(const SDL_Event& event) {
826cf176 sago007 2016-03-12 10:53 187
	if ( event.type == SDL_KEYDOWN ) {
826cf176 sago007 2016-03-12 10:53 188
		if (event.key.keysym.sym == SDLK_UP) {
826cf176 sago007 2016-03-12 10:53 189
			return true;
826cf176 sago007 2016-03-12 10:53 190
		}
826cf176 sago007 2016-03-12 10:53 191
	}
826cf176 sago007 2016-03-12 10:53 192
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 193
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP ) {
826cf176 sago007 2016-03-12 10:53 194
			return true;
826cf176 sago007 2016-03-12 10:53 195
		}
826cf176 sago007 2016-03-12 10:53 196
	}
a077dceb sago007 2016-03-19 19:19 197
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 198
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 199
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 200
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 201
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 202
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 203
				return true;
a077dceb sago007 2016-03-19 19:19 204
			}
a077dceb sago007 2016-03-19 19:19 205
		}
a077dceb sago007 2016-03-19 19:19 206
	}
826cf176 sago007 2016-03-12 10:53 207
	return false;
826cf176 sago007 2016-03-12 10:53 208
}
826cf176 sago007 2016-03-12 10:53 209
d5efd7d1 sago007 2016-03-12 11:32 210
bool isDownEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 211
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 212
		if (event.key.keysym.sym == SDLK_DOWN) {
d5efd7d1 sago007 2016-03-12 11:32 213
			return true;
d5efd7d1 sago007 2016-03-12 11:32 214
		}
d5efd7d1 sago007 2016-03-12 11:32 215
	}
d5efd7d1 sago007 2016-03-12 11:32 216
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 217
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
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
	}
a077dceb sago007 2016-03-19 19:19 221
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 222
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 223
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 224
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 225
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 226
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 227
				return true;
a077dceb sago007 2016-03-19 19:19 228
			}
a077dceb sago007 2016-03-19 19:19 229
		}
a077dceb sago007 2016-03-19 19:19 230
	}
d21782d4 sago007 2016-03-13 11:17 231
	return false;
d21782d4 sago007 2016-03-13 11:17 232
}
d21782d4 sago007 2016-03-13 11:17 233
d21782d4 sago007 2016-03-13 11:17 234
bool isLeftEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 235
	if ( event.type == SDL_KEYDOWN ) {
d21782d4 sago007 2016-03-13 11:17 236
		if (event.key.keysym.sym == SDLK_LEFT) {
d21782d4 sago007 2016-03-13 11:17 237
			return true;
d21782d4 sago007 2016-03-13 11:17 238
		}
d21782d4 sago007 2016-03-13 11:17 239
	}
d21782d4 sago007 2016-03-13 11:17 240
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 241
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ) {
d21782d4 sago007 2016-03-13 11:17 242
			return true;
d21782d4 sago007 2016-03-13 11:17 243
		}
d21782d4 sago007 2016-03-13 11:17 244
	}
a077dceb sago007 2016-03-19 19:19 245
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 246
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 247
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 248
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 249
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 250
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 251
				return true;
a077dceb sago007 2016-03-19 19:19 252
			}
a077dceb sago007 2016-03-19 19:19 253
		}
a077dceb sago007 2016-03-19 19:19 254
	}
d21782d4 sago007 2016-03-13 11:17 255
	return false;
d21782d4 sago007 2016-03-13 11:17 256
}
d21782d4 sago007 2016-03-13 11:17 257
d21782d4 sago007 2016-03-13 11:17 258
bool isRightEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 259
	if ( event.type == SDL_KEYDOWN ) {
877b7c65 sago007 2016-04-09 09:32 260
		if (event.key.keysym.sym == SDLK_RIGHT) {
d21782d4 sago007 2016-03-13 11:17 261
			return true;
d21782d4 sago007 2016-03-13 11:17 262
		}
d21782d4 sago007 2016-03-13 11:17 263
	}
d21782d4 sago007 2016-03-13 11:17 264
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 265
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) {
d5efd7d1 sago007 2016-03-12 11:32 266
			return true;
d5efd7d1 sago007 2016-03-12 11:32 267
		}
d5efd7d1 sago007 2016-03-12 11:32 268
	}
a077dceb sago007 2016-03-19 19:19 269
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 270
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 271
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 272
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 273
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 274
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 275
				return true;
a077dceb sago007 2016-03-19 19:19 276
			}
a077dceb sago007 2016-03-19 19:19 277
		}
a077dceb sago007 2016-03-19 19:19 278
	}
d5efd7d1 sago007 2016-03-12 11:32 279
	return false;
d5efd7d1 sago007 2016-03-12 11:32 280
}
d5efd7d1 sago007 2016-03-12 11:32 281
d5efd7d1 sago007 2016-03-12 11:32 282
bool isEscapeEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 283
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 284
		if ( event.key.keysym.sym == SDLK_ESCAPE ) {
d5efd7d1 sago007 2016-03-12 11:32 285
			return true;
d5efd7d1 sago007 2016-03-12 11:32 286
		}
d5efd7d1 sago007 2016-03-12 11:32 287
	}
d5efd7d1 sago007 2016-03-12 11:32 288
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d5efd7d1 sago007 2016-03-12 11:32 289
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_Y ) {
d5efd7d1 sago007 2016-03-12 11:32 290
			return true;
d5efd7d1 sago007 2016-03-12 11:32 291
		}
d5efd7d1 sago007 2016-03-12 11:32 292
	}
d5efd7d1 sago007 2016-03-12 11:32 293
	return false;
d5efd7d1 sago007 2016-03-12 11:32 294
}
d5efd7d1 sago007 2016-03-12 11:32 295
d5efd7d1 sago007 2016-03-12 11:32 296
bool isConfirmEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 297
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 298
		if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
d5efd7d1 sago007 2016-03-12 11:32 299
			return true;
d5efd7d1 sago007 2016-03-12 11:32 300
		}
d5efd7d1 sago007 2016-03-12 11:32 301
	}
d5efd7d1 sago007 2016-03-12 11:32 302
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d5efd7d1 sago007 2016-03-12 11:32 303
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
d5efd7d1 sago007 2016-03-12 11:32 304
			return true;
d5efd7d1 sago007 2016-03-12 11:32 305
		}
d5efd7d1 sago007 2016-03-12 11:32 306
	}
d5efd7d1 sago007 2016-03-12 11:32 307
	return false;
d5efd7d1 sago007 2016-03-12 11:32 308
}
d5efd7d1 sago007 2016-03-12 11:32 309
ecef5838 sago007 2015-11-24 20:01 310
void Menu::run() {
c53e6443 sago007 2012-04-17 11:04 311
	running = true;
c53e6443 sago007 2012-04-17 11:04 312
	bool bMouseUp = false;
c53e6443 sago007 2012-04-17 11:04 313
	long oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 314
	long oldmousey = mousey;
ecef5838 sago007 2015-11-24 20:01 315
	while (running && !Config::getInstance()->isShuttingDown()) {
acd79baf sago007 2015-11-22 19:37 316
		if (!(highPriority)) {
acd79baf sago007 2015-11-22 19:37 317
			SDL_Delay(10);
acd79baf sago007 2015-11-22 19:37 318
		}
c53e6443 sago007 2012-04-17 11:04 319
c53e6443 sago007 2012-04-17 11:04 320
c53e6443 sago007 2012-04-17 11:04 321
		SDL_Event event;
c53e6443 sago007 2012-04-17 11:04 322
ecef5838 sago007 2015-11-24 20:01 323
		while ( SDL_PollEvent(&event) ) {
ecef5838 sago007 2015-11-24 20:01 324
			if ( event.type == SDL_QUIT ) {
c53e6443 sago007 2012-04-17 11:04 325
				Config::getInstance()->setShuttingDown(5);
c53e6443 sago007 2012-04-17 11:04 326
				running = false;
c53e6443 sago007 2012-04-17 11:04 327
			}
c7f2082f sago007 2016-03-13 11:48 328
826cf176 sago007 2016-03-12 10:53 329
			if (isUpEvent(event)) {
826cf176 sago007 2016-03-12 10:53 330
				marked--;
826cf176 sago007 2016-03-12 10:53 331
				if (marked<0) {
826cf176 sago007 2016-03-12 10:53 332
					marked = buttons.size();    //not -1, since exit is after the last element in the list
826cf176 sago007 2016-03-12 10:53 333
				}
826cf176 sago007 2016-03-12 10:53 334
			}
c7f2082f sago007 2016-03-13 11:48 335
d5efd7d1 sago007 2016-03-12 11:32 336
			if (isDownEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 337
				marked++;
d5efd7d1 sago007 2016-03-12 11:32 338
				if (marked> (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 339
					marked = 0;
c53e6443 sago007 2012-04-17 11:04 340
				}
c53e6443 sago007 2012-04-17 11:04 341
			}
c7f2082f sago007 2016-03-13 11:48 342
d21782d4 sago007 2016-03-13 11:17 343
			if (isEscapeEvent(event) && isSubmenu) {
d5efd7d1 sago007 2016-03-12 11:32 344
				running = false;
d5efd7d1 sago007 2016-03-12 11:32 345
			}
c7f2082f sago007 2016-03-13 11:48 346
d5efd7d1 sago007 2016-03-12 11:32 347
			if (isConfirmEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 348
				if (marked < (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 349
					buttons.at(marked)->doAction();
d5efd7d1 sago007 2016-03-12 11:32 350
					if (buttons.at(marked)->isPopOnRun()) {
826cf176 sago007 2016-03-12 10:53 351
						running = false;
826cf176 sago007 2016-03-12 10:53 352
					}
826cf176 sago007 2016-03-12 10:53 353
				}
d5efd7d1 sago007 2016-03-12 11:32 354
				if (marked == (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 355
					running = false;
d5efd7d1 sago007 2016-03-12 11:32 356
				}
826cf176 sago007 2016-03-12 10:53 357
			}
c53e6443 sago007 2012-04-17 11:04 358
c53e6443 sago007 2012-04-17 11:04 359
		}
c53e6443 sago007 2012-04-17 11:04 360
daa8f1cf sago007 2016-01-23 10:57 361
		for (int i=0; i<(int)buttons.size(); i++) {
c53e6443 sago007 2012-04-17 11:04 362
			buttons.at(i)->marked = (i == marked);
c53e6443 sago007 2012-04-17 11:04 363
		}
daa8f1cf sago007 2016-01-23 10:57 364
		exit.marked = (marked == (int)buttons.size());
c53e6443 sago007 2012-04-17 11:04 365
		Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey);
c53e6443 sago007 2012-04-17 11:04 366
		// If the mouse button is released, make bMouseUp equal true
ecef5838 sago007 2015-11-24 20:01 367
		if ( (buttonState&SDL_BUTTON(1))==0) {
c53e6443 sago007 2012-04-17 11:04 368
			bMouseUp=true;
c53e6443 sago007 2012-04-17 11:04 369
		}
c53e6443 sago007 2012-04-17 11:04 370
ecef5838 sago007 2015-11-24 20:01 371
		if (abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) {
daa8f1cf sago007 2016-01-23 10:57 372
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 373
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 374
					marked = i;
c53e6443 sago007 2012-04-17 11:04 375
				}
c53e6443 sago007 2012-04-17 11:04 376
			}
ecef5838 sago007 2015-11-24 20:01 377
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 378
				marked = buttons.size();
c53e6443 sago007 2012-04-17 11:04 379
			}
c53e6443 sago007 2012-04-17 11:04 380
			oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 381
			oldmousey = mousey;
c53e6443 sago007 2012-04-17 11:04 382
		}
c53e6443 sago007 2012-04-17 11:04 383
c53e6443 sago007 2012-04-17 11:04 384
		//mouse clicked
ecef5838 sago007 2015-11-24 20:01 385
		if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) {
c53e6443 sago007 2012-04-17 11:04 386
			bMouseUp = false;
daa8f1cf sago007 2016-01-23 10:57 387
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 388
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 389
					buttons.at(i)->doAction();
ecef5838 sago007 2015-11-24 20:01 390
					if (buttons.at(i)->isPopOnRun()) {
67a32395 sago007 2012-04-19 18:40 391
						running = false;
acd79baf sago007 2015-11-22 19:37 392
					}
67a32395 sago007 2012-04-19 18:40 393
					mousex = 0;
c53e6443 sago007 2012-04-17 11:04 394
				}
c53e6443 sago007 2012-04-17 11:04 395
			}
ecef5838 sago007 2015-11-24 20:01 396
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 397
				running = false;
c53e6443 sago007 2012-04-17 11:04 398
			}
c53e6443 sago007 2012-04-17 11:04 399
		}
c53e6443 sago007 2012-04-17 11:04 400
c53e6443 sago007 2012-04-17 11:04 401
		drawSelf();
1d2e2129 sago007 2015-12-23 15:41 402
		SDL_RenderPresent(screen);
c53e6443 sago007 2012-04-17 11:04 403
	}
f3ef3495 sago007 2008-11-13 20:00 404
}
1970-01-01 00:00 405