VFXReorderableList.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. using PositionType = UnityEngine.UIElements.Position;
  5. namespace UnityEditor.VFX.UI
  6. {
  7. class LineDragger : Manipulator
  8. {
  9. VFXReorderableList m_Root;
  10. VisualElement m_Line;
  11. public LineDragger(VFXReorderableList root, VisualElement item)
  12. {
  13. m_Root = root;
  14. m_Line = item;
  15. }
  16. protected override void RegisterCallbacksOnTarget()
  17. {
  18. target.RegisterCallback<MouseDownEvent>(OnMouseDown);
  19. target.RegisterCallback<MouseUpEvent>(OnMouseUp);
  20. }
  21. protected override void UnregisterCallbacksFromTarget()
  22. {
  23. target.UnregisterCallback<MouseDownEvent>(OnMouseDown);
  24. target.UnregisterCallback<MouseUpEvent>(OnMouseUp);
  25. }
  26. Vector2 startPosition;
  27. object m_Ctx;
  28. void Release()
  29. {
  30. target.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
  31. target.ReleaseMouse();
  32. m_Ctx = null;
  33. }
  34. protected void OnMouseDown(MouseDownEvent evt)
  35. {
  36. if (evt.button == 0)
  37. {
  38. evt.StopPropagation();
  39. target.CaptureMouse();
  40. startPosition = m_Root.WorldToLocal(evt.mousePosition);
  41. target.RegisterCallback<MouseMoveEvent>(OnMouseMove);
  42. m_Ctx = m_Root.StartDragging(m_Line);
  43. }
  44. }
  45. protected void OnMouseUp(MouseUpEvent evt)
  46. {
  47. Vector2 listRelativeMouse = m_Root.WorldToLocal(evt.mousePosition);
  48. m_Root.EndDragging(m_Ctx, m_Line, listRelativeMouse.y - startPosition.y, evt.mousePosition);
  49. evt.StopPropagation();
  50. Release();
  51. }
  52. protected void OnMouseMove(MouseMoveEvent evt)
  53. {
  54. evt.StopPropagation();
  55. Vector2 listRelativeMouse = m_Root.WorldToLocal(evt.mousePosition);
  56. m_Root.ItemDragging(m_Ctx, m_Line, listRelativeMouse.y - startPosition.y, evt.mousePosition);
  57. }
  58. }
  59. class LineSelecter : Manipulator
  60. {
  61. VFXReorderableList m_Root;
  62. VisualElement m_Line;
  63. public LineSelecter(VFXReorderableList root, VisualElement item)
  64. {
  65. m_Root = root;
  66. m_Line = item;
  67. }
  68. protected override void RegisterCallbacksOnTarget()
  69. {
  70. target.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
  71. }
  72. protected override void UnregisterCallbacksFromTarget()
  73. {
  74. target.UnregisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
  75. }
  76. void OnMouseDown(MouseDownEvent e)
  77. {
  78. m_Root.Select(m_Line);
  79. }
  80. }
  81. class VFXReorderableList : VisualElement
  82. {
  83. int m_SelectedLine = -1;
  84. class DraggingContext
  85. {
  86. public Rect[] originalPositions;
  87. public VisualElement[] items;
  88. public Rect myOriginalPosition;
  89. public int draggedIndex;
  90. }
  91. public void Select(int index)
  92. {
  93. if (m_SelectedLine != -1 && m_SelectedLine < m_ListContainer.childCount)
  94. {
  95. m_ListContainer.ElementAt(m_SelectedLine).RemoveFromClassList("selected");
  96. }
  97. m_SelectedLine = index;
  98. if (m_SelectedLine != -1 && m_SelectedLine < m_ListContainer.childCount)
  99. {
  100. m_ListContainer.ElementAt(m_SelectedLine).AddToClassList("selected");
  101. m_Remove.SetEnabled(CanRemove());
  102. }
  103. else
  104. {
  105. m_Remove.SetEnabled(false);
  106. }
  107. }
  108. public virtual bool CanRemove()
  109. {
  110. return true;
  111. }
  112. public void Select(VisualElement item)
  113. {
  114. Select(m_ListContainer.IndexOf(item));
  115. }
  116. public object StartDragging(VisualElement item)
  117. {
  118. //Fix all item so that they can be animated and we can control their positions
  119. DraggingContext context = new DraggingContext();
  120. context.items = m_ListContainer.Children().ToArray();
  121. context.originalPositions = context.items.Select(t => t.layout).ToArray();
  122. context.draggedIndex = m_ListContainer.IndexOf(item);
  123. context.myOriginalPosition = m_ListContainer.layout;
  124. Select(context.draggedIndex);
  125. for (int i = 0; i < context.items.Length; ++i)
  126. {
  127. VisualElement child = context.items[i];
  128. Rect rect = context.originalPositions[i];
  129. child.style.position = PositionType.Absolute;
  130. child.style.left = rect.x;
  131. child.style.top = rect.y;
  132. child.style.width = rect.width;
  133. child.style.height = rect.height;
  134. }
  135. item.BringToFront();
  136. m_ListContainer.style.width = context.myOriginalPosition.width;
  137. m_ListContainer.style.height = context.myOriginalPosition.height;
  138. return context;
  139. }
  140. public void EndDragging(object ctx, VisualElement item, float offset, Vector2 mouseWorldPosition)
  141. {
  142. DraggingContext context = (DraggingContext)ctx;
  143. foreach (var child in m_ListContainer.Children())
  144. {
  145. child.ResetPositionProperties();
  146. }
  147. if (context != null)
  148. {
  149. int hoveredIndex = GetHoveredIndex(context, mouseWorldPosition);
  150. m_ListContainer.Insert(hoveredIndex != -1 ? hoveredIndex : context.draggedIndex, item);
  151. m_ListContainer.ResetPositionProperties();
  152. if (hoveredIndex != -1)
  153. {
  154. ElementMoved(context.draggedIndex, hoveredIndex);
  155. }
  156. }
  157. }
  158. public void ItemDragging(object ctx, VisualElement item, float offset, Vector2 mouseWorldPosition)
  159. {
  160. DraggingContext context = (DraggingContext)ctx;
  161. if (context == null)
  162. return;
  163. item.style.top = context.originalPositions[context.draggedIndex].y + offset;
  164. int hoveredIndex = GetHoveredIndex(context, mouseWorldPosition);
  165. if (hoveredIndex != -1)
  166. {
  167. float draggedHeight = context.originalPositions[context.draggedIndex].height;
  168. if (hoveredIndex < context.draggedIndex)
  169. {
  170. for (int i = 0; i < hoveredIndex; ++i)
  171. {
  172. context.items[i].style.top = context.originalPositions[i].y;
  173. }
  174. for (int i = hoveredIndex; i < context.draggedIndex; ++i)
  175. {
  176. context.items[i].style.top = context.originalPositions[i].y + draggedHeight;
  177. }
  178. for (int i = context.draggedIndex + 1; i < context.items.Length; ++i)
  179. {
  180. context.items[i].style.top = context.originalPositions[i].y;
  181. }
  182. }
  183. else if (hoveredIndex > context.draggedIndex)
  184. {
  185. for (int i = 0; i < context.draggedIndex; ++i)
  186. {
  187. context.items[i].style.top = context.originalPositions[i].y;
  188. }
  189. for (int i = hoveredIndex; i > context.draggedIndex; --i)
  190. {
  191. context.items[i].style.top = context.originalPositions[i].y - draggedHeight;
  192. }
  193. for (int i = hoveredIndex + 1; i < context.items.Length; ++i)
  194. {
  195. context.items[i].style.top = context.originalPositions[i].y;
  196. }
  197. }
  198. }
  199. else
  200. {
  201. for (int i = 0; i < context.items.Length; ++i)
  202. {
  203. if (i != context.draggedIndex)
  204. context.items[i].style.top = context.originalPositions[i].y;
  205. }
  206. }
  207. }
  208. int GetHoveredIndex(DraggingContext context, Vector2 mouseWorldPosition)
  209. {
  210. Vector2 mousePosition = m_ListContainer.WorldToLocal(mouseWorldPosition);
  211. int hoveredIndex = -1;
  212. for (int i = 0; i < context.items.Length; ++i)
  213. {
  214. if (i != context.draggedIndex && context.originalPositions[i].Contains(mousePosition))
  215. {
  216. hoveredIndex = i;
  217. break;
  218. }
  219. }
  220. return hoveredIndex;
  221. }
  222. protected virtual void ElementMoved(int movedIndex, int targetIndex)
  223. {
  224. if (m_SelectedLine == movedIndex)
  225. {
  226. m_SelectedLine = targetIndex;
  227. }
  228. }
  229. VisualElement m_ListContainer;
  230. VisualElement m_Toolbar;
  231. public VFXReorderableList()
  232. {
  233. m_ListContainer = new VisualElement() { name = "ListContainer" };
  234. Add(m_ListContainer);
  235. m_Toolbar = new VisualElement() { name = "Toolbar" };
  236. var add = new VisualElement() { name = "Add" };
  237. add.Add(new VisualElement() { name = "icon" });
  238. add.AddManipulator(new Clickable(OnAdd));
  239. m_Toolbar.Add(add);
  240. m_Remove = new VisualElement() { name = "Remove" };
  241. m_Remove.Add(new VisualElement() { name = "icon" });
  242. m_Remove.AddManipulator(new Clickable(OnRemoveButton));
  243. m_Toolbar.Add(m_Remove);
  244. m_Remove.SetEnabled(false);
  245. Add(m_Toolbar);
  246. this.AddStyleSheetPathWithSkinVariant("VFXReorderableList");
  247. AddToClassList("ReorderableList");
  248. }
  249. VisualElement m_Remove;
  250. public void AddItem(VisualElement item)
  251. {
  252. m_ListContainer.Add(item);
  253. item.AddManipulator(new LineSelecter(this, item));
  254. if (reorderable)
  255. {
  256. AddDragToItem(item);
  257. }
  258. Select(m_ListContainer.childCount - 1);
  259. m_Remove.SetEnabled(CanRemove());
  260. }
  261. void AddDragToItem(VisualElement item)
  262. {
  263. var draggingHandle = new VisualElement() { name = "DraggingHandle" };
  264. draggingHandle.Add(new VisualElement() { name = "icon" });
  265. item.Insert(0, draggingHandle);
  266. draggingHandle.AddManipulator(new LineDragger(this, item));
  267. }
  268. void RemoveDragFromItem(VisualElement item)
  269. {
  270. if (item.childCount < 1 || item.ElementAt(0).name != "DraggingHandle")
  271. return;
  272. item.RemoveAt(0);
  273. }
  274. public bool toolbar
  275. {
  276. get { return m_Toolbar.parent != null; }
  277. set
  278. {
  279. if (value)
  280. {
  281. if (m_Toolbar.parent == null)
  282. {
  283. Add(m_Toolbar);
  284. }
  285. }
  286. else
  287. {
  288. if (m_Toolbar.parent != null)
  289. {
  290. m_Toolbar.RemoveFromHierarchy();
  291. }
  292. }
  293. }
  294. }
  295. bool m_Reorderable = true;
  296. public bool reorderable
  297. {
  298. get
  299. {
  300. return m_Reorderable;
  301. }
  302. set
  303. {
  304. if (m_Reorderable != value)
  305. {
  306. m_Reorderable = value;
  307. foreach (var item in m_ListContainer.Children())
  308. {
  309. if (m_Reorderable)
  310. {
  311. AddDragToItem(item);
  312. }
  313. else
  314. {
  315. RemoveDragFromItem(item);
  316. }
  317. }
  318. }
  319. }
  320. }
  321. public void RemoveItemAt(int index)
  322. {
  323. m_ListContainer.RemoveAt(index);
  324. if (m_SelectedLine >= m_ListContainer.childCount)
  325. {
  326. Select(m_ListContainer.childCount - 1);
  327. }
  328. }
  329. public VisualElement ItemAt(int index)
  330. {
  331. return m_ListContainer.ElementAt(index);
  332. }
  333. public int itemCount
  334. {
  335. get { return m_ListContainer.childCount; }
  336. }
  337. public virtual void OnAdd()
  338. {
  339. }
  340. void OnRemoveButton()
  341. {
  342. OnRemove(m_SelectedLine);
  343. }
  344. public virtual void OnRemove(int index)
  345. {
  346. }
  347. }
  348. }