No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResizableElement.cs 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections.Generic;
  2. using UnityEditor.ShaderGraph.Drawing;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.ShaderGraph.Drawing
  6. {
  7. [System.Obsolete("ResizableElementFactory is deprecated and will be removed. Use UxmlElementAttribute instead.", false)]
  8. class ResizableElementFactory : UxmlFactory<ResizableElement>
  9. { }
  10. class ResizableElement : VisualElement
  11. {
  12. Dictionary<Resizer, VisualElement> m_Resizers = new Dictionary<Resizer, VisualElement>();
  13. List<Manipulator> m_Manipulators = new List<Manipulator>();
  14. public ResizableElement() : this("uxml/Resizable")
  15. {
  16. pickingMode = PickingMode.Ignore;
  17. }
  18. public ResizableElement(string uiFile)
  19. {
  20. var tpl = Resources.Load<VisualTreeAsset>(uiFile);
  21. var sheet = Resources.Load<StyleSheet>("Resizable");
  22. styleSheets.Add(sheet);
  23. tpl.CloneTree(this);
  24. foreach (Resizer direction in new[] { Resizer.Top, Resizer.Bottom, Resizer.Left, Resizer.Right })
  25. {
  26. VisualElement resizer = this.Q(direction.ToString().ToLower() + "-resize");
  27. if (resizer != null)
  28. {
  29. var manipulator = new ElementResizer(this, direction);
  30. resizer.AddManipulator(manipulator);
  31. m_Manipulators.Add(manipulator);
  32. }
  33. m_Resizers[direction] = resizer;
  34. }
  35. foreach (Resizer vertical in new[] { Resizer.Top, Resizer.Bottom })
  36. foreach (Resizer horizontal in new[] { Resizer.Left, Resizer.Right })
  37. {
  38. VisualElement resizer = this.Q(vertical.ToString().ToLower() + "-" + horizontal.ToString().ToLower() + "-resize");
  39. if (resizer != null)
  40. {
  41. var manipulator = new ElementResizer(this, vertical | horizontal);
  42. resizer.AddManipulator(manipulator);
  43. m_Manipulators.Add(manipulator);
  44. }
  45. m_Resizers[vertical | horizontal] = resizer;
  46. }
  47. }
  48. public void SetResizeRules(Resizer allowedResizeDirections)
  49. {
  50. foreach (var manipulator in m_Manipulators)
  51. {
  52. if (manipulator == null)
  53. return;
  54. var resizeElement = manipulator as ElementResizer;
  55. // If resizer direction is not in list of allowed directions, disable the callbacks on it
  56. if ((resizeElement.direction & allowedResizeDirections) == 0)
  57. {
  58. resizeElement.isEnabled = false;
  59. }
  60. else if ((resizeElement.direction & allowedResizeDirections) != 0)
  61. {
  62. resizeElement.isEnabled = true;
  63. }
  64. }
  65. }
  66. public enum Resizer
  67. {
  68. None = 0,
  69. Top = 1 << 0,
  70. Bottom = 1 << 1,
  71. Left = 1 << 2,
  72. Right = 1 << 3,
  73. }
  74. // Lets visual element owners bind a callback to when any resize operation is completed
  75. public void BindOnResizeCallback(EventCallback<MouseUpEvent> mouseUpEvent)
  76. {
  77. foreach (var manipulator in m_Manipulators)
  78. {
  79. if (manipulator == null)
  80. return;
  81. manipulator.target.RegisterCallback(mouseUpEvent);
  82. }
  83. }
  84. }
  85. }