GeneralOptionsTab.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "renderSDL/SDL_Extensions.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 resolutionToEntryString( int w, int h)
  35. {
  36. std::string string = "%wx%h";
  37. boost::replace_all(string, "%w", std::to_string(w));
  38. boost::replace_all(string, "%h", std::to_string(h));
  39. return string;
  40. }
  41. static std::string resolutionToLabelString( int w, int h)
  42. {
  43. std::string string = CGI->generaltexth->translate("vcmi.systemOptions.resolutionButton.hover");
  44. boost::replace_all(string, "%w", std::to_string(w));
  45. boost::replace_all(string, "%h", std::to_string(h));
  46. return string;
  47. }
  48. GeneralOptionsTab::GeneralOptionsTab()
  49. : InterfaceObjectConfigurable(),
  50. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  51. {
  52. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  53. type |= REDRAW_PARENT;
  54. const JsonNode config(ResourceID("config/widgets/settings/generalOptionsTab.json"));
  55. addCallback("spellbookAnimationChanged", [](bool value)
  56. {
  57. setBoolSetting("video", "spellbookAnimation", value);
  58. });
  59. addCallback("setMusic", [this](int value)
  60. {
  61. setIntSetting("general", "music", value);
  62. widget<CSlider>("musicSlider")->redraw();
  63. auto targetLabel = widget<CLabel>("musicValueLabel");
  64. if (targetLabel)
  65. targetLabel->setText(std::to_string(value) + "%");
  66. });
  67. addCallback("setVolume", [this](int value)
  68. {
  69. setIntSetting("general", "sound", value);
  70. widget<CSlider>("soundVolumeSlider")->redraw();
  71. auto targetLabel = widget<CLabel>("soundValueLabel");
  72. if (targetLabel)
  73. targetLabel->setText(std::to_string(value) + "%");
  74. });
  75. //settings that do not belong to base game:
  76. addCallback("fullscreenChanged", [this](bool value)
  77. {
  78. setFullscreenMode(value);
  79. });
  80. addCallback("setGameResolution", [this](int dummyValue)
  81. {
  82. selectGameResolution();
  83. });
  84. addCallback("framerateChanged", [](bool value)
  85. {
  86. setBoolSetting("video", "showfps", value);
  87. });
  88. //moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
  89. addCallback("availableCreaturesAsDwellingChanged", [=](int value)
  90. {
  91. setBoolSetting("gameTweaks", "availableCreaturesAsDwellingLabel", value > 0);
  92. });
  93. addCallback("compactTownCreatureInfoChanged", [](bool value)
  94. {
  95. setBoolSetting("gameTweaks", "compactTownCreatureInfo", value);
  96. });
  97. build(config);
  98. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  99. const auto & currentResolution = settings["video"]["screenRes"];
  100. resolutionLabel->setText(resolutionToLabelString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  101. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  102. spellbookAnimationCheckbox->setSelected(settings["video"]["spellbookAnimation"].Bool());
  103. std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
  104. fullscreenCheckbox->setSelected(settings["video"]["fullscreen"].Bool());
  105. onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
  106. {
  107. widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
  108. });
  109. std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
  110. framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
  111. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  112. musicSlider->moveTo(CCS->musich->getVolume());
  113. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  114. volumeSlider->moveTo(CCS->soundh->getVolume());
  115. std::shared_ptr<CToggleGroup> creatureGrowthAsDwellingPicker = widget<CToggleGroup>("availableCreaturesAsDwellingPicker");
  116. creatureGrowthAsDwellingPicker->setSelected(settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool());
  117. std::shared_ptr<CToggleButton> compactTownCreatureInfo = widget<CToggleButton>("compactTownCreatureInfoCheckbox");
  118. compactTownCreatureInfo->setSelected(settings["gameTweaks"]["compactTownCreatureInfo"].Bool());
  119. }
  120. bool GeneralOptionsTab::isResolutionSupported(const Point & resolution)
  121. {
  122. return isResolutionSupported( resolution, settings["video"]["fullscreen"].Bool());
  123. }
  124. bool GeneralOptionsTab::isResolutionSupported(const Point & resolution, bool fullscreen)
  125. {
  126. if (!fullscreen)
  127. return true;
  128. auto supportedList = CSDL_Ext::getSupportedResolutions();
  129. return CSDL_Ext::isResolutionSupported(supportedList, resolution);
  130. }
  131. void GeneralOptionsTab::selectGameResolution()
  132. {
  133. fillSelectableResolutions();
  134. std::vector<std::string> items;
  135. size_t currentResolutionIndex = 0;
  136. size_t i = 0;
  137. for(const auto & it : selectableResolutions)
  138. {
  139. auto resolutionStr = resolutionToEntryString(it.x, it.y);
  140. if(widget<CLabel>("resolutionLabel")->getText() == resolutionToLabelString(it.x, it.y))
  141. currentResolutionIndex = i;
  142. items.push_back(std::move(resolutionStr));
  143. ++i;
  144. }
  145. GH.pushIntT<CObjectListWindow>(items, nullptr,
  146. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  147. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  148. [this](int index)
  149. {
  150. setGameResolution(index);
  151. },
  152. currentResolutionIndex);
  153. }
  154. void GeneralOptionsTab::setGameResolution(int index)
  155. {
  156. assert(index >= 0 && index < selectableResolutions.size());
  157. if ( index < 0 || index >= selectableResolutions.size() )
  158. return;
  159. Point resolution = selectableResolutions[index];
  160. Settings gameRes = settings.write["video"]["screenRes"];
  161. gameRes["width"].Float() = resolution.x;
  162. gameRes["height"].Float() = resolution.y;
  163. widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(resolution.x, resolution.y));
  164. }
  165. void GeneralOptionsTab::setFullscreenMode(bool on)
  166. {
  167. fillSelectableResolutions();
  168. const auto & screenRes = settings["video"]["screenRes"];
  169. const Point desiredResolution(screenRes["width"].Integer(), screenRes["height"].Integer());
  170. const Point currentResolution = GH.screenDimensions();
  171. if (!isResolutionSupported(currentResolution, on))
  172. {
  173. widget<CToggleButton>("fullscreenCheckbox")->setSelected(!on);
  174. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.systemOptions.fullscreenFailed"));
  175. return;
  176. }
  177. setBoolSetting("video", "fullscreen", on);
  178. if (!isResolutionSupported(desiredResolution, on))
  179. {
  180. // user changed his desired resolution and switched to fullscreen
  181. // however resolution he selected before is not available in fullscreen
  182. // so reset it back to currect resolution which is confirmed to be supported earlier
  183. Settings gameRes = settings.write["video"]["screenRes"];
  184. gameRes["width"].Float() = currentResolution.x;
  185. gameRes["height"].Float() = currentResolution.y;
  186. widget<CLabel>("resolutionLabel")->setText(resolutionToLabelString(currentResolution.x, currentResolution.y));
  187. }
  188. }
  189. void GeneralOptionsTab::fillSelectableResolutions()
  190. {
  191. selectableResolutions.clear();
  192. for(const auto & it : conf.guiOptions)
  193. {
  194. const Point dimensions(it.first.first, it.first.second);
  195. if(isResolutionSupported(dimensions))
  196. selectableResolutions.push_back(dimensions);
  197. }
  198. boost::range::sort(selectableResolutions, [](const auto & left, const auto & right)
  199. {
  200. return left.x * left.y < right.x * right.y;
  201. });
  202. }