AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
BasePropertyDrawer.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 
12 namespace XCharts
13 {
14  [CustomPropertyDrawer(typeof(Settings), true)]
15  public class BasePropertyDrawer : PropertyDrawer
16  {
17  protected int m_Index;
18  protected int m_DataSize;
19  protected float m_DefaultWidth;
20  protected string m_DisplayName;
21  protected string m_KeyName;
22  protected Rect m_DrawRect;
23  protected Dictionary<string, float> m_Heights = new Dictionary<string, float>();
24  protected Dictionary<string, bool> m_PropToggles = new Dictionary<string, bool>();
25  protected Dictionary<string, bool> m_DataToggles = new Dictionary<string, bool>();
26 
27  public virtual string ClassName { get { return ""; } }
28  public virtual List<string> IngorePropertys { get { return new List<string> { }; } }
29 
30  public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
31  {
32  m_DrawRect = pos;
33  m_DrawRect.height = EditorGUIUtility.singleLineHeight;
34  m_DefaultWidth = pos.width;
35  var list = prop.displayName.Split(' ');
36  if (list.Length > 0)
37  {
38  if (!int.TryParse(list[list.Length - 1], out m_Index))
39  {
40  m_Index = 0;
41  m_DisplayName = prop.displayName;
42  m_KeyName = prop.propertyPath + "_" + m_Index;
43  }
44  else
45  {
46  m_DisplayName = ClassName + " " + m_Index;
47  m_KeyName = prop.propertyPath + "_" + m_Index;
48  }
49  }
50  else
51  {
52  m_DisplayName = prop.displayName;
53  }
54  if (!m_PropToggles.ContainsKey(m_KeyName))
55  {
56  m_PropToggles.Add(m_KeyName, false);
57  }
58  if (!m_DataToggles.ContainsKey(m_KeyName))
59  {
60  m_DataToggles.Add(m_KeyName, false);
61  }
62  if (!m_Heights.ContainsKey(m_KeyName))
63  {
64  m_Heights.Add(m_KeyName, 0);
65  }
66  else
67  {
68  m_Heights[m_KeyName] = 0;
69  }
70  }
71 
72  private string GetKeyName(SerializedProperty prop)
73  {
74  var index = 0;
75  var list = prop.displayName.Split(' ');
76  if (list.Length > 0)
77  {
78  int.TryParse(list[list.Length - 1], out index);
79  }
80  return prop.propertyPath + "_" + index;
81  }
82 
83  protected void AddSingleLineHeight()
84  {
85  m_Heights[m_KeyName] += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
86  m_DrawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
87  }
88 
89  protected void AddHeight(float height)
90  {
91  m_Heights[m_KeyName] += height;
92  m_DrawRect.y += height;
93  }
94 
95  protected void PropertyListField(SerializedProperty prop, string relativePropName, bool showOrder = true)
96  {
97  if (IngorePropertys.Contains(relativePropName)) return;
98  var height = m_Heights[m_KeyName];
99  var toggleKeyName = m_KeyName + relativePropName;
100  m_DataToggles[toggleKeyName] = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height,
101  prop.FindPropertyRelative(relativePropName),
102  m_DataToggles.ContainsKey(toggleKeyName) && m_DataToggles[toggleKeyName], showOrder);
103  m_Heights[m_KeyName] = height;
104  }
105 
106  protected void PropertyField(SerializedProperty prop, string relativePropName)
107  {
108  if (IngorePropertys.Contains(relativePropName)) return;
109  if (!ChartEditorHelper.PropertyField(ref m_DrawRect, m_Heights, m_KeyName, prop, relativePropName))
110  {
111  Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
112  }
113  }
114 
115  protected void PropertyFieldLimitMin(SerializedProperty prop, string relativePropName, float minValue)
116  {
117  if (IngorePropertys.Contains(relativePropName)) return;
118  if (!ChartEditorHelper.PropertyFieldWithMinValue(ref m_DrawRect, m_Heights, m_KeyName, prop,
119  relativePropName, minValue))
120  {
121  Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
122  }
123  }
124  protected void PropertyFieldLimitMax(SerializedProperty prop, string relativePropName, float maxValue)
125  {
126  if (IngorePropertys.Contains(relativePropName)) return;
127  if (!ChartEditorHelper.PropertyFieldWithMaxValue(ref m_DrawRect, m_Heights, m_KeyName, prop,
128  relativePropName, maxValue))
129  {
130  Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
131  }
132  }
133 
134  protected void PropertyField(SerializedProperty prop, SerializedProperty relativeProp)
135  {
136  if (!ChartEditorHelper.PropertyField(ref m_DrawRect, m_Heights, m_KeyName, relativeProp))
137  {
138  Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativeProp);
139  }
140  }
141 
142  protected void PropertyTwoFiled(SerializedProperty prop, string relativeListProp, string labelName = null)
143  {
144  PropertyTwoFiled(prop, prop.FindPropertyRelative(relativeListProp), labelName);
145  }
146  protected void PropertyTwoFiled(SerializedProperty prop, SerializedProperty relativeListProp,
147  string labelName = null)
148  {
149  if (string.IsNullOrEmpty(labelName))
150  {
151  labelName = relativeListProp.displayName;
152  }
153  ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DefaultWidth, relativeListProp, labelName);
154  m_Heights[m_KeyName] += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
155  }
156 
157  protected bool MakeFoldout(SerializedProperty prop, string relativePropName)
158  {
159  if (string.IsNullOrEmpty(relativePropName))
160  {
161  return ChartEditorHelper.MakeFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
162  m_DisplayName, null);
163  }
164  else
165  {
166  var relativeProp = prop.FindPropertyRelative(relativePropName);
167  return ChartEditorHelper.MakeFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
168  m_DisplayName, relativeProp);
169  }
170  }
171 
172  protected virtual void DrawExtendeds(SerializedProperty prop)
173  {
174  }
175 
176  public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
177  {
178  var key = GetKeyName(prop);
179  if (m_Heights.ContainsKey(key)) return m_Heights[key] + GetExtendedHeight();
180  else return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
181  }
182 
183  protected virtual float GetExtendedHeight()
184  {
185  return 0;
186  }
187  }
188 }
XCharts.ChartEditorHelper
Definition: ChartEditorHelper.cs:14
XCharts.BasePropertyDrawer
Definition: BasePropertyDrawer.cs:15
XCharts
Definition: RewardChart.cs:14
XCharts.SerieSymbolType.Rect
@ Rect
正方形。可通过设置itemStyle的cornerRadius变成圆角矩形。