CSavingScreen.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 = std::make_shared<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. }
  43. const CMapInfo * CSavingScreen::getMapInfo()
  44. {
  45. return localMi.get();
  46. }
  47. const StartInfo * CSavingScreen::getStartInfo()
  48. {
  49. return localSi;
  50. }
  51. void CSavingScreen::changeSelection(std::shared_ptr<CMapInfo> to)
  52. {
  53. if(localMi == to)
  54. return;
  55. localMi = to;
  56. localSi = localMi->scenarioOptionsOfSave;
  57. card->changeSelection();
  58. }
  59. void CSavingScreen::saveGame()
  60. {
  61. if(!(tabSel && tabSel->inputName && tabSel->inputName->text.size()))
  62. return;
  63. std::string path = "Saves/" + tabSel->inputName->text;
  64. auto overWrite = [this, path]() -> void
  65. {
  66. Settings lastSave = settings.write["general"]["lastSave"];
  67. lastSave->String() = path;
  68. LOCPLINT->cb->save(path);
  69. close();
  70. };
  71. if(CResourceHandler::get("local")->existsResource(ResourceID(path, EResType::CLIENT_SAVEGAME)))
  72. {
  73. std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite?
  74. boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->text);
  75. LOCPLINT->showYesNoDialog(hlp, overWrite, nullptr);
  76. }
  77. else
  78. {
  79. overWrite();
  80. }
  81. }