git repos / blockattack-game

blame: source/code/DialogBox.cpp

normal view · raw

59dcb571 sago007 2016-02-14 15:34 1
/*
7a956470 sago007 2016-02-14 17:09 2
===========================================================================
7a956470 sago007 2016-02-14 17:09 3
blockattack - Block Attack - Rise of the Blocks
7a956470 sago007 2016-02-14 17:09 4
Copyright (C) 2005-2016 Poul Sander
7a956470 sago007 2016-02-14 17:09 5
7a956470 sago007 2016-02-14 17:09 6
This program is free software: you can redistribute it and/or modify
7a956470 sago007 2016-02-14 17:09 7
it under the terms of the GNU General Public License as published by
7a956470 sago007 2016-02-14 17:09 8
the Free Software Foundation, either version 2 of the License, or
7a956470 sago007 2016-02-14 17:09 9
(at your option) any later version.
7a956470 sago007 2016-02-14 17:09 10
7a956470 sago007 2016-02-14 17:09 11
This program is distributed in the hope that it will be useful,
7a956470 sago007 2016-02-14 17:09 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
7a956470 sago007 2016-02-14 17:09 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7a956470 sago007 2016-02-14 17:09 14
GNU General Public License for more details.
7a956470 sago007 2016-02-14 17:09 15
7a956470 sago007 2016-02-14 17:09 16
You should have received a copy of the GNU General Public License
7a956470 sago007 2016-02-14 17:09 17
along with this program.  If not, see http://www.gnu.org/licenses/
7a956470 sago007 2016-02-14 17:09 18
7a956470 sago007 2016-02-14 17:09 19
Source information and contacts persons can be found at
7a956470 sago007 2016-02-14 17:09 20
http://www.blockattack.net
7a956470 sago007 2016-02-14 17:09 21
===========================================================================
7a956470 sago007 2016-02-14 17:09 22
*/
59dcb571 sago007 2016-02-14 15:34 23
59dcb571 sago007 2016-02-14 15:34 24
#include "DialogBox.hpp"
59dcb571 sago007 2016-02-14 15:34 25
#include "global.hpp"
59dcb571 sago007 2016-02-14 15:34 26
#include "common.h"
59dcb571 sago007 2016-02-14 15:34 27
#include "ReadKeyboard.h"
fc425e41 Poul Sander 2019-08-14 18:10 28
#include "utf8.h"
eb4cc88e Poul Sander 2020-05-30 20:42 29
#include "MenuSystem.h"
59dcb571 sago007 2016-02-14 15:34 30
d2ba3cf5 sago007 2018-05-13 15:00 31
static void setButtonFont(const sago::SagoDataHolder* holder, sago::SagoTextField& field, const char* text) {
34f6ec6d sago007 2018-03-29 16:17 32
	field.SetHolder(holder);
34f6ec6d sago007 2018-03-29 16:17 33
	field.SetFont("freeserif");
34f6ec6d sago007 2018-03-29 16:17 34
	field.SetColor({255,255,255,255});
34f6ec6d sago007 2018-03-29 16:17 35
	field.SetFontSize(24);
34f6ec6d sago007 2018-03-29 16:17 36
	field.SetOutline(1, {0,0,0,255});
34f6ec6d sago007 2018-03-29 16:17 37
	field.SetText(text);
34f6ec6d sago007 2018-03-29 16:17 38
}
34f6ec6d sago007 2018-03-29 16:17 39
41f037b2 sago007 2016-04-17 16:37 40
static void DrawRect(SDL_Renderer* target, int topx, int topy, int height, int width, const std::string& name) {
daa660ab sago007 2016-04-16 18:13 41
	const int size = 32;
daa660ab sago007 2016-04-16 18:13 42
	SDL_Rect bounds_ns = {topx, topy+size, width, height-2*size};  //bounds for south
daa660ab sago007 2016-04-16 18:13 43
	SDL_Rect bounds_e = {topx, topy, width-size, height};
72383a9e sago007 2016-10-30 11:56 44
	const sago::SagoSprite& n = globalData.spriteHolder->GetSprite(name+"n");
72383a9e sago007 2016-10-30 11:56 45
	const sago::SagoSprite& s = globalData.spriteHolder->GetSprite(name+"s");
72383a9e sago007 2016-10-30 11:56 46
	const sago::SagoSprite& e = globalData.spriteHolder->GetSprite(name+"e");
72383a9e sago007 2016-10-30 11:56 47
	const sago::SagoSprite& w = globalData.spriteHolder->GetSprite(name+"w");
72383a9e sago007 2016-10-30 11:56 48
	const sago::SagoSprite& fill = globalData.spriteHolder->GetSprite(name+"fill");
daa660ab sago007 2016-04-16 18:13 49
	for (int i = 1; i < width/size; ++i) {
daa660ab sago007 2016-04-16 18:13 50
		n.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy, bounds_e);
daa660ab sago007 2016-04-16 18:13 51
		for (int j = 1; j < height/size; ++j) {
daa660ab sago007 2016-04-16 18:13 52
			w.DrawBounded(target, SDL_GetTicks(), topx, topy+j*size, bounds_ns);
daa660ab sago007 2016-04-16 18:13 53
			fill.Draw(target, SDL_GetTicks(),topx+i*size, topy+j*size);
daa660ab sago007 2016-04-16 18:13 54
			e.DrawBounded(target, SDL_GetTicks(), topx+width-size, topy+j*size, bounds_ns);
daa660ab sago007 2016-04-16 18:13 55
		}
daa660ab sago007 2016-04-16 18:13 56
		s.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy+height-size, bounds_e);
daa660ab sago007 2016-04-16 18:13 57
	}
daa660ab sago007 2016-04-16 18:13 58
	//Corners
72383a9e sago007 2016-10-30 11:56 59
	const sago::SagoSprite& nw = globalData.spriteHolder->GetSprite(name+"nw");
72383a9e sago007 2016-10-30 11:56 60
	const sago::SagoSprite& ne = globalData.spriteHolder->GetSprite(name+"ne");
72383a9e sago007 2016-10-30 11:56 61
	const sago::SagoSprite& se = globalData.spriteHolder->GetSprite(name+"se");
72383a9e sago007 2016-10-30 11:56 62
	const sago::SagoSprite& sw = globalData.spriteHolder->GetSprite(name+"sw");
daa660ab sago007 2016-04-16 18:13 63
	nw.Draw(target, SDL_GetTicks(), topx, topy);
daa660ab sago007 2016-04-16 18:13 64
	ne.Draw(target, SDL_GetTicks(), topx+width-size, topy);
daa660ab sago007 2016-04-16 18:13 65
	se.Draw(target, SDL_GetTicks(), topx+width-size, topy+height-size);
daa660ab sago007 2016-04-16 18:13 66
	sw.Draw(target, SDL_GetTicks(), topx, topy+height-size);
41f037b2 sago007 2016-04-17 16:37 67
}
41f037b2 sago007 2016-04-17 16:37 68
41f037b2 sago007 2016-04-17 16:37 69
static void DrawRectWhite(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 70
	std::string name = "ui_rect_white_";
41f037b2 sago007 2016-04-17 16:37 71
	DrawRect(target, topx, topy, height, width, name);
41f037b2 sago007 2016-04-17 16:37 72
}
41f037b2 sago007 2016-04-17 16:37 73
db2f559e sago007 2018-05-10 10:36 74
void DrawRectYellow(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 75
	std::string name = "ui_rect_yellow_";
41f037b2 sago007 2016-04-17 16:37 76
	DrawRect(target, topx, topy, height, width, name);
daa660ab sago007 2016-04-16 18:13 77
}
daa660ab sago007 2016-04-16 18:13 78
96e759d9 sago007 2016-04-17 18:45 79
bool OpenDialogbox(int x, int y, std::string& name, const std::string& header) {
96e759d9 sago007 2016-04-17 18:45 80
	DialogBox d(x, y, name, header);
59dcb571 sago007 2016-02-14 15:34 81
	RunGameState(d);
59dcb571 sago007 2016-02-14 15:34 82
	if (d.IsUpdated()) {
59dcb571 sago007 2016-02-14 15:34 83
		name = d.GetName();
59dcb571 sago007 2016-02-14 15:34 84
		return true;
59dcb571 sago007 2016-02-14 15:34 85
	}
59dcb571 sago007 2016-02-14 15:34 86
	return false;
59dcb571 sago007 2016-02-14 15:34 87
}
59dcb571 sago007 2016-02-14 15:34 88
96e759d9 sago007 2016-04-17 18:45 89
DialogBox::DialogBox(int x, int y, const std::string& name, const std::string& header) : header(header) {
59dcb571 sago007 2016-02-14 15:34 90
	this->x = x;
59dcb571 sago007 2016-02-14 15:34 91
	this->y = y;
59dcb571 sago007 2016-02-14 15:34 92
	SetName(name);
34f6ec6d sago007 2018-03-29 16:17 93
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), headerLabel, header.c_str());
34f6ec6d sago007 2018-03-29 16:17 94
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), enterLabel, _("Enter to accept"));
34f6ec6d sago007 2018-03-29 16:17 95
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), cancelLabel, _("Esc to cancel"));
bd788178 sago007 2018-03-31 16:03 96
	sagoTextSetBlueFont(textField);
bd788178 sago007 2018-03-31 16:03 97
	sagoTextSetBlueFont(cursorLabel);
bd788178 sago007 2018-03-31 16:03 98
	cursorLabel.SetText("|");
45d36316 Poul Sander 2020-06-01 19:23 99
	for (auto position = virtualKeyboard.alphabet.begin(); position != virtualKeyboard.alphabet.end() ; utf8::advance(position, 1, virtualKeyboard.alphabet.end())) {
fc425e41 Poul Sander 2019-08-14 18:10 100
		auto endPosition = position;
45d36316 Poul Sander 2020-06-01 19:23 101
		utf8::advance(endPosition, 1, virtualKeyboard.alphabet.end());
fc425e41 Poul Sander 2019-08-14 18:10 102
		std::string theChar(position, endPosition);
45d36316 Poul Sander 2020-06-01 19:23 103
		virtualKeyboard.gamePadChars.push_back(theChar);
45d36316 Poul Sander 2020-06-01 19:23 104
		virtualKeyboard.gamePadCharFields.emplace_back();
45d36316 Poul Sander 2020-06-01 19:23 105
		sago::SagoTextField& tf = virtualKeyboard.gamePadCharFields.back();
fc425e41 Poul Sander 2019-08-14 18:10 106
		setButtonFont(&globalData.spriteHolder->GetDataHolder(), tf, theChar.c_str());
d0739659 Poul Sander 2020-06-01 19:17 107
		std::cout << *position;
fc425e41 Poul Sander 2019-08-14 18:10 108
	}
d0739659 Poul Sander 2020-06-01 19:17 109
	std::cout << "\n";
59dcb571 sago007 2016-02-14 15:34 110
}
59dcb571 sago007 2016-02-14 15:34 111
59dcb571 sago007 2016-02-14 15:34 112
59dcb571 sago007 2016-02-14 15:34 113
DialogBox::~DialogBox() {
59dcb571 sago007 2016-02-14 15:34 114
}
59dcb571 sago007 2016-02-14 15:34 115
59dcb571 sago007 2016-02-14 15:34 116
bool DialogBox::IsActive() {
59dcb571 sago007 2016-02-14 15:34 117
	return isActive;
59dcb571 sago007 2016-02-14 15:34 118
}
59dcb571 sago007 2016-02-14 15:34 119
610539a5 Poul Sander 2019-12-04 16:47 120
static bool insideRect (int x, int y, int height, int width) {
610539a5 Poul Sander 2019-12-04 16:47 121
	if (globalData.mousex < x) {
610539a5 Poul Sander 2019-12-04 16:47 122
		return false;
610539a5 Poul Sander 2019-12-04 16:47 123
	}
610539a5 Poul Sander 2019-12-04 16:47 124
	if (globalData.mousex > x+width) {
610539a5 Poul Sander 2019-12-04 16:47 125
		return false;
610539a5 Poul Sander 2019-12-04 16:47 126
	}
610539a5 Poul Sander 2019-12-04 16:47 127
	if (globalData.mousey < y) {
610539a5 Poul Sander 2019-12-04 16:47 128
		return false;
610539a5 Poul Sander 2019-12-04 16:47 129
	}
610539a5 Poul Sander 2019-12-04 16:47 130
	if (globalData.mousey > y+height) {
610539a5 Poul Sander 2019-12-04 16:47 131
		return false;
610539a5 Poul Sander 2019-12-04 16:47 132
	}
610539a5 Poul Sander 2019-12-04 16:47 133
	return true;
610539a5 Poul Sander 2019-12-04 16:47 134
}
610539a5 Poul Sander 2019-12-04 16:47 135
afe1bac4 Poul Sander 2020-05-24 18:34 136
// For use with the mouse/gamepad keyboard
afe1bac4 Poul Sander 2020-05-24 18:34 137
int keyboardRowLimit = 20;
afe1bac4 Poul Sander 2020-05-24 18:34 138
59dcb571 sago007 2016-02-14 15:34 139
void DialogBox::Draw(SDL_Renderer* target) {
72383a9e sago007 2016-10-30 11:56 140
	DrawBackground(globalData.screen);
72383a9e sago007 2016-10-30 11:56 141
	this->x = globalData.xsize/2-300;
72383a9e sago007 2016-10-30 11:56 142
	this->y = globalData.ysize/2-100;
647f12da sago007 2016-09-12 20:07 143
	DrawRectYellow(target, x, y, 200, 600);
34f6ec6d sago007 2018-03-29 16:17 144
	headerLabel.Draw(target, x+300, y+20, sago::SagoTextField::Alignment::center);
610539a5 Poul Sander 2019-12-04 16:47 145
	DrawRectYellow(target, x+25, y+128, 50, 250);
34f6ec6d sago007 2018-03-29 16:17 146
	enterLabel.Draw(target, x+150, y+140, sago::SagoTextField::Alignment::center);
610539a5 Poul Sander 2019-12-04 16:47 147
	DrawRectYellow(target, x+325, y+128, 50, 250);
34f6ec6d sago007 2018-03-29 16:17 148
	cancelLabel.Draw(target, x+450, y+140, sago::SagoTextField::Alignment::center);
647f12da sago007 2016-09-12 20:07 149
	DrawRectWhite(target, x+26, y+64, 54, 600-2*26);
0b8fb137 sago007 2018-03-31 19:53 150
	textField.SetText(rk->GetString());
bd788178 sago007 2018-03-31 16:03 151
	textField.Draw(target, x+40, y+76);
59dcb571 sago007 2016-02-14 15:34 152
	std::string strHolder = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 153
	strHolder.erase((int)rk->CharsBeforeCursor());
59dcb571 sago007 2016-02-14 15:34 154
59dcb571 sago007 2016-02-14 15:34 155
	if (((SDL_GetTicks()/600)%2)==1) {
bd788178 sago007 2018-03-31 16:03 156
		int width = 0;
bd788178 sago007 2018-03-31 16:03 157
		textField.GetRenderedSize( strHolder.c_str(), &width);
bd788178 sago007 2018-03-31 16:03 158
		width -= 2;
bd788178 sago007 2018-03-31 16:03 159
		cursorLabel.Draw(target, x+40+width,y+76);
59dcb571 sago007 2016-02-14 15:34 160
	}
c5cd33e7 Poul Sander 2019-12-04 19:54 161
	const sago::SagoSprite& marked = globalData.spriteHolder->GetSprite("i_level_check_box_marked");
45d36316 Poul Sander 2020-06-01 19:23 162
	for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
45d36316 Poul Sander 2020-06-01 19:23 163
		if (virtualKeyboard.selectedChar == static_cast<int>(i)) {
afe1bac4 Poul Sander 2020-05-24 18:34 164
			marked.Draw(target, SDL_GetTicks(), globalData.xsize/2-400+(i%keyboardRowLimit)*40-5, globalData.ysize/2+150+(i/keyboardRowLimit)*40-5);
c5cd33e7 Poul Sander 2019-12-04 19:54 165
		}
45d36316 Poul Sander 2020-06-01 19:23 166
		sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(i);
afe1bac4 Poul Sander 2020-05-24 18:34 167
		f.Draw(target, globalData.xsize/2-400+(i%keyboardRowLimit)*40, globalData.ysize/2+150+(i/keyboardRowLimit)*40);
fc425e41 Poul Sander 2019-08-14 18:10 168
	}
59dcb571 sago007 2016-02-14 15:34 169
}
59dcb571 sago007 2016-02-14 15:34 170
eb4cc88e Poul Sander 2020-05-30 20:42 171
eb4cc88e Poul Sander 2020-05-30 20:42 172
static bool isGamePadStartEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 173
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 174
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_START ) {
eb4cc88e Poul Sander 2020-05-30 20:42 175
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 176
		}
eb4cc88e Poul Sander 2020-05-30 20:42 177
	}
eb4cc88e Poul Sander 2020-05-30 20:42 178
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 179
}
eb4cc88e Poul Sander 2020-05-30 20:42 180
eb4cc88e Poul Sander 2020-05-30 20:42 181
eb4cc88e Poul Sander 2020-05-30 20:42 182
static bool isGamePadBackEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 183
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 184
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK ) {
eb4cc88e Poul Sander 2020-05-30 20:42 185
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 186
		}
eb4cc88e Poul Sander 2020-05-30 20:42 187
	}
eb4cc88e Poul Sander 2020-05-30 20:42 188
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 189
}
eb4cc88e Poul Sander 2020-05-30 20:42 190
eb4cc88e Poul Sander 2020-05-30 20:42 191
static bool isGamePadLEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 192
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 193
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ) {
eb4cc88e Poul Sander 2020-05-30 20:42 194
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 195
		}
eb4cc88e Poul Sander 2020-05-30 20:42 196
	}
eb4cc88e Poul Sander 2020-05-30 20:42 197
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 198
}
eb4cc88e Poul Sander 2020-05-30 20:42 199
eb4cc88e Poul Sander 2020-05-30 20:42 200
static bool isGamePadREvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 201
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 202
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
eb4cc88e Poul Sander 2020-05-30 20:42 203
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 204
		}
eb4cc88e Poul Sander 2020-05-30 20:42 205
	}
eb4cc88e Poul Sander 2020-05-30 20:42 206
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 207
}
eb4cc88e Poul Sander 2020-05-30 20:42 208
eb4cc88e Poul Sander 2020-05-30 20:42 209
eb4cc88e Poul Sander 2020-05-30 20:42 210
void DialogBox::virtualKeyboardWriteSelectedChar(ReadKeyboard *rk, const std::string& insertChar) const {
45d36316 Poul Sander 2020-06-01 19:23 211
	if (insertChar == virtualKeyboard.backspace) {
eb4cc88e Poul Sander 2020-05-30 20:42 212
		rk->emulateBackspace();
eb4cc88e Poul Sander 2020-05-30 20:42 213
	}
45d36316 Poul Sander 2020-06-01 19:23 214
	else if (insertChar == virtualKeyboard.leftChar) {
eb4cc88e Poul Sander 2020-05-30 20:42 215
		rk->cursorLeft();
eb4cc88e Poul Sander 2020-05-30 20:42 216
	}
45d36316 Poul Sander 2020-06-01 19:23 217
	else if (insertChar == virtualKeyboard.rightChar) {
eb4cc88e Poul Sander 2020-05-30 20:42 218
		rk->cursorRight();
eb4cc88e Poul Sander 2020-05-30 20:42 219
	}
eb4cc88e Poul Sander 2020-05-30 20:42 220
	else {
eb4cc88e Poul Sander 2020-05-30 20:42 221
		rk->putchar(insertChar);
eb4cc88e Poul Sander 2020-05-30 20:42 222
	}
eb4cc88e Poul Sander 2020-05-30 20:42 223
}
eb4cc88e Poul Sander 2020-05-30 20:42 224
eb4cc88e Poul Sander 2020-05-30 20:42 225
c7f2082f sago007 2016-03-13 11:48 226
void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
59dcb571 sago007 2016-02-14 15:34 227
	if (event.type == SDL_TEXTINPUT) {
72383a9e sago007 2016-10-30 11:56 228
		if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 229
			Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 230
		}
59dcb571 sago007 2016-02-14 15:34 231
	}
59dcb571 sago007 2016-02-14 15:34 232
59dcb571 sago007 2016-02-14 15:34 233
	if ( event.type == SDL_KEYDOWN ) {
59dcb571 sago007 2016-02-14 15:34 234
		if ( (event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER) ) {
59dcb571 sago007 2016-02-14 15:34 235
			name = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 236
			updated = true;
59dcb571 sago007 2016-02-14 15:34 237
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 238
		}
59dcb571 sago007 2016-02-14 15:34 239
		else if ( (event.key.keysym.sym == SDLK_ESCAPE) ) {
59dcb571 sago007 2016-02-14 15:34 240
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 241
		}
59dcb571 sago007 2016-02-14 15:34 242
		else {
72383a9e sago007 2016-10-30 11:56 243
			if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 244
				Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 245
			}
59dcb571 sago007 2016-02-14 15:34 246
		}
59dcb571 sago007 2016-02-14 15:34 247
	}
eb4cc88e Poul Sander 2020-05-30 20:42 248
	else {
eb4cc88e Poul Sander 2020-05-30 20:42 249
		if (isGamePadStartEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 250
			name = rk->GetString();
eb4cc88e Poul Sander 2020-05-30 20:42 251
			updated = true;
eb4cc88e Poul Sander 2020-05-30 20:42 252
			isActive = false;
eb4cc88e Poul Sander 2020-05-30 20:42 253
		}
eb4cc88e Poul Sander 2020-05-30 20:42 254
		if (isGamePadBackEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 255
			isActive = false;
eb4cc88e Poul Sander 2020-05-30 20:42 256
		}
eb4cc88e Poul Sander 2020-05-30 20:42 257
		if (isConfirmEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 258
			const sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(virtualKeyboard.selectedChar);
eb4cc88e Poul Sander 2020-05-30 20:42 259
			const std::string& insertChar = f.GetText();
eb4cc88e Poul Sander 2020-05-30 20:42 260
			virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
eb4cc88e Poul Sander 2020-05-30 20:42 261
		}
eb4cc88e Poul Sander 2020-05-30 20:42 262
		if (isEscapeEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 263
			rk->emulateBackspace();
eb4cc88e Poul Sander 2020-05-30 20:42 264
		}
eb4cc88e Poul Sander 2020-05-30 20:42 265
		if (isGamePadLEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 266
			rk->cursorLeft();
eb4cc88e Poul Sander 2020-05-30 20:42 267
		}
eb4cc88e Poul Sander 2020-05-30 20:42 268
		if (isGamePadREvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 269
			rk->cursorRight();
eb4cc88e Poul Sander 2020-05-30 20:42 270
		}
eb4cc88e Poul Sander 2020-05-30 20:42 271
		if (isRightEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 272
			++virtualKeyboard.selectedChar;
eb4cc88e Poul Sander 2020-05-30 20:42 273
		}
eb4cc88e Poul Sander 2020-05-30 20:42 274
		if (isLeftEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 275
			--virtualKeyboard.selectedChar;
eb4cc88e Poul Sander 2020-05-30 20:42 276
		}
eb4cc88e Poul Sander 2020-05-30 20:42 277
		if (isDownEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 278
			virtualKeyboard.selectedChar+= keyboardRowLimit;
eb4cc88e Poul Sander 2020-05-30 20:42 279
		}
eb4cc88e Poul Sander 2020-05-30 20:42 280
		if (isUpEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 281
			virtualKeyboard.selectedChar -= keyboardRowLimit;
eb4cc88e Poul Sander 2020-05-30 20:42 282
		}
45d36316 Poul Sander 2020-06-01 19:23 283
		if (virtualKeyboard.selectedChar < 0) {
45d36316 Poul Sander 2020-06-01 19:23 284
			virtualKeyboard.selectedChar = 0;
eb4cc88e Poul Sander 2020-05-30 20:42 285
		}
45d36316 Poul Sander 2020-06-01 19:23 286
		if (virtualKeyboard.selectedChar >= static_cast<int>(virtualKeyboard.gamePadCharFields.size())) {
45d36316 Poul Sander 2020-06-01 19:23 287
			virtualKeyboard.selectedChar = virtualKeyboard.gamePadCharFields.size()-1;
eb4cc88e Poul Sander 2020-05-30 20:42 288
		}
eb4cc88e Poul Sander 2020-05-30 20:42 289
	}
7a956470 sago007 2016-02-14 17:09 290
	processed = true;
610539a5 Poul Sander 2019-12-04 16:47 291
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
610539a5 Poul Sander 2019-12-04 16:47 292
		bMouseUp=true;
610539a5 Poul Sander 2019-12-04 16:47 293
	}
610539a5 Poul Sander 2019-12-04 16:47 294
610539a5 Poul Sander 2019-12-04 16:47 295
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
610539a5 Poul Sander 2019-12-04 16:47 296
		bMouseUp = false;
610539a5 Poul Sander 2019-12-04 16:47 297
		if (insideRect(x+25, y+128, 50, 250)) {
610539a5 Poul Sander 2019-12-04 16:47 298
			name = rk->GetString();
610539a5 Poul Sander 2019-12-04 16:47 299
			updated = true;
610539a5 Poul Sander 2019-12-04 16:47 300
			isActive = false;
610539a5 Poul Sander 2019-12-04 16:47 301
		}
610539a5 Poul Sander 2019-12-04 16:47 302
		if (insideRect(x+325, y+128, 50, 250)) {
610539a5 Poul Sander 2019-12-04 16:47 303
			isActive = false;
610539a5 Poul Sander 2019-12-04 16:47 304
		}
45d36316 Poul Sander 2020-06-01 19:23 305
		for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
45d36316 Poul Sander 2020-06-01 19:23 306
			sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(i);
afe1bac4 Poul Sander 2020-05-24 18:34 307
			auto topx = globalData.xsize/2-400+(i%keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 308
			auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 309
			if (insideRect(topx, topy, 30, 30)) {
c634a901 Poul Sander 2020-05-24 19:37 310
				std::string insertChar = f.GetText();
eb4cc88e Poul Sander 2020-05-30 20:42 311
				virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
afe1bac4 Poul Sander 2020-05-24 18:34 312
			}
afe1bac4 Poul Sander 2020-05-24 18:34 313
		}
afe1bac4 Poul Sander 2020-05-24 18:34 314
	}
afe1bac4 Poul Sander 2020-05-24 18:34 315
d0739659 Poul Sander 2020-06-01 19:17 316
	if (globalData.mousex != oldmousex || globalData.mousey != oldmousey) {
45d36316 Poul Sander 2020-06-01 19:23 317
		for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
afe1bac4 Poul Sander 2020-05-24 18:34 318
			auto topx = globalData.xsize/2-400+(i%keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 319
			auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 320
			if (insideRect(topx, topy, 30, 30)) {
45d36316 Poul Sander 2020-06-01 19:23 321
				virtualKeyboard.selectedChar = i;
afe1bac4 Poul Sander 2020-05-24 18:34 322
			}
afe1bac4 Poul Sander 2020-05-24 18:34 323
		}
d0739659 Poul Sander 2020-06-01 19:17 324
		oldmousex = globalData.mousex;
d0739659 Poul Sander 2020-06-01 19:17 325
		oldmousey = globalData.mousey;
610539a5 Poul Sander 2019-12-04 16:47 326
	}
59dcb571 sago007 2016-02-14 15:34 327
}
59dcb571 sago007 2016-02-14 15:34 328
59dcb571 sago007 2016-02-14 15:34 329
void DialogBox::SetName(const std::string& name) {
59dcb571 sago007 2016-02-14 15:34 330
	this->name = name;
59dcb571 sago007 2016-02-14 15:34 331
	rk = std::make_shared<ReadKeyboard>(name.c_str());
59dcb571 sago007 2016-02-14 15:34 332
}
59dcb571 sago007 2016-02-14 15:34 333
59dcb571 sago007 2016-02-14 15:34 334
std::string DialogBox::GetName() const {
59dcb571 sago007 2016-02-14 15:34 335
	return name;
59dcb571 sago007 2016-02-14 15:34 336
}
59dcb571 sago007 2016-02-14 15:34 337
59dcb571 sago007 2016-02-14 15:34 338
bool DialogBox::IsUpdated() const {
59dcb571 sago007 2016-02-14 15:34 339
	return updated;
b1a7bd3e Poul Sander 2016-12-19 21:53 340
}
1970-01-01 00:00 341