SettingsMainWindow.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "AdventureOptionsTab.h"
  13. #include "BattleOptionsTab.h"
  14. #include "GeneralOptionsTab.h"
  15. #include "OtherOptionsTab.h"
  16. #include "CGameInfo.h"
  17. #include "CGeneralTextHandler.h"
  18. #include "CPlayerInterface.h"
  19. #include "CServerHandler.h"
  20. #include "filesystem/ResourceID.h"
  21. #include "gui/CGuiHandler.h"
  22. #include "gui/WindowHandler.h"
  23. #include "render/Canvas.h"
  24. #include "lobby/CSavingScreen.h"
  25. #include "widgets/Buttons.h"
  26. #include "widgets/Images.h"
  27. #include "widgets/ObjectLists.h"
  28. #include "windows/CMessage.h"
  29. SettingsMainWindow::SettingsMainWindow(BattleInterface * parentBattleUi) : InterfaceObjectConfigurable()
  30. {
  31. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  32. const JsonNode config(ResourceID("config/widgets/settings/settingsMainContainer.json"));
  33. addCallback("activateSettingsTab", [this](int tabId) { openTab(tabId); });
  34. addCallback("loadGame", [this](int) { loadGameButtonCallback(); });
  35. addCallback("saveGame", [this](int) { saveGameButtonCallback(); });
  36. addCallback("restartGame", [this](int) { restartGameButtonCallback(); });
  37. addCallback("quitGame", [this](int) { quitGameButtonCallback(); });
  38. addCallback("returnToMainMenu", [this](int) { mainMenuButtonCallback(); });
  39. addCallback("closeWindow", [this](int) { backButtonCallback(); });
  40. build(config);
  41. std::shared_ptr<CIntObject> background = widget<CIntObject>("background");
  42. pos.w = background->pos.w;
  43. pos.h = background->pos.h;
  44. pos = center();
  45. std::shared_ptr<CButton> loadButton = widget<CButton>("loadButton");
  46. assert(loadButton);
  47. std::shared_ptr<CButton> saveButton = widget<CButton>("saveButton");
  48. assert(saveButton);
  49. std::shared_ptr<CButton> restartButton = widget<CButton>("restartButton");
  50. assert(restartButton);
  51. loadButton->block(CSH->isGuest());
  52. saveButton->block(CSH->isGuest() || parentBattleUi);
  53. restartButton->block(CSH->isGuest() || parentBattleUi);
  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. tabContentArea->type |= REDRAW_PARENT;
  62. std::shared_ptr<CToggleGroup> mainTabs = widget<CToggleGroup>("settingsTabs");
  63. mainTabs->setSelected(defaultTabIndex);
  64. }
  65. std::shared_ptr<CIntObject> SettingsMainWindow::createTab(size_t index)
  66. {
  67. switch(index)
  68. {
  69. case 0:
  70. return std::make_shared<GeneralOptionsTab>();
  71. case 1:
  72. return std::make_shared<AdventureOptionsTab>();
  73. case 2:
  74. return std::make_shared<BattleOptionsTab>(parentBattleInterface);
  75. case 3:
  76. return std::make_shared<OtherOptionsTab>();
  77. default:
  78. logGlobal->error("Wrong settings tab ID!");
  79. return std::make_shared<GeneralOptionsTab>();
  80. }
  81. }
  82. void SettingsMainWindow::openTab(size_t index)
  83. {
  84. tabContentArea->setActive(index);
  85. CIntObject::redraw();
  86. Settings lastUsedTab = settings.write["general"]["lastSettingsTab"];
  87. lastUsedTab->Integer() = index;
  88. }
  89. void SettingsMainWindow::close()
  90. {
  91. if(!GH.windows().isTopWindow(this))
  92. logGlobal->error("Only top interface must be closed");
  93. GH.windows().popWindows(1);
  94. }
  95. void SettingsMainWindow::quitGameButtonCallback()
  96. {
  97. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(EUserEvent::FORCE_QUIT); }, 0);
  98. }
  99. void SettingsMainWindow::backButtonCallback()
  100. {
  101. close();
  102. }
  103. void SettingsMainWindow::mainMenuButtonCallback()
  104. {
  105. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(EUserEvent::RETURN_TO_MAIN_MENU); }, 0);
  106. }
  107. void SettingsMainWindow::loadGameButtonCallback()
  108. {
  109. close();
  110. LOCPLINT->proposeLoadingGame();
  111. }
  112. void SettingsMainWindow::saveGameButtonCallback()
  113. {
  114. close();
  115. GH.windows().createAndPushWindow<CSavingScreen>();
  116. }
  117. void SettingsMainWindow::restartGameButtonCallback()
  118. {
  119. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(EUserEvent::RESTART_GAME); }, 0);
  120. }
  121. void SettingsMainWindow::closeAndPushEvent(EUserEvent code)
  122. {
  123. close();
  124. GH.pushUserEvent(code);
  125. }
  126. void SettingsMainWindow::showAll(Canvas & to)
  127. {
  128. auto color = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1);
  129. if(settings["session"]["spectate"].Bool())
  130. color = PlayerColor(1); // TODO: Spectator shouldn't need special code for UI colors
  131. CIntObject::showAll(to);
  132. CMessage::drawBorder(color, to.getInternalSurface(), pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  133. }
  134. void SettingsMainWindow::onScreenResize()
  135. {
  136. InterfaceObjectConfigurable::onScreenResize();
  137. auto tab = std::dynamic_pointer_cast<GeneralOptionsTab>(tabContentArea->getItem());
  138. if (tab)
  139. tab->updateResolutionSelector();
  140. }