Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseChartEditor.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using XCharts.Runtime;
  7. namespace XCharts.Editor
  8. {
  9. [CustomEditor(typeof(BaseChart), true)]
  10. public class BaseChartEditor : UnityEditor.Editor
  11. {
  12. class Styles
  13. {
  14. public static readonly GUIContent btnAddSerie = new GUIContent("Add Serie", "");
  15. public static readonly GUIContent btnAddComponent = new GUIContent("Add Main Component", "");
  16. public static readonly GUIContent btnConvertXYAxis = new GUIContent("Convert XY Axis", "");
  17. public static readonly GUIContent btnRebuildChartObject = new GUIContent("Rebuild Chart Object", "");
  18. public static readonly GUIContent btnSaveAsImage = new GUIContent("Save As Image", "");
  19. public static readonly GUIContent btnCheckWarning = new GUIContent("Check Warning", "");
  20. public static readonly GUIContent btnHideWarning = new GUIContent("Hide Warning", "");
  21. }
  22. protected BaseChart m_Chart;
  23. protected SerializedProperty m_Script;
  24. protected SerializedProperty m_EnableTextMeshPro;
  25. protected SerializedProperty m_Settings;
  26. protected SerializedProperty m_Theme;
  27. protected SerializedProperty m_ChartName;
  28. protected SerializedProperty m_DebugInfo;
  29. protected SerializedProperty m_RaycastTarget;
  30. protected List<SerializedProperty> m_Components = new List<SerializedProperty>();
  31. protected List<SerializedProperty> m_Series = new List<SerializedProperty>();
  32. private bool m_BaseFoldout;
  33. private bool m_CheckWarning = false;
  34. private int m_LastComponentCount = 0;
  35. private int m_LastSerieCount = 0;
  36. private string m_VersionString = "";
  37. private StringBuilder sb = new StringBuilder();
  38. MainComponentListEditor m_ComponentList;
  39. SerieListEditor m_SerieList;
  40. protected virtual void OnEnable()
  41. {
  42. if (target == null) return;
  43. m_Chart = (BaseChart) target;
  44. m_Script = serializedObject.FindProperty("m_Script");
  45. m_EnableTextMeshPro = serializedObject.FindProperty("m_EnableTextMeshPro");
  46. m_ChartName = serializedObject.FindProperty("m_ChartName");
  47. m_Theme = serializedObject.FindProperty("m_Theme");
  48. m_Settings = serializedObject.FindProperty("m_Settings");
  49. m_DebugInfo = serializedObject.FindProperty("m_DebugInfo");
  50. m_RaycastTarget = serializedObject.FindProperty("m_RaycastTarget");
  51. RefreshComponent();
  52. m_ComponentList = new MainComponentListEditor(this);
  53. m_ComponentList.Init(m_Chart, serializedObject, m_Components);
  54. RefreshSeries();
  55. m_SerieList = new SerieListEditor(this);
  56. m_SerieList.Init(m_Chart, serializedObject, m_Series);
  57. m_VersionString = "v" + XChartsMgr.fullVersion;
  58. if (m_EnableTextMeshPro.boolValue)
  59. m_VersionString += "-tmp";
  60. }
  61. public List<SerializedProperty> RefreshComponent()
  62. {
  63. m_Components.Clear();
  64. serializedObject.UpdateIfRequiredOrScript();
  65. foreach (var kv in m_Chart.typeListForComponent)
  66. {
  67. InitComponent(kv.Value.Name);
  68. }
  69. return m_Components;
  70. }
  71. public List<SerializedProperty> RefreshSeries()
  72. {
  73. m_Series.Clear();
  74. serializedObject.UpdateIfRequiredOrScript();
  75. foreach (var kv in m_Chart.typeListForSerie)
  76. {
  77. InitSerie(kv.Value.Name);
  78. }
  79. return m_Series;
  80. }
  81. public override void OnInspectorGUI()
  82. {
  83. if (m_Chart == null && target == null)
  84. {
  85. base.OnInspectorGUI();
  86. return;
  87. }
  88. serializedObject.UpdateIfRequiredOrScript();
  89. if (m_LastComponentCount != m_Chart.components.Count)
  90. {
  91. m_LastComponentCount = m_Chart.components.Count;
  92. RefreshComponent();
  93. m_ComponentList.UpdateComponentsProperty(m_Components);
  94. }
  95. if (m_LastSerieCount != m_Chart.series.Count)
  96. {
  97. m_LastSerieCount = m_Chart.series.Count;
  98. RefreshSeries();
  99. m_SerieList.UpdateSeriesProperty(m_Series);
  100. }
  101. OnStartInspectorGUI();
  102. OnDebugInspectorGUI();
  103. EditorGUILayout.Space();
  104. serializedObject.ApplyModifiedProperties();
  105. }
  106. protected virtual void OnStartInspectorGUI()
  107. {
  108. ShowVersion();
  109. m_BaseFoldout = ChartEditorHelper.DrawHeader("Base", m_BaseFoldout, false, null, null);
  110. if (m_BaseFoldout)
  111. {
  112. EditorGUILayout.PropertyField(m_Script);
  113. EditorGUILayout.PropertyField(m_ChartName);
  114. EditorGUILayout.PropertyField(m_RaycastTarget);
  115. if (XChartsMgr.IsRepeatChartName(m_Chart, m_ChartName.stringValue))
  116. {
  117. EditorGUILayout.BeginHorizontal();
  118. EditorGUILayout.HelpBox("chart name is repeated: " + m_ChartName.stringValue, MessageType.Error);
  119. EditorGUILayout.EndHorizontal();
  120. }
  121. }
  122. EditorGUILayout.PropertyField(m_Theme);
  123. EditorGUILayout.PropertyField(m_Settings);
  124. m_ComponentList.OnGUI();
  125. m_SerieList.OnGUI();
  126. }
  127. protected virtual void OnDebugInspectorGUI()
  128. {
  129. EditorGUILayout.PropertyField(m_DebugInfo, true);
  130. EditorGUILayout.Space();
  131. AddSerie();
  132. AddComponent();
  133. CheckWarning();
  134. }
  135. protected void PropertyComponnetList(SerializedProperty prop)
  136. {
  137. for (int i = 0; i < prop.arraySize; i++)
  138. {
  139. EditorGUILayout.PropertyField(prop.GetArrayElementAtIndex(i), true);
  140. }
  141. }
  142. private void InitComponent(string propName)
  143. {
  144. var prop = serializedObject.FindProperty(propName);
  145. for (int i = 0; i < prop.arraySize; i++)
  146. {
  147. m_Components.Add(prop.GetArrayElementAtIndex(i));
  148. }
  149. m_Components.Sort((a, b) => { return a.propertyPath.CompareTo(b.propertyPath); });
  150. }
  151. private void InitSerie(string propName)
  152. {
  153. var prop = serializedObject.FindProperty(propName);
  154. for (int i = 0; i < prop.arraySize; i++)
  155. {
  156. m_Series.Add(prop.GetArrayElementAtIndex(i));
  157. }
  158. m_Series.Sort(delegate(SerializedProperty a, SerializedProperty b)
  159. {
  160. var index1 = a.FindPropertyRelative("m_Index").intValue;
  161. var index2 = b.FindPropertyRelative("m_Index").intValue;
  162. return index1.CompareTo(index2);
  163. });
  164. }
  165. private void ShowVersion()
  166. {
  167. EditorGUILayout.HelpBox(m_VersionString, MessageType.None);
  168. }
  169. private void AddComponent()
  170. {
  171. if (GUILayout.Button(Styles.btnAddComponent))
  172. {
  173. var menu = new GenericMenu();
  174. foreach (var type in GetMainComponentTypeNames())
  175. {
  176. var title = ChartEditorHelper.GetContent(type.Name);
  177. bool exists = !m_Chart.CanAddChartComponent(type);
  178. if (!exists)
  179. menu.AddItem(title, false, () =>
  180. {
  181. m_ComponentList.AddChartComponent(type);
  182. });
  183. else
  184. {
  185. menu.AddDisabledItem(title);
  186. }
  187. }
  188. menu.ShowAsContext();
  189. }
  190. }
  191. private void AddSerie()
  192. {
  193. if (GUILayout.Button(Styles.btnAddSerie))
  194. {
  195. var menu = new GenericMenu();
  196. foreach (var type in GetSerieTypeNames())
  197. {
  198. var title = ChartEditorHelper.GetContent(type.Name);
  199. if (m_Chart.CanAddSerie(type))
  200. {
  201. menu.AddItem(title, false, () =>
  202. {
  203. m_SerieList.AddSerie(type);
  204. });
  205. }
  206. else
  207. {
  208. menu.AddDisabledItem(title);
  209. }
  210. }
  211. menu.ShowAsContext();
  212. }
  213. }
  214. private List<Type> GetMainComponentTypeNames()
  215. {
  216. var list = new List<Type>();
  217. var typeMap = RuntimeUtil.GetAllTypesDerivedFrom<MainComponent>();
  218. foreach (var kvp in typeMap)
  219. {
  220. var type = kvp;
  221. if (RuntimeUtil.HasSubclass(type)) continue;
  222. if (type.IsDefined(typeof(ComponentHandlerAttribute), false))
  223. {
  224. var attribute = type.GetAttribute<ComponentHandlerAttribute>();
  225. if (attribute != null && attribute.handler != null)
  226. list.Add(type);
  227. }
  228. else
  229. {
  230. list.Add(type);
  231. }
  232. }
  233. list.Sort((a, b) => { return a.Name.CompareTo(b.Name); });
  234. return list;
  235. }
  236. private List<Type> GetSerieTypeNames()
  237. {
  238. var list = new List<Type>();
  239. var typeMap = RuntimeUtil.GetAllTypesDerivedFrom<Serie>();
  240. foreach (var kvp in typeMap)
  241. {
  242. var type = kvp;
  243. if (type.IsDefined(typeof(SerieHandlerAttribute), false))
  244. list.Add(type);
  245. }
  246. list.Sort((a, b) => { return a.Name.CompareTo(b.Name); });
  247. return list;
  248. }
  249. private void CheckWarning()
  250. {
  251. if (m_Chart.HasChartComponent<XAxis>() && m_Chart.HasChartComponent<YAxis>())
  252. {
  253. if (GUILayout.Button(Styles.btnConvertXYAxis))
  254. m_Chart.ConvertXYAxis(0);
  255. }
  256. if (GUILayout.Button(Styles.btnRebuildChartObject))
  257. {
  258. m_Chart.RebuildChartObject();
  259. }
  260. if (GUILayout.Button(Styles.btnSaveAsImage))
  261. {
  262. m_Chart.SaveAsImage();
  263. }
  264. if (m_CheckWarning)
  265. {
  266. EditorGUILayout.BeginHorizontal();
  267. if (GUILayout.Button(Styles.btnCheckWarning))
  268. {
  269. m_CheckWarning = true;
  270. m_Chart.CheckWarning();
  271. }
  272. if (GUILayout.Button(Styles.btnHideWarning))
  273. {
  274. m_CheckWarning = false;
  275. }
  276. EditorGUILayout.EndHorizontal();
  277. sb.Length = 0;
  278. sb.AppendFormat("v{0}", XChartsMgr.fullVersion);
  279. if (!string.IsNullOrEmpty(m_Chart.warningInfo))
  280. {
  281. sb.AppendLine();
  282. sb.Append(m_Chart.warningInfo);
  283. }
  284. else
  285. {
  286. sb.AppendLine();
  287. sb.Append("Perfect! No warning!");
  288. }
  289. EditorGUILayout.HelpBox(sb.ToString(), MessageType.Warning);
  290. }
  291. else
  292. {
  293. if (GUILayout.Button(Styles.btnCheckWarning))
  294. {
  295. m_CheckWarning = true;
  296. m_Chart.CheckWarning();
  297. }
  298. }
  299. }
  300. }
  301. }