SettingsMainWindow.cpp 4.1 KB

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