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