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"
3ee46333 Poul Sander 2021-09-26 13:05 30
#include <unordered_map>
59dcb571 sago007 2016-02-14 15:34 31
d2ba3cf5 sago007 2018-05-13 15:00 32
static void setButtonFont(const sago::SagoDataHolder* holder, sago::SagoTextField& field, const char* text) {
34f6ec6d sago007 2018-03-29 16:17 33
	field.SetHolder(holder);
34f6ec6d sago007 2018-03-29 16:17 34
	field.SetFont("freeserif");
34f6ec6d sago007 2018-03-29 16:17 35
	field.SetColor({255,255,255,255});
34f6ec6d sago007 2018-03-29 16:17 36
	field.SetFontSize(24);
34f6ec6d sago007 2018-03-29 16:17 37
	field.SetOutline(1, {0,0,0,255});
34f6ec6d sago007 2018-03-29 16:17 38
	field.SetText(text);
34f6ec6d sago007 2018-03-29 16:17 39
}
34f6ec6d sago007 2018-03-29 16:17 40
3ee46333 Poul Sander 2021-09-26 13:05 41
3ee46333 Poul Sander 2021-09-26 13:05 42
// Cache for DrawRect
3ee46333 Poul Sander 2021-09-26 13:05 43
static std::unordered_map<std::string, SDL_Texture*> draw_rect_cache;
3ee46333 Poul Sander 2021-09-26 13:05 44
static Uint64 draw_rect_cache_version=0;
3ee46333 Poul Sander 2021-09-26 13:05 45
3ee46333 Poul Sander 2021-09-26 13:05 46
static void draw_rect_cache_clear() {
3ee46333 Poul Sander 2021-09-26 13:05 47
	for (auto&& it: draw_rect_cache) {
3ee46333 Poul Sander 2021-09-26 13:05 48
		SDL_DestroyTexture(it.second);
3ee46333 Poul Sander 2021-09-26 13:05 49
	}
3ee46333 Poul Sander 2021-09-26 13:05 50
	draw_rect_cache.clear();
3ee46333 Poul Sander 2021-09-26 13:05 51
}
3ee46333 Poul Sander 2021-09-26 13:05 52
3ee46333 Poul Sander 2021-09-26 13:05 53
41f037b2 sago007 2016-04-17 16:37 54
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 55
	const int size = 32;
3ee46333 Poul Sander 2021-09-26 13:05 56
	SDL_Rect dstrect = { topx, topy, width, height };
3ee46333 Poul Sander 2021-09-26 13:05 57
	std::string key_name = SPrintStringF("%s-%d-%d", name.c_str(),width, height);
3ee46333 Poul Sander 2021-09-26 13:05 58
	Uint64 new_version = globalData.spriteHolder->GetDataHolder().getVersion();
3ee46333 Poul Sander 2021-09-26 13:05 59
	if (draw_rect_cache_version != new_version) {
3ee46333 Poul Sander 2021-09-26 13:05 60
		draw_rect_cache_clear();
3ee46333 Poul Sander 2021-09-26 13:05 61
		draw_rect_cache_version = new_version;
3ee46333 Poul Sander 2021-09-26 13:05 62
	}
3ee46333 Poul Sander 2021-09-26 13:05 63
	if (draw_rect_cache.find(key_name) != draw_rect_cache.end()) {
3ee46333 Poul Sander 2021-09-26 13:05 64
		SDL_RenderCopy( target, draw_rect_cache[key_name], NULL, &dstrect );
3ee46333 Poul Sander 2021-09-26 13:05 65
		return;
3ee46333 Poul Sander 2021-09-26 13:05 66
	}
3ee46333 Poul Sander 2021-09-26 13:05 67
	SDL_Rect bounds_ns = {0, size, width, height-2*size};  //bounds for south
3ee46333 Poul Sander 2021-09-26 13:05 68
	SDL_Rect bounds_e = {0, 0, width-size, height};
3ee46333 Poul Sander 2021-09-26 13:05 69
	SDL_Texture* mTexture = SDL_CreateTexture( target, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height );
3ee46333 Poul Sander 2021-09-26 13:05 70
	draw_rect_cache[key_name] = mTexture;
3ee46333 Poul Sander 2021-09-26 13:05 71
	SDL_SetRenderTarget( target, mTexture );
3ee46333 Poul Sander 2021-09-26 13:05 72
3ee46333 Poul Sander 2021-09-26 13:05 73
	SDL_SetRenderDrawBlendMode(target, SDL_BLENDMODE_NONE);
3ee46333 Poul Sander 2021-09-26 13:05 74
	SDL_SetTextureBlendMode(mTexture, SDL_BLENDMODE_BLEND);
3ee46333 Poul Sander 2021-09-26 13:05 75
	SDL_SetRenderDrawColor(target, 0, 0, 0, 0);
3ee46333 Poul Sander 2021-09-26 13:05 76
	SDL_RenderFillRect(target, NULL);
3ee46333 Poul Sander 2021-09-26 13:05 77
	SDL_SetRenderDrawBlendMode(target, SDL_BLENDMODE_BLEND);
3ee46333 Poul Sander 2021-09-26 13:05 78
72383a9e sago007 2016-10-30 11:56 79
	const sago::SagoSprite& n = globalData.spriteHolder->GetSprite(name+"n");
72383a9e sago007 2016-10-30 11:56 80
	const sago::SagoSprite& s = globalData.spriteHolder->GetSprite(name+"s");
72383a9e sago007 2016-10-30 11:56 81
	const sago::SagoSprite& e = globalData.spriteHolder->GetSprite(name+"e");
72383a9e sago007 2016-10-30 11:56 82
	const sago::SagoSprite& w = globalData.spriteHolder->GetSprite(name+"w");
72383a9e sago007 2016-10-30 11:56 83
	const sago::SagoSprite& fill = globalData.spriteHolder->GetSprite(name+"fill");
daa660ab sago007 2016-04-16 18:13 84
	for (int i = 1; i < width/size; ++i) {
3ee46333 Poul Sander 2021-09-26 13:05 85
		n.DrawBounded(target, SDL_GetTicks(), i*size, 0, bounds_e);
daa660ab sago007 2016-04-16 18:13 86
		for (int j = 1; j < height/size; ++j) {
3ee46333 Poul Sander 2021-09-26 13:05 87
			w.DrawBounded(target, SDL_GetTicks(), 0, j*size, bounds_ns);
3ee46333 Poul Sander 2021-09-26 13:05 88
			fill.Draw(target, SDL_GetTicks(),i*size, j*size);
3ee46333 Poul Sander 2021-09-26 13:05 89
			e.DrawBounded(target, SDL_GetTicks(), width-size, j*size, bounds_ns);
daa660ab sago007 2016-04-16 18:13 90
		}
3ee46333 Poul Sander 2021-09-26 13:05 91
		s.DrawBounded(target, SDL_GetTicks(), i*size, height-size, bounds_e);
daa660ab sago007 2016-04-16 18:13 92
	}
daa660ab sago007 2016-04-16 18:13 93
	//Corners
72383a9e sago007 2016-10-30 11:56 94
	const sago::SagoSprite& nw = globalData.spriteHolder->GetSprite(name+"nw");
72383a9e sago007 2016-10-30 11:56 95
	const sago::SagoSprite& ne = globalData.spriteHolder->GetSprite(name+"ne");
72383a9e sago007 2016-10-30 11:56 96
	const sago::SagoSprite& se = globalData.spriteHolder->GetSprite(name+"se");
72383a9e sago007 2016-10-30 11:56 97
	const sago::SagoSprite& sw = globalData.spriteHolder->GetSprite(name+"sw");
3ee46333 Poul Sander 2021-09-26 13:05 98
	nw.Draw(target, SDL_GetTicks(), 0, 0);
3ee46333 Poul Sander 2021-09-26 13:05 99
	ne.Draw(target, SDL_GetTicks(), width-size, 0);
3ee46333 Poul Sander 2021-09-26 13:05 100
	se.Draw(target, SDL_GetTicks(), width-size, height-size);
3ee46333 Poul Sander 2021-09-26 13:05 101
	sw.Draw(target, SDL_GetTicks(), 0, height-size);
3ee46333 Poul Sander 2021-09-26 13:05 102
	SDL_SetRenderTarget( target, nullptr );
3ee46333 Poul Sander 2021-09-26 13:05 103
	SDL_RenderCopy( target, mTexture, NULL, &dstrect );
41f037b2 sago007 2016-04-17 16:37 104
}
41f037b2 sago007 2016-04-17 16:37 105
41f037b2 sago007 2016-04-17 16:37 106
static void DrawRectWhite(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 107
	std::string name = "ui_rect_white_";
41f037b2 sago007 2016-04-17 16:37 108
	DrawRect(target, topx, topy, height, width, name);
41f037b2 sago007 2016-04-17 16:37 109
}
41f037b2 sago007 2016-04-17 16:37 110
db2f559e sago007 2018-05-10 10:36 111
void DrawRectYellow(SDL_Renderer* target, int topx, int topy, int height, int width) {
41f037b2 sago007 2016-04-17 16:37 112
	std::string name = "ui_rect_yellow_";
41f037b2 sago007 2016-04-17 16:37 113
	DrawRect(target, topx, topy, height, width, name);
daa660ab sago007 2016-04-16 18:13 114
}
daa660ab sago007 2016-04-16 18:13 115
96e759d9 sago007 2016-04-17 18:45 116
bool OpenDialogbox(int x, int y, std::string& name, const std::string& header) {
96e759d9 sago007 2016-04-17 18:45 117
	DialogBox d(x, y, name, header);
59dcb571 sago007 2016-02-14 15:34 118
	RunGameState(d);
59dcb571 sago007 2016-02-14 15:34 119
	if (d.IsUpdated()) {
59dcb571 sago007 2016-02-14 15:34 120
		name = d.GetName();
59dcb571 sago007 2016-02-14 15:34 121
		return true;
59dcb571 sago007 2016-02-14 15:34 122
	}
59dcb571 sago007 2016-02-14 15:34 123
	return false;
59dcb571 sago007 2016-02-14 15:34 124
}
59dcb571 sago007 2016-02-14 15:34 125
96e759d9 sago007 2016-04-17 18:45 126
DialogBox::DialogBox(int x, int y, const std::string& name, const std::string& header) : header(header) {
59dcb571 sago007 2016-02-14 15:34 127
	this->x = x;
59dcb571 sago007 2016-02-14 15:34 128
	this->y = y;
59dcb571 sago007 2016-02-14 15:34 129
	SetName(name);
34f6ec6d sago007 2018-03-29 16:17 130
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), headerLabel, header.c_str());
34f6ec6d sago007 2018-03-29 16:17 131
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), enterLabel, _("Enter to accept"));
34f6ec6d sago007 2018-03-29 16:17 132
	setButtonFont(&globalData.spriteHolder->GetDataHolder(), cancelLabel, _("Esc to cancel"));
bd788178 sago007 2018-03-31 16:03 133
	sagoTextSetBlueFont(textField);
bd788178 sago007 2018-03-31 16:03 134
	sagoTextSetBlueFont(cursorLabel);
bd788178 sago007 2018-03-31 16:03 135
	cursorLabel.SetText("|");
45d36316 Poul Sander 2020-06-01 19:23 136
	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 137
		auto endPosition = position;
45d36316 Poul Sander 2020-06-01 19:23 138
		utf8::advance(endPosition, 1, virtualKeyboard.alphabet.end());
fc425e41 Poul Sander 2019-08-14 18:10 139
		std::string theChar(position, endPosition);
45d36316 Poul Sander 2020-06-01 19:23 140
		virtualKeyboard.gamePadChars.push_back(theChar);
45d36316 Poul Sander 2020-06-01 19:23 141
		virtualKeyboard.gamePadCharFields.emplace_back();
45d36316 Poul Sander 2020-06-01 19:23 142
		sago::SagoTextField& tf = virtualKeyboard.gamePadCharFields.back();
fc425e41 Poul Sander 2019-08-14 18:10 143
		setButtonFont(&globalData.spriteHolder->GetDataHolder(), tf, theChar.c_str());
a0887514 Poul Sander 2020-06-27 12:37 144
		if (globalData.verboseLevel) {
a0887514 Poul Sander 2020-06-27 12:37 145
			std::cout << *position;
a0887514 Poul Sander 2020-06-27 12:37 146
		}
a0887514 Poul Sander 2020-06-27 12:37 147
	}
a0887514 Poul Sander 2020-06-27 12:37 148
	if (globalData.verboseLevel) {
a0887514 Poul Sander 2020-06-27 12:37 149
		std::cout << "\n";
fc425e41 Poul Sander 2019-08-14 18:10 150
	}
59dcb571 sago007 2016-02-14 15:34 151
}
59dcb571 sago007 2016-02-14 15:34 152
59dcb571 sago007 2016-02-14 15:34 153
59dcb571 sago007 2016-02-14 15:34 154
DialogBox::~DialogBox() {
59dcb571 sago007 2016-02-14 15:34 155
}
59dcb571 sago007 2016-02-14 15:34 156
59dcb571 sago007 2016-02-14 15:34 157
bool DialogBox::IsActive() {
59dcb571 sago007 2016-02-14 15:34 158
	return isActive;
59dcb571 sago007 2016-02-14 15:34 159
}
59dcb571 sago007 2016-02-14 15:34 160
610539a5 Poul Sander 2019-12-04 16:47 161
static bool insideRect (int x, int y, int height, int width) {
610539a5 Poul Sander 2019-12-04 16:47 162
	if (globalData.mousex < x) {
610539a5 Poul Sander 2019-12-04 16:47 163
		return false;
610539a5 Poul Sander 2019-12-04 16:47 164
	}
610539a5 Poul Sander 2019-12-04 16:47 165
	if (globalData.mousex > x+width) {
610539a5 Poul Sander 2019-12-04 16:47 166
		return false;
610539a5 Poul Sander 2019-12-04 16:47 167
	}
610539a5 Poul Sander 2019-12-04 16:47 168
	if (globalData.mousey < y) {
610539a5 Poul Sander 2019-12-04 16:47 169
		return false;
610539a5 Poul Sander 2019-12-04 16:47 170
	}
610539a5 Poul Sander 2019-12-04 16:47 171
	if (globalData.mousey > y+height) {
610539a5 Poul Sander 2019-12-04 16:47 172
		return false;
610539a5 Poul Sander 2019-12-04 16:47 173
	}
610539a5 Poul Sander 2019-12-04 16:47 174
	return true;
610539a5 Poul Sander 2019-12-04 16:47 175
}
610539a5 Poul Sander 2019-12-04 16:47 176
afe1bac4 Poul Sander 2020-05-24 18:34 177
// For use with the mouse/gamepad keyboard
afe1bac4 Poul Sander 2020-05-24 18:34 178
int keyboardRowLimit = 20;
afe1bac4 Poul Sander 2020-05-24 18:34 179
59dcb571 sago007 2016-02-14 15:34 180
void DialogBox::Draw(SDL_Renderer* target) {
72383a9e sago007 2016-10-30 11:56 181
	DrawBackground(globalData.screen);
72383a9e sago007 2016-10-30 11:56 182
	this->x = globalData.xsize/2-300;
72383a9e sago007 2016-10-30 11:56 183
	this->y = globalData.ysize/2-100;
647f12da sago007 2016-09-12 20:07 184
	DrawRectYellow(target, x, y, 200, 600);
34f6ec6d sago007 2018-03-29 16:17 185
	headerLabel.Draw(target, x+300, y+20, sago::SagoTextField::Alignment::center);
610539a5 Poul Sander 2019-12-04 16:47 186
	DrawRectYellow(target, x+25, y+128, 50, 250);
34f6ec6d sago007 2018-03-29 16:17 187
	enterLabel.Draw(target, x+150, y+140, sago::SagoTextField::Alignment::center);
610539a5 Poul Sander 2019-12-04 16:47 188
	DrawRectYellow(target, x+325, y+128, 50, 250);
34f6ec6d sago007 2018-03-29 16:17 189
	cancelLabel.Draw(target, x+450, y+140, sago::SagoTextField::Alignment::center);
647f12da sago007 2016-09-12 20:07 190
	DrawRectWhite(target, x+26, y+64, 54, 600-2*26);
0b8fb137 sago007 2018-03-31 19:53 191
	textField.SetText(rk->GetString());
bd788178 sago007 2018-03-31 16:03 192
	textField.Draw(target, x+40, y+76);
59dcb571 sago007 2016-02-14 15:34 193
	std::string strHolder = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 194
	strHolder.erase((int)rk->CharsBeforeCursor());
59dcb571 sago007 2016-02-14 15:34 195
59dcb571 sago007 2016-02-14 15:34 196
	if (((SDL_GetTicks()/600)%2)==1) {
bd788178 sago007 2018-03-31 16:03 197
		int width = 0;
bd788178 sago007 2018-03-31 16:03 198
		textField.GetRenderedSize( strHolder.c_str(), &width);
bd788178 sago007 2018-03-31 16:03 199
		width -= 2;
bd788178 sago007 2018-03-31 16:03 200
		cursorLabel.Draw(target, x+40+width,y+76);
59dcb571 sago007 2016-02-14 15:34 201
	}
c5cd33e7 Poul Sander 2019-12-04 19:54 202
	const sago::SagoSprite& marked = globalData.spriteHolder->GetSprite("i_level_check_box_marked");
45d36316 Poul Sander 2020-06-01 19:23 203
	for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
45d36316 Poul Sander 2020-06-01 19:23 204
		if (virtualKeyboard.selectedChar == static_cast<int>(i)) {
afe1bac4 Poul Sander 2020-05-24 18:34 205
			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 206
		}
45d36316 Poul Sander 2020-06-01 19:23 207
		sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(i);
afe1bac4 Poul Sander 2020-05-24 18:34 208
		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 209
	}
59dcb571 sago007 2016-02-14 15:34 210
}
59dcb571 sago007 2016-02-14 15:34 211
eb4cc88e Poul Sander 2020-05-30 20:42 212
eb4cc88e Poul Sander 2020-05-30 20:42 213
static bool isGamePadStartEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 214
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 215
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_START ) {
eb4cc88e Poul Sander 2020-05-30 20:42 216
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 217
		}
eb4cc88e Poul Sander 2020-05-30 20:42 218
	}
eb4cc88e Poul Sander 2020-05-30 20:42 219
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 220
}
eb4cc88e Poul Sander 2020-05-30 20:42 221
eb4cc88e Poul Sander 2020-05-30 20:42 222
static bool isGamePadLEvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 223
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 224
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ) {
eb4cc88e Poul Sander 2020-05-30 20:42 225
			return true;
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
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 229
}
eb4cc88e Poul Sander 2020-05-30 20:42 230
eb4cc88e Poul Sander 2020-05-30 20:42 231
static bool isGamePadREvent(const SDL_Event& event) {
eb4cc88e Poul Sander 2020-05-30 20:42 232
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
eb4cc88e Poul Sander 2020-05-30 20:42 233
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
eb4cc88e Poul Sander 2020-05-30 20:42 234
			return true;
eb4cc88e Poul Sander 2020-05-30 20:42 235
		}
eb4cc88e Poul Sander 2020-05-30 20:42 236
	}
eb4cc88e Poul Sander 2020-05-30 20:42 237
	return false;
eb4cc88e Poul Sander 2020-05-30 20:42 238
}
eb4cc88e Poul Sander 2020-05-30 20:42 239
eb4cc88e Poul Sander 2020-05-30 20:42 240
9c7fa9a1 sago007 2020-06-27 22:09 241
void DialogBox::virtualKeyboardWriteSelectedChar(ReadKeyboard* rk, const std::string& insertChar) const {
45d36316 Poul Sander 2020-06-01 19:23 242
	if (insertChar == virtualKeyboard.backspace) {
eb4cc88e Poul Sander 2020-05-30 20:42 243
		rk->emulateBackspace();
eb4cc88e Poul Sander 2020-05-30 20:42 244
	}
45d36316 Poul Sander 2020-06-01 19:23 245
	else if (insertChar == virtualKeyboard.leftChar) {
eb4cc88e Poul Sander 2020-05-30 20:42 246
		rk->cursorLeft();
eb4cc88e Poul Sander 2020-05-30 20:42 247
	}
45d36316 Poul Sander 2020-06-01 19:23 248
	else if (insertChar == virtualKeyboard.rightChar) {
eb4cc88e Poul Sander 2020-05-30 20:42 249
		rk->cursorRight();
eb4cc88e Poul Sander 2020-05-30 20:42 250
	}
eb4cc88e Poul Sander 2020-05-30 20:42 251
	else {
eb4cc88e Poul Sander 2020-05-30 20:42 252
		rk->putchar(insertChar);
eb4cc88e Poul Sander 2020-05-30 20:42 253
	}
eb4cc88e Poul Sander 2020-05-30 20:42 254
}
eb4cc88e Poul Sander 2020-05-30 20:42 255
eb4cc88e Poul Sander 2020-05-30 20:42 256
c7f2082f sago007 2016-03-13 11:48 257
void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
59dcb571 sago007 2016-02-14 15:34 258
	if (event.type == SDL_TEXTINPUT) {
72383a9e sago007 2016-10-30 11:56 259
		if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 260
			Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 261
		}
59dcb571 sago007 2016-02-14 15:34 262
	}
59dcb571 sago007 2016-02-14 15:34 263
59dcb571 sago007 2016-02-14 15:34 264
	if ( event.type == SDL_KEYDOWN ) {
59dcb571 sago007 2016-02-14 15:34 265
		if ( (event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER) ) {
59dcb571 sago007 2016-02-14 15:34 266
			name = rk->GetString();
59dcb571 sago007 2016-02-14 15:34 267
			updated = true;
59dcb571 sago007 2016-02-14 15:34 268
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 269
		}
59dcb571 sago007 2016-02-14 15:34 270
		else if ( (event.key.keysym.sym == SDLK_ESCAPE) ) {
59dcb571 sago007 2016-02-14 15:34 271
			isActive = false;
59dcb571 sago007 2016-02-14 15:34 272
		}
59dcb571 sago007 2016-02-14 15:34 273
		else {
72383a9e sago007 2016-10-30 11:56 274
			if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
524f188c sago007 2017-04-22 09:01 275
				Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
59dcb571 sago007 2016-02-14 15:34 276
			}
59dcb571 sago007 2016-02-14 15:34 277
		}
59dcb571 sago007 2016-02-14 15:34 278
	}
eb4cc88e Poul Sander 2020-05-30 20:42 279
	else {
eb4cc88e Poul Sander 2020-05-30 20:42 280
		if (isGamePadStartEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 281
			name = rk->GetString();
eb4cc88e Poul Sander 2020-05-30 20:42 282
			updated = true;
eb4cc88e Poul Sander 2020-05-30 20:42 283
			isActive = false;
eb4cc88e Poul Sander 2020-05-30 20:42 284
		}
fbd9841c Poul Sander 2020-08-09 17:26 285
		if (isEscapeEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 286
			isActive = false;
eb4cc88e Poul Sander 2020-05-30 20:42 287
		}
eb4cc88e Poul Sander 2020-05-30 20:42 288
		if (isConfirmEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 289
			const sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(virtualKeyboard.selectedChar);
eb4cc88e Poul Sander 2020-05-30 20:42 290
			const std::string& insertChar = f.GetText();
eb4cc88e Poul Sander 2020-05-30 20:42 291
			virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
eb4cc88e Poul Sander 2020-05-30 20:42 292
		}
fbd9841c Poul Sander 2020-08-09 17:26 293
		if (event.type == SDL_CONTROLLERBUTTONDOWN) {
fbd9841c Poul Sander 2020-08-09 17:26 294
			if (event.cbutton.button == SDL_CONTROLLER_BUTTON_X ) {
fbd9841c Poul Sander 2020-08-09 17:26 295
				rk->emulateBackspace();
fbd9841c Poul Sander 2020-08-09 17:26 296
			}
fbd9841c Poul Sander 2020-08-09 17:26 297
			if (event.cbutton.button == SDL_CONTROLLER_BUTTON_Y ) {
fbd9841c Poul Sander 2020-08-09 17:26 298
				rk->putchar(" ");
fbd9841c Poul Sander 2020-08-09 17:26 299
			}
eb4cc88e Poul Sander 2020-05-30 20:42 300
		}
eb4cc88e Poul Sander 2020-05-30 20:42 301
		if (isGamePadLEvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 302
			rk->cursorLeft();
eb4cc88e Poul Sander 2020-05-30 20:42 303
		}
eb4cc88e Poul Sander 2020-05-30 20:42 304
		if (isGamePadREvent(event)) {
eb4cc88e Poul Sander 2020-05-30 20:42 305
			rk->cursorRight();
eb4cc88e Poul Sander 2020-05-30 20:42 306
		}
eb4cc88e Poul Sander 2020-05-30 20:42 307
		if (isRightEvent(event)) {
7cc28ffd sago007 2020-06-20 21:40 308
			if ( (virtualKeyboard.selectedChar+1)/keyboardRowLimit != virtualKeyboard.selectedChar/keyboardRowLimit ) {
7cc28ffd sago007 2020-06-20 21:40 309
				virtualKeyboard.selectedChar -= keyboardRowLimit;
7cc28ffd sago007 2020-06-20 21:40 310
			}
45d36316 Poul Sander 2020-06-01 19:23 311
			++virtualKeyboard.selectedChar;
7cc28ffd sago007 2020-06-20 21:40 312
			if (virtualKeyboard.selectedChar >= static_cast<int>(virtualKeyboard.gamePadCharFields.size())) {
7cc28ffd sago007 2020-06-20 21:40 313
				virtualKeyboard.selectedChar -= virtualKeyboard.selectedChar%keyboardRowLimit;
7cc28ffd sago007 2020-06-20 21:40 314
			}
eb4cc88e Poul Sander 2020-05-30 20:42 315
		}
eb4cc88e Poul Sander 2020-05-30 20:42 316
		if (isLeftEvent(event)) {
7cc28ffd sago007 2020-06-20 21:40 317
			if ( (virtualKeyboard.selectedChar-1)/keyboardRowLimit != virtualKeyboard.selectedChar/keyboardRowLimit || virtualKeyboard.selectedChar == 0 ) {
7cc28ffd sago007 2020-06-20 21:40 318
				virtualKeyboard.selectedChar += keyboardRowLimit;
7cc28ffd sago007 2020-06-20 21:40 319
			}
45d36316 Poul Sander 2020-06-01 19:23 320
			--virtualKeyboard.selectedChar;
eb4cc88e Poul Sander 2020-05-30 20:42 321
		}
eb4cc88e Poul Sander 2020-05-30 20:42 322
		if (isDownEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 323
			virtualKeyboard.selectedChar+= keyboardRowLimit;
eb4cc88e Poul Sander 2020-05-30 20:42 324
		}
eb4cc88e Poul Sander 2020-05-30 20:42 325
		if (isUpEvent(event)) {
45d36316 Poul Sander 2020-06-01 19:23 326
			virtualKeyboard.selectedChar -= keyboardRowLimit;
eb4cc88e Poul Sander 2020-05-30 20:42 327
		}
45d36316 Poul Sander 2020-06-01 19:23 328
		if (virtualKeyboard.selectedChar < 0) {
45d36316 Poul Sander 2020-06-01 19:23 329
			virtualKeyboard.selectedChar = 0;
eb4cc88e Poul Sander 2020-05-30 20:42 330
		}
45d36316 Poul Sander 2020-06-01 19:23 331
		if (virtualKeyboard.selectedChar >= static_cast<int>(virtualKeyboard.gamePadCharFields.size())) {
45d36316 Poul Sander 2020-06-01 19:23 332
			virtualKeyboard.selectedChar = virtualKeyboard.gamePadCharFields.size()-1;
eb4cc88e Poul Sander 2020-05-30 20:42 333
		}
eb4cc88e Poul Sander 2020-05-30 20:42 334
	}
7a956470 sago007 2016-02-14 17:09 335
	processed = true;
610539a5 Poul Sander 2019-12-04 16:47 336
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
610539a5 Poul Sander 2019-12-04 16:47 337
		bMouseUp=true;
610539a5 Poul Sander 2019-12-04 16:47 338
	}
610539a5 Poul Sander 2019-12-04 16:47 339
610539a5 Poul Sander 2019-12-04 16:47 340
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
610539a5 Poul Sander 2019-12-04 16:47 341
		bMouseUp = false;
610539a5 Poul Sander 2019-12-04 16:47 342
		if (insideRect(x+25, y+128, 50, 250)) {
610539a5 Poul Sander 2019-12-04 16:47 343
			name = rk->GetString();
610539a5 Poul Sander 2019-12-04 16:47 344
			updated = true;
610539a5 Poul Sander 2019-12-04 16:47 345
			isActive = false;
610539a5 Poul Sander 2019-12-04 16:47 346
		}
610539a5 Poul Sander 2019-12-04 16:47 347
		if (insideRect(x+325, y+128, 50, 250)) {
610539a5 Poul Sander 2019-12-04 16:47 348
			isActive = false;
610539a5 Poul Sander 2019-12-04 16:47 349
		}
45d36316 Poul Sander 2020-06-01 19:23 350
		for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
45d36316 Poul Sander 2020-06-01 19:23 351
			sago::SagoTextField& f = virtualKeyboard.gamePadCharFields.at(i);
afe1bac4 Poul Sander 2020-05-24 18:34 352
			auto topx = globalData.xsize/2-400+(i%keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 353
			auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 354
			if (insideRect(topx, topy, 30, 30)) {
c634a901 Poul Sander 2020-05-24 19:37 355
				std::string insertChar = f.GetText();
eb4cc88e Poul Sander 2020-05-30 20:42 356
				virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
afe1bac4 Poul Sander 2020-05-24 18:34 357
			}
afe1bac4 Poul Sander 2020-05-24 18:34 358
		}
afe1bac4 Poul Sander 2020-05-24 18:34 359
	}
afe1bac4 Poul Sander 2020-05-24 18:34 360
d0739659 Poul Sander 2020-06-01 19:17 361
	if (globalData.mousex != oldmousex || globalData.mousey != oldmousey) {
45d36316 Poul Sander 2020-06-01 19:23 362
		for (size_t i = 0; i<virtualKeyboard.gamePadCharFields.size(); ++i) {
afe1bac4 Poul Sander 2020-05-24 18:34 363
			auto topx = globalData.xsize/2-400+(i%keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 364
			auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
afe1bac4 Poul Sander 2020-05-24 18:34 365
			if (insideRect(topx, topy, 30, 30)) {
45d36316 Poul Sander 2020-06-01 19:23 366
				virtualKeyboard.selectedChar = i;
afe1bac4 Poul Sander 2020-05-24 18:34 367
			}
afe1bac4 Poul Sander 2020-05-24 18:34 368
		}
d0739659 Poul Sander 2020-06-01 19:17 369
		oldmousex = globalData.mousex;
d0739659 Poul Sander 2020-06-01 19:17 370
		oldmousey = globalData.mousey;
610539a5 Poul Sander 2019-12-04 16:47 371
	}
59dcb571 sago007 2016-02-14 15:34 372
}
59dcb571 sago007 2016-02-14 15:34 373
59dcb571 sago007 2016-02-14 15:34 374
void DialogBox::SetName(const std::string& name) {
59dcb571 sago007 2016-02-14 15:34 375
	this->name = name;
59dcb571 sago007 2016-02-14 15:34 376
	rk = std::make_shared<ReadKeyboard>(name.c_str());
59dcb571 sago007 2016-02-14 15:34 377
}
59dcb571 sago007 2016-02-14 15:34 378
59dcb571 sago007 2016-02-14 15:34 379
std::string DialogBox::GetName() const {
59dcb571 sago007 2016-02-14 15:34 380
	return name;
59dcb571 sago007 2016-02-14 15:34 381
}
59dcb571 sago007 2016-02-14 15:34 382
59dcb571 sago007 2016-02-14 15:34 383
bool DialogBox::IsUpdated() const {
59dcb571 sago007 2016-02-14 15:34 384
	return updated;
b1a7bd3e Poul Sander 2016-12-19 21:53 385
}
1970-01-01 00:00 386