GeneralOptionsTab.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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> resolutionLabel = widget<CLabel>("resolutionLabel");
  125. if (resolutionLabel)
  126. resolutionLabel->setText(resolutionToLabelString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  127. std::shared_ptr<CLabel> scalingLabel = widget<CLabel>("scalingLabel");
  128. scalingLabel->setText(scalingToLabelString(currentResolution["scaling"].Integer()));
  129. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  130. spellbookAnimationCheckbox->setSelected(settings["video"]["spellbookAnimation"].Bool());
  131. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  132. if (fullscreenBorderlessCheckbox)
  133. fullscreenBorderlessCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool());
  134. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  135. if (fullscreenExclusiveCheckbox)
  136. fullscreenExclusiveCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && settings["video"]["realFullscreen"].Bool());
  137. std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
  138. framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
  139. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  140. musicSlider->moveTo(CCS->musich->getVolume());
  141. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  142. volumeSlider->moveTo(CCS->soundh->getVolume());
  143. std::shared_ptr<CToggleGroup> creatureGrowthAsDwellingPicker = widget<CToggleGroup>("availableCreaturesAsDwellingPicker");
  144. creatureGrowthAsDwellingPicker->setSelected(settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
  145. std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
  146. compactTownCreatureInfo->setSelected(settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
  147. std::shared_ptr<CLabel> musicVolumeLabel = widget<CLabel>("musicValueLabel");
  148. musicVolumeLabel->setText(std::to_string(CCS->musich->getVolume()) + "%");
  149. std::shared_ptr<CLabel> soundVolumeLabel = widget<CLabel>("soundValueLabel");
  150. soundVolumeLabel->setText(std::to_string(CCS->soundh->getVolume()) + "%");
  151. }
  152. void GeneralOptionsTab::selectGameResolution()
  153. {
  154. supportedResolutions = GH.screenHandler().getSupportedResolutions();
  155. std::vector<std::string> items;
  156. size_t currentResolutionIndex = 0;
  157. size_t i = 0;
  158. for(const auto & it : supportedResolutions)
  159. {
  160. auto resolutionStr = resolutionToEntryString(it.x, it.y);
  161. if(widget<CLabel>("resolutionLabel")->getText() == resolutionToLabelString(it.x, it.y))
  162. currentResolutionIndex = i;
  163. items.push_back(std::move(resolutionStr));
  164. ++i;
  165. }
  166. GH.windows().createAndPushWindow<CObjectListWindow>(items, nullptr,
  167. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  168. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  169. [this](int index)
  170. {
  171. setGameResolution(index);
  172. },
  173. currentResolutionIndex);
  174. }
  175. void GeneralOptionsTab::setGameResolution(int index)
  176. {
  177. assert(index >= 0 && index < supportedResolutions.size());
  178. if ( index < 0 || index >= supportedResolutions.size() )
  179. return;
  180. Point resolution = supportedResolutions[index];
  181. Settings gameRes = settings.write["video"]["resolution"];
  182. gameRes["width"].Float() = resolution.x;
  183. gameRes["height"].Float() = resolution.y;
  184. widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(resolution.x, resolution.y));
  185. }
  186. void GeneralOptionsTab::setFullscreenMode(bool on, bool exclusive)
  187. {
  188. setBoolSetting("video", "realFullscreen", exclusive);
  189. setBoolSetting("video", "fullscreen", on);
  190. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  191. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  192. if (fullscreenBorderlessCheckbox)
  193. fullscreenBorderlessCheckbox->setSelected(on && !exclusive);
  194. if (fullscreenExclusiveCheckbox)
  195. fullscreenExclusiveCheckbox->setSelected(on && exclusive);
  196. }
  197. void GeneralOptionsTab::selectGameScaling()
  198. {
  199. supportedScaling.clear();
  200. auto [minimalScaling, maximalScaling] = GH.screenHandler().getSupportedScalingRange();
  201. for (int i = 0; i <= maximalScaling; i += 10)
  202. {
  203. if (i >= minimalScaling)
  204. supportedScaling.push_back(i);
  205. }
  206. std::vector<std::string> items;
  207. size_t currentIndex = 0;
  208. size_t i = 0;
  209. for(const auto & it : supportedScaling)
  210. {
  211. auto resolutionStr = scalingToEntryString(it);
  212. if(widget<CLabel>("scalingLabel")->getText() == scalingToLabelString(it))
  213. currentIndex = i;
  214. items.push_back(std::move(resolutionStr));
  215. ++i;
  216. }
  217. GH.windows().createAndPushWindow<CObjectListWindow>(
  218. items,
  219. nullptr,
  220. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.hover"),
  221. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.help"),
  222. [this](int index)
  223. {
  224. setGameScaling(index);
  225. },
  226. currentIndex
  227. );
  228. }
  229. void GeneralOptionsTab::setGameScaling(int index)
  230. {
  231. assert(index >= 0 && index < supportedScaling.size());
  232. if ( index < 0 || index >= supportedScaling.size() )
  233. return;
  234. int scaling = supportedScaling[index];
  235. Settings gameRes = settings.write["video"]["resolution"];
  236. gameRes["scaling"].Float() = scaling;
  237. widget<CLabel>("scalingLabel")->setText(scalingToLabelString(scaling));
  238. }