1
0

GraphicsTab.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using Content.Shared.CCVar;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.Graphics;
  4. using Robust.Client.UserInterface;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Client.UserInterface.XAML;
  7. using Robust.Shared;
  8. using Robust.Shared.Configuration;
  9. namespace Content.Client.Options.UI.Tabs;
  10. [GenerateTypedNameReferences]
  11. public sealed partial class GraphicsTab : Control
  12. {
  13. [Dependency] private readonly IConfigurationManager _cfg = default!;
  14. public GraphicsTab()
  15. {
  16. IoCManager.InjectDependencies(this);
  17. RobustXamlLoader.Load(this);
  18. Control.AddOptionCheckBox(CVars.DisplayVSync, VSyncCheckBox);
  19. Control.AddOption(new OptionFullscreen(Control, _cfg, FullscreenCheckBox));
  20. Control.AddOption(new OptionLightingQuality(Control, _cfg, DropDownLightingQuality));
  21. Control.AddOptionDropDown(
  22. CVars.DisplayUIScale,
  23. DropDownUIScale,
  24. [
  25. new OptionDropDownCVar<float>.ValueOption(
  26. 0f,
  27. Loc.GetString("ui-options-scale-auto", ("scale", UserInterfaceManager.DefaultUIScale))),
  28. new OptionDropDownCVar<float>.ValueOption(0.75f, Loc.GetString("ui-options-scale-75")),
  29. new OptionDropDownCVar<float>.ValueOption(1.00f, Loc.GetString("ui-options-scale-100")),
  30. new OptionDropDownCVar<float>.ValueOption(1.25f, Loc.GetString("ui-options-scale-125")),
  31. new OptionDropDownCVar<float>.ValueOption(1.50f, Loc.GetString("ui-options-scale-150")),
  32. new OptionDropDownCVar<float>.ValueOption(1.75f, Loc.GetString("ui-options-scale-175")),
  33. new OptionDropDownCVar<float>.ValueOption(2.00f, Loc.GetString("ui-options-scale-200")),
  34. ]);
  35. var vpStretch = Control.AddOptionCheckBox(CCVars.ViewportStretch, ViewportStretchCheckBox);
  36. var vpVertFit = Control.AddOptionCheckBox(CCVars.ViewportVerticalFit, ViewportVerticalFitCheckBox);
  37. Control.AddOptionSlider(
  38. CCVars.ViewportFixedScaleFactor,
  39. ViewportScaleSlider,
  40. 1,
  41. 5,
  42. (_, value) => Loc.GetString("ui-options-vp-scale-value", ("scale", value)));
  43. vpStretch.ImmediateValueChanged += _ => UpdateViewportSettingsVisibility();
  44. vpVertFit.ImmediateValueChanged += _ => UpdateViewportSettingsVisibility();
  45. Control.AddOptionSlider(
  46. CCVars.ViewportWidth,
  47. ViewportWidthSlider,
  48. (int)ViewportWidthSlider.Slider.MinValue,
  49. (int)ViewportWidthSlider.Slider.MaxValue);
  50. Control.AddOption(new OptionIntegerScaling(Control, _cfg, IntegerScalingCheckBox));
  51. Control.AddOptionCheckBox(CCVars.ViewportScaleRender, ViewportLowResCheckBox, invert: true);
  52. Control.AddOptionCheckBox(CCVars.ParallaxLowQuality, ParallaxLowQualityCheckBox);
  53. Control.AddOptionCheckBox(CCVars.HudFpsCounterVisible, FpsCounterCheckBox);
  54. Control.Initialize();
  55. _cfg.OnValueChanged(CCVars.ViewportMinimumWidth, _ => UpdateViewportWidthRange());
  56. _cfg.OnValueChanged(CCVars.ViewportMaximumWidth, _ => UpdateViewportWidthRange());
  57. UpdateViewportWidthRange();
  58. UpdateViewportSettingsVisibility();
  59. }
  60. private void UpdateViewportSettingsVisibility()
  61. {
  62. ViewportScaleSlider.Visible = !ViewportStretchCheckBox.Pressed;
  63. IntegerScalingCheckBox.Visible = ViewportStretchCheckBox.Pressed;
  64. ViewportVerticalFitCheckBox.Visible = ViewportStretchCheckBox.Pressed;
  65. ViewportWidthSlider.Visible = !ViewportStretchCheckBox.Pressed || !ViewportVerticalFitCheckBox.Pressed;
  66. }
  67. private void UpdateViewportWidthRange()
  68. {
  69. var min = _cfg.GetCVar(CCVars.ViewportMinimumWidth);
  70. var max = _cfg.GetCVar(CCVars.ViewportMaximumWidth);
  71. ViewportWidthSlider.Slider.MinValue = min;
  72. ViewportWidthSlider.Slider.MaxValue = max;
  73. }
  74. private sealed class OptionLightingQuality : BaseOption
  75. {
  76. private readonly IConfigurationManager _cfg;
  77. private readonly OptionDropDown _dropDown;
  78. private const int QualityVeryLow = 0;
  79. private const int QualityLow = 1;
  80. private const int QualityMedium = 2;
  81. private const int QualityHigh = 3;
  82. private const int QualityDefault = QualityMedium;
  83. public OptionLightingQuality(OptionsTabControlRow controller, IConfigurationManager cfg, OptionDropDown dropDown) : base(controller)
  84. {
  85. _cfg = cfg;
  86. _dropDown = dropDown;
  87. var button = dropDown.Button;
  88. button.AddItem(Loc.GetString("ui-options-lighting-very-low"), QualityVeryLow);
  89. button.AddItem(Loc.GetString("ui-options-lighting-low"), QualityLow);
  90. button.AddItem(Loc.GetString("ui-options-lighting-medium"), QualityMedium);
  91. button.AddItem(Loc.GetString("ui-options-lighting-high"), QualityHigh);
  92. button.OnItemSelected += OnOptionSelected;
  93. }
  94. private void OnOptionSelected(OptionButton.ItemSelectedEventArgs obj)
  95. {
  96. _dropDown.Button.SelectId(obj.Id);
  97. ValueChanged();
  98. }
  99. public override void LoadValue()
  100. {
  101. _dropDown.Button.SelectId(GetConfigLightingQuality());
  102. }
  103. public override void SaveValue()
  104. {
  105. switch (_dropDown.Button.SelectedId)
  106. {
  107. case QualityVeryLow:
  108. _cfg.SetCVar(CVars.LightResolutionScale, 0.125f);
  109. _cfg.SetCVar(CVars.LightSoftShadows, false);
  110. _cfg.SetCVar(CVars.LightBlur, false);
  111. break;
  112. case QualityLow:
  113. _cfg.SetCVar(CVars.LightResolutionScale, 0.5f);
  114. _cfg.SetCVar(CVars.LightSoftShadows, false);
  115. _cfg.SetCVar(CVars.LightBlur, true);
  116. break;
  117. default: // = QualityMedium
  118. _cfg.SetCVar(CVars.LightResolutionScale, 0.5f);
  119. _cfg.SetCVar(CVars.LightSoftShadows, true);
  120. _cfg.SetCVar(CVars.LightBlur, true);
  121. break;
  122. case QualityHigh:
  123. _cfg.SetCVar(CVars.LightResolutionScale, 1);
  124. _cfg.SetCVar(CVars.LightSoftShadows, true);
  125. _cfg.SetCVar(CVars.LightBlur, true);
  126. break;
  127. }
  128. }
  129. public override void ResetToDefault()
  130. {
  131. _dropDown.Button.SelectId(QualityDefault);
  132. }
  133. public override bool IsModified()
  134. {
  135. return _dropDown.Button.SelectedId != GetConfigLightingQuality();
  136. }
  137. public override bool IsModifiedFromDefault()
  138. {
  139. return _dropDown.Button.SelectedId != QualityDefault;
  140. }
  141. private int GetConfigLightingQuality()
  142. {
  143. var val = _cfg.GetCVar(CVars.LightResolutionScale);
  144. var soft = _cfg.GetCVar(CVars.LightSoftShadows);
  145. if (val <= 0.125)
  146. return QualityVeryLow;
  147. if ((val <= 0.5) && !soft)
  148. return QualityLow;
  149. if (val <= 0.5)
  150. return QualityMedium;
  151. return QualityHigh;
  152. }
  153. }
  154. private sealed class OptionFullscreen : BaseOptionCVar<int>
  155. {
  156. private readonly CheckBox _checkBox;
  157. protected override int Value
  158. {
  159. get => _checkBox.Pressed ? (int) WindowMode.Fullscreen : (int) WindowMode.Windowed;
  160. set => _checkBox.Pressed = (value == (int) WindowMode.Fullscreen);
  161. }
  162. public OptionFullscreen(
  163. OptionsTabControlRow controller,
  164. IConfigurationManager cfg,
  165. CheckBox checkBox)
  166. : base(controller, cfg, CVars.DisplayWindowMode)
  167. {
  168. _checkBox = checkBox;
  169. _checkBox.OnToggled += _ =>
  170. {
  171. ValueChanged();
  172. };
  173. }
  174. }
  175. private sealed class OptionIntegerScaling : BaseOptionCVar<int>
  176. {
  177. private readonly CheckBox _checkBox;
  178. protected override int Value
  179. {
  180. get => _checkBox.Pressed ? CCVars.ViewportSnapToleranceMargin.DefaultValue : 0;
  181. set => _checkBox.Pressed = (value != 0);
  182. }
  183. public OptionIntegerScaling(
  184. OptionsTabControlRow controller,
  185. IConfigurationManager cfg,
  186. CheckBox checkBox)
  187. : base(controller, cfg, CCVars.ViewportSnapToleranceMargin)
  188. {
  189. _checkBox = checkBox;
  190. _checkBox.OnToggled += _ =>
  191. {
  192. ValueChanged();
  193. };
  194. }
  195. }
  196. }