GeneralOptionsTab.cpp 8.6 KB

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