CinemachineImpulseEnvelopePropertyDrawer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace Cinemachine.Editor
  4. {
  5. [CustomPropertyDrawer(typeof(CinemachineImpulseEnvelopePropertyAttribute))]
  6. internal sealed class CinemachineImpulseEnvelopePropertyDrawer : PropertyDrawer
  7. {
  8. const int vSpace = 2;
  9. static bool mExpanded = true;
  10. CinemachineImpulseManager.EnvelopeDefinition myClass
  11. = new CinemachineImpulseManager.EnvelopeDefinition(); // to access name strings
  12. public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  13. {
  14. float height = EditorGUIUtility.singleLineHeight;
  15. rect.height = height;
  16. mExpanded = EditorGUI.Foldout(rect, mExpanded, label, true);
  17. if (mExpanded)
  18. {
  19. const float indentAmount = 15;
  20. rect.width -= indentAmount; rect.x += indentAmount;
  21. float oldWidth = EditorGUIUtility.labelWidth;
  22. EditorGUIUtility.labelWidth -= indentAmount;
  23. rect.y += EditorGUIUtility.singleLineHeight + vSpace;
  24. DrawCurveTimeProperty(
  25. rect, new GUIContent("Attack", "The custom shape of the attack curve. Leave it blank for a default shape"),
  26. property.FindPropertyRelative(() => myClass.m_AttackShape),
  27. property.FindPropertyRelative(() => myClass.m_AttackTime));
  28. rect.y += EditorGUIUtility.singleLineHeight + vSpace;
  29. #if false // with "forever" button... dangerous because signal never goes away!
  30. var holdProp = property.FindPropertyRelative(() => myClass.m_SustainTime);
  31. InspectorUtility.MultiPropertyOnLine(
  32. rect, new GUIContent(holdProp.displayName, holdProp.tooltip),
  33. new SerializedProperty[] { holdProp, property.FindPropertyRelative(() => myClass.m_HoldForever) },
  34. new GUIContent[] { GUIContent.none, new GUIContent("forever") });
  35. #else
  36. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => myClass.m_SustainTime));
  37. #endif
  38. rect.y += EditorGUIUtility.singleLineHeight + vSpace;
  39. DrawCurveTimeProperty(
  40. rect, new GUIContent("Decay", "The custom shape of the decay curve. Leave it blank for a default shape"),
  41. property.FindPropertyRelative(() => myClass.m_DecayShape),
  42. property.FindPropertyRelative(() => myClass.m_DecayTime));
  43. rect.y += EditorGUIUtility.singleLineHeight + vSpace;
  44. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => myClass.m_ScaleWithImpact));
  45. EditorGUIUtility.labelWidth = oldWidth;
  46. }
  47. }
  48. void DrawCurveTimeProperty(
  49. Rect rect, GUIContent label,
  50. SerializedProperty curveProp, SerializedProperty timeProp)
  51. {
  52. float floatFieldWidth = EditorGUIUtility.singleLineHeight * 2.5f;
  53. GUIContent timeText = new GUIContent(" s", timeProp.tooltip);
  54. var textDimensions = GUI.skin.label.CalcSize(timeText);
  55. rect = EditorGUI.PrefixLabel(rect, label);
  56. rect.height = EditorGUIUtility.singleLineHeight;
  57. rect.width -= floatFieldWidth + textDimensions.x;
  58. Rect r = rect; r.height += 1; r.y -= 1;
  59. EditorGUI.BeginChangeCheck();
  60. EditorGUI.PropertyField(r, curveProp, GUIContent.none);
  61. if (EditorGUI.EndChangeCheck())
  62. {
  63. curveProp.animationCurveValue = InspectorUtility.NormalizeCurve(curveProp.animationCurveValue);
  64. if (curveProp.animationCurveValue.length < 1)
  65. curveProp.animationCurveValue = new AnimationCurve();
  66. curveProp.serializedObject.ApplyModifiedProperties();
  67. }
  68. float oldWidth = EditorGUIUtility.labelWidth;
  69. EditorGUIUtility.labelWidth = textDimensions.x;
  70. rect.x += rect.width; rect.width = floatFieldWidth + EditorGUIUtility.labelWidth;
  71. EditorGUI.BeginChangeCheck();
  72. EditorGUI.PropertyField(rect, timeProp, timeText);
  73. if (EditorGUI.EndChangeCheck())
  74. timeProp.floatValue = Mathf.Max(timeProp.floatValue, 0);
  75. EditorGUIUtility.labelWidth = oldWidth;
  76. }
  77. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  78. {
  79. float height = EditorGUIUtility.singleLineHeight + vSpace;
  80. if (mExpanded)
  81. height *= 5;
  82. return height;
  83. }
  84. }
  85. }