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
f6d8a699 sago007 2018-10-07 12:16 20
https://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
20d77c6e sago007 2016-11-12 16:28 31
static int oldmousex = 0;
20d77c6e sago007 2016-11-12 16:28 32
static int oldmousey = 0;
f3ef3495 sago007 2008-11-13 20:00 33
094b2dfe sago007 2016-06-12 10:50 34
void ButtonGfx::setSurfaces() {
72383a9e sago007 2016-10-30 11:56 35
	this->xsize = globalData.spriteHolder->GetSprite(menu_marked).GetWidth();
72383a9e sago007 2016-10-30 11:56 36
	this->ysize = globalData.spriteHolder->GetSprite(menu_marked).GetHeight();
72383a9e sago007 2016-10-30 11:56 37
	if (globalData.verboseLevel) {
f6d8a699 sago007 2018-10-07 12:16 38
		std::cout << "Surfaces set, size: " << this->xsize << " , " << this->ysize << "\n";
acd79baf sago007 2015-11-22 19:37 39
	}
f3ef3495 sago007 2008-11-13 20:00 40
}
f3ef3495 sago007 2008-11-13 20:00 41
60b2bac0 Poul Sander 2023-11-10 20:43 42
sago::SagoTextField* ButtonGfx::getLabel(const std::string& text, bool marked) const {
342ea507 Poul Sander 2023-07-27 20:38 43
	if (!marked) {
342ea507 Poul Sander 2023-07-27 20:38 44
		const auto& theLabel = labels.find(text);
342ea507 Poul Sander 2023-07-27 20:38 45
		if (theLabel != labels.end()) {
342ea507 Poul Sander 2023-07-27 20:38 46
			return labels[text].get();
342ea507 Poul Sander 2023-07-27 20:38 47
		}
342ea507 Poul Sander 2023-07-27 20:38 48
	}
342ea507 Poul Sander 2023-07-27 20:38 49
	const auto& theLabel = labels_marked.find(text);
342ea507 Poul Sander 2023-07-27 20:38 50
	if (theLabel != labels_marked.end()) {
342ea507 Poul Sander 2023-07-27 20:38 51
		return labels_marked[text].get();
d91e7363 sago007 2018-03-26 13:09 52
	}
9f7a6344 sago007 2018-03-30 12:05 53
	std::shared_ptr<sago::SagoTextField> newField = std::make_shared<sago::SagoTextField>();
d91e7363 sago007 2018-03-26 13:09 54
	newField->SetHolder(&globalData.spriteHolder->GetDataHolder());
d91e7363 sago007 2018-03-26 13:09 55
	newField->SetFont("freeserif");
36bbe751 sago007 2018-03-26 17:27 56
	newField->SetFontSize(30);
d8fe60bd sago007 2018-04-02 16:15 57
	newField->SetOutline(1, {64,64,64,255});
342ea507 Poul Sander 2023-07-27 20:38 58
	if (marked) {
342ea507 Poul Sander 2023-07-27 20:38 59
		newField->SetOutline(3, {32,32,32,255});
342ea507 Poul Sander 2023-07-27 20:38 60
	}
0b8fb137 sago007 2018-03-31 19:53 61
	newField->SetText(text);
342ea507 Poul Sander 2023-07-27 20:38 62
	if (!marked) {
342ea507 Poul Sander 2023-07-27 20:38 63
		labels[text] = newField;
342ea507 Poul Sander 2023-07-27 20:38 64
		return labels[text].get();
342ea507 Poul Sander 2023-07-27 20:38 65
	}
342ea507 Poul Sander 2023-07-27 20:38 66
	labels_marked[text] = newField;
342ea507 Poul Sander 2023-07-27 20:38 67
	return labels_marked[text].get();
d91e7363 sago007 2018-03-26 13:09 68
}
d91e7363 sago007 2018-03-26 13:09 69
5f8ce673 Poul Sander 2021-06-05 22:18 70
Button::Button(const Button& b) : action{b.action}, popOnRun{b.popOnRun}, label{b.label}, marked{b.marked} {
5db03af6 Poul Sander 2018-11-07 19:39 71
}
5db03af6 Poul Sander 2018-11-07 19:39 72
5db03af6 Poul Sander 2018-11-07 19:39 73
Button& Button::operator=(const Button& other) {
5db03af6 Poul Sander 2018-11-07 19:39 74
	action = other.action;
5db03af6 Poul Sander 2018-11-07 19:39 75
	label = other.label;
5db03af6 Poul Sander 2018-11-07 19:39 76
	marked = other.marked;
5f8ce673 Poul Sander 2021-06-05 22:18 77
	popOnRun = other.popOnRun;
5db03af6 Poul Sander 2018-11-07 19:39 78
	return *this;
f3ef3495 sago007 2008-11-13 20:00 79
}
f3ef3495 sago007 2008-11-13 20:00 80
f6d8a699 sago007 2018-10-07 12:16 81
void Button::setLabel(const std::string& text) {
c53e6443 sago007 2012-04-17 11:04 82
	label = text;
60b2bac0 Poul Sander 2023-11-10 20:43 83
	standardButton.setSurfaces();
f3ef3495 sago007 2008-11-13 20:00 84
}
f3ef3495 sago007 2008-11-13 20:00 85
29edda2d sago007 2016-10-08 18:06 86
void Button::setAction(void (*action2run)(void)) {
c53e6443 sago007 2012-04-17 11:04 87
	action = action2run;
f3ef3495 sago007 2008-11-13 20:00 88
}
f3ef3495 sago007 2008-11-13 20:00 89
ecef5838 sago007 2015-11-24 20:01 90
void Button::doAction() {
6258a442 sago007 2015-11-14 22:11 91
	if (action) {
29edda2d sago007 2016-10-08 18:06 92
		action();
6258a442 sago007 2015-11-14 22:11 93
		return;
6258a442 sago007 2015-11-14 22:11 94
	}
f6d8a699 sago007 2018-10-07 12:16 95
	std::cerr << "Warning: button \"" << label << "\" has no action assigned!";
f3ef3495 sago007 2008-11-13 20:00 96
}
f3ef3495 sago007 2008-11-13 20:00 97
ecef5838 sago007 2015-11-24 20:01 98
void Button::setPopOnRun(bool popOnRun) {
67a32395 sago007 2012-04-19 18:40 99
	this->popOnRun = popOnRun;
67a32395 sago007 2012-04-19 18:40 100
}
67a32395 sago007 2012-04-19 18:40 101
ecef5838 sago007 2015-11-24 20:01 102
bool Button::isPopOnRun() const {
67a32395 sago007 2012-04-19 18:40 103
	return popOnRun;
67a32395 sago007 2012-04-19 18:40 104
}
67a32395 sago007 2012-04-19 18:40 105
092bdebe sago007 2017-03-19 14:39 106
static void drawToScreen(const Button& b) {
c0a3362c sago007 2016-10-07 20:23 107
	if (b.marked) {
60b2bac0 Poul Sander 2023-11-10 20:43 108
		globalData.spriteHolder->GetSprite(b.standardButton.menu_marked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y);
c0a3362c sago007 2016-10-07 20:23 109
	}
c0a3362c sago007 2016-10-07 20:23 110
	else {
60b2bac0 Poul Sander 2023-11-10 20:43 111
		globalData.spriteHolder->GetSprite(b.standardButton.menu_unmarked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y);
c0a3362c sago007 2016-10-07 20:23 112
	}
092bdebe sago007 2017-03-19 14:39 113
60b2bac0 Poul Sander 2023-11-10 20:43 114
	b.standardButton.getLabel(b.getLabel(), b.marked)->Draw(globalData.screen, b.x+b.standardButton.xsize/2,b.y+b.standardButton.ysize/2,
55e33610 Poul Sander 2022-01-09 11:45 115
	        sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::center);
1b710a1e sago007 2016-09-28 19:01 116
}
1b710a1e sago007 2016-09-28 19:01 117
7b384c3f sago007 2016-10-08 17:34 118
092bdebe sago007 2017-03-19 14:39 119
static bool isClicked(const Button& b, int x,int y) {
60b2bac0 Poul Sander 2023-11-10 20:43 120
	if ( x >= b.x && y >= b.y && x<= b.x+b.standardButton.xsize && y <= b.y + b.standardButton.ysize) {
7b384c3f sago007 2016-10-08 17:34 121
		return true;
7b384c3f sago007 2016-10-08 17:34 122
	}
7b384c3f sago007 2016-10-08 17:34 123
	return false;
7b384c3f sago007 2016-10-08 17:34 124
}
7b384c3f sago007 2016-10-08 17:34 125
95ab6b0d sago007 2016-11-12 16:11 126
void Menu::drawSelf(SDL_Renderer* target) {
95ab6b0d sago007 2016-11-12 16:11 127
	DrawBackground(target);
a1e83d35 sago007 2016-10-08 17:29 128
	for (const Button* b : buttons) {
a1e83d35 sago007 2016-10-08 17:29 129
		drawToScreen(*b);
6258a442 sago007 2015-11-14 22:11 130
	}
c0a3362c sago007 2016-10-07 20:23 131
	drawToScreen(exit);
60b2bac0 Poul Sander 2023-11-10 20:43 132
	exit.standardButton.getLabel(title, false)->Draw(target, 50, 50);
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;
1b710a1e sago007 2016-09-28 19:01 138
	int X = 50;
1b710a1e sago007 2016-09-28 19:01 139
	for (Button* it : buttons) {
60b2bac0 Poul Sander 2023-11-10 20:43 140
		X = (globalData.xsize - it->standardButton.xsize)/2;
1b710a1e sago007 2016-09-28 19:01 141
		it->x = X;
1b710a1e sago007 2016-09-28 19:01 142
		it->y = nextY;
60b2bac0 Poul Sander 2023-11-10 20:43 143
		nextY += it->standardButton.ysize+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
ddceb7e8 Poul Sander 2023-10-18 11:02 155
Menu::Menu(SDL_Renderer* screen) {
ddceb7e8 Poul Sander 2023-10-18 11:02 156
	buttons = std::vector<Button*>(10);
ddceb7e8 Poul Sander 2023-10-18 11:02 157
	isSubmenu = true;
ddceb7e8 Poul Sander 2023-10-18 11:02 158
	this->screen = screen;
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
ddceb7e8 Poul Sander 2023-10-18 11:02 162
Menu::Menu(SDL_Renderer* screen,bool submenu) {
ddceb7e8 Poul Sander 2023-10-18 11:02 163
	buttons = std::vector<Button*>(0);
ddceb7e8 Poul Sander 2023-10-18 11:02 164
	isSubmenu = submenu;
ddceb7e8 Poul Sander 2023-10-18 11:02 165
	this->screen = screen;
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
ddceb7e8 Poul Sander 2023-10-18 11:02 174
Menu::Menu(SDL_Renderer* screen, const std::string& title, bool submenu) {
ddceb7e8 Poul Sander 2023-10-18 11:02 175
	buttons = std::vector<Button*>(0);
ddceb7e8 Poul Sander 2023-10-18 11:02 176
	isSubmenu = submenu;
ddceb7e8 Poul Sander 2023-10-18 11:02 177
	this->screen = screen;
ddceb7e8 Poul Sander 2023-10-18 11:02 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
	}
f73e7c50 sago007 2020-07-04 08:03 193
	return isControllerUpEvent(event);
826cf176 sago007 2016-03-12 10:53 194
}
826cf176 sago007 2016-03-12 10:53 195
d5efd7d1 sago007 2016-03-12 11:32 196
bool isDownEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 197
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 198
		if (event.key.keysym.sym == SDLK_DOWN) {
d5efd7d1 sago007 2016-03-12 11:32 199
			return true;
d5efd7d1 sago007 2016-03-12 11:32 200
		}
d5efd7d1 sago007 2016-03-12 11:32 201
	}
f73e7c50 sago007 2020-07-04 08:03 202
	return isControllerDownEvent(event);
d21782d4 sago007 2016-03-13 11:17 203
}
d21782d4 sago007 2016-03-13 11:17 204
d21782d4 sago007 2016-03-13 11:17 205
bool isLeftEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 206
	if ( event.type == SDL_KEYDOWN ) {
d21782d4 sago007 2016-03-13 11:17 207
		if (event.key.keysym.sym == SDLK_LEFT) {
d21782d4 sago007 2016-03-13 11:17 208
			return true;
d21782d4 sago007 2016-03-13 11:17 209
		}
d21782d4 sago007 2016-03-13 11:17 210
	}
f73e7c50 sago007 2020-07-04 08:03 211
	return isControllerLeftEvent(event);
d21782d4 sago007 2016-03-13 11:17 212
}
d21782d4 sago007 2016-03-13 11:17 213
d21782d4 sago007 2016-03-13 11:17 214
bool isRightEvent(const SDL_Event& event) {
d21782d4 sago007 2016-03-13 11:17 215
	if ( event.type == SDL_KEYDOWN ) {
877b7c65 sago007 2016-04-09 09:32 216
		if (event.key.keysym.sym == SDLK_RIGHT) {
d21782d4 sago007 2016-03-13 11:17 217
			return true;
d21782d4 sago007 2016-03-13 11:17 218
		}
d21782d4 sago007 2016-03-13 11:17 219
	}
f73e7c50 sago007 2020-07-04 08:03 220
	return isControllerRightEvent(event);
d5efd7d1 sago007 2016-03-12 11:32 221
}
d5efd7d1 sago007 2016-03-12 11:32 222
d5efd7d1 sago007 2016-03-12 11:32 223
bool isEscapeEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 224
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 225
		if ( event.key.keysym.sym == SDLK_ESCAPE ) {
d5efd7d1 sago007 2016-03-12 11:32 226
			return true;
d5efd7d1 sago007 2016-03-12 11:32 227
		}
d5efd7d1 sago007 2016-03-12 11:32 228
	}
d5efd7d1 sago007 2016-03-12 11:32 229
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
c257f027 Poul Sander 2020-08-08 16:29 230
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_B || event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK ) {
d5efd7d1 sago007 2016-03-12 11:32 231
			return true;
d5efd7d1 sago007 2016-03-12 11:32 232
		}
d5efd7d1 sago007 2016-03-12 11:32 233
	}
d5efd7d1 sago007 2016-03-12 11:32 234
	return false;
d5efd7d1 sago007 2016-03-12 11:32 235
}
d5efd7d1 sago007 2016-03-12 11:32 236
d5efd7d1 sago007 2016-03-12 11:32 237
bool isConfirmEvent(const SDL_Event& event) {
d5efd7d1 sago007 2016-03-12 11:32 238
	if ( event.type == SDL_KEYDOWN ) {
d5efd7d1 sago007 2016-03-12 11:32 239
		if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
d5efd7d1 sago007 2016-03-12 11:32 240
			return true;
d5efd7d1 sago007 2016-03-12 11:32 241
		}
d5efd7d1 sago007 2016-03-12 11:32 242
	}
d5efd7d1 sago007 2016-03-12 11:32 243
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
c257f027 Poul Sander 2020-08-08 16:29 244
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A ) {
d5efd7d1 sago007 2016-03-12 11:32 245
			return true;
d5efd7d1 sago007 2016-03-12 11:32 246
		}
d5efd7d1 sago007 2016-03-12 11:32 247
	}
d5efd7d1 sago007 2016-03-12 11:32 248
	return false;
d5efd7d1 sago007 2016-03-12 11:32 249
}
d5efd7d1 sago007 2016-03-12 11:32 250
00e866da sago007 2016-10-31 20:47 251
00e866da sago007 2016-10-31 20:47 252
bool Menu::IsActive() {
00e866da sago007 2016-10-31 20:47 253
	return running;
00e866da sago007 2016-10-31 20:47 254
}
00e866da sago007 2016-10-31 20:47 255
void Menu::Draw(SDL_Renderer* target) {
00e866da sago007 2016-10-31 20:47 256
	placeButtons();
95ab6b0d sago007 2016-11-12 16:11 257
	drawSelf(target);
a44568bd sago007 2018-03-26 11:13 258
#if DEBUG
a44568bd sago007 2018-03-26 11:13 259
	static unsigned long int Frames;
a44568bd sago007 2018-03-26 11:13 260
	static unsigned long int Ticks;
a44568bd sago007 2018-03-26 11:13 261
	static char FPS[10];
e7e189c7 sago007 2018-03-31 15:30 262
	static sago::SagoTextField fpsField;
e7e189c7 sago007 2018-03-31 15:30 263
	sagoTextSetBlueFont(fpsField);
a44568bd sago007 2018-03-26 11:13 264
	Frames++;
a44568bd sago007 2018-03-26 11:13 265
	if (SDL_GetTicks() >= Ticks + 1000) {
a44568bd sago007 2018-03-26 11:13 266
		if (Frames > 999) {
a44568bd sago007 2018-03-26 11:13 267
			Frames=999;
a44568bd sago007 2018-03-26 11:13 268
		}
a44568bd sago007 2018-03-26 11:13 269
		snprintf(FPS, sizeof(FPS), "%lu fps", Frames);
a44568bd sago007 2018-03-26 11:13 270
		Frames = 0;
a44568bd sago007 2018-03-26 11:13 271
		Ticks = SDL_GetTicks();
a44568bd sago007 2018-03-26 11:13 272
	}
e7e189c7 sago007 2018-03-31 15:30 273
	fpsField.SetText(FPS);
95e9e364 Poul Sander 2023-11-11 14:55 274
	fpsField.Draw(globalData.screen, globalData.xsize - 4, 4, sago::SagoTextField::Alignment::right, sago::SagoTextField::VerticalAlignment::top);
a44568bd sago007 2018-03-26 11:13 275
#endif
00e866da sago007 2016-10-31 20:47 276
}
092bdebe sago007 2017-03-19 14:39 277
void Menu::ProcessInput(const SDL_Event& event, bool& processed) {
409492c9 sago007 2020-06-27 22:22 278
	if (isGameControllerConnectionEvent(event)) {
409492c9 sago007 2020-06-27 22:22 279
		UnInitGameControllers();
409492c9 sago007 2020-06-27 22:22 280
		InitGameControllers();
409492c9 sago007 2020-06-27 22:22 281
		processed = true;
409492c9 sago007 2020-06-27 22:22 282
	}
409492c9 sago007 2020-06-27 22:22 283
00e866da sago007 2016-10-31 20:47 284
	if (isUpEvent(event)) {
00e866da sago007 2016-10-31 20:47 285
		marked--;
00e866da sago007 2016-10-31 20:47 286
		if (marked<0) {
00e866da sago007 2016-10-31 20:47 287
			marked = buttons.size();    //not -1, since exit is after the last element in the list
00e866da sago007 2016-10-31 20:47 288
		}
c79aae93 sago007 2016-12-23 16:55 289
		processed = true;
00e866da sago007 2016-10-31 20:47 290
	}
00e866da sago007 2016-10-31 20:47 291
00e866da sago007 2016-10-31 20:47 292
	if (isDownEvent(event)) {
00e866da sago007 2016-10-31 20:47 293
		marked++;
00e866da sago007 2016-10-31 20:47 294
		if (marked> (int)buttons.size()) {
00e866da sago007 2016-10-31 20:47 295
			marked = 0;
00e866da sago007 2016-10-31 20:47 296
		}
c79aae93 sago007 2016-12-23 16:55 297
		processed = true;
00e866da sago007 2016-10-31 20:47 298
	}
00e866da sago007 2016-10-31 20:47 299
00e866da sago007 2016-10-31 20:47 300
	if (isEscapeEvent(event) && isSubmenu) {
00e866da sago007 2016-10-31 20:47 301
		running = false;
c79aae93 sago007 2016-12-23 16:55 302
		processed = true;
00e866da sago007 2016-10-31 20:47 303
	}
00e866da sago007 2016-10-31 20:47 304
00e866da sago007 2016-10-31 20:47 305
	if (isConfirmEvent(event)) {
8283ed72 Poul Sander 2019-05-08 16:15 306
		bMouseUp = false;
00e866da sago007 2016-10-31 20:47 307
		if (marked < (int)buttons.size()) {
00e866da sago007 2016-10-31 20:47 308
			buttons.at(marked)->doAction();
00e866da sago007 2016-10-31 20:47 309
			if (buttons.at(marked)->isPopOnRun()) {
00e866da sago007 2016-10-31 20:47 310
				running = false;
00e866da sago007 2016-10-31 20:47 311
			}
00e866da sago007 2016-10-31 20:47 312
		}
00e866da sago007 2016-10-31 20:47 313
		if (marked == (int)buttons.size()) {
00e866da sago007 2016-10-31 20:47 314
			running = false;
00e866da sago007 2016-10-31 20:47 315
		}
c79aae93 sago007 2016-12-23 16:55 316
		processed = true;
00e866da sago007 2016-10-31 20:47 317
	}
00e866da sago007 2016-10-31 20:47 318
}
00e866da sago007 2016-10-31 20:47 319
50fd2c5f sago007 2016-11-12 16:05 320
void Menu::Update() {
50fd2c5f sago007 2016-11-12 16:05 321
	for (int i=0; i<(int)buttons.size(); i++) {
50fd2c5f sago007 2016-11-12 16:05 322
		buttons.at(i)->marked = (i == marked);
50fd2c5f sago007 2016-11-12 16:05 323
	}
50fd2c5f sago007 2016-11-12 16:05 324
	exit.marked = (marked == (int)buttons.size());
50fd2c5f sago007 2016-11-12 16:05 325
	Uint8 buttonState = SDL_GetMouseState(nullptr,nullptr);
50fd2c5f sago007 2016-11-12 16:05 326
	// If the mouse button is released, make bMouseUp equal true
50fd2c5f sago007 2016-11-12 16:05 327
	if ( (buttonState&SDL_BUTTON(1))==0) {
50fd2c5f sago007 2016-11-12 16:05 328
		bMouseUp=true;
50fd2c5f sago007 2016-11-12 16:05 329
	}
c7f2082f sago007 2016-03-13 11:48 330
20d77c6e sago007 2016-11-12 16:28 331
	if (abs(globalData.mousex-oldmousex)>5 || abs(globalData.mousey-oldmousey)>5) {
50fd2c5f sago007 2016-11-12 16:05 332
		for (int i=0; i< (int)buttons.size(); ++i) {
20d77c6e sago007 2016-11-12 16:28 333
			if (isClicked(*buttons.at(i), globalData.mousex, globalData.mousey)) {
50fd2c5f sago007 2016-11-12 16:05 334
				marked = i;
d5efd7d1 sago007 2016-03-12 11:32 335
			}
50fd2c5f sago007 2016-11-12 16:05 336
		}
20d77c6e sago007 2016-11-12 16:28 337
		if (isClicked(exit, globalData.mousex, globalData.mousey)) {
50fd2c5f sago007 2016-11-12 16:05 338
			marked = buttons.size();
50fd2c5f sago007 2016-11-12 16:05 339
		}
20d77c6e sago007 2016-11-12 16:28 340
		oldmousex = globalData.mousex;
20d77c6e sago007 2016-11-12 16:28 341
		oldmousey = globalData.mousey;
50fd2c5f sago007 2016-11-12 16:05 342
	}
c7f2082f sago007 2016-03-13 11:48 343
50fd2c5f sago007 2016-11-12 16:05 344
	//mouse clicked
50fd2c5f sago007 2016-11-12 16:05 345
	if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) {
50fd2c5f sago007 2016-11-12 16:05 346
		bMouseUp = false;
50fd2c5f sago007 2016-11-12 16:05 347
		for (int i=0; i< (int)buttons.size(); ++i) {
20d77c6e sago007 2016-11-12 16:28 348
			if (isClicked(*buttons.at(i), globalData.mousex, globalData.mousey)) {
50fd2c5f sago007 2016-11-12 16:05 349
				buttons.at(i)->doAction();
50fd2c5f sago007 2016-11-12 16:05 350
				if (buttons.at(i)->isPopOnRun()) {
d5efd7d1 sago007 2016-03-12 11:32 351
					running = false;
d5efd7d1 sago007 2016-03-12 11:32 352
				}
4dbeef9f sago007 2018-04-14 10:48 353
				//Quit here to ensure that we do not continue checking buttons after we have done the action.
4dbeef9f sago007 2018-04-14 10:48 354
				return;
826cf176 sago007 2016-03-12 10:53 355
			}
c53e6443 sago007 2012-04-17 11:04 356
		}
20d77c6e sago007 2016-11-12 16:28 357
		if (isClicked(exit,  globalData.mousex, globalData.mousey)) {
50fd2c5f sago007 2016-11-12 16:05 358
			running = false;
c53e6443 sago007 2012-04-17 11:04 359
		}
50fd2c5f sago007 2016-11-12 16:05 360
	}
50fd2c5f sago007 2016-11-12 16:05 361
}
c53e6443 sago007 2012-04-17 11:04 362
1970-01-01 00:00 363