GeneralOptionsTab.cpp 7.5 KB

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