PinnedUIServiceImpl.cs 8.1 KB

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