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
021de090 sago007 2015-12-30 18:56 20
http://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"
f3ef3495 sago007 2008-11-13 20:00 28
0c201300 sago007 2016-02-20 12:12 29
static int mousex;
0c201300 sago007 2016-02-20 12:12 30
static int mousey;
f3ef3495 sago007 2008-11-13 20:00 31
33f4e975 sago007 2015-11-14 21:49 32
using namespace std;
33f4e975 sago007 2015-11-14 21:49 33
f3ef3495 sago007 2008-11-13 20:00 34
b283e0f7 sago007 2012-07-29 14:01 35
ButtonGfx standardButton;
f3ef3495 sago007 2008-11-13 20:00 36
1d2e2129 sago007 2015-12-23 15:41 37
void ButtonGfx::setSurfaces(sago::SagoSprite& marked, sago::SagoSprite& unmarked) {
78ab168e sago007 2015-11-14 22:02 38
	this->marked = marked;
78ab168e sago007 2015-11-14 22:02 39
	this->unmarked = unmarked;
1d2e2129 sago007 2015-12-23 15:41 40
	xsize=(marked).GetWidth();
1d2e2129 sago007 2015-12-23 15:41 41
	ysize=(marked).GetHeight();
ecef5838 sago007 2015-11-24 20:01 42
	if (verboseLevel) {
56241205 sago007 2012-08-05 11:15 43
		cout << "Surfaces set, size: " <<xsize << " , " << ysize << endl;
acd79baf sago007 2015-11-22 19:37 44
	}
f3ef3495 sago007 2008-11-13 20:00 45
}
f3ef3495 sago007 2008-11-13 20:00 46
ecef5838 sago007 2015-11-24 20:01 47
Button::Button() {
b283e0f7 sago007 2012-07-29 14:01 48
	gfx = &standardButton;
c53e6443 sago007 2012-04-17 11:04 49
	label = "";
c53e6443 sago007 2012-04-17 11:04 50
	marked = false;
acd79baf sago007 2015-11-22 19:37 51
	action = nullptr;
67a32395 sago007 2012-04-19 18:40 52
	popOnRun = false;
f3ef3495 sago007 2008-11-13 20:00 53
}
f3ef3495 sago007 2008-11-13 20:00 54
ecef5838 sago007 2015-11-24 20:01 55
Button::~Button() {
f3ef3495 sago007 2008-11-13 20:00 56
}
f3ef3495 sago007 2008-11-13 20:00 57
ecef5838 sago007 2015-11-24 20:01 58
Button::Button(const Button& b) {
b283e0f7 sago007 2012-07-29 14:01 59
	gfx = b.gfx;
c53e6443 sago007 2012-04-17 11:04 60
	label = b.label;
c53e6443 sago007 2012-04-17 11:04 61
	marked = b.marked;
c53e6443 sago007 2012-04-17 11:04 62
	action = b.action;
67a32395 sago007 2012-04-19 18:40 63
	popOnRun = false;
f3ef3495 sago007 2008-11-13 20:00 64
}
f3ef3495 sago007 2008-11-13 20:00 65
ecef5838 sago007 2015-11-24 20:01 66
void Button::setLabel(const string& text) {
c53e6443 sago007 2012-04-17 11:04 67
	label = text;
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::setAction(void (*action2run)(Button*)) {
c53e6443 sago007 2012-04-17 11:04 71
	action = action2run;
f3ef3495 sago007 2008-11-13 20:00 72
}
f3ef3495 sago007 2008-11-13 20:00 73
ecef5838 sago007 2015-11-24 20:01 74
bool Button::isClicked(int x,int y) {
6258a442 sago007 2015-11-14 22:11 75
	if ( x >= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) {
c53e6443 sago007 2012-04-17 11:04 76
		return true;
6258a442 sago007 2015-11-14 22:11 77
	}
6258a442 sago007 2015-11-14 22:11 78
	return false;
f3ef3495 sago007 2008-11-13 20:00 79
}
f3ef3495 sago007 2008-11-13 20:00 80
ecef5838 sago007 2015-11-24 20:01 81
void Button::doAction() {
6258a442 sago007 2015-11-14 22:11 82
	if (action) {
c53e6443 sago007 2012-04-17 11:04 83
		action(this);
6258a442 sago007 2015-11-14 22:11 84
		return;
6258a442 sago007 2015-11-14 22:11 85
	}
6258a442 sago007 2015-11-14 22:11 86
	cerr << "Warning: button \"" << label << "\" has no action assigned!";
f3ef3495 sago007 2008-11-13 20:00 87
}
f3ef3495 sago007 2008-11-13 20:00 88
0c1de203 sago007 2015-12-05 22:15 89
void Button::drawToScreen() {
c53e6443 sago007 2012-04-17 11:04 90
#if DEBUG
c53e6443 sago007 2012-04-17 11:04 91
	//cout << "Painting button: " << label << " at: " << x << "," << y << endl;
c53e6443 sago007 2012-04-17 11:04 92
#endif
6258a442 sago007 2015-11-14 22:11 93
	if (marked) {
1d2e2129 sago007 2015-12-23 15:41 94
		gfx->marked.Draw(screen, SDL_GetTicks(), x, y);
6258a442 sago007 2015-11-14 22:11 95
	}
6258a442 sago007 2015-11-14 22:11 96
	else {
1d2e2129 sago007 2015-12-23 15:41 97
		gfx->unmarked.Draw(screen, SDL_GetTicks(), x, y);
6258a442 sago007 2015-11-14 22:11 98
	}
3a23aa5e sago007 2015-12-23 18:06 99
	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 100
}
f3ef3495 sago007 2008-11-13 20:00 101
ecef5838 sago007 2015-11-24 20:01 102
void Button::setPopOnRun(bool popOnRun) {
67a32395 sago007 2012-04-19 18:40 103
	this->popOnRun = popOnRun;
67a32395 sago007 2012-04-19 18:40 104
}
67a32395 sago007 2012-04-19 18:40 105
ecef5838 sago007 2015-11-24 20:01 106
bool Button::isPopOnRun() const {
67a32395 sago007 2012-04-19 18:40 107
	return popOnRun;
67a32395 sago007 2012-04-19 18:40 108
}
67a32395 sago007 2012-04-19 18:40 109
ecef5838 sago007 2015-11-24 20:01 110
void Button::setGfx(ButtonGfx* gfx) {
b283e0f7 sago007 2012-07-29 14:01 111
	this->gfx = gfx;
b283e0f7 sago007 2012-07-29 14:01 112
}
b283e0f7 sago007 2012-07-29 14:01 113
ecef5838 sago007 2015-11-24 20:01 114
int Button::getHeight() {
b283e0f7 sago007 2012-07-29 14:01 115
	return this->gfx->ysize;
b283e0f7 sago007 2012-07-29 14:01 116
}
b283e0f7 sago007 2012-07-29 14:01 117
ecef5838 sago007 2015-11-24 20:01 118
void Menu::drawSelf() {
b539639c sago007 2015-12-29 11:51 119
	SDL_RenderClear(screen);
1d2e2129 sago007 2015-12-23 15:41 120
	backgroundImage.Draw(screen, SDL_GetTicks(), 0, 0);
c53e6443 sago007 2012-04-17 11:04 121
	vector<Button*>::iterator it;
6258a442 sago007 2015-11-14 22:11 122
	for (it = buttons.begin(); it < buttons.end(); it++) {
0c1de203 sago007 2015-12-05 22:15 123
		(*it)->drawToScreen();
6258a442 sago007 2015-11-14 22:11 124
	}
0c1de203 sago007 2015-12-05 22:15 125
	exit.drawToScreen();
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
ecef5838 sago007 2015-11-24 20:01 130
void Menu::performClick(int x,int y) {
c53e6443 sago007 2012-04-17 11:04 131
	vector<Button*>::iterator it;
ecef5838 sago007 2015-11-24 20:01 132
	for (it = buttons.begin(); it < buttons.end(); it++) {
ecef5838 sago007 2015-11-24 20:01 133
		Button* b = (*it);
6258a442 sago007 2015-11-14 22:11 134
		if (b->isClicked(x,y)) {
c53e6443 sago007 2012-04-17 11:04 135
			b->doAction();
6258a442 sago007 2015-11-14 22:11 136
		}
6258a442 sago007 2015-11-14 22:11 137
		if (b->isPopOnRun()) {
67a32395 sago007 2012-04-19 18:40 138
			running = false;
6258a442 sago007 2015-11-14 22:11 139
		}
c53e6443 sago007 2012-04-17 11:04 140
	}
6258a442 sago007 2015-11-14 22:11 141
	if (exit.isClicked(x,y)) {
c53e6443 sago007 2012-04-17 11:04 142
		running = false;
6258a442 sago007 2015-11-14 22:11 143
	}
f3ef3495 sago007 2008-11-13 20:00 144
}
f3ef3495 sago007 2008-11-13 20:00 145
ecef5838 sago007 2015-11-24 20:01 146
void Menu::placeButtons() {
c53e6443 sago007 2012-04-17 11:04 147
	int nextY = 100;
c53e6443 sago007 2012-04-17 11:04 148
	const int X = 50;
c53e6443 sago007 2012-04-17 11:04 149
	vector<Button*>::iterator it;
ecef5838 sago007 2015-11-24 20:01 150
	for (it = buttons.begin(); it < buttons.end(); it++) {
c53e6443 sago007 2012-04-17 11:04 151
		(*it)->x = X;
c53e6443 sago007 2012-04-17 11:04 152
		(*it)->y = nextY;
b283e0f7 sago007 2012-07-29 14:01 153
		nextY += (*it)->getHeight()+10;
c53e6443 sago007 2012-04-17 11:04 154
	}
c53e6443 sago007 2012-04-17 11:04 155
	exit.x = X;
c53e6443 sago007 2012-04-17 11:04 156
	exit.y = nextY;
f3ef3495 sago007 2008-11-13 20:00 157
}
f3ef3495 sago007 2008-11-13 20:00 158
ecef5838 sago007 2015-11-24 20:01 159
void Menu::addButton(Button* b) {
c53e6443 sago007 2012-04-17 11:04 160
	buttons.push_back(b);
c53e6443 sago007 2012-04-17 11:04 161
	b->marked = false;
c53e6443 sago007 2012-04-17 11:04 162
	placeButtons();
f3ef3495 sago007 2008-11-13 20:00 163
}
f3ef3495 sago007 2008-11-13 20:00 164
1d2e2129 sago007 2015-12-23 15:41 165
Menu::Menu(SDL_Renderer* screen) {
0c1de203 sago007 2015-12-05 22:15 166
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 167
	buttons = vector<Button*>(10);
c53e6443 sago007 2012-04-17 11:04 168
	isSubmenu = true;
c53e6443 sago007 2012-04-17 11:04 169
	exit.setLabel( _("Back") );
f3ef3495 sago007 2008-11-13 20:00 170
}
f3ef3495 sago007 2008-11-13 20:00 171
1d2e2129 sago007 2015-12-23 15:41 172
Menu::Menu(SDL_Renderer* screen,bool submenu) {
0c1de203 sago007 2015-12-05 22:15 173
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 174
	buttons = vector<Button*>(0);
c53e6443 sago007 2012-04-17 11:04 175
	isSubmenu = submenu;
ecef5838 sago007 2015-11-24 20:01 176
	if (isSubmenu) {
c53e6443 sago007 2012-04-17 11:04 177
		exit.setLabel( _("Back") );
acd79baf sago007 2015-11-22 19:37 178
	}
acd79baf sago007 2015-11-22 19:37 179
	else {
c53e6443 sago007 2012-04-17 11:04 180
		exit.setLabel( _("Exit") );
acd79baf sago007 2015-11-22 19:37 181
	}
f3ef3495 sago007 2008-11-13 20:00 182
}
f3ef3495 sago007 2008-11-13 20:00 183
1d2e2129 sago007 2015-12-23 15:41 184
Menu::Menu(SDL_Renderer* screen, const string& title, bool submenu) {
0c1de203 sago007 2015-12-05 22:15 185
	this->screen = screen;
c53e6443 sago007 2012-04-17 11:04 186
	buttons = vector<Button*>(0);
c53e6443 sago007 2012-04-17 11:04 187
	isSubmenu = submenu;
c53e6443 sago007 2012-04-17 11:04 188
	this->title = title;
ecef5838 sago007 2015-11-24 20:01 189
	if (isSubmenu) {
c53e6443 sago007 2012-04-17 11:04 190
		exit.setLabel(_("Back") );
acd79baf sago007 2015-11-22 19:37 191
	}
acd79baf sago007 2015-11-22 19:37 192
	else {
c53e6443 sago007 2012-04-17 11:04 193
		exit.setLabel(_("Exit") );
acd79baf sago007 2015-11-22 19:37 194
	}
cc3ef158 sago007 2011-07-03 16:13 195
}
cc3ef158 sago007 2011-07-03 16:13 196
ecef5838 sago007 2015-11-24 20:01 197
void Menu::run() {
c53e6443 sago007 2012-04-17 11:04 198
	running = true;
c53e6443 sago007 2012-04-17 11:04 199
	bool bMouseUp = false;
c53e6443 sago007 2012-04-17 11:04 200
	long oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 201
	long oldmousey = mousey;
ecef5838 sago007 2015-11-24 20:01 202
	while (running && !Config::getInstance()->isShuttingDown()) {
acd79baf sago007 2015-11-22 19:37 203
		if (!(highPriority)) {
acd79baf sago007 2015-11-22 19:37 204
			SDL_Delay(10);
acd79baf sago007 2015-11-22 19:37 205
		}
c53e6443 sago007 2012-04-17 11:04 206
c53e6443 sago007 2012-04-17 11:04 207
c53e6443 sago007 2012-04-17 11:04 208
		SDL_Event event;
c53e6443 sago007 2012-04-17 11:04 209
ecef5838 sago007 2015-11-24 20:01 210
		while ( SDL_PollEvent(&event) ) {
ecef5838 sago007 2015-11-24 20:01 211
			if ( event.type == SDL_QUIT ) {
c53e6443 sago007 2012-04-17 11:04 212
				Config::getInstance()->setShuttingDown(5);
c53e6443 sago007 2012-04-17 11:04 213
				running = false;
c53e6443 sago007 2012-04-17 11:04 214
			}
c53e6443 sago007 2012-04-17 11:04 215
ecef5838 sago007 2015-11-24 20:01 216
			if ( event.type == SDL_KEYDOWN ) {
ecef5838 sago007 2015-11-24 20:01 217
				if ( event.key.keysym.sym == SDLK_ESCAPE ) {
c53e6443 sago007 2012-04-17 11:04 218
					running = false;
c53e6443 sago007 2012-04-17 11:04 219
				}
c53e6443 sago007 2012-04-17 11:04 220
ecef5838 sago007 2015-11-24 20:01 221
				if (event.key.keysym.sym == SDLK_UP) {
c53e6443 sago007 2012-04-17 11:04 222
					marked--;
ecef5838 sago007 2015-11-24 20:01 223
					if (marked<0) {
acd79baf sago007 2015-11-22 19:37 224
						marked = buttons.size();    //not -1, since exit is after the last element in the list
acd79baf sago007 2015-11-22 19:37 225
					}
c53e6443 sago007 2012-04-17 11:04 226
				}
c53e6443 sago007 2012-04-17 11:04 227
ecef5838 sago007 2015-11-24 20:01 228
				if (event.key.keysym.sym == SDLK_DOWN) {
c53e6443 sago007 2012-04-17 11:04 229
					marked++;
daa8f1cf sago007 2016-01-23 10:57 230
					if (marked> (int)buttons.size()) {
c53e6443 sago007 2012-04-17 11:04 231
						marked = 0;
acd79baf sago007 2015-11-22 19:37 232
					}
c53e6443 sago007 2012-04-17 11:04 233
				}
c53e6443 sago007 2012-04-17 11:04 234
ecef5838 sago007 2015-11-24 20:01 235
				if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
daa8f1cf sago007 2016-01-23 10:57 236
					if (marked < (int)buttons.size()) {
c53e6443 sago007 2012-04-17 11:04 237
						buttons.at(marked)->doAction();
ecef5838 sago007 2015-11-24 20:01 238
						if (buttons.at(marked)->isPopOnRun()) {
67a32395 sago007 2012-04-19 18:40 239
							running = false;
acd79baf sago007 2015-11-22 19:37 240
						}
67a32395 sago007 2012-04-19 18:40 241
					}
daa8f1cf sago007 2016-01-23 10:57 242
					if (marked == (int)buttons.size()) {
c53e6443 sago007 2012-04-17 11:04 243
						running = false;
acd79baf sago007 2015-11-22 19:37 244
					}
c53e6443 sago007 2012-04-17 11:04 245
				}
c53e6443 sago007 2012-04-17 11:04 246
			}
c53e6443 sago007 2012-04-17 11:04 247
c53e6443 sago007 2012-04-17 11:04 248
c53e6443 sago007 2012-04-17 11:04 249
		}
c53e6443 sago007 2012-04-17 11:04 250
daa8f1cf sago007 2016-01-23 10:57 251
		for (int i=0; i<(int)buttons.size(); i++) {
c53e6443 sago007 2012-04-17 11:04 252
			buttons.at(i)->marked = (i == marked);
c53e6443 sago007 2012-04-17 11:04 253
		}
daa8f1cf sago007 2016-01-23 10:57 254
		exit.marked = (marked == (int)buttons.size());
c53e6443 sago007 2012-04-17 11:04 255
		Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey);
c53e6443 sago007 2012-04-17 11:04 256
		// If the mouse button is released, make bMouseUp equal true
ecef5838 sago007 2015-11-24 20:01 257
		if ( (buttonState&SDL_BUTTON(1))==0) {
c53e6443 sago007 2012-04-17 11:04 258
			bMouseUp=true;
c53e6443 sago007 2012-04-17 11:04 259
		}
c53e6443 sago007 2012-04-17 11:04 260
ecef5838 sago007 2015-11-24 20:01 261
		if (abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) {
daa8f1cf sago007 2016-01-23 10:57 262
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 263
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 264
					marked = i;
c53e6443 sago007 2012-04-17 11:04 265
				}
c53e6443 sago007 2012-04-17 11:04 266
			}
ecef5838 sago007 2015-11-24 20:01 267
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 268
				marked = buttons.size();
c53e6443 sago007 2012-04-17 11:04 269
			}
c53e6443 sago007 2012-04-17 11:04 270
			oldmousex = mousex;
c53e6443 sago007 2012-04-17 11:04 271
			oldmousey = mousey;
c53e6443 sago007 2012-04-17 11:04 272
		}
c53e6443 sago007 2012-04-17 11:04 273
c53e6443 sago007 2012-04-17 11:04 274
		//mouse clicked
ecef5838 sago007 2015-11-24 20:01 275
		if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) {
c53e6443 sago007 2012-04-17 11:04 276
			bMouseUp = false;
daa8f1cf sago007 2016-01-23 10:57 277
			for (int i=0; i< (int)buttons.size(); ++i) {
ecef5838 sago007 2015-11-24 20:01 278
				if (buttons.at(i)->isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 279
					buttons.at(i)->doAction();
ecef5838 sago007 2015-11-24 20:01 280
					if (buttons.at(i)->isPopOnRun()) {
67a32395 sago007 2012-04-19 18:40 281
						running = false;
acd79baf sago007 2015-11-22 19:37 282
					}
67a32395 sago007 2012-04-19 18:40 283
					mousex = 0;
c53e6443 sago007 2012-04-17 11:04 284
				}
c53e6443 sago007 2012-04-17 11:04 285
			}
ecef5838 sago007 2015-11-24 20:01 286
			if (exit.isClicked(mousex,mousey)) {
c53e6443 sago007 2012-04-17 11:04 287
				running = false;
c53e6443 sago007 2012-04-17 11:04 288
			}
c53e6443 sago007 2012-04-17 11:04 289
		}
c53e6443 sago007 2012-04-17 11:04 290
c53e6443 sago007 2012-04-17 11:04 291
		drawSelf();
1d2e2129 sago007 2015-12-23 15:41 292
		SDL_RenderPresent(screen);
c53e6443 sago007 2012-04-17 11:04 293
	}
f3ef3495 sago007 2008-11-13 20:00 294
}
1970-01-01 00:00 295