GeneralOptionsTab.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "../../gui/WindowHandler.h"
  16. #include "../../widgets/Buttons.h"
  17. #include "../../widgets/Slider.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 "windows/GUIClasses.h"
  24. #include "CServerHandler.h"
  25. #include "render/IScreenHandler.h"
  26. static void setIntSetting(std::string group, std::string field, int value)
  27. {
  28. Settings entry = settings.write[group][field];
  29. entry->Float() = value;
  30. }
  31. static void setBoolSetting(std::string group, std::string field, bool value)
  32. {
  33. Settings entry = settings.write[group][field];
  34. entry->Bool() = value;
  35. }
  36. static std::string scalingToEntryString( int scaling)
  37. {
  38. return std::to_string(scaling) + '%';
  39. }
  40. static std::string scalingToLabelString( int scaling)
  41. {
  42. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.scalingButton.hover");
  43. boost::replace_all(string, "%p", std::to_string(scaling));
  44. return string;
  45. }
  46. static std::string resolutionToEntryString( int w, int h)
  47. {
  48. std::string string = "%wx%h";
  49. boost::replace_all(string, "%w", std::to_string(w));
  50. boost::replace_all(string, "%h", std::to_string(h));
  51. return string;
  52. }
  53. static std::string resolutionToLabelString( int w, int h)
  54. {
  55. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.resolutionButton.hover");
  56. boost::replace_all(string, "%w", std::to_string(w));
  57. boost::replace_all(string, "%h", std::to_string(h));
  58. return string;
  59. }
  60. GeneralOptionsTab::GeneralOptionsTab()
  61. : InterfaceObjectConfigurable(),
  62. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  63. {
  64. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  65. type |= REDRAW_PARENT;
  66. addConditional("mobile", false);
  67. addConditional("desktop", true);
  68. #ifdef VCMI_MOBILE
  69. addConditional("mobile", true);
  70. addConditional("desktop", false);
  71. #endif
  72. const JsonNode config(ResourceID("config/widgets/settings/generalOptionsTab.json"));
  73. addCallback("spellbookAnimationChanged", [](bool value)
  74. {
  75. setBoolSetting("video", "spellbookAnimation", value);
  76. });
  77. addCallback("setMusic", [this](int value)
  78. {
  79. setIntSetting("general", "music", value);
  80. widget<CSlider>("musicSlider")->redraw();
  81. auto targetLabel = widget<CLabel>("musicValueLabel");
  82. if (targetLabel)
  83. targetLabel->setText(std::to_string(value) + "%");
  84. });
  85. addCallback("setVolume", [this](int value)
  86. {
  87. setIntSetting("general", "sound", value);
  88. widget<CSlider>("soundVolumeSlider")->redraw();
  89. auto targetLabel = widget<CLabel>("soundValueLabel");
  90. if (targetLabel)
  91. targetLabel->setText(std::to_string(value) + "%");
  92. });
  93. //settings that do not belong to base game:
  94. addCallback("fullscreenBorderlessChanged", [this](bool value)
  95. {
  96. setFullscreenMode(value, false);
  97. });
  98. addCallback("fullscreenExclusiveChanged", [this](bool value)
  99. {
  100. setFullscreenMode(value, true);
  101. });
  102. addCallback("setGameResolution", [this](int dummyValue)
  103. {
  104. selectGameResolution();
  105. });
  106. addCallback("setGameScaling", [this](int dummyValue)
  107. {
  108. selectGameScaling();
  109. });
  110. addCallback("framerateChanged", [](bool value)
  111. {
  112. setBoolSetting("video", "showfps", value);
  113. });
  114. //moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
  115. addCallback("availableCreaturesAsDwellingChanged", [=](int value)
  116. {
  117. setBoolSetting("gameTweaks", "availableCreaturesAsDwellingLabel", value > 0);
  118. });
  119. addCallback("compactTownCreatureInfoChanged", [](bool value)
  120. {
  121. setBoolSetting("gameTweaks", "compactTownCreatureInfo", value);
  122. });
  123. build(config);
  124. const auto & currentResolution = settings["video"]["resolution"];
  125. std::shared_ptr<CLabel> scalingLabel = widget<CLabel>("scalingLabel");
  126. scalingLabel->setText(scalingToLabelString(currentResolution["scaling"].Integer()));
  127. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  128. spellbookAnimationCheckbox->setSelected(settings["video"]["spellbookAnimation"].Bool());
  129. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  130. if (fullscreenBorderlessCheckbox)
  131. fullscreenBorderlessCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool());
  132. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  133. if (fullscreenExclusiveCheckbox)
  134. fullscreenExclusiveCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && settings["video"]["realFullscreen"].Bool());
  135. std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
  136. framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
  137. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  138. musicSlider->scrollTo(CCS->musich->getVolume());
  139. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  140. volumeSlider->scrollTo(CCS->soundh->getVolume());
  141. std::shared_ptr<CToggleGroup> creatureGrowthAsDwellingPicker = widget<CToggleGroup>("availableCreaturesAsDwellingPicker");
  142. creatureGrowthAsDwellingPicker->setSelected(settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
  143. std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
  144. compactTownCreatureInfo->setSelected(settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
  145. std::shared_ptr<CLabel> musicVolumeLabel = widget<CLabel>("musicValueLabel");
  146. musicVolumeLabel->setText(std::to_string(CCS->musich->getVolume()) + "%");
  147. std::shared_ptr<CLabel> soundVolumeLabel = widget<CLabel>("soundValueLabel");
  148. soundVolumeLabel->setText(std::to_string(CCS->soundh->getVolume()) + "%");
  149. updateResolutionSelector();
  150. }
  151. void GeneralOptionsTab::updateResolutionSelector()
  152. {
  153. std::shared_ptr<CButton> resolutionButton = widget<CButton>("resolutionButton");
  154. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  155. if (settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool())
  156. {
  157. if (resolutionButton)
  158. resolutionButton->disable();
  159. if (resolutionLabel)
  160. resolutionLabel->setText(resolutionToLabelString(GH.screenDimensions().x, GH.screenDimensions().y));
  161. }
  162. else
  163. {
  164. const auto & currentResolution = settings["video"]["resolution"];
  165. if (resolutionButton)
  166. resolutionButton->enable();
  167. if (resolutionLabel)
  168. resolutionLabel->setText(resolutionToLabelString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  169. }
  170. }
  171. void GeneralOptionsTab::selectGameResolution()
  172. {
  173. supportedResolutions = GH.screenHandler().getSupportedResolutions();
  174. std::vector<std::string> items;
  175. size_t currentResolutionIndex = 0;
  176. size_t i = 0;
  177. for(const auto & it : supportedResolutions)
  178. {
  179. auto resolutionStr = resolutionToEntryString(it.x, it.y);
  180. if(widget<CLabel>("resolutionLabel")->getText() == resolutionToLabelString(it.x, it.y))
  181. currentResolutionIndex = i;
  182. items.push_back(std::move(resolutionStr));
  183. ++i;
  184. }
  185. GH.windows().createAndPushWindow<CObjectListWindow>(items, nullptr,
  186. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  187. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  188. [this](int index)
  189. {
  190. setGameResolution(index);
  191. },
  192. currentResolutionIndex);
  193. }
  194. void GeneralOptionsTab::setGameResolution(int index)
  195. {
  196. assert(index >= 0 && index < supportedResolutions.size());
  197. if ( index < 0 || index >= supportedResolutions.size() )
  198. return;
  199. Point resolution = supportedResolutions[index];
  200. Settings gameRes = settings.write["video"]["resolution"];
  201. gameRes["width"].Float() = resolution.x;
  202. gameRes["height"].Float() = resolution.y;
  203. widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(resolution.x, resolution.y));
  204. }
  205. void GeneralOptionsTab::setFullscreenMode(bool on, bool exclusive)
  206. {
  207. setBoolSetting("video", "realFullscreen", exclusive);
  208. setBoolSetting("video", "fullscreen", on);
  209. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  210. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  211. if (fullscreenBorderlessCheckbox)
  212. fullscreenBorderlessCheckbox->setSelected(on && !exclusive);
  213. if (fullscreenExclusiveCheckbox)
  214. fullscreenExclusiveCheckbox->setSelected(on && exclusive);
  215. updateResolutionSelector();
  216. }
  217. void GeneralOptionsTab::selectGameScaling()
  218. {
  219. supportedScaling.clear();
  220. // generate list of all possible scaling values, with 10% step
  221. // also add one value over maximum, so if player can use scaling up to 123.456% he will be able to select 130%
  222. // and let screen handler clamp that value to actual maximum
  223. auto [minimalScaling, maximalScaling] = GH.screenHandler().getSupportedScalingRange();
  224. for (int i = 0; i <= maximalScaling + 10 - 1; i += 10)
  225. {
  226. if (i >= minimalScaling)
  227. supportedScaling.push_back(i);
  228. }
  229. std::vector<std::string> items;
  230. size_t currentIndex = 0;
  231. size_t i = 0;
  232. for(const auto & it : supportedScaling)
  233. {
  234. auto resolutionStr = scalingToEntryString(it);
  235. if(widget<CLabel>("scalingLabel")->getText() == scalingToLabelString(it))
  236. currentIndex = i;
  237. items.push_back(std::move(resolutionStr));
  238. ++i;
  239. }
  240. GH.windows().createAndPushWindow<CObjectListWindow>(
  241. items,
  242. nullptr,
  243. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.hover"),
  244. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.help"),
  245. [this](int index)
  246. {
  247. setGameScaling(index);
  248. },
  249. currentIndex
  250. );
  251. }
  252. void GeneralOptionsTab::setGameScaling(int index)
  253. {
  254. assert(index >= 0 && index < supportedScaling.size());
  255. if ( index < 0 || index >= supportedScaling.size() )
  256. return;
  257. int scaling = supportedScaling[index];
  258. Settings gameRes = settings.write["video"]["resolution"];
  259. gameRes["scaling"].Float() = scaling;
  260. widget<CLabel>("scalingLabel")->setText(scalingToLabelString(scaling));
  261. }