SettingsMainWindow.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "CMT.h"
  17. #include "CGameInfo.h"
  18. #include "CGeneralTextHandler.h"
  19. #include "CPlayerInterface.h"
  20. #include "CServerHandler.h"
  21. #include "filesystem/ResourcePath.h"
  22. #include "gui/CGuiHandler.h"
  23. #include "gui/WindowHandler.h"
  24. #include "render/Canvas.h"
  25. #include "lobby/CSavingScreen.h"
  26. #include "widgets/Buttons.h"
  27. #include "widgets/Images.h"
  28. #include "widgets/ObjectLists.h"
  29. #include "windows/CMessage.h"
  30. SettingsMainWindow::SettingsMainWindow(BattleInterface * parentBattleUi) : InterfaceObjectConfigurable()
  31. {
  32. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  33. const JsonNode config(JsonPath::builtin("config/widgets/settings/settingsMainContainer.json"));
  34. addCallback("activateSettingsTab", [this](int tabId) { openTab(tabId); });
  35. addCallback("loadGame", [this](int) { loadGameButtonCallback(); });
  36. addCallback("saveGame", [this](int) { saveGameButtonCallback(); });
  37. addCallback("restartGame", [this](int) { restartGameButtonCallback(); });
  38. addCallback("quitGame", [this](int) { quitGameButtonCallback(); });
  39. addCallback("returnToMainMenu", [this](int) { mainMenuButtonCallback(); });
  40. addCallback("closeWindow", [this](int) { backButtonCallback(); });
  41. build(config);
  42. std::shared_ptr<CIntObject> background = widget<CIntObject>("background");
  43. pos.w = background->pos.w;
  44. pos.h = background->pos.h;
  45. pos = center();
  46. std::shared_ptr<CButton> loadButton = widget<CButton>("loadButton");
  47. assert(loadButton);
  48. std::shared_ptr<CButton> saveButton = widget<CButton>("saveButton");
  49. assert(saveButton);
  50. std::shared_ptr<CButton> restartButton = widget<CButton>("restartButton");
  51. assert(restartButton);
  52. loadButton->block(CSH->isGuest());
  53. saveButton->block(CSH->isGuest() || parentBattleUi);
  54. restartButton->block(CSH->isGuest() || parentBattleUi);
  55. int defaultTabIndex = 0;
  56. if(parentBattleUi != nullptr)
  57. defaultTabIndex = 2;
  58. else if(settings["general"]["lastSettingsTab"].isNumber())
  59. defaultTabIndex = settings["general"]["lastSettingsTab"].Integer();
  60. parentBattleInterface = parentBattleUi;
  61. tabContentArea = std::make_shared<CTabbedInt>(std::bind(&SettingsMainWindow::createTab, this, _1), Point(0, 0), defaultTabIndex);
  62. tabContentArea->setRedrawParent(true);
  63. std::shared_ptr<CToggleGroup> mainTabs = widget<CToggleGroup>("settingsTabs");
  64. mainTabs->setSelected(defaultTabIndex);
  65. }
  66. std::shared_ptr<CIntObject> SettingsMainWindow::createTab(size_t index)
  67. {
  68. switch(index)
  69. {
  70. case 0:
  71. return std::make_shared<GeneralOptionsTab>();
  72. case 1:
  73. return std::make_shared<AdventureOptionsTab>();
  74. case 2:
  75. return std::make_shared<BattleOptionsTab>(parentBattleInterface);
  76. case 3:
  77. return std::make_shared<OtherOptionsTab>();
  78. default:
  79. logGlobal->error("Wrong settings tab ID!");
  80. return std::make_shared<GeneralOptionsTab>();
  81. }
  82. }
  83. void SettingsMainWindow::openTab(size_t index)
  84. {
  85. tabContentArea->setActive(index);
  86. CIntObject::redraw();
  87. Settings lastUsedTab = settings.write["general"]["lastSettingsTab"];
  88. lastUsedTab->Integer() = index;
  89. }
  90. void SettingsMainWindow::close()
  91. {
  92. if(!GH.windows().isTopWindow(this))
  93. logGlobal->error("Only top interface must be closed");
  94. GH.windows().popWindows(1);
  95. }
  96. void SettingsMainWindow::quitGameButtonCallback()
  97. {
  98. LOCPLINT->showYesNoDialog(
  99. CGI->generaltexth->allTexts[578],
  100. [this]()
  101. {
  102. close();
  103. GH.dispatchMainThread( []()
  104. {
  105. handleQuit(false);
  106. });
  107. },
  108. 0
  109. );
  110. }
  111. void SettingsMainWindow::backButtonCallback()
  112. {
  113. close();
  114. }
  115. void SettingsMainWindow::mainMenuButtonCallback()
  116. {
  117. LOCPLINT->showYesNoDialog(
  118. CGI->generaltexth->allTexts[578],
  119. [this]()
  120. {
  121. close();
  122. GH.dispatchMainThread( []()
  123. {
  124. CSH->endGameplay();
  125. GH.defActionsDef = 63;
  126. CMM->menu->switchToTab("main");
  127. });
  128. },
  129. 0
  130. );
  131. }
  132. void SettingsMainWindow::loadGameButtonCallback()
  133. {
  134. close();
  135. LOCPLINT->proposeLoadingGame();
  136. }
  137. void SettingsMainWindow::saveGameButtonCallback()
  138. {
  139. close();
  140. GH.windows().createAndPushWindow<CSavingScreen>();
  141. }
  142. void SettingsMainWindow::restartGameButtonCallback()
  143. {
  144. LOCPLINT->showYesNoDialog(
  145. CGI->generaltexth->allTexts[67],
  146. [this]()
  147. {
  148. close();
  149. GH.dispatchMainThread([](){
  150. CSH->sendRestartGame();
  151. });
  152. },
  153. 0
  154. );
  155. }
  156. void SettingsMainWindow::showAll(Canvas & to)
  157. {
  158. auto color = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1);
  159. if(settings["session"]["spectate"].Bool())
  160. color = PlayerColor(1); // TODO: Spectator shouldn't need special code for UI colors
  161. CIntObject::showAll(to);
  162. CMessage::drawBorder(color, to.getInternalSurface(), pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  163. }
  164. void SettingsMainWindow::onScreenResize()
  165. {
  166. InterfaceObjectConfigurable::onScreenResize();
  167. auto tab = std::dynamic_pointer_cast<GeneralOptionsTab>(tabContentArea->getItem());
  168. if (tab)
  169. tab->updateResolutionSelector();
  170. }