A game about forced loneliness, made by TACStudios
at master 130 lines 4.3 kB view raw
1using UnityEngine; 2using UnityEditor.U2D.Sprites; 3 4namespace UnityEditor.U2D.Animation 5{ 6 internal class WeightInspector 7 { 8 private SpriteMeshDataController m_SpriteMeshDataController = new SpriteMeshDataController(); 9 private GUIContent[] m_BoneNameContents; 10 11 public BaseSpriteMeshData spriteMeshData 12 { 13 get { return m_SpriteMeshDataController.spriteMeshData; } 14 set 15 { 16 if (spriteMeshData != value) 17 m_SpriteMeshDataController.spriteMeshData = value; 18 } 19 } 20 21 public GUIContent[] boneNames 22 { 23 get { return m_BoneNameContents; } 24 set { m_BoneNameContents = value; } 25 } 26 27 public ICacheUndo cacheUndo { get; set; } 28 public ISelection<int> selection { get; set; } 29 30 public int controlID 31 { 32 get { return 0; } 33 } 34 35 private bool m_UndoRegistered = false; 36 37 protected ISpriteEditor spriteEditor { get; private set; } 38 39 public void OnInspectorGUI() 40 { 41 ChannelsGUI(); 42 } 43 44 private void ChannelsGUI() 45 { 46 if (GUIUtility.hotControl == 0) 47 m_UndoRegistered = false; 48 49 for (int channel = 0; channel < 4; ++channel) 50 { 51 var enabled = false; 52 var boneIndex = -1; 53 var weight = 0f; 54 var isChannelEnabledMixed = false; 55 var isBoneIndexMixed = false; 56 var isWeightMixed = false; 57 58 if (spriteMeshData != null) 59 m_SpriteMeshDataController.GetMultiEditChannelData(selection, channel, out enabled, out boneIndex, out weight, out isChannelEnabledMixed, out isBoneIndexMixed, out isWeightMixed); 60 61 var newEnabled = enabled; 62 var newBoneIndex = boneIndex; 63 var newWeight = weight; 64 65 EditorGUI.BeginChangeCheck(); 66 67 WeightChannelDrawer(ref newEnabled, ref newBoneIndex, ref newWeight, isChannelEnabledMixed, isBoneIndexMixed, isWeightMixed); 68 69 if (EditorGUI.EndChangeCheck()) 70 { 71 RegisterUndo(); 72 m_SpriteMeshDataController.SetMultiEditChannelData(selection, channel, enabled, newEnabled, boneIndex, newBoneIndex, weight, newWeight); 73 } 74 } 75 } 76 77 private void WeightChannelDrawer( 78 ref bool isChannelEnabled, ref int boneIndex, ref float weight, 79 bool isChannelEnabledMixed = false, bool isBoneIndexMixed = false, bool isWeightMixed = false) 80 { 81 EditorGUILayout.BeginHorizontal(); 82 83 EditorGUIUtility.fieldWidth = 1f; 84 EditorGUIUtility.labelWidth = 1f; 85 86 EditorGUI.showMixedValue = isChannelEnabledMixed; 87 isChannelEnabled = EditorGUILayout.Toggle(GUIContent.none, isChannelEnabled); 88 89 EditorGUIUtility.fieldWidth = 30f; 90 EditorGUIUtility.labelWidth = 30f; 91 92 using (new EditorGUI.DisabledScope(!isChannelEnabled && !isChannelEnabledMixed)) 93 { 94 int tempBoneIndex = GUI.enabled ? boneIndex : -1; 95 96 EditorGUI.BeginChangeCheck(); 97 98 EditorGUIUtility.fieldWidth = 80f; 99 100 EditorGUI.showMixedValue = GUI.enabled && isBoneIndexMixed; 101 tempBoneIndex = EditorGUILayout.Popup(tempBoneIndex, m_BoneNameContents); 102 103 if (EditorGUI.EndChangeCheck()) 104 boneIndex = tempBoneIndex; 105 106 EditorGUIUtility.fieldWidth = 32f; 107 EditorGUI.showMixedValue = isWeightMixed; 108 weight = EditorGUILayout.Slider(GUIContent.none, weight, 0f, 1f); 109 } 110 111 EditorGUILayout.EndHorizontal(); 112 113 EditorGUI.showMixedValue = false; 114 EditorGUIUtility.labelWidth = -1; 115 EditorGUIUtility.fieldWidth = -1; 116 } 117 118 private void RegisterUndo() 119 { 120 if (m_UndoRegistered) 121 return; 122 123 Debug.Assert(cacheUndo != null); 124 125 cacheUndo.BeginUndoOperation(TextContent.editWeights); 126 127 m_UndoRegistered = true; 128 } 129 } 130}