A game about forced loneliness, made by TACStudios
at master 131 lines 5.0 kB view raw
1using System; 2using UnityEditor.U2D.Common; 3using UnityEditor.UIElements; 4using UnityEngine; 5using UnityEngine.UIElements; 6 7namespace UnityEditor.U2D.Animation 8{ 9#if ENABLE_UXML_SERIALIZED_DATA 10 [UxmlElement] 11#endif 12 internal partial class BoneInspectorPanel : VisualElement 13 { 14 [Flags] 15 internal enum PropertyReadOnly 16 { 17 None, 18 Name = 1, 19 Depth = 1 << 2, 20 Position = 1 << 3, 21 Rotation = 1 << 4, 22 Color = 1 << 5 23 } 24 25#if ENABLE_UXML_TRAITS 26 public class BoneInspectorPanelFactory : UxmlFactory<BoneInspectorPanel, BoneInspectorPanelUxmlTraits> { } 27 public class BoneInspectorPanelUxmlTraits : UxmlTraits { } 28#endif 29 30 public event Action<BoneCache, int> onBoneDepthChanged = (bone, depth) => { }; 31 public event Action<BoneCache, Vector2> onBonePositionChanged = (bone, position) => { }; 32 public event Action<BoneCache, float> onBoneRotationChanged = (bone, rotation) => { }; 33 public event Action<BoneCache, string> onBoneNameChanged = (bone, name) => { }; 34 public event Action<BoneCache, Color32> onBoneColorChanged = (bone, color) => { }; 35 36 private TextField m_BoneNameField; 37 private IntegerField m_BoneDepthField; 38 private FloatField m_BoneRotationField; 39 private Vector2Field m_BonePositionField; 40 private ColorField m_BoneColorField; 41 42 public string boneName 43 { 44 get { return m_BoneNameField.value; } 45 set { m_BoneNameField.value = value; } 46 } 47 48 public BoneCache target { get; set; } 49 50 public int boneDepth 51 { 52 get { return m_BoneDepthField.value; } 53 set { m_BoneDepthField.value = value; } 54 } 55 56 public Vector2 bonePosition 57 { 58 get { return m_BonePositionField.value; } 59 set { m_BonePositionField.SetValueWithoutNotify(value); } 60 } 61 62 public float boneRotation 63 { 64 get { return m_BoneRotationField.value; } 65 set { m_BoneRotationField.SetValueWithoutNotify(value); } 66 } 67 68 public Color32 boneColor 69 { 70 get => m_BoneColorField.value; 71 set { m_BoneColorField.SetValueWithoutNotify(value); } 72 } 73 74 public BoneInspectorPanel() 75 { 76 styleSheets.Add(ResourceLoader.Load<StyleSheet>("SkinningModule/BoneInspectorPanelStyle.uss")); 77 78 RegisterCallback<MouseDownEvent>((e) => { e.StopPropagation(); }); 79 RegisterCallback<MouseUpEvent>((e) => { e.StopPropagation(); }); 80 } 81 82 public void BindElements() 83 { 84 m_BoneNameField = this.Q<TextField>("BoneNameField"); 85 m_BoneDepthField = this.Q<IntegerField>("BoneDepthField"); 86 m_BoneRotationField = this.Q<FloatField>("BoneRotationField"); 87 m_BonePositionField = this.Q<Vector2Field>("BonePositionField"); 88 m_BoneColorField = this.Q<ColorField>("BoneColorField"); 89 m_BoneNameField.RegisterCallback<FocusOutEvent>(BoneNameFocusChanged); 90 m_BoneDepthField.RegisterCallback<FocusOutEvent>(BoneDepthFocusChanged); 91 m_BoneRotationField.RegisterValueChangedCallback(evt => onBoneRotationChanged(target, evt.newValue)); 92 m_BonePositionField.RegisterValueChangedCallback(evt => onBonePositionChanged(target, evt.newValue)); 93 m_BoneColorField.RegisterValueChangedCallback(evt => onBoneColorChanged(target, evt.newValue)); 94 } 95 96 private void BoneNameFocusChanged(FocusOutEvent evt) 97 { 98 onBoneNameChanged(target, boneName); 99 } 100 101 private void BoneDepthFocusChanged(FocusOutEvent evt) 102 { 103 onBoneDepthChanged(target, boneDepth); 104 } 105 public void HidePanel() 106 { 107 // We are hidding the panel, sent any unchanged value 108 this.SetHiddenFromLayout(true); 109 onBoneNameChanged(target, boneName); 110 onBoneDepthChanged(target, boneDepth); 111 } 112 public static BoneInspectorPanel GenerateFromUXML() 113 { 114 var visualTree = ResourceLoader.Load<VisualTreeAsset>("SkinningModule/BoneInspectorPanel.uxml"); 115 var clone = visualTree.CloneTree().Q<BoneInspectorPanel>("BoneInspectorPanel"); 116 clone.LocalizeTextInChildren(); 117 clone.BindElements(); 118 return clone; 119 } 120 121 public void SetReadOnly(PropertyReadOnly property) 122 { 123 m_BoneDepthField.SetEnabled(!property.HasFlag(PropertyReadOnly.Depth)); 124 m_BoneNameField.SetEnabled(!property.HasFlag(PropertyReadOnly.Name)); 125 m_BonePositionField.SetEnabled(!property.HasFlag(PropertyReadOnly.Position)); 126 m_BoneRotationField.SetEnabled(!property.HasFlag(PropertyReadOnly.Rotation)); 127 m_BoneColorField.SetEnabled(!property.HasFlag(PropertyReadOnly.Color)); 128 } 129 130 } 131}