BasePropertyDrawer.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace XCharts.Editor
  5. {
  6. public delegate void DelegateMenuAction(Vector2 postion);
  7. public class BasePropertyDrawer : PropertyDrawer
  8. {
  9. protected int m_Index;
  10. protected int m_DataSize;
  11. protected float m_DefaultWidth;
  12. protected string m_DisplayName;
  13. protected string m_KeyName;
  14. protected Rect m_DrawRect;
  15. protected Dictionary<string, float> m_Heights = new Dictionary<string, float>();
  16. protected Dictionary<string, bool> m_PropToggles = new Dictionary<string, bool>();
  17. protected Dictionary<string, bool> m_DataToggles = new Dictionary<string, bool>();
  18. public virtual string ClassName { get { return ""; } }
  19. public virtual List<string> IngorePropertys { get { return new List<string> { }; } }
  20. public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
  21. {
  22. m_DrawRect = pos;
  23. m_DrawRect.height = EditorGUIUtility.singleLineHeight;
  24. m_DefaultWidth = pos.width;
  25. var list = prop.displayName.Split(' ');
  26. if (list.Length > 0)
  27. {
  28. if (!int.TryParse(list[list.Length - 1], out m_Index))
  29. {
  30. m_Index = 0;
  31. m_DisplayName = prop.displayName;
  32. m_KeyName = prop.propertyPath + "_" + m_Index;
  33. }
  34. else
  35. {
  36. m_DisplayName = ClassName + " " + m_Index;
  37. m_KeyName = prop.propertyPath + "_" + m_Index;
  38. }
  39. }
  40. else
  41. {
  42. m_DisplayName = prop.displayName;
  43. }
  44. if (!m_PropToggles.ContainsKey(m_KeyName))
  45. {
  46. m_PropToggles.Add(m_KeyName, false);
  47. }
  48. if (!m_DataToggles.ContainsKey(m_KeyName))
  49. {
  50. m_DataToggles.Add(m_KeyName, false);
  51. }
  52. if (!m_Heights.ContainsKey(m_KeyName))
  53. {
  54. m_Heights.Add(m_KeyName, 0);
  55. }
  56. else
  57. {
  58. m_Heights[m_KeyName] = 0;
  59. }
  60. }
  61. private string GetKeyName(SerializedProperty prop)
  62. {
  63. var index = 0;
  64. var list = prop.displayName.Split(' ');
  65. if (list.Length > 0)
  66. {
  67. int.TryParse(list[list.Length - 1], out index);
  68. }
  69. return prop.propertyPath + "_" + index;
  70. }
  71. protected void AddHelpBox(string message, MessageType type = MessageType.Warning, int line = 2)
  72. {
  73. var offset = EditorGUI.indentLevel * ChartEditorHelper.INDENT_WIDTH;
  74. EditorGUI.HelpBox(new Rect(m_DrawRect.x + offset, m_DrawRect.y, m_DrawRect.width - offset, EditorGUIUtility.singleLineHeight * line), message, type);
  75. for (int i = 0; i < line; i++)
  76. AddSingleLineHeight();
  77. }
  78. protected void AddSingleLineHeight()
  79. {
  80. m_Heights[m_KeyName] += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  81. m_DrawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  82. }
  83. protected void AddHeight(float height)
  84. {
  85. m_Heights[m_KeyName] += height;
  86. m_DrawRect.y += height;
  87. }
  88. protected void PropertyListField(SerializedProperty prop, string relativePropName, bool showOrder = true)
  89. {
  90. if (IngorePropertys.Contains(relativePropName)) return;
  91. var height = m_Heights[m_KeyName];
  92. var toggleKeyName = m_KeyName + relativePropName;
  93. m_DataToggles[toggleKeyName] = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height,
  94. prop.FindPropertyRelative(relativePropName),
  95. m_DataToggles.ContainsKey(toggleKeyName) && m_DataToggles[toggleKeyName], showOrder, true);
  96. m_Heights[m_KeyName] = height;
  97. }
  98. protected void PropertyField(SerializedProperty prop, string relativePropName)
  99. {
  100. if (IngorePropertys.Contains(relativePropName)) return;
  101. if (!ChartEditorHelper.PropertyField(ref m_DrawRect, m_Heights, m_KeyName, prop, relativePropName))
  102. {
  103. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
  104. }
  105. }
  106. protected void PropertyFieldLimitMin(SerializedProperty prop, string relativePropName, float minValue)
  107. {
  108. if (IngorePropertys.Contains(relativePropName)) return;
  109. if (!ChartEditorHelper.PropertyFieldWithMinValue(ref m_DrawRect, m_Heights, m_KeyName, prop,
  110. relativePropName, minValue))
  111. {
  112. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
  113. }
  114. }
  115. protected void PropertyFieldLimitMax(SerializedProperty prop, string relativePropName, float maxValue)
  116. {
  117. if (IngorePropertys.Contains(relativePropName)) return;
  118. if (!ChartEditorHelper.PropertyFieldWithMaxValue(ref m_DrawRect, m_Heights, m_KeyName, prop,
  119. relativePropName, maxValue))
  120. {
  121. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
  122. }
  123. }
  124. protected void PropertyField(SerializedProperty prop, SerializedProperty relativeProp)
  125. {
  126. if (!ChartEditorHelper.PropertyField(ref m_DrawRect, m_Heights, m_KeyName, relativeProp))
  127. {
  128. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativeProp);
  129. }
  130. }
  131. protected void PropertyTwoFiled(SerializedProperty prop, string relativeListProp, string labelName = null)
  132. {
  133. PropertyTwoFiled(prop, prop.FindPropertyRelative(relativeListProp), labelName);
  134. }
  135. protected void PropertyTwoFiled(SerializedProperty prop, SerializedProperty relativeListProp,
  136. string labelName = null)
  137. {
  138. if (string.IsNullOrEmpty(labelName))
  139. {
  140. labelName = relativeListProp.displayName;
  141. }
  142. ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DefaultWidth, relativeListProp, labelName);
  143. m_Heights[m_KeyName] += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  144. }
  145. protected bool MakeFoldout(SerializedProperty prop, string relativePropName)
  146. {
  147. if (string.IsNullOrEmpty(relativePropName))
  148. {
  149. return ChartEditorHelper.MakeFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  150. m_DisplayName, null);
  151. }
  152. else
  153. {
  154. var relativeProp = prop.FindPropertyRelative(relativePropName);
  155. return ChartEditorHelper.MakeFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  156. m_DisplayName, relativeProp);
  157. }
  158. }
  159. protected bool MakeComponentFoldout(SerializedProperty prop, string relativePropName, bool relativePropEnable,
  160. params HeaderMenuInfo[] menus)
  161. {
  162. if (string.IsNullOrEmpty(relativePropName))
  163. {
  164. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  165. m_DisplayName, null, null, relativePropEnable, menus);
  166. }
  167. else
  168. {
  169. var relativeProp = prop.FindPropertyRelative(relativePropName);
  170. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  171. m_DisplayName, relativeProp, null, relativePropEnable, menus);
  172. }
  173. }
  174. protected bool MakeComponentFoldout(SerializedProperty prop, string relativePropName, string relativePropName2,
  175. bool relativePropEnable, params HeaderMenuInfo[] menus)
  176. {
  177. if (string.IsNullOrEmpty(relativePropName))
  178. {
  179. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  180. m_DisplayName, null, null, relativePropEnable, menus);
  181. }
  182. else
  183. {
  184. var relativeProp = prop.FindPropertyRelative(relativePropName);
  185. var relativeProp2 = prop.FindPropertyRelative(relativePropName2);
  186. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  187. m_DisplayName, relativeProp, relativeProp2, relativePropEnable, menus);
  188. }
  189. }
  190. protected virtual void DrawExtendeds(SerializedProperty prop) { }
  191. public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
  192. {
  193. var key = GetKeyName(prop);
  194. if (m_Heights.ContainsKey(key)) return m_Heights[key] + GetExtendedHeight();
  195. else return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  196. }
  197. protected virtual float GetExtendedHeight()
  198. {
  199. return 0;
  200. }
  201. }
  202. }