MainComponentBaseEditor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. using XCharts.Runtime;
  5. namespace XCharts.Editor
  6. {
  7. public class MainComponentBaseEditor
  8. {
  9. protected const string MORE = "More";
  10. protected bool m_MoreFoldout = false;
  11. public BaseChart chart { get; private set; }
  12. public MainComponent component { get; private set; }
  13. public SerializedProperty baseProperty;
  14. public SerializedProperty showProperty;
  15. internal void Init(BaseChart chart, MainComponent target, SerializedProperty property, UnityEditor.Editor inspector)
  16. {
  17. this.chart = chart;
  18. this.component = target;
  19. this.baseProperty = property;
  20. showProperty = baseProperty.FindPropertyRelative("m_Show");
  21. if (showProperty == null)
  22. showProperty = baseProperty.FindPropertyRelative("m_Enable");
  23. OnEnable();
  24. }
  25. public virtual void OnEnable()
  26. { }
  27. public virtual void OnDisable()
  28. { }
  29. internal void OnInternalInspectorGUI()
  30. {
  31. OnInspectorGUI();
  32. EditorGUILayout.Space();
  33. }
  34. public virtual void OnInspectorGUI()
  35. { }
  36. protected virtual void DrawExtendeds()
  37. { }
  38. public virtual string GetDisplayTitle()
  39. {
  40. var num = chart.GetChartComponentNum(component.GetType());
  41. if (num > 1)
  42. return ObjectNames.NicifyVariableName(component.GetType().Name) + " " + component.index;
  43. else
  44. return ObjectNames.NicifyVariableName(component.GetType().Name);
  45. }
  46. protected SerializedProperty FindProperty(string path)
  47. {
  48. return baseProperty.FindPropertyRelative(path);
  49. }
  50. protected void PropertyField(string path)
  51. {
  52. var property = FindProperty(path);
  53. if (property != null)
  54. {
  55. var title = ChartEditorHelper.GetContent(property.displayName);
  56. PropertyField(property, title);
  57. }
  58. else
  59. {
  60. Debug.LogError("Property not exist:" + baseProperty.propertyPath + "," + path);
  61. }
  62. }
  63. protected void PropertyFiledMore(System.Action action)
  64. {
  65. m_MoreFoldout = ChartEditorHelper.DrawHeader(MORE, m_MoreFoldout, false, null, null);
  66. if (m_MoreFoldout)
  67. {
  68. if (action != null) action();
  69. }
  70. }
  71. protected void PropertyField(SerializedProperty property)
  72. {
  73. Assert.IsNotNull(property);
  74. var title = ChartEditorHelper.GetContent(property.displayName);
  75. PropertyField(property, title);
  76. }
  77. protected void PropertyField(SerializedProperty property, GUIContent title)
  78. {
  79. EditorGUILayout.PropertyField(property, title);
  80. }
  81. protected void PropertyListField(string relativePropName, bool showOrder = true, params HeaderMenuInfo[] menus)
  82. {
  83. var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
  84. var height = 0f;
  85. var prop = FindProperty(relativePropName);
  86. prop.isExpanded = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height,
  87. prop, prop.isExpanded, showOrder, true, menus);
  88. if (prop.isExpanded)
  89. {
  90. GUILayoutUtility.GetRect(1f, height - 17);
  91. }
  92. }
  93. protected void PropertyTwoFiled(string relativePropName)
  94. {
  95. var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
  96. var prop = FindProperty(relativePropName);
  97. ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DrawRect.width, prop, prop.displayName);
  98. }
  99. }
  100. }