SettingsMainWindow.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "lobby/CSavingScreen.h"
  24. #include "widgets/Buttons.h"
  25. #include "widgets/Images.h"
  26. #include "widgets/ObjectLists.h"
  27. #include "windows/CMessage.h"
  28. SettingsMainWindow::SettingsMainWindow(BattleInterface * parentBattleUi) : InterfaceObjectConfigurable()
  29. {
  30. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  31. const JsonNode config(ResourceID("config/widgets/settings/settingsMainContainer.json"));
  32. addCallback("activateSettingsTab", [this](int tabId) { openTab(tabId); });
  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<CIntObject> background = widget<CIntObject>("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. loadButton->block(CSH->isGuest());
  51. saveButton->block(CSH->isGuest() || parentBattleUi);
  52. restartButton->block(CSH->isGuest() || parentBattleUi);
  53. int defaultTabIndex = 0;
  54. if(parentBattleUi != nullptr)
  55. defaultTabIndex = 2;
  56. else if(settings["general"]["lastSettingsTab"].isNumber())
  57. defaultTabIndex = settings["general"]["lastSettingsTab"].Integer();
  58. parentBattleInterface = parentBattleUi;
  59. tabContentArea = std::make_shared<CTabbedInt>(std::bind(&SettingsMainWindow::createTab, this, _1), Point(0, 0), defaultTabIndex);
  60. tabContentArea->type |= REDRAW_PARENT;
  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.windows().isTopWindow(this))
  91. logGlobal->error("Only top interface must be closed");
  92. GH.windows().popWindows(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.windows().createAndPushWindow<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. }
  125. void SettingsMainWindow::showAll(SDL_Surface *to)
  126. {
  127. auto color = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1);
  128. if(settings["session"]["spectate"].Bool())
  129. color = PlayerColor(1); // TODO: Spectator shouldn't need special code for UI colors
  130. CIntObject::showAll(to);
  131. CMessage::drawBorder(color, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  132. }
  133. void SettingsMainWindow::onScreenResize()
  134. {
  135. InterfaceObjectConfigurable::onScreenResize();
  136. auto tab = std::dynamic_pointer_cast<GeneralOptionsTab>(tabContentArea->getItem());
  137. if (tab)
  138. tab->updateResolutionSelector();
  139. }