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"
c0a3362c sago007 2016-10-07 20:23 29
#include "BlockGame.hpp"
f3ef3495 sago007 2008-11-13 20:00 30
0c201300 sago007 2016-02-20 12:12 31
static int mousex;
0c201300 sago007 2016-02-20 12:12 32
static int mousey;
f3ef3495 sago007 2008-11-13 20:00 33
96eb7a16 sago007 2016-04-29 17:01 34
using std::string;
96eb7a16 sago007 2016-04-29 17:01 35
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 36
using std::cout;
96eb7a16 sago007 2016-04-29 17:01 37
using std::vector;
33f4e975 sago007 2015-11-14 21:49 38
094b2dfe sago007 2016-06-12 10:50 39
const char* const menu_marked = "menu_marked";
094b2dfe sago007 2016-06-12 10:50 40
const char* const menu_unmarked = "menu_unmarked";
f3ef3495 sago007 2008-11-13 20:00 41
b283e0f7 sago007 2012-07-29 14:01 42
ButtonGfx standardButton;
f3ef3495 sago007 2008-11-13 20:00 43
094b2dfe sago007 2016-06-12 10:50 44
void ButtonGfx::setSurfaces() {
13476fc2 sago007 2016-09-28 18:34 45
	this->xsize = spriteHolder->GetSprite(menu_marked).GetWidth();
13476fc2 sago007 2016-09-28 18:34 46
	this->ysize = spriteHolder->GetSprite(menu_marked).GetHeight();
ecef5838 sago007 2015-11-24 20:01 47
	if (verboseLevel) {
13476fc2 sago007 2016-09-28 18:34 48
		cout << "Surfaces set, size: " << this->xsize << " , " << this->ysize << "\n";
acd79baf sago007 2015-11-22 19:37 49
	}
f3ef3495 sago007 2008-11-13 20:00 50
}
f3ef3495 sago007 2008-11-13 20:00 51
ecef5838 sago007 2015-11-24 20:01 52
Button::Button() {
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) {
c53e6443 sago007 2012-04-17 11:04 63
	label = b.label;
c53e6443 sago007 2012-04-17 11:04 64
	marked = b.marked;
c53e6443 sago007 2012-04-17 11:04 65
	action = b.action;
67a32395 sago007 2012-04-19 18:40 66
	popOnRun = false;
f3ef3495 sago007 2008-11-13 20:00 67
}
f3ef3495 sago007 2008-11-13 20:00 68
ecef5838 sago007 2015-11-24 20:01 69
void Button::setLabel(const string& text) {
c53e6443 sago007 2012-04-17 11:04 70
	label = text;
f3ef3495 sago007 2008-11-13 20:00 71
}
f3ef3495 sago007 2008-11-13 20:00 72
ecef5838 sago007 2015-11-24 20:01 73
void Button::setAction(void (*action2run)(Button*)) {
c53e6443 sago007 2012-04-17 11:04 74
	action = action2run;
f3ef3495 sago007 2008-11-13 20:00 75
}
f3ef3495 sago007 2008-11-13 20:00 76
ecef5838 sago007 2015-11-24 20:01 77
bool Button::isClicked(int x,int y) {
c0a3362c sago007 2016-10-07 20:23 78
	if ( x >= this->x && y >= this->y && x<= this->x+standardButton.xsize && y <= this->y + standardButton.ysize) {
c53e6443 sago007 2012-04-17 11:04 79
		return true;
6258a442 sago007 2015-11-14 22:11 80
	}
6258a442 sago007 2015-11-14 22:11 81
	return false;
f3ef3495 sago007 2008-11-13 20:00 82
}
f3ef3495 sago007 2008-11-13 20:00 83
ecef5838 sago007 2015-11-24 20:01 84
void Button::doAction() {
6258a442 sago007 2015-11-14 22:11 85
	if (action) {
c53e6443 sago007 2012-04-17 11:04 86
		action(this);
6258a442 sago007 2015-11-14 22:11 87
		return;
6258a442 sago007 2015-11-14 22:11 88
	}
6258a442 sago007 2015-11-14 22:11 89
	cerr << "Warning: button \"" << label << "\" has no action assigned!";
f3ef3495 sago007 2008-11-13 20:00 90
}
f3ef3495 sago007 2008-11-13 20:00 91
ecef5838 sago007 2015-11-24 20:01 92
void Button::setPopOnRun(bool popOnRun) {
67a32395 sago007 2012-04-19 18:40 93
	this->popOnRun = popOnRun;
67a32395 sago007 2012-04-19 18:40 94
}
67a32395 sago007 2012-04-19 18:40 95
ecef5838 sago007 2015-11-24 20:01 96
bool Button::isPopOnRun() const {
67a32395 sago007 2012-04-19 18:40 97
	return popOnRun;
67a32395 sago007 2012-04-19 18:40 98
}
67a32395 sago007 2012-04-19 18:40 99
b283e0f7 sago007 2012-07-29 14:01 100
1b710a1e sago007 2016-09-28 19:01 101
int Button::getHeight() const {
c0a3362c sago007 2016-10-07 20:23 102
	return standardButton.ysize;
b283e0f7 sago007 2012-07-29 14:01 103
}
b283e0f7 sago007 2012-07-29 14:01 104
1b710a1e sago007 2016-09-28 19:01 105
int Button::getWidth() const {
c0a3362c sago007 2016-10-07 20:23 106
	return standardButton.xsize;
c0a3362c sago007 2016-10-07 20:23 107
}
c0a3362c sago007 2016-10-07 20:23 108
a1e83d35 sago007 2016-10-08 17:29 109
static void drawToScreen(const Button &b) {
c0a3362c sago007 2016-10-07 20:23 110
	if (b.marked) {
c0a3362c sago007 2016-10-07 20:23 111
		spriteHolder->GetSprite(menu_marked).Draw(screen, SDL_GetTicks(), b.x, b.y);
c0a3362c sago007 2016-10-07 20:23 112
	}
c0a3362c sago007 2016-10-07 20:23 113
	else {
c0a3362c sago007 2016-10-07 20:23 114
		spriteHolder->GetSprite(menu_unmarked).Draw(screen, SDL_GetTicks(), b.x, b.y);
c0a3362c sago007 2016-10-07 20:23 115
	}
c0a3362c sago007 2016-10-07 20:23 116
	
c0a3362c sago007 2016-10-07 20:23 117
	standardButton.thefont->draw(screen, b.x+standardButton.xsize/2,b.y+standardButton.ysize/2-standardButton.thefont->getHeight("%s",  b.label.c_str())/2, NFont::CENTER, "%s", b.label.c_str());
1b710a1e sago007 2016-09-28 19:01 118
}
1b710a1e sago007 2016-09-28 19:01 119
ecef5838 sago007 2015-11-24 20:01 120
void Menu::drawSelf() {
f1708e5e sago007 2016-09-28 17:09 121
	DrawBackground(screen);
a1e83d35 sago007 2016-10-08 17:29 122
	for (const Button* b : buttons) {
a1e83d35 sago007 2016-10-08 17:29 123
		drawToScreen(*b);
6258a442 sago007 2015-11-14 22:11 124
	}
c0a3362c sago007 2016-10-07 20:23 125
	drawToScreen(exit);
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;
1b710a1e sago007 2016-09-28 19:01 133
	int X = 50;
1b710a1e sago007 2016-09-28 19:01 134
	for (Button* it : buttons) {
1b710a1e sago007 2016-09-28 19:01 135
		X = (xsize - it->getWidth())/2;
1b710a1e sago007 2016-09-28 19:01 136
		it->x = X;
1b710a1e sago007 2016-09-28 19:01 137
		it->y = nextY;
1b710a1e sago007 2016-09-28 19: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
	}
a077dceb sago007 2016-03-19 19:19 193
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 194
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 195
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 196
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 197
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 198
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 199
				return true;
a077dceb sago007 2016-03-19 19:19 200
			}
a077dceb sago007 2016-03-19 19:19 201
		}
a077dceb sago007 2016-03-19 19:19 202
	}
826cf176 sago007 2016-03-12 10:53 203
	return false;
826cf176 sago007 2016-03-12 10:53 204
}
826cf176 sago007 2016-03-12 10:53 205
d5efd7d1 sago007 2016-03-12 11:32 206
bool isDownEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 207
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 208
		if (event.key.keysym.sym == SDLK_DOWN) {
d5efd7d1 sago007 2016-03-12 11:32 209
			return true;
d5efd7d1 sago007 2016-03-12 11:32 210
		}
d5efd7d1 sago007 2016-03-12 11:32 211
	}
d5efd7d1 sago007 2016-03-12 11:32 212
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 213
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
d21782d4 sago007 2016-03-13 11:17 214
			return true;
d21782d4 sago007 2016-03-13 11:17 215
		}
d21782d4 sago007 2016-03-13 11:17 216
	}
a077dceb sago007 2016-03-19 19:19 217
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 218
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 219
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 220
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 221
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 222
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 223
				return true;
a077dceb sago007 2016-03-19 19:19 224
			}
a077dceb sago007 2016-03-19 19:19 225
		}
a077dceb sago007 2016-03-19 19:19 226
	}
d21782d4 sago007 2016-03-13 11:17 227
	return false;
d21782d4 sago007 2016-03-13 11:17 228
}
d21782d4 sago007 2016-03-13 11:17 229
d21782d4 sago007 2016-03-13 11:17 230
bool isLeftEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 231
	if ( event.type == SDL_KEYDOWN ) {
d21782d4 sago007 2016-03-13 11:17 232
		if (event.key.keysym.sym == SDLK_LEFT) {
d21782d4 sago007 2016-03-13 11:17 233
			return true;
d21782d4 sago007 2016-03-13 11:17 234
		}
d21782d4 sago007 2016-03-13 11:17 235
	}
d21782d4 sago007 2016-03-13 11:17 236
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 237
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_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
	}
a077dceb sago007 2016-03-19 19:19 241
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 242
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 243
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 244
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 245
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 246
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 247
				return true;
a077dceb sago007 2016-03-19 19:19 248
			}
a077dceb sago007 2016-03-19 19:19 249
		}
a077dceb sago007 2016-03-19 19:19 250
	}
d21782d4 sago007 2016-03-13 11:17 251
	return false;
d21782d4 sago007 2016-03-13 11:17 252
}
d21782d4 sago007 2016-03-13 11:17 253
d21782d4 sago007 2016-03-13 11:17 254
bool isRightEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 255
	if ( event.type == SDL_KEYDOWN ) {
877b7c65 sago007 2016-04-09 09:32 256
		if (event.key.keysym.sym == SDLK_RIGHT) {
d21782d4 sago007 2016-03-13 11:17 257
			return true;
d21782d4 sago007 2016-03-13 11:17 258
		}
d21782d4 sago007 2016-03-13 11:17 259
	}
d21782d4 sago007 2016-03-13 11:17 260
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d21782d4 sago007 2016-03-13 11:17 261
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) {
d5efd7d1 sago007 2016-03-12 11:32 262
			return true;
d5efd7d1 sago007 2016-03-12 11:32 263
		}
d5efd7d1 sago007 2016-03-12 11:32 264
	}
a077dceb sago007 2016-03-19 19:19 265
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 266
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 267
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 268
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 269
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 270
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 271
				return true;
a077dceb sago007 2016-03-19 19:19 272
			}
a077dceb sago007 2016-03-19 19:19 273
		}
a077dceb sago007 2016-03-19 19:19 274
	}
d5efd7d1 sago007 2016-03-12 11:32 275
	return false;
d5efd7d1 sago007 2016-03-12 11:32 276
}
d5efd7d1 sago007 2016-03-12 11:32 277
d5efd7d1 sago007 2016-03-12 11:32 278
bool isEscapeEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 279
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 280
		if ( event.key.keysym.sym == SDLK_ESCAPE ) {
d5efd7d1 sago007 2016-03-12 11:32 281
			return true;
d5efd7d1 sago007 2016-03-12 11:32 282
		}
d5efd7d1 sago007 2016-03-12 11:32 283
	}
d5efd7d1 sago007 2016-03-12 11:32 284
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d5efd7d1 sago007 2016-03-12 11:32 285
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_Y ) {
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
	return false;
d5efd7d1 sago007 2016-03-12 11:32 290
}
d5efd7d1 sago007 2016-03-12 11:32 291
d5efd7d1 sago007 2016-03-12 11:32 292
bool isConfirmEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 293
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 294
		if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
d5efd7d1 sago007 2016-03-12 11:32 295
			return true;
d5efd7d1 sago007 2016-03-12 11:32 296
		}
d5efd7d1 sago007 2016-03-12 11:32 297
	}
d5efd7d1 sago007 2016-03-12 11:32 298
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
d5efd7d1 sago007 2016-03-12 11:32 299
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
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
	return false;
d5efd7d1 sago007 2016-03-12 11:32 304
}
d5efd7d1 sago007 2016-03-12 11:32 305
ecef5838 sago007 2015-11-24 20:01 306
void Menu::run() {
c53e6443 sago007 2012-04-17 11:04 307
	running = true;
c53e6443 sago007 2012-04-17 11:04 308
	bool bMouseUp = false;
c53e6443 sago007 2012-04-17 11:04 309
	long oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 310
	long oldmousey = mousey;
ecef5838 sago007 2015-11-24 20:01 311
	while (running && !Config::getInstance()->isShuttingDown()) {
acd79baf sago007 2015-11-22 19:37 312
		if (!(highPriority)) {
acd79baf sago007 2015-11-22 19:37 313
			SDL_Delay(10);
acd79baf sago007 2015-11-22 19:37 314
		}
c53e6443 sago007 2012-04-17 11:04 315
		SDL_Event event;
ecef5838 sago007 2015-11-24 20:01 316
		while ( SDL_PollEvent(&event) ) {
ecef5838 sago007 2015-11-24 20:01 317
			if ( event.type == SDL_QUIT ) {
c53e6443 sago007 2012-04-17 11:04 318
				Config::getInstance()->setShuttingDown(5);
c53e6443 sago007 2012-04-17 11:04 319
				running = false;
c53e6443 sago007 2012-04-17 11:04 320
			}
c7f2082f sago007 2016-03-13 11:48 321
826cf176 sago007 2016-03-12 10:53 322
			if (isUpEvent(event)) {
826cf176 sago007 2016-03-12 10:53 323
				marked--;
826cf176 sago007 2016-03-12 10:53 324
				if (marked<0) {
826cf176 sago007 2016-03-12 10:53 325
					marked = buttons.size();    //not -1, since exit is after the last element in the list
826cf176 sago007 2016-03-12 10:53 326
				}
826cf176 sago007 2016-03-12 10:53 327
			}
c7f2082f sago007 2016-03-13 11:48 328
d5efd7d1 sago007 2016-03-12 11:32 329
			if (isDownEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 330
				marked++;
d5efd7d1 sago007 2016-03-12 11:32 331
				if (marked> (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 332
					marked = 0;
c53e6443 sago007 2012-04-17 11:04 333
				}
c53e6443 sago007 2012-04-17 11:04 334
			}
c7f2082f sago007 2016-03-13 11:48 335
d21782d4 sago007 2016-03-13 11:17 336
			if (isEscapeEvent(event) && isSubmenu) {
d5efd7d1 sago007 2016-03-12 11:32 337
				running = false;
d5efd7d1 sago007 2016-03-12 11:32 338
			}
c7f2082f sago007 2016-03-13 11:48 339
d5efd7d1 sago007 2016-03-12 11:32 340
			if (isConfirmEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 341
				if (marked < (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 342
					buttons.at(marked)->doAction();
d5efd7d1 sago007 2016-03-12 11:32 343
					if (buttons.at(marked)->isPopOnRun()) {
826cf176 sago007 2016-03-12 10:53 344
						running = false;
826cf176 sago007 2016-03-12 10:53 345
					}
826cf176 sago007 2016-03-12 10:53 346
				}
d5efd7d1 sago007 2016-03-12 11:32 347
				if (marked == (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 348
					running = false;
d5efd7d1 sago007 2016-03-12 11:32 349
				}
826cf176 sago007 2016-03-12 10:53 350
			}
c53e6443 sago007 2012-04-17 11:04 351
c53e6443 sago007 2012-04-17 11:04 352
		}
c53e6443 sago007 2012-04-17 11:04 353
daa8f1cf sago007 2016-01-23 10:57 354
		for (int i=0; i<(int)buttons.size(); i++) {
c53e6443 sago007 2012-04-17 11:04 355
			buttons.at(i)->marked = (i == marked);
c53e6443 sago007 2012-04-17 11:04 356
		}
daa8f1cf sago007 2016-01-23 10:57 357
		exit.marked = (marked == (int)buttons.size());
c53e6443 sago007 2012-04-17 11:04 358
		Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey);
c53e6443 sago007 2012-04-17 11:04 359
		// If the mouse button is released, make bMouseUp equal true
ecef5838 sago007 2015-11-24 20:01 360
		if ( (buttonState&SDL_BUTTON(1))==0) {
c53e6443 sago007 2012-04-17 11:04 361
			bMouseUp=true;
c53e6443 sago007 2012-04-17 11:04 362
		}
c53e6443 sago007 2012-04-17 11:04 363
ecef5838 sago007 2015-11-24 20:01 364
		if (abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) {
daa8f1cf sago007 2016-01-23 10:57 365
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 366
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 367
					marked = i;
c53e6443 sago007 2012-04-17 11:04 368
				}
c53e6443 sago007 2012-04-17 11:04 369
			}
ecef5838 sago007 2015-11-24 20:01 370
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 371
				marked = buttons.size();
c53e6443 sago007 2012-04-17 11:04 372
			}
c53e6443 sago007 2012-04-17 11:04 373
			oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 374
			oldmousey = mousey;
c53e6443 sago007 2012-04-17 11:04 375
		}
c53e6443 sago007 2012-04-17 11:04 376
c53e6443 sago007 2012-04-17 11:04 377
		//mouse clicked
ecef5838 sago007 2015-11-24 20:01 378
		if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) {
c53e6443 sago007 2012-04-17 11:04 379
			bMouseUp = false;
daa8f1cf sago007 2016-01-23 10:57 380
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 381
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 382
					buttons.at(i)->doAction();
ecef5838 sago007 2015-11-24 20:01 383
					if (buttons.at(i)->isPopOnRun()) {
67a32395 sago007 2012-04-19 18:40 384
						running = false;
acd79baf sago007 2015-11-22 19:37 385
					}
67a32395 sago007 2012-04-19 18:40 386
					mousex = 0;
c53e6443 sago007 2012-04-17 11:04 387
				}
c53e6443 sago007 2012-04-17 11:04 388
			}
ecef5838 sago007 2015-11-24 20:01 389
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 390
				running = false;
c53e6443 sago007 2012-04-17 11:04 391
			}
c53e6443 sago007 2012-04-17 11:04 392
		}
1b710a1e sago007 2016-09-28 19:01 393
		placeButtons();
c53e6443 sago007 2012-04-17 11:04 394
		drawSelf();
1d2e2129 sago007 2015-12-23 15:41 395
		SDL_RenderPresent(screen);
c53e6443 sago007 2012-04-17 11:04 396
	}
f3ef3495 sago007 2008-11-13 20:00 397
}
1970-01-01 00:00 398