GeneralOptionsTab.cpp 14 KB

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