WeightInspector.cs 4.2 KB

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