AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
BaseChartEditor.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using System.Collections.Generic;
9 using UnityEditor;
10 using UnityEngine;
11 using System.Text;
12 
13 namespace XCharts
14 {
18  [CustomEditor(typeof(BaseChart), false)]
19  public class BaseChartEditor : Editor
20  {
21 #if UNITY_2019_3_OR_NEWER
22  private const float k_IconWidth = 14;
23  private const float k_IconGap = 0f;
24  private const float k_IconXOffset = 10f;
25  private const float k_IconYOffset = -1.5f;
26 #else
27  private const float k_IconWidth = 14;
28  private const float k_IconGap = 0f;
29  private const float k_IconXOffset = 4f;
30  private const float k_IconYOffset = -3f;
31 #endif
32 
33  protected BaseChart m_Chart;
34  protected SerializedProperty m_Script;
35  protected SerializedProperty m_MultiComponentMode;
36  protected SerializedProperty m_EnableTextMeshPro;
37  protected SerializedProperty m_ChartWidth;
38  protected SerializedProperty m_ChartHeight;
39  protected SerializedProperty m_Settings;
40  protected SerializedProperty m_Theme;
41  protected SerializedProperty m_Background;
42  protected SerializedProperty m_Titles;
43  protected SerializedProperty m_Legends;
44  protected SerializedProperty m_Tooltips;
45  protected SerializedProperty m_Vessels;
46  protected SerializedProperty m_Radars;
47  protected SerializedProperty m_Series;
48  protected SerializedProperty m_Large;
49  protected SerializedProperty m_ChartName;
50  protected SerializedProperty m_DebugMode;
51 
52  protected float m_DefaultLabelWidth;
53  protected float m_DefaultFieldWidth;
54  private int m_SeriesSize;
55  private Vector2 scrollPos;
56  private bool m_CheckWarning = false;
57  private StringBuilder sb = new StringBuilder();
58 
59  private bool m_BaseFoldout;
60  private bool m_CustomFoldout;
61  protected bool m_ShowAllComponent;
62  protected Dictionary<string, bool> m_Flodouts = new Dictionary<string, bool>();
63 
64  protected virtual void OnEnable()
65  {
66  if (target == null) return;
67  m_Chart = (BaseChart)target;
68  m_Script = serializedObject.FindProperty("m_Script");
69  m_MultiComponentMode = serializedObject.FindProperty("m_MultiComponentMode");
70  m_EnableTextMeshPro = serializedObject.FindProperty("m_EnableTextMeshPro");
71  m_ChartName = serializedObject.FindProperty("m_ChartName");
72  m_ChartWidth = serializedObject.FindProperty("m_ChartWidth");
73  m_ChartHeight = serializedObject.FindProperty("m_ChartHeight");
74  m_Theme = serializedObject.FindProperty("m_Theme");
75  m_Settings = serializedObject.FindProperty("m_Settings");
76  m_Background = serializedObject.FindProperty("m_Background");
77  m_Titles = serializedObject.FindProperty("m_Titles");
78  m_Legends = serializedObject.FindProperty("m_Legends");
79  m_Tooltips = serializedObject.FindProperty("m_Tooltips");
80  m_Vessels = serializedObject.FindProperty("m_Vessels");
81  m_Series = serializedObject.FindProperty("m_Series");
82  m_Radars = serializedObject.FindProperty("m_Radars");
83 
84  m_Large = serializedObject.FindProperty("m_Large");
85  m_DebugMode = serializedObject.FindProperty("m_DebugMode");
86  }
87 
88  public override void OnInspectorGUI()
89  {
90  if (m_Chart == null && target == null)
91  {
92  base.OnInspectorGUI();
93  return;
94  }
95  serializedObject.Update();
96  m_DefaultLabelWidth = EditorGUIUtility.labelWidth;
97  m_DefaultFieldWidth = EditorGUIUtility.fieldWidth;
98 
99  OnStartInspectorGUI();
100  OnMiddleInspectorGUI();
101  OnEndInspectorGUI();
102  OnDebugInspectorGUI();
103 
104  EditorGUILayout.Space();
105  serializedObject.ApplyModifiedProperties();
106  }
107 
108  protected virtual void OnStartInspectorGUI()
109  {
110  BlockStart();
111  EditorGUILayout.BeginHorizontal();
112  var version = string.Format("v{0}_{1}", XChartsMgr.version, XChartsMgr.versionDate);
113  if (m_EnableTextMeshPro.boolValue)
114  {
115  version += " TMP";
116  }
117  EditorGUILayout.LabelField(version);
118 
119  if (GUILayout.Button("Github"))
120  {
121  Application.OpenURL("https://github.com/monitor1394/unity-ugui-XCharts");
122  }
123  EditorGUILayout.EndHorizontal();
124  BlockEnd();
125 
126  BlockStart();
127  m_BaseFoldout = EditorGUILayout.Foldout(m_BaseFoldout, "Base", true);
128  if (m_BaseFoldout)
129  {
130  EditorGUILayout.PropertyField(m_Script);
131  EditorGUILayout.PropertyField(m_ChartName);
132  var fileds = m_Chart.GetCustomChartInspectorShowFileds();
133  if (fileds != null && fileds.Length > 0)
134  {
135  foreach (var filed in fileds)
136  {
137  EditorGUILayout.PropertyField(serializedObject.FindProperty(filed));
138  }
139  }
140  }
141  BlockEnd();
142 
143  BlockField(m_Theme);
144  BlockField(m_Settings);
145  BlockField(m_Background);
146 
147  m_ShowAllComponent = m_MultiComponentMode.boolValue;
148  BlockListField(m_ShowAllComponent, m_Titles);
149  BlockListField(m_ShowAllComponent, m_Legends);
150  BlockListField(m_ShowAllComponent, m_Tooltips);
151  BlockListField(m_ShowAllComponent, SerieType.Liquid, m_Vessels);
152  BlockListField(m_ShowAllComponent, SerieType.Radar, m_Radars);
153  }
154 
155  protected virtual void OnMiddleInspectorGUI()
156  {
157  BlockField(m_Series);
158  }
159 
160  protected virtual void OnEndInspectorGUI()
161  {
162  }
163 
164  protected virtual void OnDebugInspectorGUI()
165  {
166  BlockStart();
167  EditorGUILayout.PropertyField(m_DebugMode);
168  EditorGUILayout.PropertyField(m_MultiComponentMode);
169  EditorGUILayout.Space();
170  EditorGUILayout.Space();
171  MoreDebugInspector();
172  CheckWarning();
173  BlockEnd();
174  }
175 
176  protected virtual void MoreDebugInspector()
177  {
178  }
179 
180  protected void BlockStart()
181  {
182  if (XChartsSettings.editorBlockEnable) EditorGUILayout.BeginVertical(EditorStyles.helpBox);
183  }
184 
185  protected void BlockEnd()
186  {
187  if (XChartsSettings.editorBlockEnable) EditorGUILayout.EndVertical();
188  }
189 
190  protected void BlockField(params SerializedProperty[] props)
191  {
192  if (props.Length == 0) return;
193  BlockStart();
194  foreach (var prop in props)
195  EditorGUILayout.PropertyField(prop, true);
196  BlockEnd();
197  }
198 
199  protected void BlockListField(bool all, params SerializedProperty[] props)
200  {
201  if (props.Length == 0) return;
202  BlockStart();
203  foreach (var prop in props)
204  {
205  if (all)
206  {
207  var flag = m_Flodouts.ContainsKey(prop.displayName) && m_Flodouts[prop.displayName];
208  m_Flodouts[prop.displayName] = EditorGUILayout.Foldout(flag, prop.displayName, true);
209  if (m_Flodouts[prop.displayName])
210  {
211  EditorGUI.indentLevel++;
212  var currRect = EditorGUILayout.GetControlRect(GUILayout.Height(0));
213  currRect.y -= EditorGUIUtility.singleLineHeight;
214  var rect1 = new Rect(currRect.width + k_IconXOffset,
215  currRect.y + k_IconYOffset,
216  k_IconWidth, EditorGUIUtility.singleLineHeight);
217  if (GUI.Button(rect1, ChartEditorHelper.Styles.iconAdd, ChartEditorHelper.Styles.invisibleButton))
218  {
219  prop.InsertArrayElementAtIndex(prop.arraySize > 0 ? prop.arraySize - 1 : 0);
220  var chart = prop.GetArrayElementAtIndex(0).serializedObject.targetObject as BaseChart;
221  serializedObject.ApplyModifiedProperties();
222  chart.RemoveChartObject();
223  chart.RefreshAllComponent();
224  }
225  for (int i = 0; i < prop.arraySize; i++)
226  {
227  EditorGUILayout.PropertyField(prop.GetArrayElementAtIndex(i), true);
228  currRect = EditorGUILayout.GetControlRect(GUILayout.Height(0));
229  currRect.y -= EditorGUI.GetPropertyHeight(prop.GetArrayElementAtIndex(i));
230  rect1 = new Rect(currRect.width + k_IconXOffset,
231  currRect.y + k_IconYOffset,
232  k_IconWidth, EditorGUIUtility.singleLineHeight);
233  var oldColor = GUI.contentColor;
234  GUI.contentColor = Color.black;
235  if (GUI.Button(rect1, ChartEditorHelper.Styles.iconRemove, ChartEditorHelper.Styles.invisibleButton))
236  {
237  if (prop.arraySize == 1)
238  {
239  if (EditorUtility.DisplayDialog("Delete component", "Confirm to delete last component?", "Sure", "Cancel"))
240  {
241  var chart = prop.GetArrayElementAtIndex(0).serializedObject.targetObject as BaseChart;
242  prop.DeleteArrayElementAtIndex(i);
243  serializedObject.ApplyModifiedProperties();
244  chart.RemoveChartObject();
245  chart.RefreshAllComponent();
246  }
247  }
248  else if (i < prop.arraySize && i >= 0)
249  {
250  var chart = prop.GetArrayElementAtIndex(0).serializedObject.targetObject as BaseChart;
251  prop.DeleteArrayElementAtIndex(i);
252  serializedObject.ApplyModifiedProperties();
253  chart.RemoveChartObject();
254  chart.RefreshAllComponent();
255  }
256  }
257  var rect2 = new Rect(currRect.width + k_IconXOffset - k_IconWidth - k_IconGap,
258  currRect.y + k_IconYOffset,
259  k_IconWidth, EditorGUIUtility.singleLineHeight);
260  if (GUI.Button(rect2, ChartEditorHelper.Styles.iconDown, ChartEditorHelper.Styles.invisibleButton))
261  {
262  if (i < prop.arraySize - 1) prop.MoveArrayElement(i, i + 1);
263  }
264  var rect3 = new Rect(currRect.width + k_IconXOffset - 2 * (k_IconWidth + k_IconGap),
265  currRect.y + k_IconYOffset,
266  k_IconWidth, EditorGUIUtility.singleLineHeight);
267  if (GUI.Button(rect3, ChartEditorHelper.Styles.iconUp, ChartEditorHelper.Styles.invisibleButton))
268  {
269  if (i > 0) prop.MoveArrayElement(i, i - 1);
270  }
271  GUI.contentColor = oldColor;
272  }
273  EditorGUI.indentLevel--;
274  }
275  }
276  else if (prop.arraySize > 0) EditorGUILayout.PropertyField(prop.GetArrayElementAtIndex(0), true);
277  }
278  BlockEnd();
279  }
280 
281  protected void BlockListField(bool all, SerieType serieType, params SerializedProperty[] props)
282  {
283  if (!m_Chart.ContainsSerie(serieType)) return;
284  BlockListField(all, props);
285  }
286 
287  private void CheckWarning()
288  {
289  if (GUILayout.Button("Remove All Chart Object"))
290  {
291  m_Chart.RemoveChartObject();
292  }
293  // if (GUILayout.Button("Check XCharts Update"))
294  // {
295  // CheckVersionEditor.ShowWindow();
296  // }
297  if (m_CheckWarning)
298  {
299  EditorGUILayout.BeginHorizontal();
300  if (GUILayout.Button("Check Warning"))
301  {
302  m_CheckWarning = true;
303  m_Chart.CheckWarning();
304  }
305  if (GUILayout.Button("Hide Warning"))
306  {
307  m_CheckWarning = false;
308  }
309  EditorGUILayout.EndHorizontal();
310  sb.Length = 0;
311  sb.AppendFormat("v{0}", XChartsMgr.fullVersion);
312  if (!string.IsNullOrEmpty(m_Chart.warningInfo))
313  {
314  sb.AppendLine();
315  sb.Append(m_Chart.warningInfo);
316  }
317  else
318  {
319  sb.AppendLine();
320  sb.Append("Perfect! No warning!");
321  }
322  EditorGUILayout.HelpBox(sb.ToString(), MessageType.Warning);
323  }
324  else
325  {
326  if (GUILayout.Button("Check warning"))
327  {
328  m_CheckWarning = true;
329  m_Chart.CheckWarning();
330  }
331  }
332  EditorGUILayout.Space();
333  }
334  }
335 }
XCharts.ChartEditorHelper
Definition: ChartEditorHelper.cs:14
XCharts.SerieType
SerieType
the type of serie. 系列类型。
Definition: Serie.cs:19
XCharts.ChartEditorHelper.Styles
Definition: ChartEditorHelper.cs:30
XCharts
Definition: RewardChart.cs:14
XCharts.XChartsMgr
Definition: XChartsMgr.cs:34
XCharts.SerieSymbolType.Rect
@ Rect
正方形。可通过设置itemStyle的cornerRadius变成圆角矩形。
XCharts.BaseChart
The base class of all charts. 所有Chart的基类。
Definition: BaseChart_API.cs:21
XCharts.BaseChartEditor
Editor class used to edit UI BaseChart.
Definition: BaseChartEditor.cs:19
XCharts.XChartsSettings
Definition: XChartsSettings.cs:25