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