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());
a0887514 Poul Sander 2020-06-27 12:37 107
		if (globalData.verboseLevel) {
a0887514 Poul Sander 2020-06-27 12:37 108
			std::cout << *position;
a0887514 Poul Sander 2020-06-27 12:37 109
		}
a0887514 Poul Sander 2020-06-27 12:37 110
	}
a0887514 Poul Sander 2020-06-27 12:37 111
	if (globalData.verboseLevel) {
a0887514 Poul Sander 2020-06-27 12:37 112
		std::cout << "\n";
fc425e41 Poul Sander 2019-08-14 18:10 113
	}
59dcb571 sago007 2016-02-14 15:34 114
}
59dcb571 sago007 2016-02-14 15:34 115
59dcb571 sago007 2016-02-14 15:34 116
59dcb571 sago007 2016-02-14 15:34 117
DialogBox::~DialogBox() {
59dcb571 sago007 2016-02-14 15:34 118
}
59dcb571 sago007 2016-02-14 15:34 119
59dcb571 sago007 2016-02-14 15:34 120
bool DialogBox::IsActive() {
59dcb571 sago007 2016-02-14 15:34 121
	return isActive;
59dcb571 sago007 2016-02-14 15:34 122
}
59dcb571 sago007 2016-02-14 15:34 123
610539a5 Poul Sander 2019-12-04 16:47 124
static bool insideRect (int x, int y, int height, int width) {
610539a5 Poul Sander 2019-12-04 16:47 125
	if (globalData.mousex < x) {
610539a5 Poul Sander 2019-12-04 16:47 126
		return false;
610539a5 Poul Sander 2019-12-04 16:47 127
	}
610539a5 Poul Sander 2019-12-04 16:47 128
	if (globalData.mousex > x+width) {
610539a5 Poul Sander 2019-12-04 16:47 129
		return false;
610539a5 Poul Sander 2019-12-04 16:47 130
	}
610539a5 Poul Sander 2019-12-04 16:47 131
	if (globalData.mousey < y) {
610539a5 Poul Sander 2019-12-04 16:47 132
		return false;
610539a5 Poul Sander 2019-12-04 16:47 133
	}
610539a5 Poul Sander 2019-12-04 16:47 134
	if (globalData.mousey > y+height) {
610539a5 Poul Sander 2019-12-04 16:47 135
		return false;
610539a5 Poul Sander 2019-12-04 16:47 136
	}
610539a5 Poul Sander 2019-12-04 16:47 137
	return true;
610539a5 Poul Sander 2019-12-04 16:47 138
}
610539a5 Poul Sander 2019-12-04 16:47 139
afe1bac4 Poul Sander 2020-05-24 18:34 140
// For use with the mouse/gamepad keyboard
afe1bac4 Poul Sander 2020-05-24 18:34 141
int keyboardRowLimit = 20;
afe1bac4 Poul Sander 2020-05-24 18:34 142
59dcb571 sago007 2016-02-14 15:34 143
void DialogBox::Draw(SDL_Renderer* target) {
72383a9e sago007 2016-10-30 11:56 144
	DrawBackground(globalData.screen);
72383a9e sago007 2016-10-30 11:56 145
	this->x = globalData.xsize/2-300;
72383a9e sago007 2016-10-30 11:56 146
	this->y = globalData.ysize/2-100;
647f12da sago007 2016-09-12 20:07 147
	DrawRectYellow(target, x, y, 200, 600);
34f6ec6d sago007 2018-03-29 16:17 148
	headerLabel.Draw(target, x+300, y+20, sago::SagoTextField::Alignment::center);
610539a5 Poul Sander 2019-12-04 16:47 149
	DrawRectYellow(target, x+25, y+128, 50, 250);
34f6ec6d sago007 2018-03-29 16:17 150
	enterLabel.Draw(target, x+150, y+140, sago::SagoTextField::Alignment::center);
610539a5 Poul Sander 2019-12-04 16:47 151
	DrawRectYellow(target, x+325, y+128, 50, 250);
34f6ec6d sago007 2018-03-29 16:17 152
	cancelLabel.Draw(target, x+450, y+140, sago::SagoTextField::Alignment::center);
647f12da sago007 2016-09-12 20:07 153
	DrawRectWhite(target, x+26, y+64, 54, 600-2*26);
0b8fb137 sago007 2018-03-31 19:53 154
	textField.SetText(rk->GetString());
bd788178 sago007 2018-03-31 16:03 155
	textField.Draw(target, x+40, y+76);
59dcb571 sago007 2016-02-14 15:34 156
	std::string strHolder = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 157
	strHolder.erase((int)rk->CharsBeforeCursor());
59dcb571 sago007 2016-02-14 15:34 158
59dcb571 sago007 2016-02-14 15:34 159
	if (((SDL_GetTicks()/600)%2)==1) {
bd788178 sago007 2018-03-31 16:03 160
		int width = 0;
bd788178 sago007 2018-03-31 16:03 161
		textField.GetRenderedSize( strHolder.c_str(), &width);
bd788178 sago007 2018-03-31 16:03 162
		width -= 2;
bd788178 sago007 2018-03-31 16:03 163
		cursorLabel.Draw(target, x+40+width,y+76);
59dcb571 sago007 2016-02-14 15:34 164
	}
c5cd33e7 Poul Sander 2019-12-04 19:54 165
	const sago::SagoSprite& marked = globalData.spriteHolder->GetSprite("i_level_check_box_marked");
45d36316 Poul Sander 2020-06-01 19:23 166
	for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
45d36316 Poul Sander 2020-06-01 19:23 167
		if (virtualKeyboard.selectedChar == static_cast<int>(i)) {
afe1bac4 Poul Sander 2020-05-24 18:34 168
			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 169
		}
45d36316 Poul Sander 2020-06-01 19:23 170
		sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(i);
afe1bac4 Poul Sander 2020-05-24 18:34 171
		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 172
	}
59dcb571 sago007 2016-02-14 15:34 173
}
59dcb571 sago007 2016-02-14 15:34 174
eb4cc88e Poul Sander 2020-05-30 20:42 175
eb4cc88e Poul Sander 2020-05-30 20:42 176
static bool isGamePadStartEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 177
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 178
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_START ) {
eb4cc88e Poul Sander 2020-05-30 20:42 179
			return true;
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
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 183
}
eb4cc88e Poul Sander 2020-05-30 20:42 184
eb4cc88e Poul Sander 2020-05-30 20:42 185
eb4cc88e Poul Sander 2020-05-30 20:42 186
static bool isGamePadBackEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 187
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 188
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK ) {
eb4cc88e Poul Sander 2020-05-30 20:42 189
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 190
		}
eb4cc88e Poul Sander 2020-05-30 20:42 191
	}
eb4cc88e Poul Sander 2020-05-30 20:42 192
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 193
}
eb4cc88e Poul Sander 2020-05-30 20:42 194
eb4cc88e Poul Sander 2020-05-30 20:42 195
static bool isGamePadLEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 196
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 197
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ) {
eb4cc88e Poul Sander 2020-05-30 20:42 198
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 199
		}
eb4cc88e Poul Sander 2020-05-30 20:42 200
	}
eb4cc88e Poul Sander 2020-05-30 20:42 201
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 202
}
eb4cc88e Poul Sander 2020-05-30 20:42 203
eb4cc88e Poul Sander 2020-05-30 20:42 204
static bool isGamePadREvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 205
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 206
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
eb4cc88e Poul Sander 2020-05-30 20:42 207
			return true;
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
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 211
}
eb4cc88e Poul Sander 2020-05-30 20:42 212
eb4cc88e Poul Sander 2020-05-30 20:42 213
eb4cc88e Poul Sander 2020-05-30 20:42 214
void DialogBox::virtualKeyboardWriteSelectedChar(ReadKeyboard *rk, const std::string& insertChar) const {
45d36316 Poul Sander 2020-06-01 19:23 215
	if (insertChar == virtualKeyboard.backspace) {
eb4cc88e Poul Sander 2020-05-30 20:42 216
		rk->emulateBackspace();
eb4cc88e Poul Sander 2020-05-30 20:42 217
	}
45d36316 Poul Sander 2020-06-01 19:23 218
	else if (insertChar == virtualKeyboard.leftChar) {
eb4cc88e Poul Sander 2020-05-30 20:42 219
		rk->cursorLeft();
eb4cc88e Poul Sander 2020-05-30 20:42 220
	}
45d36316 Poul Sander 2020-06-01 19:23 221
	else if (insertChar == virtualKeyboard.rightChar) {
eb4cc88e Poul Sander 2020-05-30 20:42 222
		rk->cursorRight();
eb4cc88e Poul Sander 2020-05-30 20:42 223
	}
eb4cc88e Poul Sander 2020-05-30 20:42 224
	else {
eb4cc88e Poul Sander 2020-05-30 20:42 225
		rk->putchar(insertChar);
eb4cc88e Poul Sander 2020-05-30 20:42 226
	}
eb4cc88e Poul Sander 2020-05-30 20:42 227
}
eb4cc88e Poul Sander 2020-05-30 20:42 228
eb4cc88e Poul Sander 2020-05-30 20:42 229
c7f2082f sago007 2016-03-13 11:48 230
void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
59dcb571 sago007 2016-02-14 15:34 231
	if (event.type == SDL_TEXTINPUT) {
72383a9e sago007 2016-10-30 11:56 232
		if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 233
			Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 234
		}
59dcb571 sago007 2016-02-14 15:34 235
	}
59dcb571 sago007 2016-02-14 15:34 236
59dcb571 sago007 2016-02-14 15:34 237
	if ( event.type == SDL_KEYDOWN ) {
59dcb571 sago007 2016-02-14 15:34 238
		if ( (event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER) ) {
59dcb571 sago007 2016-02-14 15:34 239
			name = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 240
			updated = true;
59dcb571 sago007 2016-02-14 15:34 241
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 242
		}
59dcb571 sago007 2016-02-14 15:34 243
		else if ( (event.key.keysym.sym == SDLK_ESCAPE) ) {
59dcb571 sago007 2016-02-14 15:34 244
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 245
		}
59dcb571 sago007 2016-02-14 15:34 246
		else {
72383a9e sago007 2016-10-30 11:56 247
			if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 248
				Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 249
			}
59dcb571 sago007 2016-02-14 15:34 250
		}
59dcb571 sago007 2016-02-14 15:34 251
	}
eb4cc88e Poul Sander 2020-05-30 20:42 252
	else {
eb4cc88e Poul Sander 2020-05-30 20:42 253
		if (isGamePadStartEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 254
			name = rk->GetString();
eb4cc88e Poul Sander 2020-05-30 20:42 255
			updated = true;
eb4cc88e Poul Sander 2020-05-30 20:42 256
			isActive = false;
eb4cc88e Poul Sander 2020-05-30 20:42 257
		}
eb4cc88e Poul Sander 2020-05-30 20:42 258
		if (isGamePadBackEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 259
			isActive = false;
eb4cc88e Poul Sander 2020-05-30 20:42 260
		}
eb4cc88e Poul Sander 2020-05-30 20:42 261
		if (isConfirmEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 262
			const sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(virtualKeyboard.selectedChar);
eb4cc88e Poul Sander 2020-05-30 20:42 263
			const std::string& insertChar = f.GetText();
eb4cc88e Poul Sander 2020-05-30 20:42 264
			virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
eb4cc88e Poul Sander 2020-05-30 20:42 265
		}
eb4cc88e Poul Sander 2020-05-30 20:42 266
		if (isEscapeEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 267
			rk->emulateBackspace();
eb4cc88e Poul Sander 2020-05-30 20:42 268
		}
eb4cc88e Poul Sander 2020-05-30 20:42 269
		if (isGamePadLEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 270
			rk->cursorLeft();
eb4cc88e Poul Sander 2020-05-30 20:42 271
		}
eb4cc88e Poul Sander 2020-05-30 20:42 272
		if (isGamePadREvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 273
			rk->cursorRight();
eb4cc88e Poul Sander 2020-05-30 20:42 274
		}
eb4cc88e Poul Sander 2020-05-30 20:42 275
		if (isRightEvent(event)) {
7cc28ffd sago007 2020-06-20 21:40 276
			if ( (virtualKeyboard.selectedChar+1)/keyboardRowLimit != virtualKeyboard.selectedChar/keyboardRowLimit ) {
7cc28ffd sago007 2020-06-20 21:40 277
				virtualKeyboard.selectedChar -= keyboardRowLimit;
7cc28ffd sago007 2020-06-20 21:40 278
			}
45d36316 Poul Sander 2020-06-01 19:23 279
			++virtualKeyboard.selectedChar;
7cc28ffd sago007 2020-06-20 21:40 280
			if (virtualKeyboard.selectedChar >= static_cast<int>(virtualKeyboard.gamePadCharFields.size())) {
7cc28ffd sago007 2020-06-20 21:40 281
				virtualKeyboard.selectedChar -= virtualKeyboard.selectedChar%keyboardRowLimit;
7cc28ffd sago007 2020-06-20 21:40 282
			}
eb4cc88e Poul Sander 2020-05-30 20:42 283
		}
eb4cc88e Poul Sander 2020-05-30 20:42 284
		if (isLeftEvent(event)) {
7cc28ffd sago007 2020-06-20 21:40 285
			if ( (virtualKeyboard.selectedChar-1)/keyboardRowLimit != virtualKeyboard.selectedChar/keyboardRowLimit || virtualKeyboard.selectedChar == 0 ) {
7cc28ffd sago007 2020-06-20 21:40 286
				virtualKeyboard.selectedChar += keyboardRowLimit;
7cc28ffd sago007 2020-06-20 21:40 287
			}
45d36316 Poul Sander 2020-06-01 19:23 288
			--virtualKeyboard.selectedChar;
eb4cc88e Poul Sander 2020-05-30 20:42 289
		}
eb4cc88e Poul Sander 2020-05-30 20:42 290
		if (isDownEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 291
			virtualKeyboard.selectedChar+= keyboardRowLimit;
eb4cc88e Poul Sander 2020-05-30 20:42 292
		}
eb4cc88e Poul Sander 2020-05-30 20:42 293
		if (isUpEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 294
			virtualKeyboard.selectedChar -= keyboardRowLimit;
eb4cc88e Poul Sander 2020-05-30 20:42 295
		}
45d36316 Poul Sander 2020-06-01 19:23 296
		if (virtualKeyboard.selectedChar < 0) {
45d36316 Poul Sander 2020-06-01 19:23 297
			virtualKeyboard.selectedChar = 0;
eb4cc88e Poul Sander 2020-05-30 20:42 298
		}
45d36316 Poul Sander 2020-06-01 19:23 299
		if (virtualKeyboard.selectedChar >= static_cast<int>(virtualKeyboard.gamePadCharFields.size())) {
45d36316 Poul Sander 2020-06-01 19:23 300
			virtualKeyboard.selectedChar = virtualKeyboard.gamePadCharFields.size()-1;
eb4cc88e Poul Sander 2020-05-30 20:42 301
		}
eb4cc88e Poul Sander 2020-05-30 20:42 302
	}
7a956470 sago007 2016-02-14 17:09 303
	processed = true;
610539a5 Poul Sander 2019-12-04 16:47 304
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
610539a5 Poul Sander 2019-12-04 16:47 305
		bMouseUp=true;
610539a5 Poul Sander 2019-12-04 16:47 306
	}
610539a5 Poul Sander 2019-12-04 16:47 307
610539a5 Poul Sander 2019-12-04 16:47 308
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
610539a5 Poul Sander 2019-12-04 16:47 309
		bMouseUp = false;
610539a5 Poul Sander 2019-12-04 16:47 310
		if (insideRect(x+25, y+128, 50, 250)) {
610539a5 Poul Sander 2019-12-04 16:47 311
			name = rk->GetString();
610539a5 Poul Sander 2019-12-04 16:47 312
			updated = true;
610539a5 Poul Sander 2019-12-04 16:47 313
			isActive = false;
610539a5 Poul Sander 2019-12-04 16:47 314
		}
610539a5 Poul Sander 2019-12-04 16:47 315
		if (insideRect(x+325, y+128, 50, 250)) {
610539a5 Poul Sander 2019-12-04 16:47 316
			isActive = false;
610539a5 Poul Sander 2019-12-04 16:47 317
		}
45d36316 Poul Sander 2020-06-01 19:23 318
		for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
45d36316 Poul Sander 2020-06-01 19:23 319
			sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(i);
afe1bac4 Poul Sander 2020-05-24 18:34 320
			auto topx = globalData.xsize/2-400+(i%keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 321
			auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 322
			if (insideRect(topx, topy, 30, 30)) {
c634a901 Poul Sander 2020-05-24 19:37 323
				std::string insertChar = f.GetText();
eb4cc88e Poul Sander 2020-05-30 20:42 324
				virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
afe1bac4 Poul Sander 2020-05-24 18:34 325
			}
afe1bac4 Poul Sander 2020-05-24 18:34 326
		}
afe1bac4 Poul Sander 2020-05-24 18:34 327
	}
afe1bac4 Poul Sander 2020-05-24 18:34 328
d0739659 Poul Sander 2020-06-01 19:17 329
	if (globalData.mousex != oldmousex || globalData.mousey != oldmousey) {
45d36316 Poul Sander 2020-06-01 19:23 330
		for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
afe1bac4 Poul Sander 2020-05-24 18:34 331
			auto topx = globalData.xsize/2-400+(i%keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 332
			auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 333
			if (insideRect(topx, topy, 30, 30)) {
45d36316 Poul Sander 2020-06-01 19:23 334
				virtualKeyboard.selectedChar = i;
afe1bac4 Poul Sander 2020-05-24 18:34 335
			}
afe1bac4 Poul Sander 2020-05-24 18:34 336
		}
d0739659 Poul Sander 2020-06-01 19:17 337
		oldmousex = globalData.mousex;
d0739659 Poul Sander 2020-06-01 19:17 338
		oldmousey = globalData.mousey;
610539a5 Poul Sander 2019-12-04 16:47 339
	}
59dcb571 sago007 2016-02-14 15:34 340
}
59dcb571 sago007 2016-02-14 15:34 341
59dcb571 sago007 2016-02-14 15:34 342
void DialogBox::SetName(const std::string& name) {
59dcb571 sago007 2016-02-14 15:34 343
	this->name = name;
59dcb571 sago007 2016-02-14 15:34 344
	rk = std::make_shared<ReadKeyboard>(name.c_str());
59dcb571 sago007 2016-02-14 15:34 345
}
59dcb571 sago007 2016-02-14 15:34 346
59dcb571 sago007 2016-02-14 15:34 347
std::string DialogBox::GetName() const {
59dcb571 sago007 2016-02-14 15:34 348
	return name;
59dcb571 sago007 2016-02-14 15:34 349
}
59dcb571 sago007 2016-02-14 15:34 350
59dcb571 sago007 2016-02-14 15:34 351
bool DialogBox::IsUpdated() const {
59dcb571 sago007 2016-02-14 15:34 352
	return updated;
b1a7bd3e Poul Sander 2016-12-19 21:53 353
}
1970-01-01 00:00 354