2
0

CSavingScreen.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "../CPlayerInterface.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.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. #include "../../lib/GameLibrary.h"
  27. CSavingScreen::CSavingScreen()
  28. : CSelectionBase(ESelectionScreen::saveGame)
  29. {
  30. OBJECT_CONSTRUCTION;
  31. center(pos);
  32. localMi = std::make_shared<CMapInfo>();
  33. localMi->mapHeader = std::unique_ptr<CMapHeader>(new CMapHeader(*GAME->interface()->cb->getMapHeader()));
  34. tabSel = std::make_shared<SelectionTab>(screenType);
  35. tabSel->callOnSelect = std::bind(&CSavingScreen::changeSelection, this, _1);
  36. tabSel->toggleMode();
  37. curTab = tabSel;
  38. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRSAV.DEF"), LIBRARY->generaltexth->zelp[103], std::bind(&CSavingScreen::saveGame, this), EShortcut::LOBBY_SAVE_GAME);
  39. GAME->interface()->gamePause(true);
  40. }
  41. const CMapInfo * CSavingScreen::getMapInfo()
  42. {
  43. return localMi.get();
  44. }
  45. const StartInfo * CSavingScreen::getStartInfo()
  46. {
  47. if (localMi)
  48. return localMi->scenarioOptionsOfSave;
  49. return GAME->interface()->cb->getStartInfo();
  50. }
  51. void CSavingScreen::changeSelection(std::shared_ptr<CMapInfo> to)
  52. {
  53. if(localMi == to)
  54. return;
  55. localMi = to;
  56. card->changeSelection();
  57. card->redraw();
  58. }
  59. void CSavingScreen::close()
  60. {
  61. GAME->interface()->gamePause(false);
  62. CSelectionBase::close();
  63. }
  64. void CSavingScreen::saveGame()
  65. {
  66. if(!(tabSel && tabSel->inputName && tabSel->inputName->getText().size()))
  67. return;
  68. std::string path = "Saves/" + tabSel->curFolder + tabSel->inputName->getText();
  69. auto overWrite = [this, path]() -> void
  70. {
  71. Settings lastSave = settings.write["general"]["lastSave"];
  72. lastSave->String() = path;
  73. GAME->interface()->cb->save(path);
  74. close();
  75. };
  76. if(CResourceHandler::get("local")->existsResource(ResourcePath(path, EResType::SAVEGAME)))
  77. {
  78. std::string hlp = LIBRARY->generaltexth->allTexts[493]; //%s exists. Overwrite?
  79. boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->getText());
  80. GAME->interface()->showYesNoDialog(hlp, overWrite, nullptr);
  81. }
  82. else
  83. {
  84. overWrite();
  85. }
  86. }