CSavingScreen.cpp 2.4 KB

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