GeneralOptionsTab.cpp 7.4 KB

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