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