2
0

CSavingScreen.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "../widgets/Buttons.h"
  17. #include "../widgets/TextControls.h"
  18. #include "../../CCallback.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../../lib/CGeneralTextHandler.h"
  21. #include "../../lib/StartInfo.h"
  22. #include "../../lib/filesystem/Filesystem.h"
  23. #include "../../lib/mapping/CMapInfo.h"
  24. CSavingScreen::CSavingScreen()
  25. : CSelectionBase(ESelectionScreen::saveGame)
  26. {
  27. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  28. center(pos);
  29. // TODO: we should really use std::shared_ptr for passing StartInfo around.
  30. localSi = new StartInfo(*LOCPLINT->cb->getStartInfo());
  31. localMi = new CMapInfo();
  32. localMi->mapHeader = std::unique_ptr<CMapHeader>(new CMapHeader(*LOCPLINT->cb->getMapHeader()));
  33. tabSel = std::make_shared<SelectionTab>(screenType);
  34. curTab = tabSel;
  35. tabSel->toggleMode();
  36. tabSel->callOnSelect = std::bind(&CSavingScreen::changeSelection, this, _1);
  37. buttonStart = std::make_shared<CButton>(Point(411, 535), "SCNRSAV.DEF", CGI->generaltexth->zelp[103], std::bind(&CSavingScreen::saveGame, this), SDLK_s);
  38. buttonStart->assignedKeys.insert(SDLK_RETURN);
  39. }
  40. CSavingScreen::~CSavingScreen()
  41. {
  42. vstd::clear_pointer(localMi);
  43. }
  44. const CMapInfo * CSavingScreen::getMapInfo()
  45. {
  46. return localMi;
  47. }
  48. const StartInfo * CSavingScreen::getStartInfo()
  49. {
  50. return localSi;
  51. }
  52. void CSavingScreen::changeSelection(std::shared_ptr<CMapInfo> to)
  53. {
  54. if(localMi == to.get())
  55. return;
  56. localMi = to.get();
  57. localSi = localMi->scenarioOptionsOfSave;
  58. card->changeSelection();
  59. }
  60. void CSavingScreen::saveGame()
  61. {
  62. if(!(tabSel && tabSel->inputName && tabSel->inputName->text.size()))
  63. return;
  64. std::string path = "Saves/" + tabSel->inputName->text;
  65. auto overWrite = [&]() -> void
  66. {
  67. Settings lastSave = settings.write["general"]["lastSave"];
  68. lastSave->String() = path;
  69. LOCPLINT->cb->save(path);
  70. GH.popIntTotally(this);
  71. };
  72. if(CResourceHandler::get("local")->existsResource(ResourceID(path, EResType::CLIENT_SAVEGAME)))
  73. {
  74. std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite?
  75. boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->text);
  76. LOCPLINT->showYesNoDialog(hlp, overWrite, 0, false);
  77. }
  78. else
  79. {
  80. overWrite();
  81. }
  82. }