(Feat) Add Remmaping
This commit is contained in:
@@ -1,90 +1,115 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Ashwild.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// Binds the display widgets (resolution, fullscreen mode, monitor, brightness) to the
|
||||
/// <see cref="SettingsManager"/>. The panel builds the dropdown option lists from the current
|
||||
/// hardware and converts the fullscreen dropdown index to and from <see cref="FullScreenMode"/>;
|
||||
/// the brightness slider is configured from the authored <see cref="SettingsDefinition"/>. The
|
||||
/// widgets themselves stay pure UI.
|
||||
/// </summary>
|
||||
public class DisplaySettingsSubPanel : SettingsSubPanel
|
||||
{
|
||||
[Header("Controls")]
|
||||
[SerializeField] private TMP_Dropdown resolutionDropdown;
|
||||
[SerializeField] private TMP_Dropdown fullscreenDropdown;
|
||||
[SerializeField] private TMP_Dropdown monitorDropdown;
|
||||
[SerializeField] private Slider brightnessSlider;
|
||||
[SerializeField] private TMP_Text brightnessLabel;
|
||||
#region Serialized Fields
|
||||
|
||||
private bool wiring;
|
||||
[Header("Widgets")]
|
||||
[SerializeField] private SettingDropdownWidget resolutionDropdown;
|
||||
[SerializeField] private SettingDropdownWidget fullscreenDropdown;
|
||||
[SerializeField] private SettingDropdownWidget monitorDropdown;
|
||||
[SerializeField] private SettingSliderWidget brightnessSlider;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
|
||||
/// <summary>
|
||||
/// Initializes each display widget with its apply action (brightness range from the definition).
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
resolutionDropdown.onValueChanged.AddListener(OnResolutionChanged);
|
||||
fullscreenDropdown.onValueChanged.AddListener(OnFullscreenChanged);
|
||||
monitorDropdown.onValueChanged.AddListener(OnMonitorChanged);
|
||||
brightnessSlider.onValueChanged.AddListener(OnBrightnessChanged);
|
||||
SettingsManager s = SettingsManager.Instance;
|
||||
if (s == null || s.Definition == null)
|
||||
{
|
||||
Debug.LogError($"[DisplaySettingsSubPanel] SettingsManager/Definition unavailable on '{name}'.", this);
|
||||
return;
|
||||
}
|
||||
SettingsDefinition def = s.Definition;
|
||||
|
||||
resolutionDropdown.Init(v => s.SetResolution(v));
|
||||
fullscreenDropdown.Init(v => s.SetFullscreenMode(IndexToMode(v)));
|
||||
monitorDropdown.Init(v => s.SetMonitor(v));
|
||||
brightnessSlider.Init(def.Brightness.Min, def.Brightness.Max, def.Brightness.LabelScale, def.Brightness.LabelFormat, v => s.SetBrightness(v));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
/// <summary>
|
||||
/// Rebuilds the dropdown options from current hardware and pushes the manager's values.
|
||||
/// </summary>
|
||||
public override void Refresh()
|
||||
{
|
||||
SettingsManager s = SettingsManager.Instance;
|
||||
if (s == null) return;
|
||||
|
||||
wiring = true;
|
||||
resolutionDropdown.SetOptions(BuildResolutionOptions());
|
||||
resolutionDropdown.SetValue(s.ResolutionIndex);
|
||||
|
||||
PopulateResolutionDropdown();
|
||||
resolutionDropdown.value = Mathf.Clamp(s.ResolutionIndex, 0, resolutionDropdown.options.Count - 1);
|
||||
resolutionDropdown.RefreshShownValue();
|
||||
fullscreenDropdown.SetOptions(FullscreenOptions);
|
||||
fullscreenDropdown.SetValue(ModeToIndex(s.FullscreenMode));
|
||||
|
||||
PopulateFullscreenDropdown();
|
||||
fullscreenDropdown.value = FullscreenModeToIndex(s.FullscreenMode);
|
||||
fullscreenDropdown.RefreshShownValue();
|
||||
monitorDropdown.SetOptions(BuildMonitorOptions());
|
||||
monitorDropdown.SetValue(s.MonitorIndex);
|
||||
|
||||
PopulateMonitorDropdown();
|
||||
monitorDropdown.value = Mathf.Clamp(s.MonitorIndex, 0, monitorDropdown.options.Count - 1);
|
||||
monitorDropdown.RefreshShownValue();
|
||||
|
||||
brightnessSlider.SetValueWithoutNotify(s.Brightness);
|
||||
if (brightnessLabel != null) brightnessLabel.text = $"{Mathf.RoundToInt(s.Brightness * 100f)}%";
|
||||
|
||||
wiring = false;
|
||||
brightnessSlider.SetValue(s.Brightness);
|
||||
}
|
||||
|
||||
private void PopulateResolutionDropdown()
|
||||
#endregion
|
||||
|
||||
#region Internal Helpers
|
||||
|
||||
private static readonly string[] FullscreenOptions =
|
||||
{
|
||||
"Exclusive Fullscreen",
|
||||
"Borderless Window",
|
||||
"Maximized Window",
|
||||
"Windowed"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Builds the human-readable resolution list from the monitor's supported modes.
|
||||
/// </summary>
|
||||
private static List<string> BuildResolutionOptions()
|
||||
{
|
||||
resolutionDropdown.ClearOptions();
|
||||
List<string> options = new List<string>();
|
||||
foreach (Resolution r in Screen.resolutions)
|
||||
options.Add($"{r.width}×{r.height} @{Mathf.RoundToInt((float)r.refreshRateRatio.value)}Hz");
|
||||
resolutionDropdown.AddOptions(options);
|
||||
return options;
|
||||
}
|
||||
|
||||
private void PopulateFullscreenDropdown()
|
||||
/// <summary>
|
||||
/// Builds the monitor list from the current display layout.
|
||||
/// </summary>
|
||||
private static List<string> BuildMonitorOptions()
|
||||
{
|
||||
fullscreenDropdown.ClearOptions();
|
||||
fullscreenDropdown.AddOptions(new List<string>
|
||||
{
|
||||
"Exclusive Fullscreen",
|
||||
"Borderless Window",
|
||||
"Maximized Window",
|
||||
"Windowed"
|
||||
});
|
||||
}
|
||||
|
||||
private void PopulateMonitorDropdown()
|
||||
{
|
||||
monitorDropdown.ClearOptions();
|
||||
List<DisplayInfo> displays = new List<DisplayInfo>();
|
||||
Screen.GetDisplayLayout(displays);
|
||||
List<string> options = new List<string>();
|
||||
for (int i = 0; i < displays.Count; i++)
|
||||
{
|
||||
string name = string.IsNullOrEmpty(displays[i].name) ? $"Monitor {i + 1}" : displays[i].name;
|
||||
options.Add($"{name} ({displays[i].width}×{displays[i].height})");
|
||||
string label = string.IsNullOrEmpty(displays[i].name) ? $"Monitor {i + 1}" : displays[i].name;
|
||||
options.Add($"{label} ({displays[i].width}×{displays[i].height})");
|
||||
}
|
||||
monitorDropdown.AddOptions(options);
|
||||
return options;
|
||||
}
|
||||
|
||||
private static int FullscreenModeToIndex(FullScreenMode mode) => mode switch
|
||||
/// <summary>
|
||||
/// Maps a fullscreen mode to its dropdown index.
|
||||
/// </summary>
|
||||
private static int ModeToIndex(FullScreenMode mode) => mode switch
|
||||
{
|
||||
FullScreenMode.ExclusiveFullScreen => 0,
|
||||
FullScreenMode.FullScreenWindow => 1,
|
||||
@@ -93,7 +118,10 @@ namespace Ashwild.Settings
|
||||
_ => 1
|
||||
};
|
||||
|
||||
private static FullScreenMode IndexToFullscreenMode(int index) => index switch
|
||||
/// <summary>
|
||||
/// Maps a dropdown index back to its fullscreen mode.
|
||||
/// </summary>
|
||||
private static FullScreenMode IndexToMode(int index) => index switch
|
||||
{
|
||||
0 => FullScreenMode.ExclusiveFullScreen,
|
||||
1 => FullScreenMode.FullScreenWindow,
|
||||
@@ -102,13 +130,6 @@ namespace Ashwild.Settings
|
||||
_ => FullScreenMode.FullScreenWindow
|
||||
};
|
||||
|
||||
private void OnResolutionChanged(int v) { if (!wiring) SettingsManager.Instance.SetResolution(v); }
|
||||
private void OnFullscreenChanged(int v) { if (!wiring) SettingsManager.Instance.SetFullscreenMode(IndexToFullscreenMode(v)); }
|
||||
private void OnMonitorChanged(int v) { if (!wiring) SettingsManager.Instance.SetMonitor(v); }
|
||||
private void OnBrightnessChanged(float v)
|
||||
{
|
||||
if (brightnessLabel != null) brightnessLabel.text = $"{Mathf.RoundToInt(v * 100f)}%";
|
||||
if (!wiring) SettingsManager.Instance.SetBrightness(v);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user