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