GeneralOptionsTab.cpp 7.8 KB

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