GeneralOptionsTab.cpp 15 KB

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