GeneralOptionsTab.cpp 10 KB

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