説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ChartEditorHelper.cs 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using XCharts.Runtime;
  6. namespace XCharts.Editor
  7. {
  8. public class HeaderMenuInfo
  9. {
  10. public string name;
  11. public Action action;
  12. public bool enable = true;
  13. public HeaderMenuInfo() { }
  14. public HeaderMenuInfo(string name, Action action)
  15. {
  16. this.name = name;
  17. this.action = action;
  18. }
  19. public HeaderMenuInfo(string name, Action action, bool enable)
  20. {
  21. this.name = name;
  22. this.action = action;
  23. this.enable = enable;
  24. }
  25. }
  26. public static class ChartEditorHelper
  27. {
  28. public const float HEADER_HEIGHT = 17f;
  29. public const float FOLDOUT_WIDTH = 13f;
  30. #if UNITY_2019_3_OR_NEWER
  31. public const float INDENT_WIDTH = 15;
  32. public const float BOOL_WIDTH = 15;
  33. public const float ARROW_WIDTH = 20;
  34. public const float GAP_WIDTH = 2;
  35. public const float DIFF_WIDTH = 0;
  36. #else
  37. public const float INDENT_WIDTH = 15;
  38. public const float BOOL_WIDTH = 15;
  39. public const float ARROW_WIDTH = 14f;
  40. public const float GAP_WIDTH = 0;
  41. public const float DIFF_WIDTH = 1;
  42. #endif
  43. static Dictionary<string, GUIContent> s_GUIContentCache;
  44. static ChartEditorHelper()
  45. {
  46. s_GUIContentCache = new Dictionary<string, GUIContent>();
  47. }
  48. public static void SecondField(Rect drawRect, SerializedProperty prop)
  49. {
  50. RectOffset offset = new RectOffset(-(int) EditorGUIUtility.labelWidth, 0, 0, 0);
  51. drawRect = offset.Add(drawRect);
  52. EditorGUI.PropertyField(drawRect, prop, GUIContent.none);
  53. drawRect = offset.Remove(drawRect);
  54. }
  55. public static void MakeTwoField(ref Rect drawRect, float rectWidth, SerializedProperty arrayProp,
  56. string name)
  57. {
  58. while (arrayProp.arraySize < 2) arrayProp.arraySize++;
  59. var prop1 = arrayProp.GetArrayElementAtIndex(0);
  60. var prop2 = arrayProp.GetArrayElementAtIndex(1);
  61. MakeTwoField(ref drawRect, rectWidth, prop1, prop2, name);
  62. }
  63. public static void MakeDivideList(ref Rect drawRect, float rectWidth, SerializedProperty arrayProp,
  64. string name, int showNum)
  65. {
  66. while (arrayProp.arraySize < showNum) arrayProp.arraySize++;
  67. EditorGUI.LabelField(drawRect, name);
  68. #if UNITY_2019_3_OR_NEWER
  69. var gap = 2;
  70. #else
  71. var gap = 0;
  72. #endif
  73. var startX = drawRect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + gap;
  74. var dataWidTotal = (rectWidth - (startX + INDENT_WIDTH + 1));
  75. EditorGUI.DrawRect(new Rect(startX, drawRect.y, dataWidTotal, drawRect.height), Color.grey);
  76. var dataWid = dataWidTotal / showNum;
  77. var xWid = dataWid - gap;
  78. for (int i = 0; i < 1; i++)
  79. {
  80. drawRect.x = startX + i * xWid;
  81. drawRect.width = dataWid + (EditorGUI.indentLevel - 2) * 40.5f;
  82. EditorGUI.PropertyField(drawRect, arrayProp.GetArrayElementAtIndex(i), GUIContent.none);
  83. }
  84. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  85. }
  86. public static void MakeTwoField(ref Rect drawRect, float rectWidth, SerializedProperty prop1,
  87. SerializedProperty prop2, string name)
  88. {
  89. EditorGUI.LabelField(drawRect, name);
  90. var startX = drawRect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + GAP_WIDTH;
  91. var diff = 13 + EditorGUI.indentLevel * 14;
  92. var offset = diff - INDENT_WIDTH;
  93. var tempWidth = (rectWidth - startX + diff) / 2;
  94. var centerXRect = new Rect(startX, drawRect.y, tempWidth, drawRect.height - 1);
  95. var centerYRect = new Rect(centerXRect.x + tempWidth - offset, drawRect.y, tempWidth - 1, drawRect.height - 1);
  96. EditorGUI.PropertyField(centerXRect, prop1, GUIContent.none);
  97. EditorGUI.PropertyField(centerYRect, prop2, GUIContent.none);
  98. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  99. }
  100. public static void MakeVector2(ref Rect drawRect, float rectWidth, SerializedProperty prop, string name)
  101. {
  102. EditorGUI.LabelField(drawRect, name);
  103. var startX = drawRect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + GAP_WIDTH;
  104. var diff = 14 + EditorGUI.indentLevel * 14;
  105. var offset = diff - INDENT_WIDTH;
  106. var tempWidth = (rectWidth - startX + diff) / 2;
  107. var centerXRect = new Rect(startX, drawRect.y, tempWidth, drawRect.height);
  108. var centerYRect = new Rect(centerXRect.x + tempWidth - offset, drawRect.y, tempWidth, drawRect.height);
  109. var x = EditorGUI.FloatField(centerXRect, prop.vector3Value.x);
  110. var y = EditorGUI.FloatField(centerYRect, prop.vector3Value.y);
  111. prop.vector3Value = new Vector3(x, y);
  112. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  113. }
  114. public static bool MakeFoldout(ref Rect drawRect, ref bool moduleToggle, string content,
  115. SerializedProperty prop = null, bool bold = false)
  116. {
  117. float defaultWidth = drawRect.width;
  118. float defaultX = drawRect.x;
  119. var style = bold ? EditorCustomStyles.foldoutStyle : UnityEditor.EditorStyles.foldout;
  120. drawRect.width = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH;
  121. moduleToggle = EditorGUI.Foldout(drawRect, moduleToggle, content, true, style);
  122. MakeBool(drawRect, prop);
  123. drawRect.width = defaultWidth;
  124. drawRect.x = defaultX;
  125. return moduleToggle;
  126. }
  127. public static bool MakeFoldout(ref Rect drawRect, Dictionary<string, float> heights,
  128. Dictionary<string, bool> moduleToggle, string key, string content, SerializedProperty prop, bool bold = false)
  129. {
  130. float defaultWidth = drawRect.width;
  131. float defaultX = drawRect.x;
  132. var style = bold ? EditorCustomStyles.foldoutStyle : UnityEditor.EditorStyles.foldout;
  133. drawRect.width = EditorGUIUtility.labelWidth;
  134. moduleToggle[key] = EditorGUI.Foldout(drawRect, moduleToggle[key], content, true, style);
  135. if (prop != null)
  136. {
  137. if (prop.propertyType == SerializedPropertyType.Boolean)
  138. {
  139. MakeBool(drawRect, prop);
  140. }
  141. else
  142. {
  143. drawRect.x = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + ARROW_WIDTH;
  144. drawRect.width = defaultWidth - drawRect.x + ARROW_WIDTH - 2;
  145. EditorGUI.PropertyField(drawRect, prop, GUIContent.none);
  146. }
  147. }
  148. drawRect.width = defaultWidth;
  149. drawRect.x = defaultX;
  150. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  151. heights[key] += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  152. return moduleToggle[key];
  153. }
  154. public static bool MakeComponentFoldout(ref Rect drawRect, Dictionary<string, float> heights,
  155. Dictionary<string, bool> moduleToggle, string key, string content, SerializedProperty prop,
  156. SerializedProperty prop2, bool propEnable, params HeaderMenuInfo[] menus)
  157. {
  158. var sourRect = drawRect;
  159. float defaultWidth = drawRect.width;
  160. float defaultX = drawRect.x;
  161. float headerHeight = DrawSplitterAndBackground(drawRect);
  162. drawRect.width = EditorGUIUtility.labelWidth;
  163. moduleToggle[key] = EditorGUI.Foldout(drawRect, moduleToggle[key], content, true, EditorStyles.foldout);
  164. if (prop != null)
  165. {
  166. if (prop.propertyType == SerializedPropertyType.Boolean)
  167. {
  168. if (!propEnable)
  169. using(new EditorGUI.DisabledScope(true))
  170. MakeBool(drawRect, prop);
  171. else
  172. MakeBool(drawRect, prop);
  173. if (prop2 != null && !moduleToggle[key])
  174. {
  175. drawRect.x = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + ARROW_WIDTH + BOOL_WIDTH;
  176. drawRect.width = defaultWidth - drawRect.x + ARROW_WIDTH;
  177. EditorGUI.PropertyField(drawRect, prop2, GUIContent.none);
  178. }
  179. }
  180. else
  181. {
  182. drawRect.x = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + ARROW_WIDTH;
  183. drawRect.width = defaultWidth - drawRect.x + ARROW_WIDTH - 2;
  184. EditorGUI.PropertyField(drawRect, prop, GUIContent.none);
  185. }
  186. }
  187. DrawMenu(sourRect, menus);
  188. drawRect.width = defaultWidth;
  189. drawRect.x = defaultX;
  190. drawRect.y += headerHeight;
  191. heights[key] += headerHeight;
  192. return moduleToggle[key];
  193. }
  194. public static void MakeBool(Rect drawRect, SerializedProperty boolProp, int index = 0, string name = null)
  195. {
  196. float defaultWidth = drawRect.width;
  197. float defaultX = drawRect.x;
  198. float boolWidth = index * (BOOL_WIDTH + GAP_WIDTH);
  199. drawRect.x = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + ARROW_WIDTH + boolWidth;
  200. drawRect.width = (EditorGUI.indentLevel + 1) * BOOL_WIDTH + index * 110;
  201. if (boolProp != null)
  202. {
  203. EditorGUI.PropertyField(drawRect, boolProp, GUIContent.none);
  204. if (!string.IsNullOrEmpty(name))
  205. {
  206. drawRect.x += BOOL_WIDTH;
  207. drawRect.width = 200;
  208. EditorGUI.LabelField(drawRect, name);
  209. }
  210. }
  211. drawRect.width = defaultWidth;
  212. drawRect.x = defaultX;
  213. }
  214. public static bool MakeFoldout(ref Rect drawRect, ref float height, ref Dictionary<string, bool> moduleToggle,
  215. SerializedProperty prop, string moduleName, string showPropName, bool bold = false)
  216. {
  217. var relativeProp = prop.FindPropertyRelative(showPropName);
  218. var flag = MakeFoldout(ref drawRect, ref moduleToggle, prop, moduleName, relativeProp, bold);
  219. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  220. height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  221. return flag;
  222. }
  223. public static bool MakeFoldout(ref Rect drawRect, ref Dictionary<string, bool> moduleToggle, SerializedProperty prop,
  224. string moduleName, SerializedProperty showProp = null, bool bold = false)
  225. {
  226. var key = prop.propertyPath;
  227. if (!moduleToggle.ContainsKey(key))
  228. {
  229. moduleToggle.Add(key, false);
  230. }
  231. var toggle = moduleToggle[key];
  232. float defaultWidth = drawRect.width;
  233. float defaultX = drawRect.x;
  234. #if UNITY_2019_3_OR_NEWER
  235. drawRect.width = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH;
  236. #else
  237. drawRect.width = EditorGUIUtility.labelWidth;
  238. #endif
  239. var displayName = string.IsNullOrEmpty(moduleName) ? prop.displayName : moduleName;
  240. var foldoutStyle = bold ? EditorCustomStyles.foldoutStyle : UnityEditor.EditorStyles.foldout;
  241. toggle = EditorGUI.Foldout(drawRect, toggle, displayName, true, foldoutStyle);
  242. if (moduleToggle[key] != toggle)
  243. {
  244. moduleToggle[key] = toggle;
  245. }
  246. if (showProp != null)
  247. {
  248. drawRect.x = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + ARROW_WIDTH;
  249. if (showProp.propertyType == SerializedPropertyType.Boolean)
  250. {
  251. drawRect.width = (EditorGUI.indentLevel + 1) * BOOL_WIDTH;
  252. }
  253. else
  254. {
  255. drawRect.width = defaultWidth - drawRect.x + ARROW_WIDTH - GAP_WIDTH;
  256. }
  257. EditorGUI.PropertyField(drawRect, showProp, GUIContent.none);
  258. }
  259. drawRect.width = defaultWidth;
  260. drawRect.x = defaultX;
  261. return toggle;
  262. }
  263. public static bool MakeListWithFoldout(ref Rect drawRect, SerializedProperty listProp, bool foldout,
  264. bool showOrder, bool showSize, params HeaderMenuInfo[] menus)
  265. {
  266. var height = 0f;
  267. return MakeListWithFoldout(ref drawRect, ref height, listProp, foldout, showOrder, showSize, menus);
  268. }
  269. public static bool MakeListWithFoldout(ref Rect drawRect, ref float height, SerializedProperty listProp,
  270. bool foldout, bool showOrder, bool showSize, params HeaderMenuInfo[] menus)
  271. {
  272. var rawWidth = drawRect.width;
  273. var headerHeight = DrawSplitterAndBackground(drawRect);
  274. var foldoutRect = drawRect;
  275. foldoutRect.xMax -= 10;
  276. bool flag = EditorGUI.Foldout(foldoutRect, foldout, listProp.displayName, true);
  277. ChartEditorHelper.DrawMenu(drawRect, menus);
  278. height += headerHeight;
  279. drawRect.y += headerHeight;
  280. drawRect.width = rawWidth;
  281. if (flag)
  282. {
  283. MakeList(ref drawRect, ref height, listProp, showOrder, showSize);
  284. }
  285. return flag;
  286. }
  287. public static void MakeList(ref Rect drawRect, SerializedProperty listProp, bool showOrder = false,
  288. bool showSize = true)
  289. {
  290. var height = 0f;
  291. MakeList(ref drawRect, ref height, listProp, showOrder, showSize);
  292. }
  293. public static void MakeList(ref Rect drawRect, ref float height, SerializedProperty listProp,
  294. bool showOrder = false, bool showSize = true)
  295. {
  296. EditorGUI.indentLevel++;
  297. var listSize = listProp.arraySize;
  298. var iconWidth = 10;
  299. var iconGap = 0f;
  300. if (showSize)
  301. {
  302. var headerHeight = DrawSplitterAndBackground(drawRect);
  303. if (showOrder)
  304. {
  305. var elementRect = new Rect(drawRect.x, drawRect.y, drawRect.width - iconWidth + 2, drawRect.height);
  306. var oldColor = GUI.contentColor;
  307. GUI.contentColor = Color.black;
  308. GUI.contentColor = oldColor;
  309. listSize = listProp.arraySize;
  310. listSize = EditorGUI.IntField(elementRect, "Size", listSize);
  311. }
  312. else
  313. {
  314. listSize = EditorGUI.IntField(drawRect, "Size", listSize);
  315. }
  316. if (listSize < 0) listSize = 0;
  317. drawRect.y += headerHeight;
  318. height += headerHeight;
  319. if (listSize != listProp.arraySize)
  320. {
  321. while (listSize > listProp.arraySize) listProp.arraySize++;
  322. while (listSize < listProp.arraySize) listProp.arraySize--;
  323. }
  324. }
  325. if (listSize > 30 && !XCSettings.editorShowAllListData)
  326. {
  327. SerializedProperty element;
  328. int num = listSize > 10 ? 10 : listSize;
  329. for (int i = 0; i < num; i++)
  330. {
  331. element = listProp.GetArrayElementAtIndex(i);
  332. DrawSplitterAndBackground(drawRect);
  333. EditorGUI.PropertyField(drawRect, element, new GUIContent("Element " + i));
  334. drawRect.y += EditorGUI.GetPropertyHeight(element);
  335. height += EditorGUI.GetPropertyHeight(element);
  336. }
  337. if (num >= 10)
  338. {
  339. EditorGUI.LabelField(drawRect, "...");
  340. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  341. height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  342. element = listProp.GetArrayElementAtIndex(listSize - 1);
  343. DrawSplitterAndBackground(drawRect);
  344. EditorGUI.PropertyField(drawRect, element, new GUIContent("Element " + (listSize - 1)));
  345. drawRect.y += EditorGUI.GetPropertyHeight(element) + EditorGUIUtility.standardVerticalSpacing;
  346. height += EditorGUI.GetPropertyHeight(element) + EditorGUIUtility.standardVerticalSpacing;
  347. }
  348. }
  349. else
  350. {
  351. for (int i = 0; i < listProp.arraySize; i++)
  352. {
  353. SerializedProperty element = listProp.GetArrayElementAtIndex(i);
  354. DrawSplitterAndBackground(drawRect);
  355. if (showOrder)
  356. {
  357. var temp = INDENT_WIDTH + GAP_WIDTH + iconGap;
  358. var isSerie = "Serie".Equals(element.type);
  359. var elementRect = isSerie ?
  360. new Rect(drawRect.x, drawRect.y, drawRect.width + INDENT_WIDTH - 2 * iconGap, drawRect.height) :
  361. new Rect(drawRect.x, drawRect.y, drawRect.width - 4 * iconWidth, drawRect.height);
  362. EditorGUI.PropertyField(elementRect, element, new GUIContent("Element " + i));
  363. var iconRect = new Rect(drawRect.width - 4 * iconWidth + temp, drawRect.y, iconWidth, drawRect.height);
  364. var oldColor = GUI.contentColor;
  365. GUI.contentColor = Color.black;
  366. if (GUI.Button(iconRect, EditorCustomStyles.iconUp, EditorCustomStyles.invisibleButton))
  367. {
  368. if (i > 0) listProp.MoveArrayElement(i, i - 1);
  369. }
  370. iconRect = new Rect(drawRect.width - 3 * iconWidth + temp, drawRect.y, iconWidth, drawRect.height);
  371. if (GUI.Button(iconRect, EditorCustomStyles.iconDown, EditorCustomStyles.invisibleButton))
  372. {
  373. if (i < listProp.arraySize - 1) listProp.MoveArrayElement(i, i + 1);
  374. }
  375. iconRect = new Rect(drawRect.width - 2 * iconWidth + temp, drawRect.y, iconWidth, drawRect.height);
  376. if (GUI.Button(iconRect, EditorCustomStyles.iconAdd, EditorCustomStyles.invisibleButton))
  377. {
  378. if (i < listProp.arraySize && i >= 0) listProp.InsertArrayElementAtIndex(i);
  379. }
  380. iconRect = new Rect(drawRect.width - iconWidth + temp, drawRect.y, iconWidth, drawRect.height);
  381. if (GUI.Button(iconRect, EditorCustomStyles.iconRemove, EditorCustomStyles.invisibleButton))
  382. {
  383. if (i < listProp.arraySize && i >= 0) listProp.DeleteArrayElementAtIndex(i);
  384. }
  385. else
  386. {
  387. drawRect.y += EditorGUI.GetPropertyHeight(element);
  388. height += EditorGUI.GetPropertyHeight(element);
  389. }
  390. GUI.contentColor = oldColor;
  391. }
  392. else
  393. {
  394. EditorGUI.PropertyField(drawRect, element, new GUIContent("Element " + i));
  395. drawRect.y += EditorGUI.GetPropertyHeight(element);
  396. height += EditorGUI.GetPropertyHeight(element);
  397. }
  398. }
  399. }
  400. EditorGUI.indentLevel--;
  401. }
  402. public static bool PropertyField(ref Rect drawRect, Dictionary<string, float> heights, string key,
  403. SerializedProperty prop)
  404. {
  405. if (prop == null) return false;
  406. EditorGUI.PropertyField(drawRect, prop, true);
  407. var hig = EditorGUI.GetPropertyHeight(prop);
  408. drawRect.y += hig;
  409. heights[key] += hig;
  410. return true;
  411. }
  412. public static bool PropertyFieldWithMinValue(ref Rect drawRect, Dictionary<string, float> heights, string key,
  413. SerializedProperty prop, float minValue)
  414. {
  415. if (prop == null) return false;
  416. EditorGUI.PropertyField(drawRect, prop, true);
  417. if (prop.propertyType == SerializedPropertyType.Float && prop.floatValue < minValue)
  418. prop.floatValue = minValue;
  419. if (prop.propertyType == SerializedPropertyType.Integer && prop.intValue < minValue)
  420. prop.intValue = (int) minValue;
  421. var hig = EditorGUI.GetPropertyHeight(prop);
  422. drawRect.y += hig;
  423. heights[key] += hig;
  424. return true;
  425. }
  426. public static bool PropertyFieldWithMaxValue(ref Rect drawRect, Dictionary<string, float> heights, string key,
  427. SerializedProperty prop, float maxValue)
  428. {
  429. if (prop == null) return false;
  430. EditorGUI.PropertyField(drawRect, prop, true);
  431. if (prop.propertyType == SerializedPropertyType.Float && prop.floatValue > maxValue)
  432. prop.floatValue = maxValue;
  433. if (prop.propertyType == SerializedPropertyType.Integer && prop.intValue > maxValue)
  434. prop.intValue = (int) maxValue;
  435. var hig = EditorGUI.GetPropertyHeight(prop);
  436. drawRect.y += hig;
  437. heights[key] += hig;
  438. return true;
  439. }
  440. public static bool PropertyField(ref Rect drawRect, Dictionary<string, float> heights, string key,
  441. SerializedProperty parentProp, string relativeName)
  442. {
  443. return PropertyField(ref drawRect, heights, key, parentProp.FindPropertyRelative(relativeName));
  444. }
  445. public static bool PropertyFieldWithMinValue(ref Rect drawRect, Dictionary<string, float> heights, string key,
  446. SerializedProperty parentProp, string relativeName, float minValue)
  447. {
  448. var relativeProp = parentProp.FindPropertyRelative(relativeName);
  449. return PropertyFieldWithMinValue(ref drawRect, heights, key, relativeProp, minValue);
  450. }
  451. public static bool PropertyFieldWithMaxValue(ref Rect drawRect, Dictionary<string, float> heights, string key,
  452. SerializedProperty parentProp, string relativeName, float maxValue)
  453. {
  454. var relativeProp = parentProp.FindPropertyRelative(relativeName);
  455. return PropertyFieldWithMaxValue(ref drawRect, heights, key, relativeProp, maxValue);
  456. }
  457. public static GUIContent GetContent(string textAndTooltip)
  458. {
  459. if (string.IsNullOrEmpty(textAndTooltip))
  460. return GUIContent.none;
  461. GUIContent content;
  462. if (!s_GUIContentCache.TryGetValue(textAndTooltip, out content))
  463. {
  464. var s = textAndTooltip.Split('|');
  465. content = new GUIContent(s[0]);
  466. if (s.Length > 1 && !string.IsNullOrEmpty(s[1]))
  467. content.tooltip = s[1];
  468. s_GUIContentCache.Add(textAndTooltip, content);
  469. }
  470. return content;
  471. }
  472. public static void DrawSplitter()
  473. {
  474. var rect = GUILayoutUtility.GetRect(1f, 1f);
  475. rect.xMin = 0f;
  476. rect.width += 4f;
  477. DrawSplitter(rect);
  478. }
  479. public static void DrawSplitter(Rect rect)
  480. {
  481. if (Event.current.type != EventType.Repaint)
  482. return;
  483. EditorGUI.DrawRect(rect, EditorCustomStyles.splitter);
  484. }
  485. public static float DrawSplitterAndBackground(Rect drawRect, bool drawBackground = false)
  486. {
  487. float defaultWidth = drawRect.width;
  488. float defaultX = drawRect.x;
  489. var splitRect = drawRect;
  490. splitRect.y = drawRect.y;
  491. splitRect.x = EditorGUI.indentLevel * INDENT_WIDTH + 4;
  492. splitRect.xMax = drawRect.xMax;
  493. splitRect.height = 1f;
  494. DrawSplitter(splitRect);
  495. if (drawBackground)
  496. {
  497. var bgRect = drawRect;
  498. bgRect.y = drawRect.y;
  499. bgRect.x -= 10 - EditorGUI.indentLevel * INDENT_WIDTH;
  500. bgRect.xMax = drawRect.xMax;
  501. bgRect.height = HEADER_HEIGHT + (EditorGUI.indentLevel < 1 ? 2 : 0);
  502. EditorGUI.DrawRect(bgRect, EditorCustomStyles.headerBackground);
  503. }
  504. return HEADER_HEIGHT;
  505. }
  506. public static bool DrawHeader(string title, bool state, bool drawBackground, SerializedProperty activeField,
  507. Action<Rect> drawCallback, params HeaderMenuInfo[] menus)
  508. {
  509. var rect = GUILayoutUtility.GetRect(1f, HEADER_HEIGHT);
  510. var labelRect = DrawHeaderInternal(rect, title, ref state, drawBackground, activeField);
  511. DrawMenu(rect, menus);
  512. if (drawCallback != null)
  513. {
  514. drawCallback(rect);
  515. }
  516. var e = Event.current;
  517. if (e.type == EventType.MouseDown)
  518. {
  519. if (labelRect.Contains(e.mousePosition))
  520. {
  521. if (e.button == 0)
  522. {
  523. state = !state;
  524. e.Use();
  525. }
  526. }
  527. }
  528. return state;
  529. }
  530. internal static bool DrawHeader(string title, bool state, bool drawBackground, SerializedProperty activeField,
  531. Action<Rect> drawCallback, List<HeaderMenuInfo> menus)
  532. {
  533. var rect = GUILayoutUtility.GetRect(1f, HEADER_HEIGHT);
  534. var labelRect = DrawHeaderInternal(rect, title, ref state, drawBackground, activeField);
  535. DrawMenu(rect, menus);
  536. if (drawCallback != null)
  537. {
  538. drawCallback(rect);
  539. }
  540. var e = Event.current;
  541. if (e.type == EventType.MouseDown)
  542. {
  543. if (labelRect.Contains(e.mousePosition))
  544. {
  545. if (e.button == 0)
  546. {
  547. state = !state;
  548. e.Use();
  549. }
  550. }
  551. }
  552. return state;
  553. }
  554. private static Rect DrawHeaderInternal(Rect rect, string title, ref bool state, bool drawBackground, SerializedProperty activeField)
  555. {
  556. var splitRect = rect;
  557. splitRect.x = EditorGUI.indentLevel * INDENT_WIDTH + 4;
  558. splitRect.xMax = rect.xMax;
  559. splitRect.height = 1f;
  560. var backgroundRect = rect;
  561. backgroundRect.x = splitRect.x;
  562. backgroundRect.xMax = rect.xMax;
  563. var labelRect = rect;
  564. labelRect.xMin += 0f;
  565. labelRect.xMax -= 35f;
  566. var foldoutRect = rect;
  567. //foldoutRect.x -= 12f - EditorGUI.indentLevel * INDENT_WIDTH ;
  568. foldoutRect.x = rect.x - FOLDOUT_WIDTH + EditorGUI.indentLevel * INDENT_WIDTH + DIFF_WIDTH;
  569. foldoutRect.y += 1f;
  570. foldoutRect.width = FOLDOUT_WIDTH;
  571. foldoutRect.height = FOLDOUT_WIDTH;
  572. DrawSplitter(splitRect);
  573. if (drawBackground)
  574. EditorGUI.DrawRect(backgroundRect, EditorCustomStyles.headerBackground);
  575. if (!string.IsNullOrEmpty(title))
  576. EditorGUI.LabelField(labelRect, GetContent(title));
  577. state = GUI.Toggle(foldoutRect, state, GUIContent.none, EditorStyles.foldout);
  578. if (activeField != null)
  579. {
  580. var toggleRect = backgroundRect;
  581. toggleRect.x = rect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * INDENT_WIDTH + GAP_WIDTH;
  582. toggleRect.y += 1f;
  583. toggleRect.width = 13f;
  584. toggleRect.height = 13f;
  585. activeField.boolValue = GUI.Toggle(toggleRect, activeField.boolValue, GUIContent.none);
  586. }
  587. return labelRect;
  588. }
  589. internal static bool DrawHeader(string title, SerializedProperty group, SerializedProperty activeField,
  590. Action resetAction, Action removeAction)
  591. {
  592. if (group == null) return false;
  593. group.isExpanded = DrawHeader(title, group.isExpanded, false, activeField, null,
  594. new HeaderMenuInfo("Reset", resetAction), new HeaderMenuInfo("Remove", removeAction));
  595. return group.isExpanded;
  596. }
  597. internal static bool DrawHeader(string title, SerializedProperty group, SerializedProperty activeField,
  598. params HeaderMenuInfo[] menus)
  599. {
  600. group.isExpanded = DrawHeader(title, group.isExpanded, false, activeField, null, menus);
  601. return group.isExpanded;
  602. }
  603. internal static bool DrawHeader(string title, SerializedProperty group, SerializedProperty activeField,
  604. List<HeaderMenuInfo> menus)
  605. {
  606. group.isExpanded = DrawHeader(title, group.isExpanded, false, activeField, null, menus);
  607. return group.isExpanded;
  608. }
  609. internal static void DrawMenu(Rect parentRect, params HeaderMenuInfo[] menus)
  610. {
  611. if (menus == null || menus.Length <= 0) return;
  612. var menuIcon = EditorCustomStyles.paneOptionsIcon;
  613. var menuRect = new Rect(parentRect.xMax - menuIcon.width, parentRect.y + 2f,
  614. menuIcon.width, menuIcon.height);
  615. GUI.DrawTexture(menuRect, menuIcon);
  616. var e = Event.current;
  617. if (e.type == EventType.MouseDown)
  618. {
  619. if (menuRect.Contains(e.mousePosition))
  620. {
  621. ShowHeaderContextMenu(new Vector2(menuRect.x, menuRect.yMax), menus);
  622. e.Use();
  623. }
  624. else if (parentRect.Contains(e.mousePosition))
  625. {
  626. if (e.button != 0)
  627. {
  628. ShowHeaderContextMenu(e.mousePosition, menus);
  629. e.Use();
  630. }
  631. }
  632. }
  633. }
  634. internal static void DrawMenu(Rect parentRect, List<HeaderMenuInfo> menus)
  635. {
  636. if (menus == null || menus.Count <= 0) return;
  637. var menuIcon = EditorCustomStyles.paneOptionsIcon;
  638. var menuRect = new Rect(parentRect.xMax - menuIcon.width, parentRect.y + 2f,
  639. menuIcon.width, menuIcon.height);
  640. GUI.DrawTexture(menuRect, menuIcon);
  641. var e = Event.current;
  642. if (e.type == EventType.MouseDown)
  643. {
  644. if (menuRect.Contains(e.mousePosition))
  645. {
  646. ShowHeaderContextMenu(new Vector2(menuRect.x, menuRect.yMax), menus);
  647. e.Use();
  648. }
  649. else if (parentRect.Contains(e.mousePosition))
  650. {
  651. if (e.button != 0)
  652. {
  653. ShowHeaderContextMenu(e.mousePosition, menus);
  654. e.Use();
  655. }
  656. }
  657. }
  658. }
  659. static void ShowHeaderContextMenu(Vector2 position, params HeaderMenuInfo[] menus)
  660. {
  661. if (menus == null || menus.Length <= 0) return;
  662. var menu = new GenericMenu();
  663. foreach (var info in menus)
  664. {
  665. if (info.enable)
  666. menu.AddItem(GetContent(info.name), false, () => info.action());
  667. else
  668. menu.AddDisabledItem(GetContent(info.name));
  669. }
  670. menu.DropDown(new Rect(position, Vector2.zero));
  671. }
  672. static void ShowHeaderContextMenu(Vector2 position, List<HeaderMenuInfo> menus)
  673. {
  674. if (menus == null || menus.Count <= 0) return;
  675. var menu = new GenericMenu();
  676. foreach (var info in menus)
  677. {
  678. if (info.enable)
  679. menu.AddItem(GetContent(info.name), false, () => info.action());
  680. else
  681. menu.AddDisabledItem(GetContent(info.name));
  682. }
  683. menu.DropDown(new Rect(position, Vector2.zero));
  684. }
  685. }
  686. }