using System.Collections.Generic; using SRDebugger.Internal; using SRDebugger.UI.Controls; using SRDebugger.UI.Other; using SRF; using SRF.Service; using UnityEngine; namespace SRDebugger.Services.Implementation { [Service(typeof(IPinnedUIService))] public class PinnedUIServiceImpl : SRServiceBase, IPinnedUIService { private readonly List _controlList = new List(); private readonly Dictionary _pinnedObjects = new Dictionary(); private bool _queueRefresh; private PinnedUIRoot _uiRoot; public DockConsoleController DockConsoleController { get { if (_uiRoot == null) { Load(); } return _uiRoot.DockConsoleController; } } public bool IsProfilerPinned { get { if (_uiRoot == null) { return false; } return _uiRoot.Profiler.activeSelf; } set { if (_uiRoot == null) { Load(); } _uiRoot.Profiler.SetActive(value); } } public void Pin(OptionDefinition obj, int order = -1) { if (_uiRoot == null) { Load(); } OptionsControlBase optionsControlBase = OptionControlFactory.CreateControl(obj); optionsControlBase.CachedTransform.SetParent(_uiRoot.Container, false); if (order >= 0) { optionsControlBase.CachedTransform.SetSiblingIndex(order); } _pinnedObjects.Add(obj, optionsControlBase); _controlList.Add(optionsControlBase); } public void Unpin(OptionDefinition obj) { if (!_pinnedObjects.ContainsKey(obj)) { Debug.LogWarning("[SRDebugger.PinnedUI] Attempted to unpin option which isn't pinned."); return; } OptionsControlBase optionsControlBase = _pinnedObjects[obj]; _pinnedObjects.Remove(obj); _controlList.Remove(optionsControlBase); Object.Destroy(optionsControlBase.CachedGameObject); } public bool HasPinned(OptionDefinition option) { return _pinnedObjects.ContainsKey(option); } protected override void Awake() { base.Awake(); base.CachedTransform.SetParent(Hierarchy.Get("SRDebugger")); } private void Load() { PinnedUIRoot pinnedUIRoot = Resources.Load("SRDebugger/UI/Prefabs/PinnedUI"); if (pinnedUIRoot == null) { Debug.LogError("[SRDebugger.PinnedUI] Error loading ui prefab"); return; } PinnedUIRoot pinnedUIRoot2 = SRInstantiate.Instantiate(pinnedUIRoot); pinnedUIRoot2.CachedTransform.SetParent(base.CachedTransform, false); _uiRoot = pinnedUIRoot2; UpdateAnchors(); SRDebug.Instance.PanelVisibilityChanged += OnDebugPanelVisibilityChanged; SROptions.Current.PropertyChanged += OptionsOnPropertyChanged; } private void UpdateAnchors() { switch (Settings.Instance.ProfilerAlignment) { case PinAlignment.TopLeft: case PinAlignment.BottomLeft: _uiRoot.Profiler.transform.SetSiblingIndex(0); break; case PinAlignment.TopRight: case PinAlignment.BottomRight: _uiRoot.Profiler.transform.SetSiblingIndex(1); break; } switch (Settings.Instance.ProfilerAlignment) { case PinAlignment.TopLeft: case PinAlignment.TopRight: _uiRoot.ProfilerVerticalLayoutGroup.childAlignment = TextAnchor.UpperCenter; break; case PinAlignment.BottomLeft: case PinAlignment.BottomRight: _uiRoot.ProfilerVerticalLayoutGroup.childAlignment = TextAnchor.LowerCenter; break; } _uiRoot.ProfilerHandleManager.SetAlignment(Settings.Instance.ProfilerAlignment); switch (Settings.Instance.OptionsAlignment) { case PinAlignment.BottomLeft: _uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.LowerLeft; break; case PinAlignment.TopLeft: _uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.UpperLeft; break; case PinAlignment.BottomRight: _uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.LowerRight; break; case PinAlignment.TopRight: _uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.UpperRight; break; } } protected override void Update() { base.Update(); if (_queueRefresh) { _queueRefresh = false; Refresh(); } } private void OptionsOnPropertyChanged(object sender, string propertyName) { _queueRefresh = true; } private void OnDebugPanelVisibilityChanged(bool isVisible) { if (!isVisible) { _queueRefresh = true; } } private void Refresh() { for (int i = 0; i < _controlList.Count; i++) { _controlList[i].Refresh(); } } } }