Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

GraphSubWindow.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System.Collections.Generic;
  2. using UnityEditor.Experimental.GraphView;
  3. using UnityEditor.ShaderGraph.Drawing.Interfaces;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.ShaderGraph.Drawing.Views
  7. {
  8. interface ISelectionProvider
  9. {
  10. List<ISelectable> GetSelection { get; }
  11. }
  12. class GraphSubWindow : GraphElement, ISGResizable
  13. {
  14. ISGViewModel m_ViewModel;
  15. ISGViewModel ViewModel
  16. {
  17. get => m_ViewModel;
  18. set => m_ViewModel = value;
  19. }
  20. Dragger m_Dragger;
  21. // This needs to be something that each subclass defines for itself at creation time
  22. // if they all use the same they'll be stacked on top of each other at SG window creation
  23. protected WindowDockingLayout windowDockingLayout { get; private set; } = new WindowDockingLayout
  24. {
  25. dockingTop = true,
  26. dockingLeft = false,
  27. verticalOffset = 8,
  28. horizontalOffset = 8,
  29. };
  30. // Used to cache the window docking layout between resizing operations as it interferes with window resizing operations
  31. private IStyle cachedWindowDockingStyle;
  32. protected VisualElement m_MainContainer;
  33. protected VisualElement m_Root;
  34. protected Label m_TitleLabel;
  35. protected Label m_SubTitleLabel;
  36. protected ScrollView m_ScrollView;
  37. protected VisualElement m_ContentContainer;
  38. protected VisualElement m_HeaderItem;
  39. protected VisualElement m_ParentView;
  40. // Added for test assembly access
  41. internal ScrollView scrollView => m_ScrollView;
  42. // These are used as default values for styling and layout purposes
  43. // They can be overriden if a child class wants to roll its own style and layout behavior
  44. public virtual string layoutKey => "UnityEditor.ShaderGraph.SubWindow";
  45. public virtual string styleName => "GraphSubWindow";
  46. public virtual string UxmlName => "GraphSubWindow";
  47. // Each sub-window will override these if they need to
  48. public virtual string elementName => "";
  49. public virtual string windowTitle => "";
  50. public VisualElement ParentView
  51. {
  52. get
  53. {
  54. if (!isWindowed && m_ParentView == null)
  55. m_ParentView = GetFirstAncestorOfType<GraphView>();
  56. return m_ParentView;
  57. }
  58. set
  59. {
  60. if (!isWindowed)
  61. return;
  62. m_ParentView = value;
  63. }
  64. }
  65. public List<ISelectable> selection
  66. {
  67. get
  68. {
  69. if (ParentView is ISelectionProvider selectionProvider)
  70. return selectionProvider.GetSelection;
  71. AssertHelpers.Fail("GraphSubWindow was unable to find a selection provider. Please check if parent view of: " + name + " implements ISelectionProvider::GetSelection");
  72. return new List<ISelectable>();
  73. }
  74. }
  75. public override string title
  76. {
  77. get { return m_TitleLabel.text; }
  78. set { m_TitleLabel.text = value; }
  79. }
  80. public string subTitle
  81. {
  82. get { return m_SubTitleLabel.text; }
  83. set { m_SubTitleLabel.text = value; }
  84. }
  85. // Intended for future handling of docking to sides of the shader graph window
  86. bool m_IsWindowed;
  87. public bool isWindowed
  88. {
  89. get { return m_IsWindowed; }
  90. set
  91. {
  92. if (m_IsWindowed == value) return;
  93. if (value)
  94. {
  95. capabilities &= ~Capabilities.Movable;
  96. AddToClassList("windowed");
  97. this.RemoveManipulator(m_Dragger);
  98. }
  99. else
  100. {
  101. capabilities |= Capabilities.Movable;
  102. RemoveFromClassList("windowed");
  103. this.AddManipulator(m_Dragger);
  104. }
  105. m_IsWindowed = value;
  106. }
  107. }
  108. public override VisualElement contentContainer => m_ContentContainer;
  109. private bool m_IsResizable = false;
  110. // Can be set by child classes as needed
  111. protected bool isWindowResizable
  112. {
  113. get => m_IsResizable;
  114. set
  115. {
  116. if (m_IsResizable != value)
  117. {
  118. m_IsResizable = value;
  119. HandleResizingBehavior(m_IsResizable);
  120. }
  121. }
  122. }
  123. void HandleResizingBehavior(bool isResizable)
  124. {
  125. if (isResizable)
  126. {
  127. var resizeElement = this.Q<ResizableElement>();
  128. resizeElement.BindOnResizeCallback(OnWindowResize);
  129. hierarchy.Add(resizeElement);
  130. }
  131. else
  132. {
  133. var resizeElement = this.Q<ResizableElement>();
  134. resizeElement.SetResizeRules(ResizableElement.Resizer.None);
  135. hierarchy.Remove(resizeElement);
  136. }
  137. }
  138. protected void SetResizingRules(ResizableElement.Resizer resizeDirections)
  139. {
  140. var resizeElement = this.Q<ResizableElement>();
  141. resizeElement.SetResizeRules(resizeDirections);
  142. }
  143. private bool m_IsScrollable = false;
  144. // Can be set by child classes as needed
  145. protected bool isWindowScrollable
  146. {
  147. get => m_IsScrollable;
  148. set
  149. {
  150. if (m_IsScrollable != value)
  151. {
  152. m_IsScrollable = value;
  153. HandleScrollingBehavior(m_IsScrollable);
  154. }
  155. }
  156. }
  157. protected float scrollableWidth
  158. {
  159. get { return m_ScrollView.contentContainer.layout.width - m_ScrollView.contentViewport.layout.width; }
  160. }
  161. protected float scrollableHeight
  162. {
  163. get { return contentContainer.layout.height - m_ScrollView.contentViewport.layout.height; }
  164. }
  165. void HandleScrollingBehavior(bool scrollable)
  166. {
  167. if (scrollable)
  168. {
  169. // Remove the categories container from the content item and add it to the scrollview
  170. m_ContentContainer.RemoveFromHierarchy();
  171. m_ScrollView.Add(m_ContentContainer);
  172. AddToClassList("scrollable");
  173. }
  174. else
  175. {
  176. // Remove the categories container from the scrollview and add it to the content item
  177. m_ContentContainer.RemoveFromHierarchy();
  178. m_Root.Add(m_ContentContainer);
  179. RemoveFromClassList("scrollable");
  180. }
  181. }
  182. protected GraphSubWindow(ISGViewModel viewModel)
  183. {
  184. ViewModel = viewModel;
  185. m_ParentView = ViewModel.parentView;
  186. ParentView.Add(this);
  187. var styleSheet = Resources.Load<StyleSheet>($"Styles/{styleName}");
  188. // Setup VisualElement from Stylesheet and UXML file
  189. styleSheets.Add(styleSheet);
  190. var uxml = Resources.Load<VisualTreeAsset>($"UXML/{UxmlName}");
  191. m_MainContainer = uxml.Instantiate();
  192. m_MainContainer.AddToClassList("mainContainer");
  193. m_Root = m_MainContainer.Q("content");
  194. m_HeaderItem = m_MainContainer.Q("header");
  195. m_HeaderItem.AddToClassList("subWindowHeader");
  196. m_ScrollView = m_MainContainer.Q<ScrollView>("scrollView");
  197. m_TitleLabel = m_MainContainer.Q<Label>(name: "titleLabel");
  198. m_SubTitleLabel = m_MainContainer.Q<Label>(name: "subTitleLabel");
  199. m_ContentContainer = m_MainContainer.Q(name: "contentContainer");
  200. hierarchy.Add(m_MainContainer);
  201. capabilities |= Capabilities.Movable | Capabilities.Resizable;
  202. style.overflow = Overflow.Hidden;
  203. focusable = false;
  204. name = elementName;
  205. title = windowTitle;
  206. ClearClassList();
  207. AddToClassList(name);
  208. BuildManipulators();
  209. /* Event interception to prevent GraphView manipulators from being triggered */
  210. //RegisterCallback<DragUpdatedEvent>(e =>
  211. //{
  212. // e.StopPropagation();
  213. //});
  214. // prevent Zoomer manipulator
  215. RegisterCallback<WheelEvent>(e =>
  216. {
  217. e.StopPropagation();
  218. });
  219. //RegisterCallback<MouseDownEvent>(e =>
  220. //{
  221. // // prevent ContentDragger manipulator
  222. // e.StopPropagation();
  223. //});
  224. }
  225. public void ShowWindow()
  226. {
  227. this.style.visibility = Visibility.Visible;
  228. this.m_ScrollView.style.display = DisplayStyle.Flex;
  229. this.MarkDirtyRepaint();
  230. }
  231. public void HideWindow()
  232. {
  233. this.style.visibility = Visibility.Hidden;
  234. this.m_ScrollView.style.display = DisplayStyle.None;
  235. this.MarkDirtyRepaint();
  236. }
  237. void BuildManipulators()
  238. {
  239. m_Dragger = new Dragger { clampToParentEdges = true };
  240. RegisterCallback<MouseUpEvent>(OnMoveEnd);
  241. this.AddManipulator(m_Dragger);
  242. }
  243. #region Layout
  244. public void ClampToParentLayout(Rect parentLayout)
  245. {
  246. windowDockingLayout.CalculateDockingCornerAndOffset(layout, parentLayout);
  247. windowDockingLayout.ClampToParentWindow();
  248. // If the parent shader graph window is being resized smaller than this window on either axis
  249. if (parentLayout.width < this.layout.width || parentLayout.height < this.layout.height)
  250. {
  251. // Don't adjust the sub window in this case as it causes flickering errors and looks broken
  252. }
  253. else
  254. {
  255. windowDockingLayout.ApplyPosition(this);
  256. }
  257. SerializeLayout();
  258. }
  259. public void OnStartResize()
  260. {
  261. cachedWindowDockingStyle = this.style;
  262. }
  263. public void OnResized()
  264. {
  265. if (cachedWindowDockingStyle != null)
  266. {
  267. this.style.left = cachedWindowDockingStyle.left;
  268. this.style.right = cachedWindowDockingStyle.right;
  269. this.style.bottom = cachedWindowDockingStyle.bottom;
  270. this.style.top = cachedWindowDockingStyle.top;
  271. }
  272. windowDockingLayout.size = layout.size;
  273. SerializeLayout();
  274. }
  275. public void DeserializeLayout()
  276. {
  277. var serializedLayout = EditorUserSettings.GetConfigValue(layoutKey);
  278. if (!string.IsNullOrEmpty(serializedLayout))
  279. windowDockingLayout = JsonUtility.FromJson<WindowDockingLayout>(serializedLayout);
  280. else
  281. {
  282. // The window size needs to come from the stylesheet or UXML as opposed to being defined in code
  283. windowDockingLayout.size = layout.size;
  284. }
  285. windowDockingLayout.ApplySize(this);
  286. windowDockingLayout.ApplyPosition(this);
  287. }
  288. protected void AddStyleSheetFromPath(string styleSheetPath)
  289. {
  290. StyleSheet sheetAsset = Resources.Load<StyleSheet>(styleSheetPath); ;
  291. if (sheetAsset == null)
  292. {
  293. Debug.LogWarning(string.Format("Style sheet not found for path \"{0}\"", styleSheetPath));
  294. return;
  295. }
  296. styleSheets.Add(sheetAsset);
  297. }
  298. void SerializeLayout()
  299. {
  300. windowDockingLayout.size = layout.size;
  301. var serializedLayout = JsonUtility.ToJson(windowDockingLayout);
  302. EditorUserSettings.SetConfigValue(layoutKey, serializedLayout);
  303. }
  304. void OnMoveEnd(MouseUpEvent upEvent)
  305. {
  306. windowDockingLayout.CalculateDockingCornerAndOffset(layout, ParentView.layout);
  307. windowDockingLayout.ClampToParentWindow();
  308. SerializeLayout();
  309. }
  310. public bool CanResizePastParentBounds()
  311. {
  312. return false;
  313. }
  314. void OnWindowResize(MouseUpEvent upEvent)
  315. {
  316. }
  317. public virtual void Dispose()
  318. {
  319. m_MainContainer = null;
  320. m_Root = null;
  321. m_TitleLabel = null;
  322. m_SubTitleLabel = null;
  323. m_ScrollView = null;
  324. m_ContentContainer = null;
  325. m_HeaderItem = null;
  326. m_ParentView = null;
  327. cachedWindowDockingStyle = null;
  328. styleSheets.Clear();
  329. }
  330. }
  331. #endregion
  332. }