GUIAnimFieldDisplay.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /** @addtogroup AnimationEditor
  10. * @{
  11. */
  12. internal class GUIAnimFieldDisplay
  13. {
  14. private SceneObject root;
  15. private int width;
  16. private int height;
  17. private GUIScrollArea scrollArea;
  18. private List<AnimFieldInfo> fieldInfos = new List<AnimFieldInfo>();
  19. private GUIAnimFieldEntry[] fields;
  20. private GUIAnimFieldLayouts layouts;
  21. public GUIAnimFieldDisplay(GUILayout layout, int width, int height, SceneObject root)
  22. {
  23. this.root = root;
  24. scrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
  25. layout.AddElement(scrollArea);
  26. SetSize(width, height);
  27. }
  28. public Action<string> OnEntrySelected;
  29. public void SetSize(int width, int height)
  30. {
  31. this.width = width;
  32. this.height = height;
  33. scrollArea.SetWidth(width);
  34. scrollArea.SetHeight(height);
  35. Rebuild();
  36. }
  37. public void SetFields(AnimFieldInfo[] fields)
  38. {
  39. this.fieldInfos.Clear();
  40. this.fieldInfos.AddRange(fields);
  41. Rebuild();
  42. }
  43. public void AddField(AnimFieldInfo field)
  44. {
  45. bool exists = fieldInfos.Exists(x =>
  46. {
  47. return x.path == field.path;
  48. });
  49. if (!exists)
  50. {
  51. fieldInfos.Add(field);
  52. Rebuild();
  53. }
  54. }
  55. public void SetDisplayValues(GUIAnimFieldPathValue[] values)
  56. {
  57. for (int i = 0; i < fields.Length; i++)
  58. {
  59. string path = fields[i].Path;
  60. for (int j = 0; j < values.Length; j++)
  61. {
  62. if(path == values[j].path)
  63. fields[i].SetValue(values[j].value);
  64. }
  65. }
  66. }
  67. public void SetSelection(string[] paths)
  68. {
  69. Action<GUIAnimFieldEntry> updateSelection = field =>
  70. {
  71. bool foundSelected = false;
  72. for (int j = 0; j < paths.Length; j++)
  73. {
  74. if (field.Path == paths[j])
  75. {
  76. field.SetSelection(true);
  77. foundSelected = true;
  78. break;
  79. }
  80. }
  81. if (!foundSelected)
  82. field.SetSelection(false);
  83. };
  84. for (int i = 0; i < fields.Length; i++)
  85. {
  86. updateSelection(fields[i]);
  87. // Check children (only one level allowed)
  88. GUIAnimFieldEntry[] children = fields[i].GetChildren();
  89. if (children == null)
  90. continue;
  91. for (int j = 0; j < children.Length; j++)
  92. updateSelection(children[j]);
  93. }
  94. }
  95. private void Rebuild()
  96. {
  97. scrollArea.Layout.Clear();
  98. fields = null;
  99. if (fieldInfos == null || root == null)
  100. return;
  101. layouts = new GUIAnimFieldLayouts();
  102. GUIPanel rootPanel = scrollArea.Layout.AddPanel();
  103. GUIPanel mainPanel = rootPanel.AddPanel();
  104. GUIPanel underlayPanel = rootPanel.AddPanel(1);
  105. GUIPanel overlayPanel = rootPanel.AddPanel(-1);
  106. GUIPanel backgroundPanel = rootPanel.AddPanel(2);
  107. layouts.main = mainPanel.AddLayoutY();
  108. layouts.underlay = underlayPanel.AddLayoutY();
  109. layouts.overlay = overlayPanel.AddLayoutY();
  110. layouts.background = backgroundPanel.AddLayoutY();
  111. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  112. catchAll.Bounds = new Rect2I(0, 0, width, height);
  113. catchAll.OnClick += () => OnEntrySelected(null);
  114. underlayPanel.AddElement(catchAll);
  115. layouts.main.AddSpace(5);
  116. layouts.underlay.AddSpace(5);
  117. layouts.overlay.AddSpace(5);
  118. layouts.background.AddSpace(3); // Minor hack: Background starts heigher to get it to center better
  119. fields = new GUIAnimFieldEntry[fieldInfos.Count];
  120. for (int i = 0; i < fieldInfos.Count; i++)
  121. {
  122. if (string.IsNullOrEmpty(fieldInfos[i].path))
  123. continue;
  124. bool entryIsMissing;
  125. if (fieldInfos[i].isUserCurve)
  126. {
  127. string pathSuffix;
  128. SerializableProperty property = Animation.FindProperty(root, fieldInfos[i].path, out pathSuffix);
  129. entryIsMissing = property == null;
  130. }
  131. else
  132. entryIsMissing = false;
  133. if (!entryIsMissing)
  134. {
  135. Color[] colors = new Color[fieldInfos[i].curveGroup.curveInfos.Length];
  136. for (int j = 0; j < fieldInfos[i].curveGroup.curveInfos.Length; j++)
  137. colors[j] = fieldInfos[i].curveGroup.curveInfos[j].color;
  138. switch (fieldInfos[i].curveGroup.type)
  139. {
  140. case SerializableProperty.FieldType.Vector2:
  141. fields[i] = new GUIAnimVec2Entry(layouts, fieldInfos[i].path, colors);
  142. break;
  143. case SerializableProperty.FieldType.Vector3:
  144. fields[i] = new GUIAnimVec3Entry(layouts, fieldInfos[i].path, colors);
  145. break;
  146. case SerializableProperty.FieldType.Vector4:
  147. fields[i] = new GUIAnimVec4Entry(layouts, fieldInfos[i].path, colors);
  148. break;
  149. case SerializableProperty.FieldType.Color:
  150. fields[i] = new GUIAnimColorEntry(layouts, fieldInfos[i].path, colors);
  151. break;
  152. case SerializableProperty.FieldType.Bool:
  153. case SerializableProperty.FieldType.Int:
  154. case SerializableProperty.FieldType.Float:
  155. {
  156. Color color;
  157. if (colors.Length > 0)
  158. color = colors[0];
  159. else
  160. color = Color.White;
  161. fields[i] = new GUIAnimSimpleEntry(layouts, fieldInfos[i].path, color);
  162. break;
  163. }
  164. }
  165. }
  166. else
  167. {
  168. fields[i] = new GUIAnimMissingEntry(layouts, fieldInfos[i].path);
  169. }
  170. if (fields[i] != null)
  171. fields[i].OnEntrySelected += OnEntrySelected;
  172. }
  173. layouts.main.AddSpace(5);
  174. layouts.underlay.AddSpace(5);
  175. layouts.overlay.AddSpace(5);
  176. layouts.background.AddSpace(5);
  177. layouts.main.AddFlexibleSpace();
  178. layouts.underlay.AddFlexibleSpace();
  179. layouts.overlay.AddFlexibleSpace();
  180. layouts.background.AddFlexibleSpace();
  181. }
  182. }
  183. internal class GUIAnimFieldLayouts
  184. {
  185. public GUILayout main;
  186. public GUILayout underlay;
  187. public GUILayout overlay;
  188. public GUILayout background;
  189. }
  190. internal struct GUIAnimFieldPathValue
  191. {
  192. public string path;
  193. public object value;
  194. }
  195. internal abstract class GUIAnimFieldEntry
  196. {
  197. private const int MAX_PATH_LENGTH = 30;
  198. protected const int INDENT_AMOUNT = 10;
  199. protected string path;
  200. private GUIButton selectionBtn;
  201. private GUITexture backgroundTexture;
  202. private int entryHeight;
  203. public Action<string> OnEntrySelected;
  204. public string Path { get { return path; } }
  205. public GUIAnimFieldEntry(GUIAnimFieldLayouts layouts, string path, bool child)
  206. {
  207. this.path = path;
  208. GUILayoutX toggleLayout = layouts.main.AddLayoutX();
  209. toggleLayout.AddSpace(child ? 45 : 30);
  210. selectionBtn = new GUIButton(GetDisplayName(path, child), EditorStyles.Label, GUIOption.FlexibleWidth());
  211. selectionBtn.OnClick += () =>
  212. {
  213. OnEntrySelected?.Invoke(path);
  214. };
  215. toggleLayout.AddElement(selectionBtn);
  216. entryHeight = selectionBtn.Bounds.height;
  217. backgroundTexture = new GUITexture(Builtin.WhiteTexture, GUITextureScaleMode.StretchToFit,
  218. GUIOption.FlexibleWidth());
  219. backgroundTexture.SetTint(Color.Transparent);
  220. backgroundTexture.SetHeight(entryHeight);
  221. layouts.background.AddElement(backgroundTexture);
  222. }
  223. public virtual void Toggle(bool on)
  224. {
  225. selectionBtn.Active = on;
  226. backgroundTexture.Active = on;
  227. }
  228. public void SetSelection(bool selected)
  229. {
  230. if(selected)
  231. backgroundTexture.SetTint(Color.DarkCyan);
  232. else
  233. backgroundTexture.SetTint(Color.Transparent);
  234. }
  235. public virtual void SetValue(object value) { }
  236. public virtual GUIAnimFieldEntry[] GetChildren()
  237. {
  238. return null;
  239. }
  240. protected int GetEntryHeight()
  241. {
  242. return entryHeight;
  243. }
  244. protected static string GetDisplayName(string path, bool shortName)
  245. {
  246. if (string.IsNullOrEmpty(path))
  247. return "";
  248. string soName;
  249. string compName;
  250. string propertyPath;
  251. string trimmedPath = path.Trim('/');
  252. GetNames(trimmedPath, shortName, out soName, out compName, out propertyPath);
  253. if (propertyPath == null)
  254. return "";
  255. if (shortName)
  256. return propertyPath;
  257. else
  258. {
  259. string truncatedPropPath;
  260. if (propertyPath.Length > MAX_PATH_LENGTH)
  261. truncatedPropPath = "..." + propertyPath.Substring(propertyPath.Length - MAX_PATH_LENGTH);
  262. else
  263. truncatedPropPath = propertyPath;
  264. if (soName != null)
  265. {
  266. if (compName != null)
  267. return soName + "(" + compName + ") - " + truncatedPropPath;
  268. else
  269. return soName + " - " + truncatedPropPath;
  270. }
  271. else
  272. {
  273. if (compName != null)
  274. return "(" + compName + ") - " + truncatedPropPath;
  275. else
  276. return truncatedPropPath;
  277. }
  278. }
  279. }
  280. protected static void GetNames(string path, bool shortName, out string soName, out string compName, out string propertyPath)
  281. {
  282. string[] entries = path.Split('/');
  283. // Find name of the last scene object in the path
  284. int pathIdx = 0;
  285. soName = null;
  286. for (; pathIdx < entries.Length; pathIdx++)
  287. {
  288. string entry = entries[pathIdx];
  289. if (string.IsNullOrEmpty(entry))
  290. continue;
  291. // Not a scene object, break
  292. if (entry[0] != '!')
  293. {
  294. if (pathIdx > 0)
  295. {
  296. string prevEntry = entries[pathIdx - 1];
  297. soName = prevEntry.Substring(1, prevEntry.Length - 1);
  298. }
  299. break;
  300. }
  301. }
  302. if (pathIdx >= entries.Length)
  303. {
  304. compName = null;
  305. propertyPath = null;
  306. return;
  307. }
  308. // If path is referencing a component, find it
  309. {
  310. string entry = entries[pathIdx];
  311. if (entry[0] == ':')
  312. compName = entry.Substring(1, entry.Length - 1);
  313. else
  314. compName = null;
  315. }
  316. // Look for a field name
  317. if (compName != null)
  318. {
  319. pathIdx++;
  320. if (pathIdx >= entries.Length)
  321. {
  322. propertyPath = null;
  323. return;
  324. }
  325. }
  326. if (shortName)
  327. {
  328. if (pathIdx < entries.Length)
  329. propertyPath = entries[entries.Length - 1];
  330. else
  331. propertyPath = null;
  332. }
  333. else
  334. {
  335. StringBuilder pathBuilder = new StringBuilder();
  336. for (; pathIdx < entries.Length - 1; pathIdx++)
  337. pathBuilder.Append(entries[pathIdx] + "/");
  338. if (pathIdx < entries.Length)
  339. pathBuilder.Append(entries[pathIdx]);
  340. propertyPath = pathBuilder.ToString();
  341. }
  342. }
  343. }
  344. internal class GUIAnimSimpleEntry : GUIAnimFieldEntry
  345. {
  346. private GUILabel valueDisplay;
  347. private GUILayoutX underlayLayout;
  348. private GUILabel overlaySpacing;
  349. public GUIAnimSimpleEntry(GUIAnimFieldLayouts layouts, string path, Color color, bool child = false)
  350. : base(layouts, path, child)
  351. {
  352. valueDisplay = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  353. underlayLayout = layouts.underlay.AddLayoutX();
  354. underlayLayout.AddSpace(child ? 30 : 15);
  355. GUITexture colorSquare = new GUITexture(Builtin.WhiteTexture,
  356. GUIOption.FixedWidth(10), GUIOption.FixedHeight(10));
  357. colorSquare.SetTint(color);
  358. underlayLayout.AddElement(colorSquare);
  359. underlayLayout.AddFlexibleSpace();
  360. underlayLayout.AddElement(valueDisplay);
  361. underlayLayout.AddSpace(50);
  362. overlaySpacing = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  363. layouts.overlay.AddElement(overlaySpacing);
  364. }
  365. public override void Toggle(bool on)
  366. {
  367. underlayLayout.Active = on;
  368. overlaySpacing.Active = on;
  369. base.Toggle(on);
  370. }
  371. public override void SetValue(object value)
  372. {
  373. if (value == null)
  374. return;
  375. string strValue = value.ToString();
  376. valueDisplay.SetContent(strValue);
  377. }
  378. }
  379. internal class GUIAnimComplexEntry : GUIAnimFieldEntry
  380. {
  381. private GUILayout foldoutLayout;
  382. private GUIToggle foldout;
  383. private GUILabel underlaySpacing;
  384. protected GUIAnimSimpleEntry[] children;
  385. public GUIAnimComplexEntry(GUIAnimFieldLayouts layouts, string path, string[] childEntries, Color[] colors)
  386. : base(layouts, path, false)
  387. {
  388. foldout = new GUIToggle("", EditorStyles.Expand);
  389. foldout.OnToggled += Toggle;
  390. GUILabel spacer = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  391. foldoutLayout = layouts.overlay.AddLayoutX();
  392. foldoutLayout.AddElement(foldout);
  393. foldoutLayout.AddElement(spacer);
  394. foldoutLayout.AddFlexibleSpace();
  395. underlaySpacing = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  396. layouts.underlay.AddElement(underlaySpacing);
  397. children = new GUIAnimSimpleEntry[childEntries.Length];
  398. for (int i = 0; i < childEntries.Length; i++)
  399. {
  400. Color color;
  401. if (i < colors.Length)
  402. color = colors[i];
  403. else
  404. color = Color.White;
  405. children[i] = new GUIAnimSimpleEntry(layouts, path + childEntries[i], color, true);
  406. children[i].OnEntrySelected += x => { OnEntrySelected?.Invoke(x); };
  407. }
  408. Toggle(false);
  409. }
  410. public override void Toggle(bool on)
  411. {
  412. foreach(var child in children)
  413. child.Toggle(on);
  414. }
  415. public override GUIAnimFieldEntry[] GetChildren()
  416. {
  417. return children;
  418. }
  419. }
  420. internal class GUIAnimVec2Entry : GUIAnimComplexEntry
  421. {
  422. public GUIAnimVec2Entry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  423. : base(layouts, path, new[] { ".x", ".y" }, colors)
  424. { }
  425. public override void SetValue(object value)
  426. {
  427. if (value == null)
  428. return;
  429. Vector2 vector = (Vector2)value;
  430. children[0].SetValue(vector.x);
  431. children[1].SetValue(vector.y);
  432. }
  433. }
  434. internal class GUIAnimVec3Entry : GUIAnimComplexEntry
  435. {
  436. public GUIAnimVec3Entry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  437. : base(layouts, path, new[] { ".x", ".y", ".z" }, colors)
  438. { }
  439. public override void SetValue(object value)
  440. {
  441. if (value == null)
  442. return;
  443. Vector3 vector = (Vector3)value;
  444. children[0].SetValue(vector.x);
  445. children[1].SetValue(vector.y);
  446. children[2].SetValue(vector.z);
  447. }
  448. }
  449. internal class GUIAnimVec4Entry : GUIAnimComplexEntry
  450. {
  451. public GUIAnimVec4Entry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  452. : base(layouts, path, new[] { ".x", ".y", ".z", ".w" }, colors)
  453. { }
  454. public override void SetValue(object value)
  455. {
  456. if (value == null)
  457. return;
  458. Vector4 vector = (Vector4)value;
  459. children[0].SetValue(vector.x);
  460. children[1].SetValue(vector.y);
  461. children[2].SetValue(vector.z);
  462. children[3].SetValue(vector.w);
  463. }
  464. }
  465. internal class GUIAnimColorEntry : GUIAnimComplexEntry
  466. {
  467. public GUIAnimColorEntry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  468. : base(layouts, path, new[] { ".r", ".g", ".b", ".a" }, colors)
  469. { }
  470. public override void SetValue(object value)
  471. {
  472. if (value == null)
  473. return;
  474. Color color = (Color)value;
  475. children[0].SetValue(color.r);
  476. children[1].SetValue(color.g);
  477. children[2].SetValue(color.b);
  478. children[3].SetValue(color.a);
  479. }
  480. }
  481. internal class GUIAnimMissingEntry : GUIAnimFieldEntry
  482. {
  483. private GUILabel missingLabel;
  484. private GUILayoutX underlayLayout;
  485. private GUILabel overlaySpacing;
  486. public GUIAnimMissingEntry(GUIAnimFieldLayouts layouts, string path)
  487. : base(layouts, path, false)
  488. {
  489. missingLabel = new GUILabel("Missing property!", GUIOption.FixedHeight(GetEntryHeight()));
  490. underlayLayout = layouts.underlay.AddLayoutX();
  491. underlayLayout.AddFlexibleSpace();
  492. underlayLayout.AddElement(missingLabel);
  493. underlayLayout.AddSpace(50);
  494. overlaySpacing = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  495. layouts.overlay.AddElement(overlaySpacing);
  496. }
  497. public override void Toggle(bool on)
  498. {
  499. underlayLayout.Active = on;
  500. overlaySpacing.Active = on;
  501. base.Toggle(on);
  502. }
  503. }
  504. internal struct AnimFieldInfo
  505. {
  506. public AnimFieldInfo(string path, FieldAnimCurves curveGroup, bool isUserCurve)
  507. {
  508. this.path = path;
  509. this.curveGroup = curveGroup;
  510. this.isUserCurve = isUserCurve;
  511. }
  512. public string path;
  513. public FieldAnimCurves curveGroup;
  514. public bool isUserCurve;
  515. }
  516. /** @} */
  517. }