SystemOptionsWindow.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <SDL_rect.h>
  13. #include <SDL_video.h>
  14. #include "../lib/CGeneralTextHandler.h"
  15. #include "../lib/filesystem/ResourceID.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/TextControls.h"
  19. #include "../widgets/Images.h"
  20. #include "../CGameInfo.h"
  21. #include "../CMusicHandler.h"
  22. #include "../CPlayerInterface.h"
  23. #include "VcmiSettingsWindow.h"
  24. #include "GUIClasses.h"
  25. #include "CServerHandler.h"
  26. #include "renderSDL/SDL_Extensions.h"
  27. static void setIntSetting(std::string group, std::string field, int value)
  28. {
  29. Settings entry = settings.write[group][field];
  30. entry->Float() = value;
  31. }
  32. static void setBoolSetting(std::string group, std::string field, bool value)
  33. {
  34. Settings fullscreen = settings.write[group][field];
  35. fullscreen->Bool() = value;
  36. }
  37. static std::string resolutionToString(int w, int h)
  38. {
  39. return std::to_string(w) + 'x' + std::to_string(h);
  40. }
  41. SystemOptionsWindow::SystemOptionsWindow()
  42. : InterfaceObjectConfigurable(),
  43. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  44. {
  45. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  46. const JsonNode config(ResourceID("config/widgets/optionsMenu.json"));
  47. addCallback("playerHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "heroSpeed", _1));
  48. addCallback("enemyHeroSpeedChanged", std::bind(&setIntSetting, "adventure", "enemySpeed", _1));
  49. addCallback("mapScrollSpeedChanged", std::bind(&setIntSetting, "adventure", "scrollSpeed", _1));
  50. addCallback("heroReminderChanged", std::bind(&setBoolSetting, "adventure", "heroReminder", _1));
  51. addCallback("quickCombatChanged", std::bind(&setBoolSetting, "adventure", "quickCombat", _1));
  52. addCallback("spellbookAnimationChanged", std::bind(&setBoolSetting, "video", "spellbookAnimation", _1));
  53. addCallback("fullscreenChanged", std::bind(&setBoolSetting, "video", "fullscreen", _1));
  54. addCallback("setGameResolution", std::bind(&SystemOptionsWindow::selectGameResolution, this));
  55. addCallback("setMusic", [this](int value) { setIntSetting("general", "music", value); widget<CSlider>("musicSlider")->redraw(); });
  56. addCallback("setVolume", [this](int value) { setIntSetting("general", "sound", value); widget<CSlider>("soundVolumeSlider")->redraw(); });
  57. build(config);
  58. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  59. const auto & currentResolution = settings["video"]["screenRes"];
  60. resolutionLabel->setText(resolutionToString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  61. std::shared_ptr<CToggleGroup> playerHeroSpeedToggle = widget<CToggleGroup>("heroMovementSpeedPicker");
  62. playerHeroSpeedToggle->setSelected((int)settings["adventure"]["heroSpeed"].Float());
  63. std::shared_ptr<CToggleGroup> enemyHeroSpeedToggle = widget<CToggleGroup>("enemyMovementSpeedPicker");
  64. enemyHeroSpeedToggle->setSelected((int)settings["adventure"]["enemySpeed"].Float());
  65. std::shared_ptr<CToggleGroup> mapScrollSpeedToggle = widget<CToggleGroup>("mapScrollSpeedPicker");
  66. mapScrollSpeedToggle->setSelected((int)settings["adventure"]["scrollSpeed"].Float());
  67. std::shared_ptr<CToggleButton> heroReminderCheckbox = widget<CToggleButton>("heroReminderCheckbox");
  68. heroReminderCheckbox->setSelected((bool)settings["adventure"]["heroReminder"].Bool());
  69. std::shared_ptr<CToggleButton> quickCombatCheckbox = widget<CToggleButton>("quickCombatCheckbox");
  70. quickCombatCheckbox->setSelected((bool)settings["adventure"]["quickCombat"].Bool());
  71. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  72. spellbookAnimationCheckbox->setSelected((bool)settings["video"]["spellbookAnimation"].Bool());
  73. std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
  74. fullscreenCheckbox->setSelected((bool)settings["video"]["fullscreen"].Bool());
  75. onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
  76. {
  77. widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
  78. });
  79. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  80. musicSlider->moveTo(CCS->musich->getVolume());
  81. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  82. volumeSlider->moveTo(CCS->soundh->getVolume());
  83. }
  84. void SystemOptionsWindow::selectGameResolution()
  85. {
  86. std::vector<std::string> items;
  87. #ifndef VCMI_IOS
  88. Rect displayBounds = CSDL_Ext::getDisplayBounds();
  89. #endif
  90. size_t currentResolutionIndex = 0;
  91. size_t i = 0;
  92. for(const auto & it : conf.guiOptions)
  93. {
  94. const auto & resolution = it.first;
  95. #ifndef VCMI_IOS
  96. if(displayBounds.w < resolution.first || displayBounds.h < resolution.second)
  97. continue;
  98. #endif
  99. auto resolutionStr = resolutionToString(resolution.first, resolution.second);
  100. if(widget<CLabel>("resolutionLabel")->getText() == resolutionStr)
  101. currentResolutionIndex = i;
  102. items.push_back(std::move(resolutionStr));
  103. ++i;
  104. }
  105. GH.pushIntT<CObjectListWindow>(items, nullptr,
  106. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  107. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  108. std::bind(&SystemOptionsWindow::setGameResolution, this, _1),
  109. currentResolutionIndex);
  110. }
  111. void SystemOptionsWindow::setGameResolution(int index)
  112. {
  113. auto iter = conf.guiOptions.begin();
  114. std::advance(iter, index);
  115. //do not set resolution to illegal one (0x0)
  116. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  117. Settings gameRes = settings.write["video"]["screenRes"];
  118. gameRes["width"].Float() = iter->first.first;
  119. gameRes["height"].Float() = iter->first.second;
  120. std::string resText;
  121. resText += boost::lexical_cast<std::string>(iter->first.first);
  122. resText += "x";
  123. resText += boost::lexical_cast<std::string>(iter->first.second);
  124. widget<CLabel>("resolutionLabel")->setText(resText);
  125. }