GeneralOptionsTab.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 "CGameInfo.h"
  13. #include "CPlayerInterface.h"
  14. #include "CServerHandler.h"
  15. #include "media/IMusicPlayer.h"
  16. #include "media/ISoundPlayer.h"
  17. #include "render/IScreenHandler.h"
  18. #include "windows/GUIClasses.h"
  19. #include "../../eventsSDL/InputHandler.h"
  20. #include "../../gui/CGuiHandler.h"
  21. #include "../../gui/WindowHandler.h"
  22. #include "../../widgets/Buttons.h"
  23. #include "../../widgets/Images.h"
  24. #include "../../widgets/Slider.h"
  25. #include "../../widgets/TextControls.h"
  26. #include "../../../lib/texts/CGeneralTextHandler.h"
  27. #include "../../../lib/filesystem/ResourcePath.h"
  28. static void setIntSetting(std::string group, std::string field, int value)
  29. {
  30. Settings entry = settings.write[group][field];
  31. entry->Float() = value;
  32. }
  33. static void setBoolSetting(std::string group, std::string field, bool value)
  34. {
  35. Settings entry = settings.write[group][field];
  36. entry->Bool() = value;
  37. }
  38. static std::string scalingToEntryString( int scaling)
  39. {
  40. return std::to_string(scaling) + '%';
  41. }
  42. static std::string scalingToLabelString( int scaling)
  43. {
  44. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.scalingButton.hover");
  45. boost::replace_all(string, "%p", std::to_string(scaling));
  46. return string;
  47. }
  48. static std::string longTouchToEntryString( int duration)
  49. {
  50. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.longTouchMenu.entry");
  51. boost::replace_all(string, "%d", std::to_string(duration));
  52. return string;
  53. }
  54. static std::string longTouchToLabelString( int duration)
  55. {
  56. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.longTouchButton.hover");
  57. boost::replace_all(string, "%d", std::to_string(duration));
  58. return string;
  59. }
  60. static std::string resolutionToEntryString( int w, int h)
  61. {
  62. std::string string = "%wx%h";
  63. boost::replace_all(string, "%w", std::to_string(w));
  64. boost::replace_all(string, "%h", std::to_string(h));
  65. return string;
  66. }
  67. static std::string resolutionToLabelString( int w, int h)
  68. {
  69. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.resolutionButton.hover");
  70. boost::replace_all(string, "%w", std::to_string(w));
  71. boost::replace_all(string, "%h", std::to_string(h));
  72. return string;
  73. }
  74. GeneralOptionsTab::GeneralOptionsTab()
  75. : InterfaceObjectConfigurable(),
  76. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  77. {
  78. OBJECT_CONSTRUCTION;
  79. setRedrawParent(true);
  80. addConditional("touchscreen", GH.input().hasTouchInputDevice());
  81. #ifdef VCMI_MOBILE
  82. addConditional("mobile", true);
  83. addConditional("desktop", false);
  84. #else
  85. addConditional("mobile", false);
  86. addConditional("desktop", true);
  87. #endif
  88. const JsonNode config(JsonPath::builtin("config/widgets/settings/generalOptionsTab.json"));
  89. addCallback("spellbookAnimationChanged", [](bool value)
  90. {
  91. setBoolSetting("video", "spellbookAnimation", value);
  92. });
  93. addCallback("setMusic", [this](int value)
  94. {
  95. setIntSetting("general", "music", value);
  96. widget<CSlider>("musicSlider")->redraw();
  97. auto targetLabel = widget<CLabel>("musicValueLabel");
  98. if (targetLabel)
  99. targetLabel->setText(std::to_string(value) + "%");
  100. });
  101. addCallback("setVolume", [this](int value)
  102. {
  103. setIntSetting("general", "sound", value);
  104. widget<CSlider>("soundVolumeSlider")->redraw();
  105. auto targetLabel = widget<CLabel>("soundValueLabel");
  106. if (targetLabel)
  107. targetLabel->setText(std::to_string(value) + "%");
  108. });
  109. //settings that do not belong to base game:
  110. addCallback("fullscreenBorderlessChanged", [this](bool value)
  111. {
  112. setFullscreenMode(value, false);
  113. });
  114. addCallback("fullscreenExclusiveChanged", [this](bool value)
  115. {
  116. setFullscreenMode(value, true);
  117. });
  118. addCallback("setGameResolution", [this](int dummyValue)
  119. {
  120. selectGameResolution();
  121. });
  122. addCallback("setGameScaling", [this](int dummyValue)
  123. {
  124. selectGameScaling();
  125. });
  126. addCallback("setLongTouchDuration", [this](int dummyValue)
  127. {
  128. selectLongTouchDuration();
  129. });
  130. addCallback("framerateChanged", [](bool value)
  131. {
  132. setBoolSetting("video", "showfps", value);
  133. });
  134. addCallback("hapticFeedbackChanged", [](bool value)
  135. {
  136. setBoolSetting("general", "hapticFeedback", value);
  137. });
  138. addCallback("enableUiEnhancementsChanged", [](bool value)
  139. {
  140. setBoolSetting("general", "enableUiEnhancements", value);
  141. });
  142. addCallback("enableLargeSpellbookChanged", [this](bool value)
  143. {
  144. setBoolSetting("gameTweaks", "enableLargeSpellbook", value);
  145. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  146. if(value)
  147. spellbookAnimationCheckbox->disable();
  148. else
  149. spellbookAnimationCheckbox->enable();
  150. redraw();
  151. });
  152. addCallback("audioMuteFocusChanged", [](bool value)
  153. {
  154. setBoolSetting("general", "audioMuteFocus", value);
  155. });
  156. //moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
  157. addCallback("availableCreaturesAsDwellingChanged", [=](int value)
  158. {
  159. setBoolSetting("gameTweaks", "availableCreaturesAsDwellingLabel", value > 0);
  160. });
  161. addCallback("compactTownCreatureInfoChanged", [](bool value)
  162. {
  163. setBoolSetting("gameTweaks", "compactTownCreatureInfo", value);
  164. });
  165. build(config);
  166. const auto & currentResolution = settings["video"]["resolution"];
  167. std::shared_ptr<CLabel> scalingLabel = widget<CLabel>("scalingLabel");
  168. scalingLabel->setText(scalingToLabelString(currentResolution["scaling"].Integer()));
  169. std::shared_ptr<CLabel> longTouchLabel = widget<CLabel>("longTouchLabel");
  170. if (longTouchLabel)
  171. longTouchLabel->setText(longTouchToLabelString(settings["general"]["longTouchTimeMilliseconds"].Integer()));
  172. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  173. spellbookAnimationCheckbox->setSelected(settings["video"]["spellbookAnimation"].Bool());
  174. if(settings["gameTweaks"]["enableLargeSpellbook"].Bool())
  175. spellbookAnimationCheckbox->disable();
  176. else
  177. spellbookAnimationCheckbox->enable();
  178. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  179. if (fullscreenBorderlessCheckbox)
  180. fullscreenBorderlessCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool());
  181. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  182. if (fullscreenExclusiveCheckbox)
  183. fullscreenExclusiveCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && settings["video"]["realFullscreen"].Bool());
  184. std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
  185. framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
  186. std::shared_ptr<CToggleButton> hapticFeedbackCheckbox = widget<CToggleButton>("hapticFeedbackCheckbox");
  187. if (hapticFeedbackCheckbox)
  188. hapticFeedbackCheckbox->setSelected(settings["general"]["hapticFeedback"].Bool());
  189. std::shared_ptr<CToggleButton> enableUiEnhancementsCheckbox = widget<CToggleButton>("enableUiEnhancementsCheckbox");
  190. if (enableUiEnhancementsCheckbox)
  191. enableUiEnhancementsCheckbox->setSelected(settings["general"]["enableUiEnhancements"].Bool());
  192. std::shared_ptr<CToggleButton> enableLargeSpellbookCheckbox = widget<CToggleButton>("enableLargeSpellbookCheckbox");
  193. if (enableLargeSpellbookCheckbox)
  194. enableLargeSpellbookCheckbox->setSelected(settings["gameTweaks"]["enableLargeSpellbook"].Bool());
  195. std::shared_ptr<CToggleButton> audioMuteFocusCheckbox = widget<CToggleButton>("audioMuteFocusCheckbox");
  196. if (audioMuteFocusCheckbox)
  197. audioMuteFocusCheckbox->setSelected(settings["general"]["audioMuteFocus"].Bool());
  198. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  199. musicSlider->scrollTo(CCS->musich->getVolume());
  200. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  201. volumeSlider->scrollTo(CCS->soundh->getVolume());
  202. std::shared_ptr<CToggleGroup> creatureGrowthAsDwellingPicker = widget<CToggleGroup>("availableCreaturesAsDwellingPicker");
  203. creatureGrowthAsDwellingPicker->setSelected(settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
  204. std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
  205. compactTownCreatureInfo->setSelected(settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
  206. std::shared_ptr<CLabel> musicVolumeLabel = widget<CLabel>("musicValueLabel");
  207. musicVolumeLabel->setText(std::to_string(CCS->musich->getVolume()) + "%");
  208. std::shared_ptr<CLabel> soundVolumeLabel = widget<CLabel>("soundValueLabel");
  209. soundVolumeLabel->setText(std::to_string(CCS->soundh->getVolume()) + "%");
  210. updateResolutionSelector();
  211. }
  212. void GeneralOptionsTab::updateResolutionSelector()
  213. {
  214. std::shared_ptr<CButton> resolutionButton = widget<CButton>("resolutionButton");
  215. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  216. if (resolutionButton)
  217. {
  218. if (settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool())
  219. resolutionButton->disable();
  220. else
  221. resolutionButton->enable();
  222. }
  223. if (resolutionLabel)
  224. {
  225. Point resolution = GH.screenHandler().getRenderResolution();
  226. resolutionLabel->setText(resolutionToLabelString(resolution.x, resolution.y));
  227. }
  228. }
  229. void GeneralOptionsTab::selectGameResolution()
  230. {
  231. supportedResolutions = GH.screenHandler().getSupportedResolutions();
  232. std::vector<std::string> items;
  233. size_t currentResolutionIndex = 0;
  234. size_t i = 0;
  235. for(const auto & it : supportedResolutions)
  236. {
  237. auto resolutionStr = resolutionToEntryString(it.x, it.y);
  238. if(widget<CLabel>("resolutionLabel")->getText() == resolutionToLabelString(it.x, it.y))
  239. currentResolutionIndex = i;
  240. items.push_back(std::move(resolutionStr));
  241. ++i;
  242. }
  243. GH.windows().createAndPushWindow<CObjectListWindow>(items, nullptr,
  244. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  245. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  246. [this](int index)
  247. {
  248. setGameResolution(index);
  249. },
  250. currentResolutionIndex);
  251. }
  252. void GeneralOptionsTab::setGameResolution(int index)
  253. {
  254. assert(index >= 0 && index < supportedResolutions.size());
  255. if ( index < 0 || index >= supportedResolutions.size() )
  256. return;
  257. Point resolution = supportedResolutions[index];
  258. Settings gameRes = settings.write["video"]["resolution"];
  259. gameRes["width"].Float() = resolution.x;
  260. gameRes["height"].Float() = resolution.y;
  261. widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(resolution.x, resolution.y));
  262. GH.dispatchMainThread([](){
  263. GH.onScreenResize(true);
  264. });
  265. }
  266. void GeneralOptionsTab::setFullscreenMode(bool on, bool exclusive)
  267. {
  268. if (on == settings["video"]["fullscreen"].Bool() && exclusive == settings["video"]["realFullscreen"].Bool())
  269. return;
  270. setBoolSetting("video", "realFullscreen", exclusive);
  271. setBoolSetting("video", "fullscreen", on);
  272. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  273. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  274. if (fullscreenBorderlessCheckbox)
  275. fullscreenBorderlessCheckbox->setSelectedSilent(on && !exclusive);
  276. if (fullscreenExclusiveCheckbox)
  277. fullscreenExclusiveCheckbox->setSelectedSilent(on && exclusive);
  278. updateResolutionSelector();
  279. GH.dispatchMainThread([](){
  280. GH.onScreenResize(true);
  281. });
  282. }
  283. void GeneralOptionsTab::selectGameScaling()
  284. {
  285. supportedScaling.clear();
  286. // generate list of all possible scaling values, with 10% step
  287. // also add one value over maximum, so if player can use scaling up to 123.456% he will be able to select 130%
  288. // and let screen handler clamp that value to actual maximum
  289. auto [minimalScaling, maximalScaling] = GH.screenHandler().getSupportedScalingRange();
  290. for (int i = 0; i <= maximalScaling + 10 - 1; i += 10)
  291. {
  292. if (i >= minimalScaling)
  293. supportedScaling.push_back(i);
  294. }
  295. std::vector<std::string> items;
  296. size_t currentIndex = 0;
  297. size_t i = 0;
  298. for(const auto & it : supportedScaling)
  299. {
  300. auto resolutionStr = scalingToEntryString(it);
  301. if(widget<CLabel>("scalingLabel")->getText() == scalingToLabelString(it))
  302. currentIndex = i;
  303. items.push_back(std::move(resolutionStr));
  304. ++i;
  305. }
  306. GH.windows().createAndPushWindow<CObjectListWindow>(
  307. items,
  308. nullptr,
  309. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.hover"),
  310. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.help"),
  311. [this](int index)
  312. {
  313. setGameScaling(index);
  314. },
  315. currentIndex
  316. );
  317. }
  318. void GeneralOptionsTab::setGameScaling(int index)
  319. {
  320. assert(index >= 0 && index < supportedScaling.size());
  321. if ( index < 0 || index >= supportedScaling.size() )
  322. return;
  323. int scaling = supportedScaling[index];
  324. Settings gameRes = settings.write["video"]["resolution"];
  325. gameRes["scaling"].Float() = scaling;
  326. widget<CLabel>("scalingLabel")->setText(scalingToLabelString(scaling));
  327. GH.dispatchMainThread([](){
  328. GH.onScreenResize(true);
  329. });
  330. }
  331. void GeneralOptionsTab::selectLongTouchDuration()
  332. {
  333. longTouchDurations = { 500, 750, 1000, 1250, 1500, 1750, 2000 };
  334. std::vector<std::string> items;
  335. size_t currentIndex = 0;
  336. size_t i = 0;
  337. for(const auto & it : longTouchDurations)
  338. {
  339. auto resolutionStr = longTouchToEntryString(it);
  340. if(widget<CLabel>("longTouchLabel")->getText() == longTouchToLabelString(it))
  341. currentIndex = i;
  342. items.push_back(std::move(resolutionStr));
  343. ++i;
  344. }
  345. GH.windows().createAndPushWindow<CObjectListWindow>(
  346. items,
  347. nullptr,
  348. CGI->generaltexth->translate("vcmi.systemOptions.longTouchMenu.hover"),
  349. CGI->generaltexth->translate("vcmi.systemOptions.longTouchMenu.help"),
  350. [this](int index)
  351. {
  352. setLongTouchDuration(index);
  353. },
  354. currentIndex
  355. );
  356. }
  357. void GeneralOptionsTab::setLongTouchDuration(int index)
  358. {
  359. assert(index >= 0 && index < longTouchDurations.size());
  360. if ( index < 0 || index >= longTouchDurations.size() )
  361. return;
  362. int scaling = longTouchDurations[index];
  363. Settings longTouchTime = settings.write["general"]["longTouchTimeMilliseconds"];
  364. longTouchTime->Float() = scaling;
  365. widget<CLabel>("longTouchLabel")->setText(longTouchToLabelString(scaling));
  366. }