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() {
094b2dfe sago007 2016-06-12 10:50 44
	xsize = spriteHolder->GetSprite(menu_marked).GetWidth();
094b2dfe sago007 2016-06-12 10:50 45
	ysize = spriteHolder->GetSprite(menu_marked).GetHeight();
ecef5838 sago007 2015-11-24 20:01 46
	if (verboseLevel) {
161a010c sago007 2016-05-06 14:00 47
		cout << "Surfaces set, size: " <<xsize << " , " << 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() {
b539639c sago007 2015-12-29 11:51 124
	SDL_RenderClear(screen);
1d2e2129 sago007 2015-12-23 15:41 125
	backgroundImage.Draw(screen, SDL_GetTicks(), 0, 0);
c53e6443 sago007 2012-04-17 11:04 126
	vector<Button*>::iterator it;
6258a442 sago007 2015-11-14 22:11 127
	for (it = buttons.begin(); it < buttons.end(); it++) {
0c1de203 sago007 2015-12-05 22:15 128
		(*it)->drawToScreen();
6258a442 sago007 2015-11-14 22:11 129
	}
0c1de203 sago007 2015-12-05 22:15 130
	exit.drawToScreen();
3a23aa5e sago007 2015-12-23 18:06 131
	standardButton.thefont->draw(screen, 50, 50, "%s", title.c_str());
1d2e2129 sago007 2015-12-23 15:41 132
	mouse.Draw(screen, SDL_GetTicks(), mousex, mousey);
f3ef3495 sago007 2008-11-13 20:00 133
}
f3ef3495 sago007 2008-11-13 20:00 134
f3ef3495 sago007 2008-11-13 20:00 135
ecef5838 sago007 2015-11-24 20:01 136
void Menu::placeButtons() {
c53e6443 sago007 2012-04-17 11:04 137
	int nextY = 100;
c53e6443 sago007 2012-04-17 11:04 138
	const int X = 50;
c53e6443 sago007 2012-04-17 11:04 139
	vector<Button*>::iterator it;
ecef5838 sago007 2015-11-24 20:01 140
	for (it = buttons.begin(); it < buttons.end(); it++) {
c53e6443 sago007 2012-04-17 11:04 141
		(*it)->x = X;
c53e6443 sago007 2012-04-17 11:04 142
		(*it)->y = nextY;
b283e0f7 sago007 2012-07-29 14:01 143
		nextY += (*it)->getHeight()+10;
c53e6443 sago007 2012-04-17 11:04 144
	}
c53e6443 sago007 2012-04-17 11:04 145
	exit.x = X;
c53e6443 sago007 2012-04-17 11:04 146
	exit.y = nextY;
f3ef3495 sago007 2008-11-13 20:00 147
}
f3ef3495 sago007 2008-11-13 20:00 148
ecef5838 sago007 2015-11-24 20:01 149
void Menu::addButton(Button* b) {
c53e6443 sago007 2012-04-17 11:04 150
	buttons.push_back(b);
c53e6443 sago007 2012-04-17 11:04 151
	b->marked = false;
c53e6443 sago007 2012-04-17 11:04 152
	placeButtons();
f3ef3495 sago007 2008-11-13 20:00 153
}
f3ef3495 sago007 2008-11-13 20:00 154
1d2e2129 sago007 2015-12-23 15:41 155
Menu::Menu(SDL_Renderer* screen) {
0c1de203 sago007 2015-12-05 22:15 156
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 157
	buttons = vector<Button*>(10);
c53e6443 sago007 2012-04-17 11:04 158
	isSubmenu = true;
c53e6443 sago007 2012-04-17 11:04 159
	exit.setLabel( _("Back") );
f3ef3495 sago007 2008-11-13 20:00 160
}
f3ef3495 sago007 2008-11-13 20:00 161
1d2e2129 sago007 2015-12-23 15:41 162
Menu::Menu(SDL_Renderer* screen,bool submenu) {
0c1de203 sago007 2015-12-05 22:15 163
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 164
	buttons = vector<Button*>(0);
c53e6443 sago007 2012-04-17 11:04 165
	isSubmenu = submenu;
ecef5838 sago007 2015-11-24 20:01 166
	if (isSubmenu) {
c53e6443 sago007 2012-04-17 11:04 167
		exit.setLabel( _("Back") );
acd79baf sago007 2015-11-22 19:37 168
	}
acd79baf sago007 2015-11-22 19:37 169
	else {
c53e6443 sago007 2012-04-17 11:04 170
		exit.setLabel( _("Exit") );
acd79baf sago007 2015-11-22 19:37 171
	}
f3ef3495 sago007 2008-11-13 20:00 172
}
f3ef3495 sago007 2008-11-13 20:00 173
1d2e2129 sago007 2015-12-23 15:41 174
Menu::Menu(SDL_Renderer* screen, const string& title, bool submenu) {
0c1de203 sago007 2015-12-05 22:15 175
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 176
	buttons = vector<Button*>(0);
c53e6443 sago007 2012-04-17 11:04 177
	isSubmenu = submenu;
c53e6443 sago007 2012-04-17 11:04 178
	this->title = title;
ecef5838 sago007 2015-11-24 20:01 179
	if (isSubmenu) {
c53e6443 sago007 2012-04-17 11:04 180
		exit.setLabel(_("Back") );
acd79baf sago007 2015-11-22 19:37 181
	}
acd79baf sago007 2015-11-22 19:37 182
	else {
c53e6443 sago007 2012-04-17 11:04 183
		exit.setLabel(_("Exit") );
acd79baf sago007 2015-11-22 19:37 184
	}
cc3ef158 sago007 2011-07-03 16:13 185
}
cc3ef158 sago007 2011-07-03 16:13 186
826cf176 sago007 2016-03-12 10:53 187
bool isUpEvent(const SDL_Event& event) {
826cf176 sago007 2016-03-12 10:53 188
	if ( event.type == SDL_KEYDOWN ) {
826cf176 sago007 2016-03-12 10:53 189
		if (event.key.keysym.sym == SDLK_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
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 194
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP ) {
826cf176 sago007 2016-03-12 10:53 195
			return true;
826cf176 sago007 2016-03-12 10:53 196
		}
826cf176 sago007 2016-03-12 10:53 197
	}
a077dceb sago007 2016-03-19 19:19 198
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 199
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 200
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 201
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 202
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 203
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 204
				return true;
a077dceb sago007 2016-03-19 19:19 205
			}
a077dceb sago007 2016-03-19 19:19 206
		}
a077dceb sago007 2016-03-19 19:19 207
	}
826cf176 sago007 2016-03-12 10:53 208
	return false;
826cf176 sago007 2016-03-12 10:53 209
}
826cf176 sago007 2016-03-12 10:53 210
d5efd7d1 sago007 2016-03-12 11:32 211
bool isDownEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 212
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 213
		if (event.key.keysym.sym == SDLK_DOWN) {
d5efd7d1 sago007 2016-03-12 11:32 214
			return true;
d5efd7d1 sago007 2016-03-12 11:32 215
		}
d5efd7d1 sago007 2016-03-12 11:32 216
	}
d5efd7d1 sago007 2016-03-12 11:32 217
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 218
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
d21782d4 sago007 2016-03-13 11:17 219
			return true;
d21782d4 sago007 2016-03-13 11:17 220
		}
d21782d4 sago007 2016-03-13 11:17 221
	}
a077dceb sago007 2016-03-19 19:19 222
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 223
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 224
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 225
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 226
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 227
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 228
				return true;
a077dceb sago007 2016-03-19 19:19 229
			}
a077dceb sago007 2016-03-19 19:19 230
		}
a077dceb sago007 2016-03-19 19:19 231
	}
d21782d4 sago007 2016-03-13 11:17 232
	return false;
d21782d4 sago007 2016-03-13 11:17 233
}
d21782d4 sago007 2016-03-13 11:17 234
d21782d4 sago007 2016-03-13 11:17 235
bool isLeftEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 236
	if ( event.type == SDL_KEYDOWN ) {
d21782d4 sago007 2016-03-13 11:17 237
		if (event.key.keysym.sym == SDLK_LEFT) {
d21782d4 sago007 2016-03-13 11:17 238
			return true;
d21782d4 sago007 2016-03-13 11:17 239
		}
d21782d4 sago007 2016-03-13 11:17 240
	}
d21782d4 sago007 2016-03-13 11:17 241
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 242
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ) {
d21782d4 sago007 2016-03-13 11:17 243
			return true;
d21782d4 sago007 2016-03-13 11:17 244
		}
d21782d4 sago007 2016-03-13 11:17 245
	}
a077dceb sago007 2016-03-19 19:19 246
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 247
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 248
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 249
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 250
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 251
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 252
				return true;
a077dceb sago007 2016-03-19 19:19 253
			}
a077dceb sago007 2016-03-19 19:19 254
		}
a077dceb sago007 2016-03-19 19:19 255
	}
d21782d4 sago007 2016-03-13 11:17 256
	return false;
d21782d4 sago007 2016-03-13 11:17 257
}
d21782d4 sago007 2016-03-13 11:17 258
d21782d4 sago007 2016-03-13 11:17 259
bool isRightEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 260
	if ( event.type == SDL_KEYDOWN ) {
877b7c65 sago007 2016-04-09 09:32 261
		if (event.key.keysym.sym == SDLK_RIGHT) {
d21782d4 sago007 2016-03-13 11:17 262
			return true;
d21782d4 sago007 2016-03-13 11:17 263
		}
d21782d4 sago007 2016-03-13 11:17 264
	}
d21782d4 sago007 2016-03-13 11:17 265
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 266
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) {
d5efd7d1 sago007 2016-03-12 11:32 267
			return true;
d5efd7d1 sago007 2016-03-12 11:32 268
		}
d5efd7d1 sago007 2016-03-12 11:32 269
	}
a077dceb sago007 2016-03-19 19:19 270
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 271
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 272
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 273
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 274
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 275
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 276
				return true;
a077dceb sago007 2016-03-19 19:19 277
			}
a077dceb sago007 2016-03-19 19:19 278
		}
a077dceb sago007 2016-03-19 19:19 279
	}
d5efd7d1 sago007 2016-03-12 11:32 280
	return false;
d5efd7d1 sago007 2016-03-12 11:32 281
}
d5efd7d1 sago007 2016-03-12 11:32 282
d5efd7d1 sago007 2016-03-12 11:32 283
bool isEscapeEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 284
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 285
		if ( event.key.keysym.sym == SDLK_ESCAPE ) {
d5efd7d1 sago007 2016-03-12 11:32 286
			return true;
d5efd7d1 sago007 2016-03-12 11:32 287
		}
d5efd7d1 sago007 2016-03-12 11:32 288
	}
d5efd7d1 sago007 2016-03-12 11:32 289
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d5efd7d1 sago007 2016-03-12 11:32 290
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_Y ) {
d5efd7d1 sago007 2016-03-12 11:32 291
			return true;
d5efd7d1 sago007 2016-03-12 11:32 292
		}
d5efd7d1 sago007 2016-03-12 11:32 293
	}
d5efd7d1 sago007 2016-03-12 11:32 294
	return false;
d5efd7d1 sago007 2016-03-12 11:32 295
}
d5efd7d1 sago007 2016-03-12 11:32 296
d5efd7d1 sago007 2016-03-12 11:32 297
bool isConfirmEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 298
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 299
		if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
d5efd7d1 sago007 2016-03-12 11:32 300
			return true;
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 (event.type == SDL_CONTROLLERBUTTONDOWN) {
d5efd7d1 sago007 2016-03-12 11:32 304
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
d5efd7d1 sago007 2016-03-12 11:32 305
			return true;
d5efd7d1 sago007 2016-03-12 11:32 306
		}
d5efd7d1 sago007 2016-03-12 11:32 307
	}
d5efd7d1 sago007 2016-03-12 11:32 308
	return false;
d5efd7d1 sago007 2016-03-12 11:32 309
}
d5efd7d1 sago007 2016-03-12 11:32 310
ecef5838 sago007 2015-11-24 20:01 311
void Menu::run() {
c53e6443 sago007 2012-04-17 11:04 312
	running = true;
c53e6443 sago007 2012-04-17 11:04 313
	bool bMouseUp = false;
c53e6443 sago007 2012-04-17 11:04 314
	long oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 315
	long oldmousey = mousey;
ecef5838 sago007 2015-11-24 20:01 316
	while (running && !Config::getInstance()->isShuttingDown()) {
acd79baf sago007 2015-11-22 19:37 317
		if (!(highPriority)) {
acd79baf sago007 2015-11-22 19:37 318
			SDL_Delay(10);
acd79baf sago007 2015-11-22 19:37 319
		}
c53e6443 sago007 2012-04-17 11:04 320
c53e6443 sago007 2012-04-17 11:04 321
c53e6443 sago007 2012-04-17 11:04 322
		SDL_Event event;
c53e6443 sago007 2012-04-17 11:04 323
ecef5838 sago007 2015-11-24 20:01 324
		while ( SDL_PollEvent(&event) ) {
ecef5838 sago007 2015-11-24 20:01 325
			if ( event.type == SDL_QUIT ) {
c53e6443 sago007 2012-04-17 11:04 326
				Config::getInstance()->setShuttingDown(5);
c53e6443 sago007 2012-04-17 11:04 327
				running = false;
c53e6443 sago007 2012-04-17 11:04 328
			}
c7f2082f sago007 2016-03-13 11:48 329
826cf176 sago007 2016-03-12 10:53 330
			if (isUpEvent(event)) {
826cf176 sago007 2016-03-12 10:53 331
				marked--;
826cf176 sago007 2016-03-12 10:53 332
				if (marked<0) {
826cf176 sago007 2016-03-12 10:53 333
					marked = buttons.size();    //not -1, since exit is after the last element in the list
826cf176 sago007 2016-03-12 10:53 334
				}
826cf176 sago007 2016-03-12 10:53 335
			}
c7f2082f sago007 2016-03-13 11:48 336
d5efd7d1 sago007 2016-03-12 11:32 337
			if (isDownEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 338
				marked++;
d5efd7d1 sago007 2016-03-12 11:32 339
				if (marked> (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 340
					marked = 0;
c53e6443 sago007 2012-04-17 11:04 341
				}
c53e6443 sago007 2012-04-17 11:04 342
			}
c7f2082f sago007 2016-03-13 11:48 343
d21782d4 sago007 2016-03-13 11:17 344
			if (isEscapeEvent(event) && isSubmenu) {
d5efd7d1 sago007 2016-03-12 11:32 345
				running = false;
d5efd7d1 sago007 2016-03-12 11:32 346
			}
c7f2082f sago007 2016-03-13 11:48 347
d5efd7d1 sago007 2016-03-12 11:32 348
			if (isConfirmEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 349
				if (marked < (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 350
					buttons.at(marked)->doAction();
d5efd7d1 sago007 2016-03-12 11:32 351
					if (buttons.at(marked)->isPopOnRun()) {
826cf176 sago007 2016-03-12 10:53 352
						running = false;
826cf176 sago007 2016-03-12 10:53 353
					}
826cf176 sago007 2016-03-12 10:53 354
				}
d5efd7d1 sago007 2016-03-12 11:32 355
				if (marked == (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 356
					running = false;
d5efd7d1 sago007 2016-03-12 11:32 357
				}
826cf176 sago007 2016-03-12 10:53 358
			}
c53e6443 sago007 2012-04-17 11:04 359
c53e6443 sago007 2012-04-17 11:04 360
		}
c53e6443 sago007 2012-04-17 11:04 361
daa8f1cf sago007 2016-01-23 10:57 362
		for (int i=0; i<(int)buttons.size(); i++) {
c53e6443 sago007 2012-04-17 11:04 363
			buttons.at(i)->marked = (i == marked);
c53e6443 sago007 2012-04-17 11:04 364
		}
daa8f1cf sago007 2016-01-23 10:57 365
		exit.marked = (marked == (int)buttons.size());
c53e6443 sago007 2012-04-17 11:04 366
		Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey);
c53e6443 sago007 2012-04-17 11:04 367
		// If the mouse button is released, make bMouseUp equal true
ecef5838 sago007 2015-11-24 20:01 368
		if ( (buttonState&SDL_BUTTON(1))==0) {
c53e6443 sago007 2012-04-17 11:04 369
			bMouseUp=true;
c53e6443 sago007 2012-04-17 11:04 370
		}
c53e6443 sago007 2012-04-17 11:04 371
ecef5838 sago007 2015-11-24 20:01 372
		if (abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) {
daa8f1cf sago007 2016-01-23 10:57 373
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 374
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 375
					marked = i;
c53e6443 sago007 2012-04-17 11:04 376
				}
c53e6443 sago007 2012-04-17 11:04 377
			}
ecef5838 sago007 2015-11-24 20:01 378
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 379
				marked = buttons.size();
c53e6443 sago007 2012-04-17 11:04 380
			}
c53e6443 sago007 2012-04-17 11:04 381
			oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 382
			oldmousey = mousey;
c53e6443 sago007 2012-04-17 11:04 383
		}
c53e6443 sago007 2012-04-17 11:04 384
c53e6443 sago007 2012-04-17 11:04 385
		//mouse clicked
ecef5838 sago007 2015-11-24 20:01 386
		if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) {
c53e6443 sago007 2012-04-17 11:04 387
			bMouseUp = false;
daa8f1cf sago007 2016-01-23 10:57 388
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 389
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 390
					buttons.at(i)->doAction();
ecef5838 sago007 2015-11-24 20:01 391
					if (buttons.at(i)->isPopOnRun()) {
67a32395 sago007 2012-04-19 18:40 392
						running = false;
acd79baf sago007 2015-11-22 19:37 393
					}
67a32395 sago007 2012-04-19 18:40 394
					mousex = 0;
c53e6443 sago007 2012-04-17 11:04 395
				}
c53e6443 sago007 2012-04-17 11:04 396
			}
ecef5838 sago007 2015-11-24 20:01 397
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 398
				running = false;
c53e6443 sago007 2012-04-17 11:04 399
			}
c53e6443 sago007 2012-04-17 11:04 400
		}
c53e6443 sago007 2012-04-17 11:04 401
c53e6443 sago007 2012-04-17 11:04 402
		drawSelf();
1d2e2129 sago007 2015-12-23 15:41 403
		SDL_RenderPresent(screen);
c53e6443 sago007 2012-04-17 11:04 404
	}
f3ef3495 sago007 2008-11-13 20:00 405
}
1970-01-01 00:00 406