com.alicizax.unity.framework/Runtime/Debugger/DebuggerComponent.SettingsWindow.cs

187 lines
9.4 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.UIElements;
2025-09-05 19:46:30 +08:00
namespace AlicizaX.Debugger.Runtime
{
public sealed partial class DebuggerComponent
{
private sealed class SettingsWindow : ScrollableDebuggerWindowBase
{
private DebuggerComponent _mDebuggerComponent;
private float m_LastIconX;
private float m_LastIconY;
private float m_LastWindowX;
private float m_LastWindowY;
private float m_LastWindowWidth;
private float m_LastWindowHeight;
private float m_LastWindowScale;
private bool m_LastEnableFloatingToggleSnap;
2025-09-05 19:46:30 +08:00
public override void Initialize(params object[] args)
{
_mDebuggerComponent = DebuggerComponent.Instance;
if (_mDebuggerComponent == null)
2025-09-05 19:46:30 +08:00
{
Log.Error("Debugger component is invalid.");
return;
}
m_LastIconX = Utility.PlayerPrefsX.GetFloat("Debugger.Icon.X", DefaultIconRect.x);
m_LastIconY = Utility.PlayerPrefsX.GetFloat("Debugger.Icon.Y", DefaultIconRect.y);
m_LastWindowX = Utility.PlayerPrefsX.GetFloat("Debugger.Window.X", DefaultWindowRect.x);
m_LastWindowY = Utility.PlayerPrefsX.GetFloat("Debugger.Window.Y", DefaultWindowRect.y);
m_LastWindowWidth = Utility.PlayerPrefsX.GetFloat("Debugger.Window.Width", DefaultWindowRect.width);
m_LastWindowHeight = Utility.PlayerPrefsX.GetFloat("Debugger.Window.Height", DefaultWindowRect.height);
bool inspectorSnapValue = _mDebuggerComponent.EnableFloatingToggleSnap;
if (Utility.PlayerPrefsX.HasSetting("Debugger.Icon.Snap"))
{
inspectorSnapValue = Utility.PlayerPrefsX.GetBool("Debugger.Icon.Snap", inspectorSnapValue);
_mDebuggerComponent.EnableFloatingToggleSnap = inspectorSnapValue;
}
m_LastEnableFloatingToggleSnap = inspectorSnapValue;
_mDebuggerComponent.WindowScale = m_LastWindowScale = Utility.PlayerPrefsX.GetFloat("Debugger.Window.Scale", DefaultWindowScale);
_mDebuggerComponent.IconRect = new Rect(m_LastIconX, m_LastIconY, DefaultIconRect.width, DefaultIconRect.height);
_mDebuggerComponent.WindowRect = new Rect(m_LastWindowX, m_LastWindowY, m_LastWindowWidth, m_LastWindowHeight);
2025-09-05 19:46:30 +08:00
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
if (_mDebuggerComponent == null)
{
return;
}
if (!Mathf.Approximately(m_LastIconX, _mDebuggerComponent.IconRect.x))
2025-09-05 19:46:30 +08:00
{
m_LastIconX = _mDebuggerComponent.IconRect.x;
Utility.PlayerPrefsX.SetFloat("Debugger.Icon.X", _mDebuggerComponent.IconRect.x);
2025-09-05 19:46:30 +08:00
}
if (!Mathf.Approximately(m_LastIconY, _mDebuggerComponent.IconRect.y))
2025-09-05 19:46:30 +08:00
{
m_LastIconY = _mDebuggerComponent.IconRect.y;
Utility.PlayerPrefsX.SetFloat("Debugger.Icon.Y", _mDebuggerComponent.IconRect.y);
2025-09-05 19:46:30 +08:00
}
if (!Mathf.Approximately(m_LastWindowX, _mDebuggerComponent.WindowRect.x))
2025-09-05 19:46:30 +08:00
{
m_LastWindowX = _mDebuggerComponent.WindowRect.x;
Utility.PlayerPrefsX.SetFloat("Debugger.Window.X", _mDebuggerComponent.WindowRect.x);
2025-09-05 19:46:30 +08:00
}
if (!Mathf.Approximately(m_LastWindowY, _mDebuggerComponent.WindowRect.y))
2025-09-05 19:46:30 +08:00
{
m_LastWindowY = _mDebuggerComponent.WindowRect.y;
Utility.PlayerPrefsX.SetFloat("Debugger.Window.Y", _mDebuggerComponent.WindowRect.y);
2025-09-05 19:46:30 +08:00
}
if (!Mathf.Approximately(m_LastWindowWidth, _mDebuggerComponent.WindowRect.width))
2025-09-05 19:46:30 +08:00
{
m_LastWindowWidth = _mDebuggerComponent.WindowRect.width;
Utility.PlayerPrefsX.SetFloat("Debugger.Window.Width", _mDebuggerComponent.WindowRect.width);
2025-09-05 19:46:30 +08:00
}
if (!Mathf.Approximately(m_LastWindowHeight, _mDebuggerComponent.WindowRect.height))
2025-09-05 19:46:30 +08:00
{
m_LastWindowHeight = _mDebuggerComponent.WindowRect.height;
Utility.PlayerPrefsX.SetFloat("Debugger.Window.Height", _mDebuggerComponent.WindowRect.height);
2025-09-05 19:46:30 +08:00
}
if (!Mathf.Approximately(m_LastWindowScale, _mDebuggerComponent.WindowScale))
2025-09-05 19:46:30 +08:00
{
m_LastWindowScale = _mDebuggerComponent.WindowScale;
Utility.PlayerPrefsX.SetFloat("Debugger.Window.Scale", _mDebuggerComponent.WindowScale);
2025-09-05 19:46:30 +08:00
}
if (m_LastEnableFloatingToggleSnap != _mDebuggerComponent.EnableFloatingToggleSnap)
{
m_LastEnableFloatingToggleSnap = _mDebuggerComponent.EnableFloatingToggleSnap;
Utility.PlayerPrefsX.SetBool("Debugger.Icon.Snap", _mDebuggerComponent.EnableFloatingToggleSnap);
}
2025-09-05 19:46:30 +08:00
}
protected override void BuildWindow(VisualElement root)
2025-09-05 19:46:30 +08:00
{
if (_mDebuggerComponent == null)
2025-09-05 19:46:30 +08:00
{
return;
}
2025-09-05 19:46:30 +08:00
VisualElement layoutSection = CreateSection("Window Settings", out VisualElement layoutCard);
layoutCard.Add(CreateRow("Position", "Drag window header or floating entry to move."));
layoutCard.Add(CreateToggle("Enable floating entry edge snap", _mDebuggerComponent.EnableFloatingToggleSnap, value =>
{
_mDebuggerComponent.EnableFloatingToggleSnap = value;
}));
layoutCard.Add(CreateRangeControl("Width", _mDebuggerComponent.WindowRect.width, MinWindowWidth, Screen.width - 32f, value =>
{
_mDebuggerComponent.WindowRect = new Rect(_mDebuggerComponent.WindowRect.x, _mDebuggerComponent.WindowRect.y, value, _mDebuggerComponent.WindowRect.height);
}));
layoutCard.Add(CreateRangeControl("Height", _mDebuggerComponent.WindowRect.height, MinWindowHeight, Screen.height - 32f, value =>
{
_mDebuggerComponent.WindowRect = new Rect(_mDebuggerComponent.WindowRect.x, _mDebuggerComponent.WindowRect.y, _mDebuggerComponent.WindowRect.width, value);
}));
layoutCard.Add(CreateRangeControl("Scale", _mDebuggerComponent.WindowScale, MinWindowScale, MaxWindowScale, value =>
{
_mDebuggerComponent.WindowScale = value;
}, 0.01f));
root.Add(layoutSection);
VisualElement presets = CreateSection("Scale Presets", out VisualElement presetCard);
VisualElement row = CreateToolbarRow();
AddScaleButton(row, "0.5x", 0.5f);
AddScaleButton(row, "1.0x", 1f);
AddScaleButton(row, "1.5x", 1.5f);
AddScaleButton(row, "2.0x", 2f);
AddScaleButton(row, "2.5x", 2.5f);
AddScaleButton(row, "3.0x", 3f);
AddScaleButton(row, "3.5x", 3.5f);
AddScaleButton(row, "4.0x", 4f);
presetCard.Add(row);
root.Add(presets);
VisualElement actions = CreateSection("Actions", out VisualElement actionCard);
actionCard.Add(CreateActionButton("Reset Layout", _mDebuggerComponent.ResetLayout, DebuggerTheme.Danger));
root.Add(actions);
}
2025-09-05 19:46:30 +08:00
private VisualElement CreateRangeControl(string title, float value, float min, float max, System.Action<float> onChanged, float step = 1f)
{
VisualElement row = new VisualElement();
row.style.flexDirection = FlexDirection.Column;
row.style.marginBottom = 8f;
Label titleLabel = new Label(Utility.Text.Format("{0}: {1:F2}", title, value));
titleLabel.style.color = DebuggerTheme.PrimaryText;
titleLabel.style.marginBottom = 4f;
row.Add(titleLabel);
VisualElement controls = CreateToolbarRow();
Button decreaseButton = CreateActionButton("-", () => onChanged(Mathf.Clamp(value - step, min, max)), DebuggerTheme.ButtonSurface);
decreaseButton.style.marginRight = 8f;
controls.Add(decreaseButton);
Slider slider = CreateSlider(min, max, value, onChanged);
slider.style.marginRight = 8f;
controls.Add(slider);
controls.Add(CreateActionButton("+", () => onChanged(Mathf.Clamp(value + step, min, max)), DebuggerTheme.ButtonSurface));
row.Add(controls);
return row;
}
2025-09-05 19:46:30 +08:00
private void AddScaleButton(VisualElement row, string title, float scale)
{
Button button = CreateActionButton(title, () =>
{
if (_mDebuggerComponent != null)
2025-09-05 19:46:30 +08:00
{
_mDebuggerComponent.WindowScale = scale;
2025-09-05 19:46:30 +08:00
}
}, DebuggerTheme.ButtonSurface);
button.style.marginRight = 8f;
row.Add(button);
2025-09-05 19:46:30 +08:00
}
}
}
}