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