Files
XCharts/Editor/Series/SerieBaseEditor.cs

160 lines
5.6 KiB
C#
Raw Normal View History

2021-11-23 13:20:07 +08:00
using System;
using System.Collections.Generic;
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 SerieBaseEditor
{
internal BaseChart chart { get; private set; }
internal Serie serie { get; private set; }
//Editor m_Inspector;
internal SerializedProperty baseProperty;
internal SerializedProperty showProperty;
2021-12-09 21:35:23 +08:00
internal List<HeaderMenuInfo> menus = new List<HeaderMenuInfo>();
2022-05-22 22:17:38 +08:00
internal List<HeaderMenuInfo> serieDataMenus = new List<HeaderMenuInfo>();
2021-11-23 13:20:07 +08:00
protected Dictionary<string, Type> m_CoordOptionsDic;
protected List<string> m_CoordOptionsNames;
2022-03-18 08:23:17 +08:00
private string m_DisplayName;
2021-11-23 13:20:07 +08:00
2021-12-24 13:33:09 +08:00
internal void Init(BaseChart chart, Serie target, SerializedProperty property, UnityEditor.Editor inspector)
2021-11-23 13:20:07 +08:00
{
this.chart = chart;
this.serie = target;
this.baseProperty = property;
2022-03-26 17:11:12 +08:00
m_DisplayName = string.Format("Serie {0}: {1}", serie.index, serie.GetType().Name);
2021-11-23 13:20:07 +08:00
//m_Inspector = inspector;
showProperty = baseProperty.FindPropertyRelative("m_Show");
if (showProperty == null)
showProperty = baseProperty.FindPropertyRelative("m_Enable");
OnEnable();
if (serie.GetType().IsDefined(typeof(CoordOptionsAttribute), false))
{
var attribute = serie.GetType().GetAttribute<CoordOptionsAttribute>();
m_CoordOptionsDic = new Dictionary<string, Type>();
m_CoordOptionsNames = new List<string>();
if (attribute.type0 != null)
{
m_CoordOptionsDic[attribute.type0.Name] = attribute.type0;
m_CoordOptionsNames.Add(attribute.type0.Name);
}
if (attribute.type1 != null)
{
m_CoordOptionsDic[attribute.type1.Name] = attribute.type1;
m_CoordOptionsNames.Add(attribute.type1.Name);
}
if (attribute.type2 != null)
{
m_CoordOptionsDic[attribute.type2.Name] = attribute.type2;
m_CoordOptionsNames.Add(attribute.type2.Name);
}
if (attribute.type3 != null)
{
m_CoordOptionsDic[attribute.type3.Name] = attribute.type3;
m_CoordOptionsNames.Add(attribute.type3.Name);
}
}
}
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()
{
2022-03-18 08:23:17 +08:00
// var title = string.Format("serie {0}: {1}", serie.index, serie.GetType().Name);
// return ObjectNames.NicifyVariableName(title);
return m_DisplayName;
2021-11-23 13:20:07 +08:00
}
2021-12-28 08:18:24 +08:00
internal SerializedProperty FindProperty(string path)
2021-11-23 13:20:07 +08:00
{
return baseProperty.FindPropertyRelative(path);
}
2021-12-11 18:26:28 +08:00
protected SerializedProperty PropertyField(string path)
2021-11-23 13:20:07 +08:00
{
Assert.IsNotNull(path);
var property = FindProperty(path);
Assert.IsNotNull(property, "Can't find:" + path);
var title = ChartEditorHelper.GetContent(property.displayName);
PropertyField(property, title);
2021-12-11 18:26:28 +08:00
return property;
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)
{
//TODO:
PropertyField(relativePropName);
}
protected void PropertyTwoFiled(string relativePropName)
{
2021-12-11 18:26:28 +08:00
var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
var prop = FindProperty(relativePropName);
ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DrawRect.width, prop, prop.displayName);
2021-11-23 13:20:07 +08:00
}
2021-12-11 18:26:28 +08:00
protected void PropertyFieldLimitMin(string relativePropName, double min)
2021-11-23 13:20:07 +08:00
{
2021-12-11 18:26:28 +08:00
var prop = PropertyField(relativePropName);
switch (prop.propertyType)
{
case SerializedPropertyType.Float:
if (prop.floatValue < min)
2022-05-22 22:17:38 +08:00
prop.floatValue = (float) min;
2021-12-11 18:26:28 +08:00
break;
case SerializedPropertyType.Integer:
if (prop.intValue < min)
2022-05-22 22:17:38 +08:00
prop.intValue = (int) min;
2021-12-11 18:26:28 +08:00
break;
}
2021-11-23 13:20:07 +08:00
}
2021-12-11 18:26:28 +08:00
protected void PropertyFieldLimitMax(string relativePropName, int max)
2021-11-23 13:20:07 +08:00
{
2021-12-11 18:26:28 +08:00
var prop = PropertyField(relativePropName);
switch (prop.propertyType)
{
case SerializedPropertyType.Float:
if (prop.floatValue > max)
2022-05-22 22:17:38 +08:00
prop.floatValue = (float) max;
2021-12-11 18:26:28 +08:00
break;
case SerializedPropertyType.Integer:
if (prop.intValue > max)
2022-05-22 22:17:38 +08:00
prop.intValue = (int) max;
2021-12-11 18:26:28 +08:00
break;
}
2021-11-23 13:20:07 +08:00
}
}
}