AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
ThemeDrawer.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using System.IO;
9 using UnityEditor;
10 using UnityEngine;
11 #if dUI_TextMeshPro
12 using TMPro;
13 #endif
14 
15 namespace XCharts
16 {
17  [CustomPropertyDrawer(typeof(ChartTheme), true)]
19  {
20  public override string ClassName { get { return "Theme"; } }
21  public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
22  {
23  base.OnGUI(pos, prop, label);
24  var defaultWidth = pos.width;
25  var defaultX = pos.x;
26  var btnWidth = 50;
27  if (MakeFoldout(prop, ""))
28  {
29  var btnRect = new Rect(m_DrawRect);
30  btnRect.x = defaultX + defaultWidth - 2 * btnWidth - 2;
31  btnRect.y = m_DrawRect.y - EditorGUIUtility.singleLineHeight - 3;
32  btnRect.width = btnWidth;
33  var chart = prop.serializedObject.targetObject as BaseChart;
34  if (GUI.Button(btnRect, new GUIContent("Reset", "Reset to theme default color")))
35  {
36  chart.theme.ResetTheme();
37  chart.RefreshAllComponent();
38  }
39  btnRect.x = defaultX + defaultWidth - btnWidth;
40  btnRect.width = btnWidth;
41  if (GUI.Button(btnRect, new GUIContent("Export", "Export theme to asset for a new theme")))
42  {
43  ExportThemeWindow.target = chart;
44  EditorWindow.GetWindow(typeof(ExportThemeWindow));
45  }
46  ++EditorGUI.indentLevel;
47  var chartNameList = XThemeMgr.GetAllThemeNames();
48  var lastIndex = chartNameList.IndexOf(chart.theme.themeName);
49  var y = pos.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
50  var selectedIndex = EditorGUI.Popup(new Rect(pos.x, y, pos.width, EditorGUIUtility.singleLineHeight),
51  "Theme", lastIndex, chartNameList.ToArray());
52  AddSingleLineHeight();
53  if (lastIndex != selectedIndex)
54  {
55  XThemeMgr.SwitchTheme(chart, chartNameList[selectedIndex]);
56  }
57 #if dUI_TextMeshPro
58  PropertyField(prop, "m_TMPFont");
59  if(chart.theme.tmpFont == null && !string.IsNullOrEmpty(chart.theme.tmpFontName))
60  {
61  var msg = string.Format("Can't find theme font asset:{0} in project.", chart.theme.tmpFontName);
62  EditorGUILayout.HelpBox(msg, MessageType.Error);
63  }
64 #else
65  PropertyField(prop, "m_Font");
66  if (chart.theme.font == null && !string.IsNullOrEmpty(chart.theme.fontName))
67  {
68  var msg = string.Format("Can't find theme font asset:{0} in project.", chart.theme.fontName);
69  EditorGUILayout.HelpBox(msg, MessageType.Error);
70  }
71 #endif
72  PropertyField(prop, "m_ContrastColor");
73  PropertyField(prop, "m_BackgroundColor");
74  PropertyField(prop, "m_ColorPalette");
75  PropertyField(prop, "m_Common");
76  PropertyField(prop, "m_Title");
77  PropertyField(prop, "m_SubTitle");
78  PropertyField(prop, "m_Legend");
79  PropertyField(prop, "m_Axis");
80  PropertyField(prop, "m_RadiusAxis");
81  PropertyField(prop, "m_AngleAxis");
82  PropertyField(prop, "m_Polar");
83  PropertyField(prop, "m_Gauge");
84  PropertyField(prop, "m_Radar");
85  PropertyField(prop, "m_Tooltip");
86  PropertyField(prop, "m_DataZoom");
87  PropertyField(prop, "m_VisualMap");
88  PropertyField(prop, "m_Serie");
89  --EditorGUI.indentLevel;
90  }
91  }
92 
93  private void AddPropertyField(Rect pos, SerializedProperty prop, ref float y)
94  {
95  float height = EditorGUI.GetPropertyHeight(prop, new GUIContent(prop.displayName), true);
96  EditorGUI.PropertyField(new Rect(pos.x, y, pos.width, height), prop, true);
97  y += height + EditorGUIUtility.standardVerticalSpacing;
98  m_Heights[m_KeyName] += height + EditorGUIUtility.standardVerticalSpacing;
99  }
100  }
101 
102  public class ExportThemeWindow : EditorWindow
103  {
104  public static BaseChart target;
105  private static ExportThemeWindow window;
106  private string m_ChartName;
107  static void Init()
108  {
109  window = (ExportThemeWindow)EditorWindow.GetWindow(typeof(ExportThemeWindow), false, "Export Theme", true);
110  window.minSize = new Vector2(600, 50);
111  window.maxSize = new Vector2(600, 50);
112  window.Show();
113  }
114 
115  void OnInspectorUpdate()
116  {
117  Repaint();
118  }
119 
120  private void OnGUI()
121  {
122  if (target == null)
123  {
124  Close();
125  return;
126  }
127  GUILayout.Space(10);
128  GUILayout.Label("Input a new name for theme:");
129  m_ChartName = GUILayout.TextField(m_ChartName);
130 
131  GUILayout.Space(10);
132  GUILayout.Label("Export path:");
133  if (string.IsNullOrEmpty(m_ChartName))
134  {
135  GUILayout.Label("Need input a new name.");
136  }
137  else
138  {
139  GUILayout.Label(XThemeMgr.GetThemeAssetPath(m_ChartName));
140  }
141 
142  GUILayout.Space(20);
143  if (GUILayout.Button("Export"))
144  {
145  if (string.IsNullOrEmpty(m_ChartName))
146  {
147  ShowNotification(new GUIContent("ERROR:Need input a new name!"));
148  }
149  else if (XThemeMgr.ContainsTheme(m_ChartName))
150  {
151  ShowNotification(new GUIContent("ERROR:The name you entered is already in use!"));
152  }
153  else if (IsAssetsExist(XThemeMgr.GetThemeAssetPath(m_ChartName)))
154  {
155  ShowNotification(new GUIContent("ERROR:The asset is exist! \npath="
156  + XThemeMgr.GetThemeAssetPath(m_ChartName)));
157  }
158  else
159  {
160  XThemeMgr.ExportTheme(target.theme, m_ChartName);
161  ShowNotification(new GUIContent("SUCCESS:The theme is exported. \npath="
162  + XThemeMgr.GetThemeAssetPath(m_ChartName)));
163  }
164  }
165  }
166 
167  private bool IsAssetsExist(string path)
168  {
169  return File.Exists(Application.dataPath + "/../" + path);
170  }
171  }
172 }
XCharts.BasePropertyDrawer
Definition: BasePropertyDrawer.cs:15
XCharts.ExportThemeWindow
Definition: ThemeDrawer.cs:102
XCharts
Definition: RewardChart.cs:14
XCharts.SerieSymbolType.Rect
@ Rect
正方形。可通过设置itemStyle的cornerRadius变成圆角矩形。
XCharts.BaseChart
The base class of all charts. 所有Chart的基类。
Definition: BaseChart_API.cs:21
XCharts.ThemeDrawer
Definition: ThemeDrawer.cs:18
XCharts.ChartTheme.ResetTheme
void ResetTheme()
Clear all custom configurations. 重置,清除所有自定义配置。
Definition: ChartTheme.cs:346
XCharts.BaseChart.theme
ChartTheme theme
The theme.
Definition: BaseChart_API.cs:44