CSavingScreen.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * CSavingScreen.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CSavingScreen.h"
  12. #include "SelectionTab.h"
  13. #include "../CGameInfo.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/CTextInput.h"
  19. #include "../../CCallback.h"
  20. #include "../../lib/CConfigHandler.h"
  21. #include "../../lib/texts/CGeneralTextHandler.h"
  22. #include "../../lib/StartInfo.h"
  23. #include "../../lib/filesystem/Filesystem.h"
  24. #include "../../lib/mapping/CMapInfo.h"
  25. #include "../../lib/mapping/CMapHeader.h"
  26. CSavingScreen::CSavingScreen()
  27. : CSelectionBase(ESelectionScreen::saveGame)
  28. {
  29. OBJECT_CONSTRUCTION;
  30. center(pos);
  31. localMi = std::make_shared<CMapInfo>();
  32. localMi->mapHeader = std::unique_ptr<CMapHeader>(new CMapHeader(*LOCPLINT->cb->getMapHeader()));
  33. tabSel = std::make_shared<SelectionTab>(screenType);
  34. tabSel->callOnSelect = std::bind(&CSavingScreen::changeSelection, this, _1);
  35. tabSel->toggleMode();
  36. curTab = tabSel;
  37. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRSAV.DEF"), CGI->generaltexth->zelp[103], std::bind(&CSavingScreen::saveGame, this), EShortcut::LOBBY_SAVE_GAME);
  38. LOCPLINT->gamePause(true);
  39. }
  40. const CMapInfo * CSavingScreen::getMapInfo()
  41. {
  42. return localMi.get();
  43. }
  44. const StartInfo * CSavingScreen::getStartInfo()
  45. {
  46. if (localMi)
  47. return localMi->scenarioOptionsOfSave;
  48. return LOCPLINT->cb->getStartInfo();
  49. }
  50. void CSavingScreen::changeSelection(std::shared_ptr<CMapInfo> to)
  51. {
  52. if(localMi == to)
  53. return;
  54. localMi = to;
  55. card->changeSelection();
  56. card->redraw();
  57. }
  58. void CSavingScreen::close()
  59. {
  60. LOCPLINT->gamePause(false);
  61. CSelectionBase::close();
  62. }
  63. void CSavingScreen::saveGame()
  64. {
  65. if(!(tabSel && tabSel->inputName && tabSel->inputName->getText().size()))
  66. return;
  67. std::string path = "Saves/" + tabSel->curFolder + tabSel->inputName->getText();
  68. auto overWrite = [this, path]() -> void
  69. {
  70. Settings lastSave = settings.write["general"]["lastSave"];
  71. lastSave->String() = path;
  72. LOCPLINT->cb->save(path);
  73. close();
  74. };
  75. if(CResourceHandler::get("local")->existsResource(ResourcePath(path, EResType::SAVEGAME)))
  76. {
  77. std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite?
  78. boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->getText());
  79. LOCPLINT->showYesNoDialog(hlp, overWrite, nullptr);
  80. }
  81. else
  82. {
  83. overWrite();
  84. }
  85. }