SettingsMainWindow.cpp 4.6 KB

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