PinnedUIServiceImpl.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using SRDebugger.Internal;
  7. using SRDebugger.UI.Controls;
  8. using SRDebugger.UI.Other;
  9. using SRF;
  10. using SRF.Service;
  11. using UnityEngine;
  12. namespace SRDebugger.Services.Implementation
  13. {
  14. [Service(typeof(IPinnedUIService))]
  15. public class PinnedUIServiceImpl : SRServiceBase<IPinnedUIService>, IPinnedUIService
  16. {
  17. public DockConsoleController DockConsoleController
  18. {
  19. get
  20. {
  21. if (this._uiRoot == null)
  22. {
  23. this.Load();
  24. }
  25. return this._uiRoot.DockConsoleController;
  26. }
  27. }
  28. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  29. public event Action<OptionDefinition, bool> OptionPinStateChanged;
  30. public bool IsProfilerPinned
  31. {
  32. get
  33. {
  34. return !(this._uiRoot == null) && this._uiRoot.Profiler.activeSelf;
  35. }
  36. set
  37. {
  38. if (this._uiRoot == null)
  39. {
  40. this.Load();
  41. }
  42. this._uiRoot.Profiler.SetActive(value);
  43. }
  44. }
  45. public void Pin(OptionDefinition obj, int order = -1)
  46. {
  47. if (this._uiRoot == null)
  48. {
  49. this.Load();
  50. }
  51. if (this._pinnedObjects.ContainsKey(obj))
  52. {
  53. return;
  54. }
  55. OptionsControlBase optionsControlBase = OptionControlFactory.CreateControl(obj, null);
  56. optionsControlBase.CachedTransform.SetParent(this._uiRoot.Container, false);
  57. if (order >= 0)
  58. {
  59. optionsControlBase.CachedTransform.SetSiblingIndex(order);
  60. }
  61. this._pinnedObjects.Add(obj, optionsControlBase);
  62. this._controlList.Add(optionsControlBase);
  63. this.OnPinnedStateChanged(obj, true);
  64. }
  65. public void Unpin(OptionDefinition obj)
  66. {
  67. if (!this._pinnedObjects.ContainsKey(obj))
  68. {
  69. return;
  70. }
  71. OptionsControlBase optionsControlBase = this._pinnedObjects[obj];
  72. this._pinnedObjects.Remove(obj);
  73. this._controlList.Remove(optionsControlBase);
  74. UnityEngine.Object.Destroy(optionsControlBase.CachedGameObject);
  75. this.OnPinnedStateChanged(obj, false);
  76. }
  77. private void OnPinnedStateChanged(OptionDefinition option, bool isPinned)
  78. {
  79. if (this.OptionPinStateChanged != null)
  80. {
  81. this.OptionPinStateChanged(option, isPinned);
  82. }
  83. }
  84. public void UnpinAll()
  85. {
  86. foreach (KeyValuePair<OptionDefinition, OptionsControlBase> keyValuePair in this._pinnedObjects)
  87. {
  88. UnityEngine.Object.Destroy(keyValuePair.Value.CachedGameObject);
  89. }
  90. this._pinnedObjects.Clear();
  91. this._controlList.Clear();
  92. }
  93. public bool HasPinned(OptionDefinition option)
  94. {
  95. return this._pinnedObjects.ContainsKey(option);
  96. }
  97. protected override void Awake()
  98. {
  99. base.Awake();
  100. base.CachedTransform.SetParent(Hierarchy.Get("SRDebugger"));
  101. }
  102. private void Load()
  103. {
  104. PinnedUIRoot pinnedUIRoot = Resources.Load<PinnedUIRoot>("SRDebugger/UI/Prefabs/PinnedUI");
  105. if (pinnedUIRoot == null)
  106. {
  107. UnityEngine.Debug.LogError("[SRDebugger.PinnedUI] Error loading ui prefab");
  108. return;
  109. }
  110. PinnedUIRoot pinnedUIRoot2 = SRInstantiate.Instantiate<PinnedUIRoot>(pinnedUIRoot);
  111. pinnedUIRoot2.CachedTransform.SetParent(base.CachedTransform, false);
  112. this._uiRoot = pinnedUIRoot2;
  113. this.UpdateAnchors();
  114. SRDebug.Instance.PanelVisibilityChanged += this.OnDebugPanelVisibilityChanged;
  115. Service.Options.OptionsUpdated += this.OnOptionsUpdated;
  116. Service.Options.OptionsValueUpdated += this.OptionsOnPropertyChanged;
  117. }
  118. private void UpdateAnchors()
  119. {
  120. switch (Settings.Instance.ProfilerAlignment)
  121. {
  122. case PinAlignment.TopLeft:
  123. case PinAlignment.BottomLeft:
  124. case PinAlignment.CenterLeft:
  125. this._uiRoot.Profiler.transform.SetSiblingIndex(0);
  126. break;
  127. case PinAlignment.TopRight:
  128. case PinAlignment.BottomRight:
  129. case PinAlignment.CenterRight:
  130. this._uiRoot.Profiler.transform.SetSiblingIndex(1);
  131. break;
  132. }
  133. switch (Settings.Instance.ProfilerAlignment)
  134. {
  135. case PinAlignment.TopLeft:
  136. case PinAlignment.TopRight:
  137. this._uiRoot.ProfilerVerticalLayoutGroup.childAlignment = TextAnchor.UpperCenter;
  138. break;
  139. case PinAlignment.BottomLeft:
  140. case PinAlignment.BottomRight:
  141. this._uiRoot.ProfilerVerticalLayoutGroup.childAlignment = TextAnchor.LowerCenter;
  142. break;
  143. case PinAlignment.CenterLeft:
  144. case PinAlignment.CenterRight:
  145. this._uiRoot.ProfilerVerticalLayoutGroup.childAlignment = TextAnchor.MiddleCenter;
  146. break;
  147. }
  148. this._uiRoot.ProfilerHandleManager.SetAlignment(Settings.Instance.ProfilerAlignment);
  149. switch (Settings.Instance.OptionsAlignment)
  150. {
  151. case PinAlignment.TopLeft:
  152. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.UpperLeft;
  153. break;
  154. case PinAlignment.TopRight:
  155. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.UpperRight;
  156. break;
  157. case PinAlignment.BottomLeft:
  158. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.LowerLeft;
  159. break;
  160. case PinAlignment.BottomRight:
  161. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.LowerRight;
  162. break;
  163. case PinAlignment.CenterLeft:
  164. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.MiddleLeft;
  165. break;
  166. case PinAlignment.CenterRight:
  167. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.MiddleRight;
  168. break;
  169. case PinAlignment.TopCenter:
  170. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.UpperCenter;
  171. break;
  172. case PinAlignment.BottomCenter:
  173. this._uiRoot.OptionsLayoutGroup.childAlignment = TextAnchor.LowerCenter;
  174. break;
  175. }
  176. }
  177. protected override void Update()
  178. {
  179. base.Update();
  180. if (this._queueRefresh)
  181. {
  182. this._queueRefresh = false;
  183. this.Refresh();
  184. }
  185. }
  186. private void OnOptionsUpdated(object sender, EventArgs eventArgs)
  187. {
  188. List<OptionDefinition> list = this._pinnedObjects.Keys.ToList<OptionDefinition>();
  189. foreach (OptionDefinition optionDefinition in list)
  190. {
  191. if (!Service.Options.Options.Contains(optionDefinition))
  192. {
  193. this.Unpin(optionDefinition);
  194. }
  195. }
  196. }
  197. private void OptionsOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
  198. {
  199. this._queueRefresh = true;
  200. }
  201. private void OnDebugPanelVisibilityChanged(bool isVisible)
  202. {
  203. if (!isVisible)
  204. {
  205. this._queueRefresh = true;
  206. }
  207. }
  208. private void Refresh()
  209. {
  210. for (int i = 0; i < this._controlList.Count; i++)
  211. {
  212. this._controlList[i].Refresh();
  213. }
  214. }
  215. private readonly List<OptionsControlBase> _controlList = new List<OptionsControlBase>();
  216. private readonly Dictionary<OptionDefinition, OptionsControlBase> _pinnedObjects = new Dictionary<OptionDefinition, OptionsControlBase>();
  217. private bool _queueRefresh;
  218. private PinnedUIRoot _uiRoot;
  219. }
  220. }