GeneralOptionsTab.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "../../../lib/CGeneralTextHandler.h"
  13. #include "../../../lib/filesystem/ResourceID.h"
  14. #include "../../gui/CGuiHandler.h"
  15. #include "../../gui/WindowHandler.h"
  16. #include "../../widgets/Buttons.h"
  17. #include "../../widgets/TextControls.h"
  18. #include "../../widgets/Images.h"
  19. #include "CGameInfo.h"
  20. #include "CMusicHandler.h"
  21. #include "CPlayerInterface.h"
  22. #include "windows/GUIClasses.h"
  23. #include "CServerHandler.h"
  24. #include "render/IScreenHandler.h"
  25. static void setIntSetting(std::string group, std::string field, int value)
  26. {
  27. Settings entry = settings.write[group][field];
  28. entry->Float() = value;
  29. }
  30. static void setBoolSetting(std::string group, std::string field, bool value)
  31. {
  32. Settings entry = settings.write[group][field];
  33. entry->Bool() = value;
  34. }
  35. static std::string scalingToEntryString( int scaling)
  36. {
  37. return std::to_string(scaling) + '%';
  38. }
  39. static std::string scalingToLabelString( int scaling)
  40. {
  41. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.scalingButton.hover");
  42. boost::replace_all(string, "%p", std::to_string(scaling));
  43. return string;
  44. }
  45. static std::string resolutionToEntryString( int w, int h)
  46. {
  47. std::string string = "%wx%h";
  48. boost::replace_all(string, "%w", std::to_string(w));
  49. boost::replace_all(string, "%h", std::to_string(h));
  50. return string;
  51. }
  52. static std::string resolutionToLabelString( int w, int h)
  53. {
  54. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.resolutionButton.hover");
  55. boost::replace_all(string, "%w", std::to_string(w));
  56. boost::replace_all(string, "%h", std::to_string(h));
  57. return string;
  58. }
  59. GeneralOptionsTab::GeneralOptionsTab()
  60. : InterfaceObjectConfigurable(),
  61. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  62. {
  63. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  64. type |= REDRAW_PARENT;
  65. const JsonNode config(ResourceID("config/widgets/settings/generalOptionsTab.json"));
  66. addCallback("spellbookAnimationChanged", [](bool value)
  67. {
  68. setBoolSetting("video", "spellbookAnimation", value);
  69. });
  70. addCallback("setMusic", [this](int value)
  71. {
  72. setIntSetting("general", "music", value);
  73. widget<CSlider>("musicSlider")->redraw();
  74. auto targetLabel = widget<CLabel>("musicValueLabel");
  75. if (targetLabel)
  76. targetLabel->setText(std::to_string(value) + "%");
  77. });
  78. addCallback("setVolume", [this](int value)
  79. {
  80. setIntSetting("general", "sound", value);
  81. widget<CSlider>("soundVolumeSlider")->redraw();
  82. auto targetLabel = widget<CLabel>("soundValueLabel");
  83. if (targetLabel)
  84. targetLabel->setText(std::to_string(value) + "%");
  85. });
  86. //settings that do not belong to base game:
  87. addCallback("fullscreenChanged", [this](bool value)
  88. {
  89. setFullscreenMode(value);
  90. });
  91. addCallback("setGameResolution", [this](int dummyValue)
  92. {
  93. selectGameResolution();
  94. });
  95. addCallback("setGameScaling", [this](int dummyValue)
  96. {
  97. selectGameScaling();
  98. });
  99. addCallback("framerateChanged", [](bool value)
  100. {
  101. setBoolSetting("video", "showfps", value);
  102. });
  103. //moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
  104. addCallback("availableCreaturesAsDwellingChanged", [=](int value)
  105. {
  106. setBoolSetting("gameTweaks", "availableCreaturesAsDwellingLabel", value > 0);
  107. });
  108. addCallback("compactTownCreatureInfoChanged", [](bool value)
  109. {
  110. setBoolSetting("gameTweaks", "compactTownCreatureInfo", value);
  111. });
  112. build(config);
  113. const auto & currentResolution = settings["video"]["resolution"];
  114. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  115. resolutionLabel->setText(resolutionToLabelString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  116. std::shared_ptr<CLabel> scalingLabel = widget<CLabel>("scalingLabel");
  117. scalingLabel->setText(scalingToLabelString(currentResolution["scaling"].Integer()));
  118. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  119. spellbookAnimationCheckbox->setSelected(settings["video"]["spellbookAnimation"].Bool());
  120. std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
  121. fullscreenCheckbox->setSelected(settings["video"]["fullscreen"].Bool());
  122. onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
  123. {
  124. widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
  125. });
  126. std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
  127. framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
  128. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  129. musicSlider->moveTo(CCS->musich->getVolume());
  130. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  131. volumeSlider->moveTo(CCS->soundh->getVolume());
  132. std::shared_ptr<CToggleGroup> creatureGrowthAsDwellingPicker = widget<CToggleGroup>("availableCreaturesAsDwellingPicker");
  133. creatureGrowthAsDwellingPicker->setSelected(settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
  134. std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
  135. compactTownCreatureInfo->setSelected(settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
  136. std::shared_ptr<CLabel> musicVolumeLabel = widget<CLabel>("musicValueLabel");
  137. musicVolumeLabel->setText(std::to_string(CCS->musich->getVolume()) + "%");
  138. std::shared_ptr<CLabel> soundVolumeLabel = widget<CLabel>("soundValueLabel");
  139. soundVolumeLabel->setText(std::to_string(CCS->soundh->getVolume()) + "%");
  140. #ifdef VCMI_MOBILE
  141. // On mobile platforms, VCMI always uses OS screen resolutions
  142. // Players can control UI size via "Interface Scaling" option instead
  143. std::shared_ptr<CButton> resolutionButton = widget<CButton>("resolutionButton");
  144. resolutionButton->disable();
  145. resolutionLabel->disable();
  146. fullscreenCheckbox->block(true);
  147. #endif
  148. }
  149. void GeneralOptionsTab::selectGameResolution()
  150. {
  151. supportedResolutions = GH.screenHandler().getSupportedResolutions();
  152. std::vector<std::string> items;
  153. size_t currentResolutionIndex = 0;
  154. size_t i = 0;
  155. for(const auto & it : supportedResolutions)
  156. {
  157. auto resolutionStr = resolutionToEntryString(it.x, it.y);
  158. if(widget<CLabel>("resolutionLabel")->getText() == resolutionToLabelString(it.x, it.y))
  159. currentResolutionIndex = i;
  160. items.push_back(std::move(resolutionStr));
  161. ++i;
  162. }
  163. GH.windows().createAndPushWindow<CObjectListWindow>(items, nullptr,
  164. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  165. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  166. [this](int index)
  167. {
  168. setGameResolution(index);
  169. },
  170. currentResolutionIndex);
  171. }
  172. void GeneralOptionsTab::setGameResolution(int index)
  173. {
  174. assert(index >= 0 && index < supportedResolutions.size());
  175. if ( index < 0 || index >= supportedResolutions.size() )
  176. return;
  177. Point resolution = supportedResolutions[index];
  178. Settings gameRes = settings.write["video"]["resolution"];
  179. gameRes["width"].Float() = resolution.x;
  180. gameRes["height"].Float() = resolution.y;
  181. widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(resolution.x, resolution.y));
  182. }
  183. void GeneralOptionsTab::setFullscreenMode(bool on)
  184. {
  185. setBoolSetting("video", "fullscreen", on);
  186. }
  187. void GeneralOptionsTab::selectGameScaling()
  188. {
  189. supportedScaling.clear();
  190. auto [minimalScaling, maximalScaling] = GH.screenHandler().getSupportedScalingRange();
  191. for (int i = 0; i <= maximalScaling; i += 10)
  192. {
  193. if (i >= minimalScaling)
  194. supportedScaling.push_back(i);
  195. }
  196. std::vector<std::string> items;
  197. size_t currentIndex = 0;
  198. size_t i = 0;
  199. for(const auto & it : supportedScaling)
  200. {
  201. auto resolutionStr = scalingToEntryString(it);
  202. if(widget<CLabel>("scalingLabel")->getText() == scalingToLabelString(it))
  203. currentIndex = i;
  204. items.push_back(std::move(resolutionStr));
  205. ++i;
  206. }
  207. GH.windows().createAndPushWindow<CObjectListWindow>(
  208. items,
  209. nullptr,
  210. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.hover"),
  211. CGI->generaltexth->translate("vcmi.systemOptions.scalingMenu.help"),
  212. [this](int index)
  213. {
  214. setGameScaling(index);
  215. },
  216. currentIndex
  217. );
  218. }
  219. void GeneralOptionsTab::setGameScaling(int index)
  220. {
  221. assert(index >= 0 && index < supportedScaling.size());
  222. if ( index < 0 || index >= supportedScaling.size() )
  223. return;
  224. int scaling = supportedScaling[index];
  225. Settings gameRes = settings.write["video"]["resolution"];
  226. gameRes["scaling"].Float() = scaling;
  227. widget<CLabel>("scalingLabel")->setText(scalingToLabelString(scaling));
  228. }