SystemOptionsWindow.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * SystemOptionsWindow.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 "SystemOptionsWindow.h"
  12. #include "../lib/CGeneralTextHandler.h"
  13. #include "../lib/filesystem/ResourceID.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../widgets/Buttons.h"
  16. #include "../widgets/TextControls.h"
  17. #include "../widgets/Images.h"
  18. #include "../CGameInfo.h"
  19. #include "../CMusicHandler.h"
  20. #include "../CPlayerInterface.h"
  21. #include "GUIClasses.h"
  22. #include "CServerHandler.h"
  23. #include "lobby/CSavingScreen.h"
  24. static void setIntSetting(std::string group, std::string field, int value)
  25. {
  26. Settings entry = settings.write[group][field];
  27. entry->Float() = value;
  28. }
  29. static void setBoolSetting(std::string group, std::string field, bool value)
  30. {
  31. Settings fullscreen = settings.write[group][field];
  32. fullscreen->Bool() = value;
  33. }
  34. static std::string resolutionToString(int w, int h)
  35. {
  36. return std::to_string(w) + 'x' + std::to_string(h);
  37. }
  38. SystemOptionsWindow::SystemOptionsWindow()
  39. : InterfaceObjectConfigurable(),
  40. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  41. {
  42. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  43. const JsonNode config(ResourceID("config/widgets/optionsMenu.json"));
  44. addCallback("loadGame", [this](int) { loadGameButtonCallback(); });
  45. addCallback("saveGame", [this](int) { saveGameButtonCallback(); });
  46. addCallback("restartGame", [this](int) { restartGameButtonCallback(); });
  47. addCallback("quitGame", [this](int) { quitGameButtonCallback(); });
  48. addCallback("returnToMainMenu", [this](int) { mainMenuButtonCallback(); });
  49. addCallback("closeWindow", [this](int) { backButtonCallback(); });
  50. addCallback("playerHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "heroSpeed", _1));
  51. addCallback("enemyHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "enemySpeed", _1));
  52. addCallback("mapScrollSpeedChanged", std::bind(&setIntSetting, "adventure", "scrollSpeed", _1));
  53. addCallback("heroReminderChanged", std::bind(&setBoolSetting, "adventure", "heroReminder", _1));
  54. addCallback("quickCombatChanged", std::bind(&setBoolSetting, "adventure", "quickCombat", _1));
  55. addCallback("spellbookAnimationChanged", std::bind(&setBoolSetting, "video", "spellbookAnimation", _1));
  56. addCallback("fullscreenChanged", std::bind(&setBoolSetting, "video", "fullscreen", _1));
  57. addCallback("setGameResolution", std::bind(&SystemOptionsWindow::selectGameResolution, this));
  58. addCallback("setMusic", [this](int value) { setIntSetting("general", "music", value); widget<CSlider>("musicSlider")->redraw(); });
  59. addCallback("setVolume", [this](int value) { setIntSetting("general", "sound", value); widget<CSlider>("soundVolumeSlider")->redraw(); });
  60. build(config);
  61. std::shared_ptr<CPicture> background = widget<CPicture>("background");
  62. pos.w = background->pos.w;
  63. pos.h = background->pos.h;
  64. pos = center();
  65. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  66. const auto & currentResolution = settings["video"]["screenRes"];
  67. resolutionLabel->setText(resolutionToString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  68. std::shared_ptr<CToggleGroup> playerHeroSpeedToggle = widget<CToggleGroup>("heroMovementSpeedPicker");
  69. playerHeroSpeedToggle->setSelected((int)settings["adventure"]["heroSpeed"].Float());
  70. std::shared_ptr<CToggleGroup> enemyHeroSpeedToggle = widget<CToggleGroup>("enemyMovementSpeedPicker");
  71. enemyHeroSpeedToggle->setSelected((int)settings["adventure"]["enemySpeed"].Float());
  72. std::shared_ptr<CToggleGroup> mapScrollSpeedToggle = widget<CToggleGroup>("mapScrollSpeedPicker");
  73. mapScrollSpeedToggle->setSelected((int)settings["adventure"]["scrollSpeed"].Float());
  74. std::shared_ptr<CToggleButton> heroReminderCheckbox = widget<CToggleButton>("heroReminderCheckbox");
  75. heroReminderCheckbox->setSelected((bool)settings["adventure"]["heroReminder"].Bool());
  76. std::shared_ptr<CToggleButton> quickCombatCheckbox = widget<CToggleButton>("quickCombatCheckbox");
  77. quickCombatCheckbox->setSelected((bool)settings["adventure"]["quickCombat"].Bool());
  78. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  79. spellbookAnimationCheckbox->setSelected((bool)settings["video"]["spellbookAnimation"].Bool());
  80. std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
  81. fullscreenCheckbox->setSelected((bool)settings["video"]["fullscreen"].Bool());
  82. onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
  83. {
  84. widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
  85. });
  86. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  87. musicSlider->moveTo(CCS->musich->getVolume());
  88. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  89. volumeSlider->moveTo(CCS->soundh->getVolume());
  90. std::shared_ptr<CButton> loadButton = widget<CButton>("loadButton");
  91. assert(loadButton);
  92. std::shared_ptr<CButton> saveButton = widget<CButton>("saveButton");
  93. assert(saveButton);
  94. std::shared_ptr<CButton> restartButton = widget<CButton>("restartButton");
  95. assert(restartButton);
  96. if(CSH->isGuest())
  97. {
  98. loadButton->block(true);
  99. saveButton->block(true);
  100. restartButton->block(true);
  101. }
  102. }
  103. void SystemOptionsWindow::close()
  104. {
  105. if(GH.topInt().get() != this)
  106. logGlobal->error("Only top interface must be closed");
  107. GH.popInts(1);
  108. }
  109. void SystemOptionsWindow::selectGameResolution()
  110. {
  111. std::vector<std::string> items;
  112. #ifndef VCMI_IOS
  113. SDL_Rect displayBounds;
  114. SDL_GetDisplayBounds(std::max(0, SDL_GetWindowDisplayIndex(mainWindow)), &displayBounds);
  115. #endif
  116. size_t currentResolutionIndex = 0;
  117. size_t i = 0;
  118. for(const auto & it : conf.guiOptions)
  119. {
  120. const auto & resolution = it.first;
  121. #ifndef VCMI_IOS
  122. if(displayBounds.w < resolution.first || displayBounds.h < resolution.second)
  123. continue;
  124. #endif
  125. auto resolutionStr = resolutionToString(resolution.first, resolution.second);
  126. if(widget<CLabel>("resolutionLabel")->getText() == resolutionStr)
  127. currentResolutionIndex = i;
  128. items.push_back(std::move(resolutionStr));
  129. ++i;
  130. }
  131. GH.pushIntT<CObjectListWindow>(items, nullptr,
  132. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  133. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  134. std::bind(&SystemOptionsWindow::setGameResolution, this, _1),
  135. currentResolutionIndex);
  136. }
  137. void SystemOptionsWindow::setGameResolution(int index)
  138. {
  139. auto iter = conf.guiOptions.begin();
  140. std::advance(iter, index);
  141. //do not set resolution to illegal one (0x0)
  142. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  143. Settings gameRes = settings.write["video"]["screenRes"];
  144. gameRes["width"].Float() = iter->first.first;
  145. gameRes["height"].Float() = iter->first.second;
  146. std::string resText;
  147. resText += boost::lexical_cast<std::string>(iter->first.first);
  148. resText += "x";
  149. resText += boost::lexical_cast<std::string>(iter->first.second);
  150. widget<CLabel>("resolutionLabel")->setText(resText);
  151. }
  152. void SystemOptionsWindow::quitGameButtonCallback()
  153. {
  154. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::FORCE_QUIT); }, 0);
  155. }
  156. void SystemOptionsWindow::backButtonCallback()
  157. {
  158. close();
  159. }
  160. void SystemOptionsWindow::mainMenuButtonCallback()
  161. {
  162. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RETURN_TO_MAIN_MENU); }, 0);
  163. }
  164. void SystemOptionsWindow::loadGameButtonCallback()
  165. {
  166. close();
  167. LOCPLINT->proposeLoadingGame();
  168. }
  169. void SystemOptionsWindow::saveGameButtonCallback()
  170. {
  171. close();
  172. GH.pushIntT<CSavingScreen>();
  173. }
  174. void SystemOptionsWindow::restartGameButtonCallback()
  175. {
  176. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RESTART_GAME); }, 0);
  177. }
  178. void SystemOptionsWindow::closeAndPushEvent(int eventType, int code)
  179. {
  180. close();
  181. GH.pushSDLEvent(eventType, code);
  182. }