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