GeneralOptionsTab.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * GeneralOptionsTab.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 "GeneralOptionsTab.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 "windows/GUIClasses.h"
  22. #include "CServerHandler.h"
  23. #include "renderSDL/SDL_Extensions.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. GeneralOptionsTab::GeneralOptionsTab()
  39. : InterfaceObjectConfigurable(),
  40. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  41. {
  42. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  43. const JsonNode config(ResourceID("config/widgets/settings/generalOptionsTab.json"));
  44. addCallback("spellbookAnimationChanged", std::bind(&setBoolSetting, "video", "spellbookAnimation", _1));
  45. addCallback("setMusic", [this](int value) { setIntSetting("general", "music", value); widget<CSlider>("musicSlider")->redraw(); });
  46. addCallback("setVolume", [this](int value) { setIntSetting("general", "sound", value); widget<CSlider>("soundVolumeSlider")->redraw(); });
  47. //settings that do not belong to base game:
  48. addCallback("fullscreenChanged", std::bind(&GeneralOptionsTab::setFullscreenMode, this, _1));
  49. addCallback("setGameResolution", std::bind(&GeneralOptionsTab::selectGameResolution, this));
  50. addCallback("framerateChanged", std::bind(&setBoolSetting, "general", "showfps", _1));
  51. //moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
  52. addCallback("availableCreaturesAsDwellingLabelChanged", std::bind(&setBoolSetting, "gameTweaks", "availableCreaturesAsDwellingLabel", _1));
  53. addCallback("compactTownCreatureInfoChanged", std::bind(&setBoolSetting, "gameTweaks", "compactTownCreatureInfo", _1));
  54. build(config);
  55. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  56. const auto & currentResolution = settings["video"]["screenRes"];
  57. resolutionLabel->setText(resolutionToString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  58. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  59. spellbookAnimationCheckbox->setSelected((bool)settings["video"]["spellbookAnimation"].Bool());
  60. std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
  61. fullscreenCheckbox->setSelected((bool)settings["video"]["fullscreen"].Bool());
  62. onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
  63. {
  64. widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
  65. });
  66. std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
  67. framerateCheckbox->setSelected((bool)settings["general"]["showfps"].Bool());
  68. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  69. musicSlider->moveTo(CCS->musich->getVolume());
  70. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  71. volumeSlider->moveTo(CCS->soundh->getVolume());
  72. std::shared_ptr<CToggleButton> availableCreaturesAsDwellingLabelCheckbox = widget<CToggleButton>("availableCreaturesAsDwellingLabelCheckbox");
  73. availableCreaturesAsDwellingLabelCheckbox->setSelected((bool)settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
  74. std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
  75. compactTownCreatureInfo->setSelected((bool)settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
  76. }
  77. bool GeneralOptionsTab::isResolutionSupported(const Point & resolution)
  78. {
  79. return isResolutionSupported( resolution, settings["video"]["fullscreen"].Bool());
  80. }
  81. bool GeneralOptionsTab::isResolutionSupported(const Point & resolution, bool fullscreen)
  82. {
  83. if (!fullscreen)
  84. return true;
  85. auto supportedList = CSDL_Ext::getSupportedResolutions();
  86. return CSDL_Ext::isResolutionSupported(supportedList, resolution);
  87. }
  88. void GeneralOptionsTab::selectGameResolution()
  89. {
  90. fillSelectableResolutions();
  91. std::vector<std::string> items;
  92. size_t currentResolutionIndex = 0;
  93. size_t i = 0;
  94. for(const auto & it : selectableResolutions)
  95. {
  96. auto resolutionStr = resolutionToString(it.x, it.y);
  97. if(widget<CLabel>("resolutionLabel")->getText() == resolutionStr)
  98. currentResolutionIndex = i;
  99. items.push_back(std::move(resolutionStr));
  100. ++i;
  101. }
  102. GH.pushIntT<CObjectListWindow>(items, nullptr,
  103. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  104. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  105. std::bind(&GeneralOptionsTab::setGameResolution, this, _1),
  106. currentResolutionIndex);
  107. }
  108. void GeneralOptionsTab::setGameResolution(int index)
  109. {
  110. assert(index >= 0 && index < selectableResolutions.size());
  111. if ( index < 0 || index >= selectableResolutions.size() )
  112. return;
  113. Point resolution = selectableResolutions[index];
  114. Settings gameRes = settings.write["video"]["screenRes"];
  115. gameRes["width"].Float() = resolution.x;
  116. gameRes["height"].Float() = resolution.y;
  117. std::string resText;
  118. resText += std::to_string(resolution.x);
  119. resText += "x";
  120. resText += std::to_string(resolution.y);
  121. widget<CLabel>("resolutionLabel")->setText(resText);
  122. }
  123. void GeneralOptionsTab::setFullscreenMode(bool on)
  124. {
  125. fillSelectableResolutions();
  126. const auto & screenRes = settings["video"]["screenRes"];
  127. const Point desiredResolution(screenRes["width"].Integer(), screenRes["height"].Integer());
  128. const Point currentResolution = GH.screenDimensions();
  129. if (!isResolutionSupported(currentResolution, on))
  130. {
  131. widget<CToggleButton>("fullscreenCheckbox")->setSelected(!on);
  132. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.systemOptions.fullscreenFailed"));
  133. return;
  134. }
  135. setBoolSetting("video", "fullscreen", on);
  136. if (!isResolutionSupported(desiredResolution, on))
  137. {
  138. // user changed his desired resolution and switched to fullscreen
  139. // however resolution he selected before is not available in fullscreen
  140. // so reset it back to currect resolution which is confirmed to be supported earlier
  141. Settings gameRes = settings.write["video"]["screenRes"];
  142. gameRes["width"].Float() = currentResolution.x;
  143. gameRes["height"].Float() = currentResolution.y;
  144. widget<CLabel>("resolutionLabel")->setText(resolutionToString(currentResolution.x, currentResolution.y));
  145. }
  146. }
  147. void GeneralOptionsTab::fillSelectableResolutions()
  148. {
  149. selectableResolutions.clear();
  150. for(const auto & it : conf.guiOptions)
  151. {
  152. const Point dimensions(it.first.first, it.first.second);
  153. if(isResolutionSupported(dimensions))
  154. selectableResolutions.push_back(dimensions);
  155. }
  156. boost::range::sort(selectableResolutions, [](const auto & left, const auto & right)
  157. {
  158. return left.x * left.y < right.x * right.y;
  159. });
  160. }