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