CSavingScreen.cpp 2.5 KB

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