Няма описание
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.

BasePropertyDrawer.cs 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 AddSingleLineHeight()
  72. {
  73. m_Heights[m_KeyName] += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  74. m_DrawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  75. }
  76. protected void AddHeight(float height)
  77. {
  78. m_Heights[m_KeyName] += height;
  79. m_DrawRect.y += height;
  80. }
  81. protected void PropertyListField(SerializedProperty prop, string relativePropName, bool showOrder = true)
  82. {
  83. if (IngorePropertys.Contains(relativePropName)) return;
  84. var height = m_Heights[m_KeyName];
  85. var toggleKeyName = m_KeyName + relativePropName;
  86. m_DataToggles[toggleKeyName] = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height,
  87. prop.FindPropertyRelative(relativePropName),
  88. m_DataToggles.ContainsKey(toggleKeyName) && m_DataToggles[toggleKeyName], showOrder, true);
  89. m_Heights[m_KeyName] = height;
  90. }
  91. protected void PropertyField(SerializedProperty prop, string relativePropName)
  92. {
  93. if (IngorePropertys.Contains(relativePropName)) return;
  94. if (!ChartEditorHelper.PropertyField(ref m_DrawRect, m_Heights, m_KeyName, prop, relativePropName))
  95. {
  96. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
  97. }
  98. }
  99. protected void PropertyFieldLimitMin(SerializedProperty prop, string relativePropName, float minValue)
  100. {
  101. if (IngorePropertys.Contains(relativePropName)) return;
  102. if (!ChartEditorHelper.PropertyFieldWithMinValue(ref m_DrawRect, m_Heights, m_KeyName, prop,
  103. relativePropName, minValue))
  104. {
  105. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
  106. }
  107. }
  108. protected void PropertyFieldLimitMax(SerializedProperty prop, string relativePropName, float maxValue)
  109. {
  110. if (IngorePropertys.Contains(relativePropName)) return;
  111. if (!ChartEditorHelper.PropertyFieldWithMaxValue(ref m_DrawRect, m_Heights, m_KeyName, prop,
  112. relativePropName, maxValue))
  113. {
  114. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativePropName);
  115. }
  116. }
  117. protected void PropertyField(SerializedProperty prop, SerializedProperty relativeProp)
  118. {
  119. if (!ChartEditorHelper.PropertyField(ref m_DrawRect, m_Heights, m_KeyName, relativeProp))
  120. {
  121. Debug.LogError("PropertyField ERROR:" + prop.displayName + ", " + relativeProp);
  122. }
  123. }
  124. protected void PropertyTwoFiled(SerializedProperty prop, string relativeListProp, string labelName = null)
  125. {
  126. PropertyTwoFiled(prop, prop.FindPropertyRelative(relativeListProp), labelName);
  127. }
  128. protected void PropertyTwoFiled(SerializedProperty prop, SerializedProperty relativeListProp,
  129. string labelName = null)
  130. {
  131. if (string.IsNullOrEmpty(labelName))
  132. {
  133. labelName = relativeListProp.displayName;
  134. }
  135. ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DefaultWidth, relativeListProp, labelName);
  136. m_Heights[m_KeyName] += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  137. }
  138. protected bool MakeFoldout(SerializedProperty prop, string relativePropName)
  139. {
  140. if (string.IsNullOrEmpty(relativePropName))
  141. {
  142. return ChartEditorHelper.MakeFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  143. m_DisplayName, null);
  144. }
  145. else
  146. {
  147. var relativeProp = prop.FindPropertyRelative(relativePropName);
  148. return ChartEditorHelper.MakeFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  149. m_DisplayName, relativeProp);
  150. }
  151. }
  152. protected bool MakeComponentFoldout(SerializedProperty prop, string relativePropName, bool relativePropEnable,
  153. params HeaderMenuInfo[] menus)
  154. {
  155. if (string.IsNullOrEmpty(relativePropName))
  156. {
  157. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  158. m_DisplayName, null, null, relativePropEnable, menus);
  159. }
  160. else
  161. {
  162. var relativeProp = prop.FindPropertyRelative(relativePropName);
  163. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  164. m_DisplayName, relativeProp, null, relativePropEnable, menus);
  165. }
  166. }
  167. protected bool MakeComponentFoldout(SerializedProperty prop, string relativePropName, string relativePropName2,
  168. bool relativePropEnable, params HeaderMenuInfo[] menus)
  169. {
  170. if (string.IsNullOrEmpty(relativePropName))
  171. {
  172. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  173. m_DisplayName, null, null, relativePropEnable, menus);
  174. }
  175. else
  176. {
  177. var relativeProp = prop.FindPropertyRelative(relativePropName);
  178. var relativeProp2 = prop.FindPropertyRelative(relativePropName2);
  179. return ChartEditorHelper.MakeComponentFoldout(ref m_DrawRect, m_Heights, m_PropToggles, m_KeyName,
  180. m_DisplayName, relativeProp, relativeProp2, relativePropEnable, menus);
  181. }
  182. }
  183. protected virtual void DrawExtendeds(SerializedProperty prop) { }
  184. public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
  185. {
  186. var key = GetKeyName(prop);
  187. if (m_Heights.ContainsKey(key)) return m_Heights[key] + GetExtendedHeight();
  188. else return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  189. }
  190. protected virtual float GetExtendedHeight()
  191. {
  192. return 0;
  193. }
  194. }
  195. }