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