AxisStatePropertyDrawer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Reflection;
  4. namespace Cinemachine.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(AxisStatePropertyAttribute))]
  7. internal sealed class AxisStatePropertyDrawer : PropertyDrawer
  8. {
  9. const int vSpace = 2;
  10. bool mExpanded = true;
  11. AxisState def = new AxisState(); // 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. ++EditorGUI.indentLevel;
  20. rect.y += height + vSpace;
  21. EditorGUI.PropertyField(rect, property.FindPropertyRelative(() => def.Value));
  22. if (!ValueRangeIsLocked(property))
  23. {
  24. rect.y += height + vSpace;
  25. InspectorUtility.MultiPropertyOnLine(rect, new GUIContent("Value Range"),
  26. new [] { property.FindPropertyRelative(() => def.m_MinValue),
  27. property.FindPropertyRelative(() => def.m_MaxValue),
  28. property.FindPropertyRelative(() => def.m_Wrap) },
  29. new [] { GUIContent.none, new GUIContent("to "), null });
  30. }
  31. rect.y += height + vSpace;
  32. InspectorUtility.MultiPropertyOnLine(rect, new GUIContent("Speed"),
  33. new [] { property.FindPropertyRelative(() => def.m_MaxSpeed),
  34. property.FindPropertyRelative(() => def.m_SpeedMode) },
  35. new [] { GUIContent.none, new GUIContent("as") });
  36. rect.y += height + vSpace;
  37. InspectorUtility.MultiPropertyOnLine(
  38. rect, null,
  39. new [] { property.FindPropertyRelative(() => def.m_AccelTime),
  40. property.FindPropertyRelative(() => def.m_DecelTime)},
  41. new [] { GUIContent.none, null });
  42. if (HasRecentering(property))
  43. {
  44. var rDef = new AxisState.Recentering();
  45. var recentering = property.FindPropertyRelative(() => def.m_Recentering);
  46. rect.y += height + vSpace;
  47. InspectorUtility.MultiPropertyOnLine(
  48. rect, new GUIContent(recentering.displayName, recentering.tooltip),
  49. new [] {
  50. recentering.FindPropertyRelative(() => rDef.m_enabled),
  51. recentering.FindPropertyRelative(() => rDef.m_WaitTime),
  52. recentering.FindPropertyRelative(() => rDef.m_RecenteringTime)},
  53. new [] { new GUIContent(""),
  54. new GUIContent("Wait"),
  55. new GUIContent("Time")} );
  56. }
  57. if (!HasInputProvider(property))
  58. {
  59. rect.y += height + vSpace;
  60. EditorGUI.PropertyField(
  61. rect, property.FindPropertyRelative(() => def.m_InputAxisName));
  62. }
  63. rect.y += height + vSpace;
  64. InspectorUtility.MultiPropertyOnLine(rect, null,
  65. new [] { property.FindPropertyRelative(() => def.m_InputAxisValue),
  66. property.FindPropertyRelative(() => def.m_InvertInput) },
  67. new [] { GUIContent.none, new GUIContent("Invert") });
  68. --EditorGUI.indentLevel;
  69. }
  70. }
  71. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  72. {
  73. float height = EditorGUIUtility.singleLineHeight + vSpace;
  74. if (mExpanded)
  75. {
  76. int lines = 5;
  77. if (!ValueRangeIsLocked(property))
  78. ++lines;
  79. if (!HasInputProvider(property))
  80. ++lines;
  81. if (HasRecentering(property))
  82. ++lines;
  83. height *= lines;
  84. }
  85. return height - vSpace;
  86. }
  87. bool ValueRangeIsLocked(SerializedProperty property)
  88. {
  89. bool value = false;
  90. PropertyInfo pi = typeof(AxisState).GetProperty(
  91. "ValueRangeLocked", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  92. if (pi != null)
  93. value = bool.Equals(true, pi.GetValue(SerializedPropertyHelper.GetPropertyValue(property), null));
  94. return value;
  95. }
  96. bool HasRecentering(SerializedProperty property)
  97. {
  98. bool value = false;
  99. PropertyInfo pi = typeof(AxisState).GetProperty(
  100. "HasRecentering", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  101. if (pi != null)
  102. value = bool.Equals(true, pi.GetValue(SerializedPropertyHelper.GetPropertyValue(property), null));
  103. return value;
  104. }
  105. bool HasInputProvider(SerializedProperty property)
  106. {
  107. bool value = false;
  108. PropertyInfo pi = typeof(AxisState).GetProperty(
  109. "HasInputProvider", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  110. if (pi != null)
  111. value = bool.Equals(true, pi.GetValue(SerializedPropertyHelper.GetPropertyValue(property), null));
  112. return value;
  113. }
  114. }
  115. }