GeneralOptionsTab.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <SDL_surface.h>
  13. #include <SDL_rect.h>
  14. #include "../../../lib/CGeneralTextHandler.h"
  15. #include "../../../lib/filesystem/ResourceID.h"
  16. #include "../../gui/CGuiHandler.h"
  17. #include "../../widgets/Buttons.h"
  18. #include "../../widgets/TextControls.h"
  19. #include "../../widgets/Images.h"
  20. #include "CGameInfo.h"
  21. #include "CMusicHandler.h"
  22. #include "CPlayerInterface.h"
  23. #include "windows/GUIClasses.h"
  24. #include "CServerHandler.h"
  25. #include "renderSDL/SDL_Extensions.h"
  26. #include "CMT.h"
  27. static void setIntSetting(std::string group, std::string field, int value)
  28. {
  29. Settings entry = settings.write[group][field];
  30. entry->Float() = value;
  31. }
  32. static void setBoolSetting(std::string group, std::string field, bool value)
  33. {
  34. Settings fullscreen = settings.write[group][field];
  35. fullscreen->Bool() = value;
  36. }
  37. static std::string resolutionToString(int w, int h)
  38. {
  39. return std::to_string(w) + 'x' + std::to_string(h);
  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", std::bind(&setBoolSetting, "video", "spellbookAnimation", _1));
  48. addCallback("fullscreenChanged", std::bind(&GeneralOptionsTab::setFullscreenMode, this, _1));
  49. addCallback("setGameResolution", std::bind(&GeneralOptionsTab::selectGameResolution, this));
  50. addCallback("setMusic", [this](int value) { setIntSetting("general", "music", value); widget<CSlider>("musicSlider")->redraw(); });
  51. addCallback("setVolume", [this](int value) { setIntSetting("general", "sound", value); widget<CSlider>("soundVolumeSlider")->redraw(); });
  52. build(config);
  53. std::shared_ptr<CLabel> resolutionLabel = widget<CLabel>("resolutionLabel");
  54. const auto & currentResolution = settings["video"]["screenRes"];
  55. resolutionLabel->setText(resolutionToString(currentResolution["width"].Integer(), currentResolution["height"].Integer()));
  56. std::shared_ptr<CToggleButton> spellbookAnimationCheckbox = widget<CToggleButton>("spellbookAnimationCheckbox");
  57. spellbookAnimationCheckbox->setSelected((bool)settings["video"]["spellbookAnimation"].Bool());
  58. std::shared_ptr<CToggleButton> fullscreenCheckbox = widget<CToggleButton>("fullscreenCheckbox");
  59. fullscreenCheckbox->setSelected((bool)settings["video"]["fullscreen"].Bool());
  60. onFullscreenChanged([&](const JsonNode &newState) //used when pressing F4 etc. to change fullscreen checkbox state
  61. {
  62. widget<CToggleButton>("fullscreenCheckbox")->setSelected(newState.Bool());
  63. });
  64. std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
  65. musicSlider->moveTo(CCS->musich->getVolume());
  66. std::shared_ptr<CSlider> volumeSlider = widget<CSlider>("soundVolumeSlider");
  67. volumeSlider->moveTo(CCS->soundh->getVolume());
  68. }
  69. bool GeneralOptionsTab::isResolutionSupported(const Point & resolution)
  70. {
  71. return isResolutionSupported( resolution, settings["video"]["fullscreen"].Bool());
  72. }
  73. bool GeneralOptionsTab::isResolutionSupported(const Point & resolution, bool fullscreen)
  74. {
  75. if (!fullscreen)
  76. return true;
  77. auto supportedList = CSDL_Ext::getSupportedResolutions();
  78. return CSDL_Ext::isResolutionSupported(supportedList, resolution);
  79. }
  80. void GeneralOptionsTab::selectGameResolution()
  81. {
  82. fillSelectableResolutions();
  83. std::vector<std::string> items;
  84. size_t currentResolutionIndex = 0;
  85. size_t i = 0;
  86. for(const auto & it : selectableResolutions)
  87. {
  88. auto resolutionStr = resolutionToString(it.x, it.y);
  89. if(widget<CLabel>("resolutionLabel")->getText() == resolutionStr)
  90. currentResolutionIndex = i;
  91. items.push_back(std::move(resolutionStr));
  92. ++i;
  93. }
  94. GH.pushIntT<CObjectListWindow>(items, nullptr,
  95. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.hover"),
  96. CGI->generaltexth->translate("vcmi.systemOptions.resolutionMenu.help"),
  97. std::bind(&GeneralOptionsTab::setGameResolution, this, _1),
  98. currentResolutionIndex);
  99. }
  100. void GeneralOptionsTab::setGameResolution(int index)
  101. {
  102. assert(index >= 0 && index < selectableResolutions.size());
  103. if ( index < 0 || index >= selectableResolutions.size() )
  104. return;
  105. Point resolution = selectableResolutions[index];
  106. Settings gameRes = settings.write["video"]["screenRes"];
  107. gameRes["width"].Float() = resolution.x;
  108. gameRes["height"].Float() = resolution.y;
  109. std::string resText;
  110. resText += std::to_string(resolution.x);
  111. resText += "x";
  112. resText += std::to_string(resolution.y);
  113. widget<CLabel>("resolutionLabel")->setText(resText);
  114. }
  115. void GeneralOptionsTab::setFullscreenMode(bool on)
  116. {
  117. fillSelectableResolutions();
  118. const auto & screenRes = settings["video"]["screenRes"];
  119. const Point desiredResolution(screenRes["width"].Integer(), screenRes["height"].Integer());
  120. const Point currentResolution(screen->w, screen->h);
  121. if (!isResolutionSupported(currentResolution, on))
  122. {
  123. widget<CToggleButton>("fullscreenCheckbox")->setSelected(!on);
  124. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.systemOptions.fullscreenFailed"));
  125. return;
  126. }
  127. setBoolSetting("video", "fullscreen", on);
  128. if (!isResolutionSupported(desiredResolution, on))
  129. {
  130. // user changed his desired resolution and switched to fullscreen
  131. // however resolution he selected before is not available in fullscreen
  132. // so reset it back to currect resolution which is confirmed to be supported earlier
  133. Settings gameRes = settings.write["video"]["screenRes"];
  134. gameRes["width"].Float() = currentResolution.x;
  135. gameRes["height"].Float() = currentResolution.y;
  136. widget<CLabel>("resolutionLabel")->setText(resolutionToString(currentResolution.x, currentResolution.y));
  137. }
  138. }
  139. void GeneralOptionsTab::fillSelectableResolutions()
  140. {
  141. selectableResolutions.clear();
  142. for(const auto & it : conf.guiOptions)
  143. {
  144. const Point dimensions(it.first.first, it.first.second);
  145. if(isResolutionSupported(dimensions))
  146. selectableResolutions.push_back(dimensions);
  147. }
  148. boost::range::sort(selectableResolutions, [](const auto & left, const auto & right)
  149. {
  150. return left.x * left.y < right.x * right.y;
  151. });
  152. }