SettingsMainContainer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * SettingsMainContainer.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 <SDL_events.h>
  11. #include "StdInc.h"
  12. #include "SettingsMainContainer.h"
  13. #include "GeneralOptionsTab.h"
  14. #include "AdventureOptionsTab.h"
  15. #include "BattleOptionsTab.h"
  16. #include "OtherOptionsWindow.h"
  17. #include "filesystem/ResourceID.h"
  18. #include "CGeneralTextHandler.h"
  19. #include "gui/CGuiHandler.h"
  20. #include "lobby/CSavingScreen.h"
  21. #include "widgets/Images.h"
  22. #include "CGameInfo.h"
  23. #include "CPlayerInterface.h"
  24. #include "CServerHandler.h"
  25. SettingsMainContainer::SettingsMainContainer(BattleInterface * parentBattleUi) : InterfaceObjectConfigurable()
  26. {
  27. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  28. const JsonNode config(ResourceID("config/widgets/settings/settingsMainContainer.json"));
  29. addCallback("activateGeneralTab", [this](int) { openTab(0); });
  30. addCallback("activateAdventureTab", [this](int) { openTab(1); });
  31. addCallback("activateBattleTab", [this](int) { openTab(2); });
  32. addCallback("activateVcmiSettingsTab", [this](int) { openTab(3); });
  33. addCallback("loadGame", [this](int) { loadGameButtonCallback(); });
  34. addCallback("saveGame", [this](int) { saveGameButtonCallback(); });
  35. addCallback("restartGame", [this](int) { restartGameButtonCallback(); });
  36. addCallback("quitGame", [this](int) { quitGameButtonCallback(); });
  37. addCallback("returnToMainMenu", [this](int) { mainMenuButtonCallback(); });
  38. addCallback("closeWindow", [this](int) { backButtonCallback(); });
  39. build(config);
  40. std::shared_ptr<CPicture> background = widget<CPicture>("background");
  41. pos.w = background->pos.w;
  42. pos.h = background->pos.h;
  43. pos = center();
  44. std::shared_ptr<CButton> loadButton = widget<CButton>("loadButton");
  45. assert(loadButton);
  46. std::shared_ptr<CButton> saveButton = widget<CButton>("saveButton");
  47. assert(saveButton);
  48. std::shared_ptr<CButton> restartButton = widget<CButton>("restartButton");
  49. assert(restartButton);
  50. if(CSH->isGuest())
  51. {
  52. loadButton->block(true);
  53. saveButton->block(true);
  54. restartButton->block(true);
  55. }
  56. int defaultTabIndex = 0;
  57. if(settings["general"]["lastSettingsTab"].isNumber())
  58. defaultTabIndex = settings["general"]["lastSettingsTab"].Integer();
  59. parentBattleInterface = parentBattleUi;
  60. tabContentArea = std::make_shared<CTabbedInt>(std::bind(&SettingsMainContainer::createTab, this, _1), Point(0, 50), defaultTabIndex);
  61. }
  62. std::shared_ptr<CIntObject> SettingsMainContainer::createTab(size_t index)
  63. {
  64. switch(index)
  65. {
  66. case 0:
  67. return std::make_shared<GeneralOptionsTab>();
  68. case 1:
  69. return std::make_shared<AdventureOptionsTab>();
  70. case 2:
  71. return std::make_shared<BattleOptionsTab>(parentBattleInterface);
  72. case 3:
  73. return std::make_shared<OtherOptionsWindow>();
  74. default:
  75. logGlobal->error("Wrong settings tab ID!");
  76. return std::make_shared<GeneralOptionsTab>();
  77. }
  78. }
  79. void SettingsMainContainer::openTab(size_t index)
  80. {
  81. tabContentArea->setActive(index);
  82. CIntObject::redraw();
  83. Settings lastUsedTab = settings.write["general"]["lastSettingsTab"];
  84. lastUsedTab->Integer() = index;
  85. }
  86. void SettingsMainContainer::close()
  87. {
  88. if(GH.topInt().get() != this)
  89. logGlobal->error("Only top interface must be closed");
  90. GH.popInts(1);
  91. }
  92. void SettingsMainContainer::quitGameButtonCallback()
  93. {
  94. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(EUserEvent::FORCE_QUIT); }, 0);
  95. }
  96. void SettingsMainContainer::backButtonCallback()
  97. {
  98. close();
  99. }
  100. void SettingsMainContainer::mainMenuButtonCallback()
  101. {
  102. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(EUserEvent::RETURN_TO_MAIN_MENU); }, 0);
  103. }
  104. void SettingsMainContainer::loadGameButtonCallback()
  105. {
  106. close();
  107. LOCPLINT->proposeLoadingGame();
  108. }
  109. void SettingsMainContainer::saveGameButtonCallback()
  110. {
  111. close();
  112. GH.pushIntT<CSavingScreen>();
  113. }
  114. void SettingsMainContainer::restartGameButtonCallback()
  115. {
  116. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(EUserEvent::RESTART_GAME); }, 0);
  117. }
  118. void SettingsMainContainer::closeAndPushEvent(EUserEvent code)
  119. {
  120. close();
  121. GH.pushUserEvent(code);
  122. }