GeneralOptionsTab.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 "CPlayerInterface.h"
  13. #include "CServerHandler.h"
  14. #include "media/IMusicPlayer.h"
  15. #include "media/ISoundPlayer.h"
  16. #include "render/IScreenHandler.h"
  17. #include "windows/GUIClasses.h"
  18. #include "../../eventsSDL/InputHandler.h"
  19. #include "../../GameEngine.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/texts/CGeneralTextHandler.h"
  26. #include "../../../lib/filesystem/ResourcePath.h"
  27. #include "../../../lib/GameLibrary.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 = LIBRARY->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 = LIBRARY->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 = LIBRARY->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 = LIBRARY->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", ENGINE->input().getCurrentInputMode() == InputMode::TOUCH);
  81. addConditional("keyboardMouse", ENGINE->input().getCurrentInputMode() == InputMode::KEYBOARD_AND_MOUSE);
  82. addConditional("controller", ENGINE->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("performanceOverlayChanged", [](bool value)
  133. {
  134. Settings gameRes = settings.write["video"]["performanceOverlay"];
  135. gameRes["show"].Bool() = value;
  136. });
  137. addCallback("hapticFeedbackChanged", [](bool value)
  138. {
  139. setBoolSetting("general", "hapticFeedback", value);
  140. });
  141. addCallback("enableOverlayChanged", [](bool value)
  142. {
  143. setBoolSetting("general", "enableOverlay", value);
  144. });
  145. addCallback("enableUiEnhancementsChanged", [](bool value)
  146. {
  147. setBoolSetting("general", "enableUiEnhancements", value);
  148. });
  149. addCallback("enableLargeSpellbookChanged", [this](bool value)
  150. {
  151. setBoolSetting("gameTweaks", "enableLargeSpellbook", value);
  152. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  153. if(value)
  154. spellbookAnimationCheckbox->disable();
  155. else
  156. spellbookAnimationCheckbox->enable();
  157. redraw();
  158. });
  159. addCallback("audioMuteFocusChanged", [](bool value)
  160. {
  161. setBoolSetting("general", "audioMuteFocus", value);
  162. });
  163. addCallback("enableSubtitleChanged", [](bool value)
  164. {
  165. setBoolSetting("general", "enableSubtitle", value);
  166. });
  167. //moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
  168. addCallback("availableCreaturesAsDwellingChanged", [=](int value)
  169. {
  170. setBoolSetting("gameTweaks", "availableCreaturesAsDwellingLabel", value > 0);
  171. });
  172. addCallback("compactTownCreatureInfoChanged", [](bool value)
  173. {
  174. setBoolSetting("gameTweaks", "compactTownCreatureInfo", value);
  175. });
  176. build(config);
  177. std::shared_ptr<CLabel> scalingLabel = widget<CLabel>("scalingLabel");
  178. scalingLabel->setText(scalingToLabelString(ENGINE->screenHandler().getInterfaceScalingPercentage()));
  179. std::shared_ptr<CLabel> longTouchLabel = widget<CLabel>("longTouchLabel");
  180. if (longTouchLabel)
  181. longTouchLabel->setText(longTouchToLabelString(settings["general"]["longTouchTimeMilliseconds"].Integer()));
  182. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  183. spellbookAnimationCheckbox->setSelected(settings["video"]["spellbookAnimation"].Bool());
  184. if(settings["gameTweaks"]["enableLargeSpellbook"].Bool())
  185. spellbookAnimationCheckbox->disable();
  186. else
  187. spellbookAnimationCheckbox->enable();
  188. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  189. if (fullscreenBorderlessCheckbox)
  190. fullscreenBorderlessCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool());
  191. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  192. if (fullscreenExclusiveCheckbox)
  193. fullscreenExclusiveCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && settings["video"]["realFullscreen"].Bool());
  194. std::shared_ptr<CToggleButton> infoboxCheckbox = widget<CToggleButton>("performanceOverlayCheckbox");
  195. infoboxCheckbox->setSelected(settings["video"]["performanceOverlay"]["show"].Bool());
  196. std::shared_ptr<CToggleButton> hapticFeedbackCheckbox = widget<CToggleButton>("hapticFeedbackCheckbox");
  197. if (hapticFeedbackCheckbox)
  198. hapticFeedbackCheckbox->setSelected(settings["general"]["hapticFeedback"].Bool());
  199. std::shared_ptr<CToggleButton> enableOverlayCheckbox = widget<CToggleButton>("enableOverlayCheckbox");
  200. if (enableOverlayCheckbox)
  201. enableOverlayCheckbox->setSelected(settings["general"]["enableOverlay"].Bool());
  202. std::shared_ptr<CToggleButton> enableUiEnhancementsCheckbox = widget<CToggleButton>("enableUiEnhancementsCheckbox");
  203. if (enableUiEnhancementsCheckbox)
  204. enableUiEnhancementsCheckbox->setSelected(settings["general"]["enableUiEnhancements"].Bool());
  205. std::shared_ptr<CToggleButton> enableLargeSpellbookCheckbox = widget<CToggleButton>("enableLargeSpellbookCheckbox");
  206. if (enableLargeSpellbookCheckbox)
  207. enableLargeSpellbookCheckbox->setSelected(settings["gameTweaks"]["enableLargeSpellbook"].Bool());
  208. std::shared_ptr<CToggleButton> audioMuteFocusCheckbox = widget<CToggleButton>("audioMuteFocusCheckbox");
  209. if (audioMuteFocusCheckbox)
  210. audioMuteFocusCheckbox->setSelected(settings["general"]["audioMuteFocus"].Bool());
  211. std::shared_ptr<CToggleButton> enableSubtitleCheckbox = widget<CToggleButton>("enableSubtitleCheckbox");
  212. if (enableSubtitleCheckbox)
  213. enableSubtitleCheckbox->setSelected(settings["general"]["enableSubtitle"].Bool());
  214. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  215. musicSlider->scrollTo(ENGINE->music().getVolume());
  216. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  217. volumeSlider->scrollTo(ENGINE->sound().getVolume());
  218. std::shared_ptr<CToggleGroup> creatureGrowthAsDwellingPicker = widget<CToggleGroup>("availableCreaturesAsDwellingPicker");
  219. creatureGrowthAsDwellingPicker->setSelected(settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
  220. std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
  221. compactTownCreatureInfo->setSelected(settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
  222. std::shared_ptr<CLabel> musicVolumeLabel = widget<CLabel>("musicValueLabel");
  223. musicVolumeLabel->setText(std::to_string(ENGINE->music().getVolume()) + "%");
  224. std::shared_ptr<CLabel> soundVolumeLabel = widget<CLabel>("soundValueLabel");
  225. soundVolumeLabel->setText(std::to_string(ENGINE->sound().getVolume()) + "%");
  226. updateResolutionSelector();
  227. }
  228. void GeneralOptionsTab::updateResolutionSelector()
  229. {
  230. std::shared_ptr<CButton> resolutionButton = widget<CButton>("resolutionButton");
  231. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  232. if (resolutionButton)
  233. {
  234. if (settings["video"]["fullscreen"].Bool() && !settings["video"]["realFullscreen"].Bool())
  235. resolutionButton->disable();
  236. else
  237. resolutionButton->enable();
  238. }
  239. if (resolutionLabel)
  240. {
  241. Point resolution = ENGINE->screenHandler().getRenderResolution();
  242. resolutionLabel->setText(resolutionToLabelString(resolution.x, resolution.y));
  243. }
  244. }
  245. void GeneralOptionsTab::selectGameResolution()
  246. {
  247. supportedResolutions = ENGINE->screenHandler().getSupportedResolutions();
  248. std::vector<std::string> items;
  249. size_t currentResolutionIndex = 0;
  250. size_t i = 0;
  251. for(const auto & it : supportedResolutions)
  252. {
  253. auto resolutionStr = resolutionToEntryString(it.x, it.y);
  254. if(widget<CLabel>("resolutionLabel")->getText() == resolutionToLabelString(it.x, it.y))
  255. currentResolutionIndex = i;
  256. items.push_back(std::move(resolutionStr));
  257. ++i;
  258. }
  259. ENGINE->windows().createAndPushWindow<CObjectListWindow>(items, nullptr,
  260. LIBRARY->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  261. LIBRARY->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  262. [this](int index)
  263. {
  264. setGameResolution(index);
  265. },
  266. currentResolutionIndex);
  267. }
  268. void GeneralOptionsTab::setGameResolution(int index)
  269. {
  270. assert(index >= 0 && index < supportedResolutions.size());
  271. if ( index < 0 || index >= supportedResolutions.size() )
  272. return;
  273. Point resolution = supportedResolutions[index];
  274. Settings gameRes = settings.write["video"]["resolution"];
  275. gameRes["width"].Float() = resolution.x;
  276. gameRes["height"].Float() = resolution.y;
  277. widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(resolution.x, resolution.y));
  278. ENGINE->dispatchMainThread([](){
  279. ENGINE->onScreenResize(true, false);
  280. });
  281. }
  282. void GeneralOptionsTab::setFullscreenMode(bool on, bool exclusive)
  283. {
  284. if (on == settings["video"]["fullscreen"].Bool() && exclusive == settings["video"]["realFullscreen"].Bool())
  285. return;
  286. setBoolSetting("video", "realFullscreen", exclusive);
  287. setBoolSetting("video", "fullscreen", on);
  288. std::shared_ptr<CToggleButton> fullscreenExclusiveCheckbox = widget<CToggleButton>("fullscreenExclusiveCheckbox");
  289. std::shared_ptr<CToggleButton> fullscreenBorderlessCheckbox = widget<CToggleButton>("fullscreenBorderlessCheckbox");
  290. if (fullscreenBorderlessCheckbox)
  291. fullscreenBorderlessCheckbox->setSelectedSilent(on && !exclusive);
  292. if (fullscreenExclusiveCheckbox)
  293. fullscreenExclusiveCheckbox->setSelectedSilent(on && exclusive);
  294. updateResolutionSelector();
  295. ENGINE->dispatchMainThread([](){
  296. ENGINE->onScreenResize(true, false);
  297. });
  298. }
  299. void GeneralOptionsTab::selectGameScaling()
  300. {
  301. supportedScaling.clear();
  302. // generate list of all possible scaling values, with 10% step
  303. // also add one value over maximum, so if player can use scaling up to 123.456% he will be able to select 130%
  304. // and let screen handler clamp that value to actual maximum
  305. auto [minimalScaling, maximalScaling] = ENGINE->screenHandler().getSupportedScalingRange();
  306. for (int i = 0; i <= maximalScaling + 10 - 1; i += 10)
  307. {
  308. if (i >= minimalScaling)
  309. supportedScaling.push_back(i);
  310. }
  311. std::vector<std::string> items;
  312. size_t currentIndex = 0;
  313. size_t i = 0;
  314. for(const auto & it : supportedScaling)
  315. {
  316. auto resolutionStr = scalingToEntryString(it);
  317. if(widget<CLabel>("scalingLabel")->getText() == scalingToLabelString(it))
  318. currentIndex = i;
  319. items.push_back(std::move(resolutionStr));
  320. ++i;
  321. }
  322. ENGINE->windows().createAndPushWindow<CObjectListWindow>(
  323. items,
  324. nullptr,
  325. LIBRARY->generaltexth->translate("vcmi.systemOptions.scalingMenu.hover"),
  326. LIBRARY->generaltexth->translate("vcmi.systemOptions.scalingMenu.help"),
  327. [this](int index)
  328. {
  329. setGameScaling(index);
  330. },
  331. currentIndex
  332. );
  333. }
  334. void GeneralOptionsTab::setGameScaling(int index)
  335. {
  336. assert(index >= 0 && index < supportedScaling.size());
  337. if ( index < 0 || index >= supportedScaling.size() )
  338. return;
  339. int scaling = supportedScaling[index];
  340. Settings gameRes = settings.write["video"]["resolution"];
  341. gameRes["scaling"].Float() = scaling;
  342. widget<CLabel>("scalingLabel")->setText(scalingToLabelString(scaling));
  343. ENGINE->dispatchMainThread([](){
  344. ENGINE->onScreenResize(true, false);
  345. });
  346. }
  347. void GeneralOptionsTab::selectLongTouchDuration()
  348. {
  349. longTouchDurations = { 500, 750, 1000, 1250, 1500, 1750, 2000 };
  350. std::vector<std::string> items;
  351. size_t currentIndex = 0;
  352. size_t i = 0;
  353. for(const auto & it : longTouchDurations)
  354. {
  355. auto resolutionStr = longTouchToEntryString(it);
  356. if(widget<CLabel>("longTouchLabel")->getText() == longTouchToLabelString(it))
  357. currentIndex = i;
  358. items.push_back(std::move(resolutionStr));
  359. ++i;
  360. }
  361. ENGINE->windows().createAndPushWindow<CObjectListWindow>(
  362. items,
  363. nullptr,
  364. LIBRARY->generaltexth->translate("vcmi.systemOptions.longTouchMenu.hover"),
  365. LIBRARY->generaltexth->translate("vcmi.systemOptions.longTouchMenu.help"),
  366. [this](int index)
  367. {
  368. setLongTouchDuration(index);
  369. },
  370. currentIndex
  371. );
  372. }
  373. void GeneralOptionsTab::setLongTouchDuration(int index)
  374. {
  375. assert(index >= 0 && index < longTouchDurations.size());
  376. if ( index < 0 || index >= longTouchDurations.size() )
  377. return;
  378. int scaling = longTouchDurations[index];
  379. Settings longTouchTime = settings.write["general"]["longTouchTimeMilliseconds"];
  380. longTouchTime->Float() = scaling;
  381. widget<CLabel>("longTouchLabel")->setText(longTouchToLabelString(scaling));
  382. }