Files
XCharts/Editor/MainComponents/MainComponentBaseEditor.cs

116 lines
3.7 KiB
C#
Raw Normal View History

2021-11-23 13:20:07 +08:00
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;
2022-02-19 22:37:57 +08:00
using XCharts.Runtime;
2021-11-23 13:20:07 +08:00
2021-12-24 13:33:09 +08:00
namespace XCharts.Editor
2021-11-23 13:20:07 +08:00
{
public class MainComponentBaseEditor
{
2021-12-19 20:53:55 +08:00
protected const string MORE = "More";
protected bool m_MoreFoldout = false;
2022-02-25 08:10:09 +08:00
public BaseChart chart { get; private set; }
public MainComponent component { get; private set; }
2021-11-23 13:20:07 +08:00
2022-02-25 08:10:09 +08:00
public SerializedProperty baseProperty;
public SerializedProperty showProperty;
2021-11-23 13:20:07 +08:00
2021-12-24 13:33:09 +08:00
internal void Init(BaseChart chart, MainComponent target, SerializedProperty property, UnityEditor.Editor inspector)
2021-11-23 13:20:07 +08:00
{
this.chart = chart;
this.component = target;
this.baseProperty = property;
showProperty = baseProperty.FindPropertyRelative("m_Show");
if (showProperty == null)
showProperty = baseProperty.FindPropertyRelative("m_Enable");
OnEnable();
}
public virtual void OnEnable()
2022-05-22 22:17:38 +08:00
{ }
2021-11-23 13:20:07 +08:00
public virtual void OnDisable()
2022-05-22 22:17:38 +08:00
{ }
2021-11-23 13:20:07 +08:00
internal void OnInternalInspectorGUI()
{
OnInspectorGUI();
EditorGUILayout.Space();
}
public virtual void OnInspectorGUI()
2022-05-22 22:17:38 +08:00
{ }
2021-11-23 13:20:07 +08:00
protected virtual void DrawExtendeds()
2022-05-22 22:17:38 +08:00
{ }
2021-11-23 13:20:07 +08:00
public virtual string GetDisplayTitle()
{
var num = chart.GetChartComponentNum(component.GetType());
if (num > 1)
return ObjectNames.NicifyVariableName(component.GetType().Name) + " " + component.index;
else
return ObjectNames.NicifyVariableName(component.GetType().Name);
}
protected SerializedProperty FindProperty(string path)
{
return baseProperty.FindPropertyRelative(path);
}
protected void PropertyField(string path)
{
var property = FindProperty(path);
if (property != null)
{
var title = ChartEditorHelper.GetContent(property.displayName);
PropertyField(property, title);
}
else
{
Debug.LogError("Property not exist:" + baseProperty.propertyPath + "," + path);
}
}
2021-12-19 20:53:55 +08:00
protected void PropertyFiledMore(System.Action action)
{
m_MoreFoldout = ChartEditorHelper.DrawHeader(MORE, m_MoreFoldout, false, null, null);
if (m_MoreFoldout)
{
if (action != null) action();
}
}
2021-11-23 13:20:07 +08:00
protected void PropertyField(SerializedProperty property)
{
Assert.IsNotNull(property);
var title = ChartEditorHelper.GetContent(property.displayName);
PropertyField(property, title);
}
protected void PropertyField(SerializedProperty property, GUIContent title)
{
EditorGUILayout.PropertyField(property, title);
}
protected void PropertyListField(string relativePropName, bool showOrder = true, params HeaderMenuInfo[] menus)
{
var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
var height = 0f;
var prop = FindProperty(relativePropName);
prop.isExpanded = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height,
prop, prop.isExpanded, showOrder, true, menus);
if (prop.isExpanded)
{
GUILayoutUtility.GetRect(1f, height - 17);
}
}
protected void PropertyTwoFiled(string relativePropName)
{
var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
var prop = FindProperty(relativePropName);
ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DrawRect.width, prop, prop.displayName);
}
}
}