DisplayWindow.DebugSidePanel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.UIElements;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. namespace UnityEditor.Rendering.LookDev
  8. {
  9. partial class DisplayWindow
  10. {
  11. static partial class Style
  12. {
  13. internal const string k_DebugViewLabel = "Selected View";
  14. internal const string k_DebugShadowLabel = "Display Shadows";
  15. internal const string k_DebugViewMode = "View Mode";
  16. internal static readonly Texture2D k_LockOpen = CoreEditorUtils.LoadIcon(Style.k_IconFolder, "Unlocked", forceLowRes: true);
  17. internal static readonly Texture2D k_LockClose = CoreEditorUtils.LoadIcon(Style.k_IconFolder, "Locked", forceLowRes: true);
  18. // /!\ WARNING:
  19. //The following const are used in the uss.
  20. //If you change them, update the uss file too.
  21. internal const string k_DebugToolbarName = "debugToolbar";
  22. }
  23. MultipleSourceToggle m_Shadow;
  24. MultipleSourcePopupField m_DebugView;
  25. List<string> listDebugMode;
  26. bool cameraSynced
  27. => LookDev.currentContext.cameraSynced;
  28. ViewContext lastFocusedViewContext
  29. => LookDev.currentContext.GetViewContent(LookDev.currentContext.layout.lastFocusedView);
  30. TargetDebugView targetDebugView
  31. {
  32. get => layout.debugPanelSource;
  33. set
  34. {
  35. if (layout.debugPanelSource != value)
  36. {
  37. layout.debugPanelSource = value;
  38. UpdateSideDebugPanelProperties();
  39. }
  40. }
  41. }
  42. bool debugView1SidePanel
  43. => targetDebugView == TargetDebugView.First
  44. || targetDebugView == TargetDebugView.Both;
  45. bool debugView2SidePanel
  46. => targetDebugView == TargetDebugView.Second
  47. || targetDebugView == TargetDebugView.Both;
  48. void ApplyInFilteredViewsContext(Action<ViewContext> action)
  49. {
  50. if (debugView1SidePanel)
  51. action.Invoke(LookDev.currentContext.GetViewContent(ViewIndex.First));
  52. if (debugView2SidePanel)
  53. action.Invoke(LookDev.currentContext.GetViewContent(ViewIndex.Second));
  54. }
  55. T GetInFilteredViewsContext<T>(Func<ViewContext, T> getter, out bool multipleDifferentValue)
  56. {
  57. T res1 = default;
  58. T res2 = default;
  59. multipleDifferentValue = false;
  60. bool view1 = debugView1SidePanel;
  61. bool view2 = debugView2SidePanel;
  62. if (view1)
  63. res1 = getter.Invoke(LookDev.currentContext.GetViewContent(ViewIndex.First));
  64. if (view2)
  65. res2 = getter.Invoke(LookDev.currentContext.GetViewContent(ViewIndex.Second));
  66. if (view1 && view2 && !res1.Equals(res2))
  67. {
  68. multipleDifferentValue = true;
  69. return default;
  70. }
  71. else
  72. return view1 ? res1 : res2;
  73. }
  74. void ReadValueFromSourcesWithoutNotify<T, K>(K element, Func<ViewContext, T> from)
  75. where K : BaseField<T>, IMultipleSource
  76. {
  77. bool multipleDifferentValue;
  78. T newValue = GetInFilteredViewsContext(from, out multipleDifferentValue);
  79. if (element is MultipleSourcePopupField)
  80. element.inMultipleValueState = multipleDifferentValue;
  81. element.SetValueWithoutNotify(newValue);
  82. element.inMultipleValueState = multipleDifferentValue;
  83. }
  84. #region Hack_Support_UIElement_MixedValueState
  85. class MultipleDifferentValue : TextElement
  86. {
  87. public new class UxmlFactory : UxmlFactory<MultipleDifferentValue, UxmlTraits> { }
  88. public new class UxmlTraits : TextElement.UxmlTraits { }
  89. public new static readonly string ussClassName = "unity-multipledifferentevalue";
  90. public MultipleDifferentValue()
  91. {
  92. AddToClassList(ussClassName);
  93. text = "-";
  94. }
  95. }
  96. interface IMultipleSource
  97. {
  98. bool inMultipleValueState { get; set; }
  99. }
  100. class MultipleSourceToggle : Toggle, IMultipleSource
  101. {
  102. MultipleDifferentValue m_MultipleOverlay;
  103. bool m_InMultipleValueState = false;
  104. public bool inMultipleValueState
  105. {
  106. get => m_InMultipleValueState;
  107. set
  108. {
  109. if (value != m_InMultipleValueState)
  110. {
  111. m_MultipleOverlay.style.display = value ? DisplayStyle.Flex : DisplayStyle.None;
  112. m_InMultipleValueState = value;
  113. }
  114. }
  115. }
  116. public MultipleSourceToggle() : base ()
  117. {
  118. m_MultipleOverlay = new MultipleDifferentValue();
  119. this.Q(name: "unity-checkmark").Add(m_MultipleOverlay);
  120. m_MultipleOverlay.style.display = DisplayStyle.None;
  121. }
  122. public MultipleSourceToggle(string label) : base(label)
  123. {
  124. m_MultipleOverlay = new MultipleDifferentValue();
  125. this.Q(name: "unity-checkmark").Add(m_MultipleOverlay);
  126. m_MultipleOverlay.style.display = DisplayStyle.None;
  127. }
  128. public override void SetValueWithoutNotify(bool newValue)
  129. {
  130. if (inMultipleValueState)
  131. inMultipleValueState = false;
  132. base.SetValueWithoutNotify(newValue);
  133. }
  134. public override bool value
  135. {
  136. get => inMultipleValueState ? default : base.value;
  137. set
  138. {
  139. if (inMultipleValueState)
  140. inMultipleValueState = false;
  141. base.value = value;
  142. }
  143. }
  144. }
  145. class MultipleSourcePopupField : PopupField<string>, IMultipleSource
  146. {
  147. internal readonly int count;
  148. MultipleDifferentValue m_MultipleOverlay;
  149. bool m_InMultipleValueState = false;
  150. public bool inMultipleValueState
  151. {
  152. get => m_InMultipleValueState;
  153. set
  154. {
  155. if (value != m_InMultipleValueState)
  156. {
  157. m_MultipleOverlay.style.display = value ? DisplayStyle.Flex : DisplayStyle.None;
  158. m_InMultipleValueState = value;
  159. }
  160. }
  161. }
  162. public MultipleSourcePopupField(string label, List<string> choices, int defaultIndex = 0)
  163. : base(
  164. label,
  165. choices,
  166. defaultIndex,
  167. null,
  168. null)
  169. {
  170. count = choices.Count;
  171. m_MultipleOverlay = new MultipleDifferentValue();
  172. Add(m_MultipleOverlay);
  173. m_MultipleOverlay.style.display = DisplayStyle.None;
  174. }
  175. public override void SetValueWithoutNotify(string newValue)
  176. {
  177. // forbid change from not direct selection as it can be find different value at opening
  178. if (!inMultipleValueState)
  179. base.SetValueWithoutNotify(newValue);
  180. }
  181. public override string value
  182. {
  183. get => inMultipleValueState ? default : base.value;
  184. set
  185. {
  186. //when actively changing in the drop down, quit mixed value state
  187. if (inMultipleValueState)
  188. inMultipleValueState = false;
  189. base.value = value;
  190. }
  191. }
  192. }
  193. #endregion
  194. void CreateDebug()
  195. {
  196. if (m_MainContainer == null || m_MainContainer.Equals(null))
  197. throw new System.MemberAccessException("m_MainContainer should be assigned prior CreateEnvironment()");
  198. m_DebugContainer = new VisualElement() { name = Style.k_DebugContainerName };
  199. m_MainContainer.Add(m_DebugContainer);
  200. if (sidePanel == SidePanel.Debug)
  201. m_MainContainer.AddToClassList(Style.k_ShowDebugPanelClass);
  202. AddDebugViewSelector();
  203. AddDebugShadow();
  204. AddDebugViewMode();
  205. //[TODO: finish]
  206. //Toggle greyBalls = new Toggle("Grey balls");
  207. //greyBalls.SetValueWithoutNotify(LookDev.currentContext.GetViewContent(LookDev.currentContext.layout.lastFocusedView).debug.greyBalls);
  208. //greyBalls.RegisterValueChangedCallback(evt =>
  209. //{
  210. // LookDev.currentContext.GetViewContent(LookDev.currentContext.layout.lastFocusedView).debug.greyBalls = evt.newValue;
  211. //});
  212. //m_DebugContainer.Add(greyBalls);
  213. //[TODO: debug why list sometimes empty on resource reloading]
  214. //[TODO: display only per view]
  215. if (sidePanel == SidePanel.Debug)
  216. UpdateSideDebugPanelProperties();
  217. }
  218. void AddDebugViewSelector()
  219. {
  220. ToolbarRadio viewSelector = new ToolbarRadio()
  221. {
  222. name = Style.k_DebugToolbarName
  223. };
  224. viewSelector.AddRadios(new[]
  225. {
  226. Style.k_Camera1Icon,
  227. Style.k_LinkIcon,
  228. Style.k_Camera2Icon
  229. });
  230. viewSelector.SetValueWithoutNotify((int)targetDebugView);
  231. viewSelector.RegisterValueChangedCallback(evt
  232. => targetDebugView = (TargetDebugView)evt.newValue);
  233. m_DebugContainer.Add(viewSelector);
  234. }
  235. void AddDebugShadow()
  236. {
  237. m_Shadow = new MultipleSourceToggle(Style.k_DebugShadowLabel);
  238. ReadValueFromSourcesWithoutNotify(m_Shadow, view => view.debug.shadow);
  239. m_Shadow.RegisterValueChangedCallback(evt
  240. => ApplyInFilteredViewsContext(view => view.debug.shadow = evt.newValue));
  241. m_DebugContainer.Add(m_Shadow);
  242. }
  243. void AddDebugViewMode(int pos = -1)
  244. {
  245. //if debugPanel is open on script reload, at this time
  246. //RenderPipelineManager.currentPipeline is still null.
  247. //So compute the list on next frame only.
  248. if (LookDev.dataProvider == null)
  249. {
  250. EditorApplication.delayCall += () => AddDebugViewMode(2); //2 = hardcoded position of this field
  251. return;
  252. }
  253. if (m_DebugView != null && m_DebugContainer.Contains(m_DebugView))
  254. m_DebugContainer.Remove(m_DebugView);
  255. listDebugMode = new List<string>(LookDev.dataProvider?.supportedDebugModes ?? Enumerable.Empty<string>());
  256. listDebugMode.Insert(0, "None");
  257. m_DebugView = new MultipleSourcePopupField(Style.k_DebugViewMode, listDebugMode);
  258. if (sidePanel == SidePanel.Debug)
  259. ReadValueFromSourcesWithoutNotify(m_DebugView, view => listDebugMode[view.debug.viewMode + 1]);
  260. m_DebugView.RegisterValueChangedCallback(evt
  261. => ApplyInFilteredViewsContext(view => view.debug.viewMode = listDebugMode.IndexOf(evt.newValue) - 1));
  262. if (pos == -1)
  263. m_DebugContainer.Add(m_DebugView);
  264. else
  265. m_DebugContainer.Insert(pos, m_DebugView);
  266. }
  267. void UpdateSideDebugPanelProperties()
  268. {
  269. ReadValueFromSourcesWithoutNotify(m_Shadow, view => view.debug.shadow);
  270. if (m_DebugView != null)
  271. ReadValueFromSourcesWithoutNotify(m_DebugView, view => listDebugMode[view.debug.viewMode + 1]);
  272. }
  273. }
  274. }