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
c0a3362c sago007 2016-10-07 20:23 109
static void drawToScreen(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);
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++) {
c0a3362c sago007 2016-10-07 20:23 124
		drawToScreen(**it);
6258a442 sago007 2015-11-14 22:11 125
	}
c0a3362c sago007 2016-10-07 20:23 126
	drawToScreen(exit);
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;
1b710a1e sago007 2016-09-28 19:01 134
	int X = 50;
1b710a1e sago007 2016-09-28 19:01 135
	for (Button* it : buttons) {
1b710a1e sago007 2016-09-28 19:01 136
		X = (xsize - it->getWidth())/2;
1b710a1e sago007 2016-09-28 19:01 137
		it->x = X;
1b710a1e sago007 2016-09-28 19:01 138
		it->y = nextY;
1b710a1e sago007 2016-09-28 19: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
		SDL_Event event;
ecef5838 sago007 2015-11-24 20:01 317
		while ( SDL_PollEvent(&event) ) {
ecef5838 sago007 2015-11-24 20:01 318
			if ( event.type == SDL_QUIT ) {
c53e6443 sago007 2012-04-17 11:04 319
				Config::getInstance()->setShuttingDown(5);
c53e6443 sago007 2012-04-17 11:04 320
				running = false;
c53e6443 sago007 2012-04-17 11:04 321
			}
c7f2082f sago007 2016-03-13 11:48 322
826cf176 sago007 2016-03-12 10:53 323
			if (isUpEvent(event)) {
826cf176 sago007 2016-03-12 10:53 324
				marked--;
826cf176 sago007 2016-03-12 10:53 325
				if (marked<0) {
826cf176 sago007 2016-03-12 10:53 326
					marked = buttons.size();    //not -1, since exit is after the last element in the list
826cf176 sago007 2016-03-12 10:53 327
				}
826cf176 sago007 2016-03-12 10:53 328
			}
c7f2082f sago007 2016-03-13 11:48 329
d5efd7d1 sago007 2016-03-12 11:32 330
			if (isDownEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 331
				marked++;
d5efd7d1 sago007 2016-03-12 11:32 332
				if (marked> (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 333
					marked = 0;
c53e6443 sago007 2012-04-17 11:04 334
				}
c53e6443 sago007 2012-04-17 11:04 335
			}
c7f2082f sago007 2016-03-13 11:48 336
d21782d4 sago007 2016-03-13 11:17 337
			if (isEscapeEvent(event) && isSubmenu) {
d5efd7d1 sago007 2016-03-12 11:32 338
				running = false;
d5efd7d1 sago007 2016-03-12 11:32 339
			}
c7f2082f sago007 2016-03-13 11:48 340
d5efd7d1 sago007 2016-03-12 11:32 341
			if (isConfirmEvent(event)) {
d5efd7d1 sago007 2016-03-12 11:32 342
				if (marked < (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 343
					buttons.at(marked)->doAction();
d5efd7d1 sago007 2016-03-12 11:32 344
					if (buttons.at(marked)->isPopOnRun()) {
826cf176 sago007 2016-03-12 10:53 345
						running = false;
826cf176 sago007 2016-03-12 10:53 346
					}
826cf176 sago007 2016-03-12 10:53 347
				}
d5efd7d1 sago007 2016-03-12 11:32 348
				if (marked == (int)buttons.size()) {
d5efd7d1 sago007 2016-03-12 11:32 349
					running = false;
d5efd7d1 sago007 2016-03-12 11:32 350
				}
826cf176 sago007 2016-03-12 10:53 351
			}
c53e6443 sago007 2012-04-17 11:04 352
c53e6443 sago007 2012-04-17 11:04 353
		}
c53e6443 sago007 2012-04-17 11:04 354
daa8f1cf sago007 2016-01-23 10:57 355
		for (int i=0; i<(int)buttons.size(); i++) {
c53e6443 sago007 2012-04-17 11:04 356
			buttons.at(i)->marked = (i == marked);
c53e6443 sago007 2012-04-17 11:04 357
		}
daa8f1cf sago007 2016-01-23 10:57 358
		exit.marked = (marked == (int)buttons.size());
c53e6443 sago007 2012-04-17 11:04 359
		Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey);
c53e6443 sago007 2012-04-17 11:04 360
		// If the mouse button is released, make bMouseUp equal true
ecef5838 sago007 2015-11-24 20:01 361
		if ( (buttonState&SDL_BUTTON(1))==0) {
c53e6443 sago007 2012-04-17 11:04 362
			bMouseUp=true;
c53e6443 sago007 2012-04-17 11:04 363
		}
c53e6443 sago007 2012-04-17 11:04 364
ecef5838 sago007 2015-11-24 20:01 365
		if (abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) {
daa8f1cf sago007 2016-01-23 10:57 366
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 367
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 368
					marked = i;
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 (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 372
				marked = buttons.size();
c53e6443 sago007 2012-04-17 11:04 373
			}
c53e6443 sago007 2012-04-17 11:04 374
			oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 375
			oldmousey = mousey;
c53e6443 sago007 2012-04-17 11:04 376
		}
c53e6443 sago007 2012-04-17 11:04 377
c53e6443 sago007 2012-04-17 11:04 378
		//mouse clicked
ecef5838 sago007 2015-11-24 20:01 379
		if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) {
c53e6443 sago007 2012-04-17 11:04 380
			bMouseUp = false;
daa8f1cf sago007 2016-01-23 10:57 381
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 382
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 383
					buttons.at(i)->doAction();
ecef5838 sago007 2015-11-24 20:01 384
					if (buttons.at(i)->isPopOnRun()) {
67a32395 sago007 2012-04-19 18:40 385
						running = false;
acd79baf sago007 2015-11-22 19:37 386
					}
67a32395 sago007 2012-04-19 18:40 387
					mousex = 0;
c53e6443 sago007 2012-04-17 11:04 388
				}
c53e6443 sago007 2012-04-17 11:04 389
			}
ecef5838 sago007 2015-11-24 20:01 390
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 391
				running = false;
c53e6443 sago007 2012-04-17 11:04 392
			}
c53e6443 sago007 2012-04-17 11:04 393
		}
1b710a1e sago007 2016-09-28 19:01 394
		placeButtons();
c53e6443 sago007 2012-04-17 11:04 395
		drawSelf();
1d2e2129 sago007 2015-12-23 15:41 396
		SDL_RenderPresent(screen);
c53e6443 sago007 2012-04-17 11:04 397
	}
f3ef3495 sago007 2008-11-13 20:00 398
}
1970-01-01 00:00 399