GeneralOptionsTab.cpp 10 KB

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