GUIAnimFieldDisplay.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. GUILabel header = new GUILabel(new LocEdString("Properties"), EditorStyles.Header);
  102. scrollArea.Layout.AddElement(header);
  103. layouts = new GUIAnimFieldLayouts();
  104. GUIPanel rootPanel = scrollArea.Layout.AddPanel();
  105. GUIPanel mainPanel = rootPanel.AddPanel();
  106. GUIPanel underlayPanel = rootPanel.AddPanel(1);
  107. GUIPanel overlayPanel = rootPanel.AddPanel(-1);
  108. GUIPanel backgroundPanel = rootPanel.AddPanel(2);
  109. layouts.main = mainPanel.AddLayoutY();
  110. layouts.underlay = underlayPanel.AddLayoutY();
  111. layouts.overlay = overlayPanel.AddLayoutY();
  112. layouts.background = backgroundPanel.AddLayoutY();
  113. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  114. catchAll.Bounds = new Rect2I(0, 0, width, height - header.Bounds.height);
  115. catchAll.OnClick += () => OnEntrySelected(null);
  116. underlayPanel.AddElement(catchAll);
  117. layouts.main.AddSpace(5);
  118. layouts.underlay.AddSpace(5);
  119. layouts.overlay.AddSpace(5);
  120. layouts.background.AddSpace(3); // Minor hack: Background starts heigher to get it to center better
  121. fields = new GUIAnimFieldEntry[fieldInfos.Count];
  122. for (int i = 0; i < fieldInfos.Count; i++)
  123. {
  124. if (string.IsNullOrEmpty(fieldInfos[i].path))
  125. continue;
  126. bool entryIsMissing;
  127. if (fieldInfos[i].isUserCurve)
  128. {
  129. string pathSuffix;
  130. SerializableProperty property = Animation.FindProperty(root, fieldInfos[i].path, out pathSuffix);
  131. entryIsMissing = property == null;
  132. }
  133. else
  134. entryIsMissing = false;
  135. if (!entryIsMissing)
  136. {
  137. Color[] colors = new Color[fieldInfos[i].curveGroup.curveInfos.Length];
  138. for (int j = 0; j < fieldInfos[i].curveGroup.curveInfos.Length; j++)
  139. colors[j] = fieldInfos[i].curveGroup.curveInfos[j].color;
  140. switch (fieldInfos[i].curveGroup.type)
  141. {
  142. case SerializableProperty.FieldType.Vector2:
  143. fields[i] = new GUIAnimVec2Entry(layouts, fieldInfos[i].path, colors);
  144. break;
  145. case SerializableProperty.FieldType.Vector3:
  146. fields[i] = new GUIAnimVec3Entry(layouts, fieldInfos[i].path, colors);
  147. break;
  148. case SerializableProperty.FieldType.Vector4:
  149. fields[i] = new GUIAnimVec4Entry(layouts, fieldInfos[i].path, colors);
  150. break;
  151. case SerializableProperty.FieldType.Color:
  152. fields[i] = new GUIAnimColorEntry(layouts, fieldInfos[i].path, colors);
  153. break;
  154. case SerializableProperty.FieldType.Bool:
  155. case SerializableProperty.FieldType.Int:
  156. case SerializableProperty.FieldType.Float:
  157. {
  158. Color color;
  159. if (colors.Length > 0)
  160. color = colors[0];
  161. else
  162. color = Color.White;
  163. fields[i] = new GUIAnimSimpleEntry(layouts, fieldInfos[i].path, color);
  164. break;
  165. }
  166. }
  167. }
  168. else
  169. {
  170. fields[i] = new GUIAnimMissingEntry(layouts, fieldInfos[i].path);
  171. }
  172. if (fields[i] != null)
  173. fields[i].OnEntrySelected += OnEntrySelected;
  174. }
  175. if (fieldInfos.Count == 0)
  176. {
  177. GUILabel warningLbl = new GUILabel(new LocEdString("No properties. Add a new property to begin animating."));
  178. GUILayoutY vertLayout = layouts.main.AddLayoutY();
  179. vertLayout.AddFlexibleSpace();
  180. GUILayoutX horzLayout = vertLayout.AddLayoutX();
  181. vertLayout.AddFlexibleSpace();
  182. horzLayout.AddFlexibleSpace();
  183. horzLayout.AddElement(warningLbl);
  184. horzLayout.AddFlexibleSpace();
  185. }
  186. layouts.main.AddSpace(5);
  187. layouts.underlay.AddSpace(5);
  188. layouts.overlay.AddSpace(5);
  189. layouts.background.AddSpace(5);
  190. layouts.main.AddFlexibleSpace();
  191. layouts.underlay.AddFlexibleSpace();
  192. layouts.overlay.AddFlexibleSpace();
  193. layouts.background.AddFlexibleSpace();
  194. }
  195. }
  196. internal class GUIAnimFieldLayouts
  197. {
  198. public GUILayout main;
  199. public GUILayout underlay;
  200. public GUILayout overlay;
  201. public GUILayout background;
  202. }
  203. internal struct GUIAnimFieldPathValue
  204. {
  205. public string path;
  206. public object value;
  207. }
  208. internal abstract class GUIAnimFieldEntry
  209. {
  210. private const int MAX_PATH_LENGTH = 30;
  211. protected const int INDENT_AMOUNT = 10;
  212. protected string path;
  213. private GUIButton selectionBtn;
  214. private GUITexture backgroundTexture;
  215. private int entryHeight;
  216. public Action<string> OnEntrySelected;
  217. public string Path { get { return path; } }
  218. public GUIAnimFieldEntry(GUIAnimFieldLayouts layouts, string path, bool child, int indentAmount)
  219. {
  220. this.path = path;
  221. GUILayoutX toggleLayout = layouts.main.AddLayoutX();
  222. toggleLayout.AddSpace(indentAmount);
  223. selectionBtn = new GUIButton(GetDisplayName(path, child), EditorStyles.Label, GUIOption.FlexibleWidth());
  224. selectionBtn.OnClick += () =>
  225. {
  226. OnEntrySelected?.Invoke(path);
  227. };
  228. toggleLayout.AddElement(selectionBtn);
  229. entryHeight = selectionBtn.Bounds.height;
  230. backgroundTexture = new GUITexture(Builtin.WhiteTexture, GUITextureScaleMode.StretchToFit,
  231. GUIOption.FlexibleWidth());
  232. backgroundTexture.SetTint(Color.Transparent);
  233. backgroundTexture.SetHeight(entryHeight);
  234. layouts.background.AddElement(backgroundTexture);
  235. }
  236. public virtual void Toggle(bool on)
  237. {
  238. selectionBtn.Active = on;
  239. backgroundTexture.Active = on;
  240. }
  241. public void SetSelection(bool selected)
  242. {
  243. if(selected)
  244. backgroundTexture.SetTint(Color.DarkCyan);
  245. else
  246. backgroundTexture.SetTint(Color.Transparent);
  247. }
  248. public virtual void SetValue(object value) { }
  249. public virtual GUIAnimFieldEntry[] GetChildren()
  250. {
  251. return null;
  252. }
  253. protected int GetEntryHeight()
  254. {
  255. return entryHeight;
  256. }
  257. protected static string GetDisplayName(string path, bool shortName)
  258. {
  259. if (string.IsNullOrEmpty(path))
  260. return "";
  261. string soName;
  262. string compName;
  263. string propertyPath;
  264. string trimmedPath = path.Trim('/');
  265. GetNames(trimmedPath, shortName, out soName, out compName, out propertyPath);
  266. if (propertyPath == null)
  267. return "";
  268. if (shortName)
  269. return propertyPath;
  270. else
  271. {
  272. string truncatedPropPath;
  273. if (propertyPath.Length > MAX_PATH_LENGTH)
  274. truncatedPropPath = "..." + propertyPath.Substring(propertyPath.Length - MAX_PATH_LENGTH);
  275. else
  276. truncatedPropPath = propertyPath;
  277. if (soName != null)
  278. {
  279. if (compName != null)
  280. return soName + "(" + compName + ") - " + truncatedPropPath;
  281. else
  282. return soName + " - " + truncatedPropPath;
  283. }
  284. else
  285. {
  286. if (compName != null)
  287. return "(" + compName + ") - " + truncatedPropPath;
  288. else
  289. return truncatedPropPath;
  290. }
  291. }
  292. }
  293. protected static void GetNames(string path, bool shortName, out string soName, out string compName, out string propertyPath)
  294. {
  295. string[] entries = path.Split('/');
  296. // Find name of the last scene object in the path
  297. int pathIdx = 0;
  298. soName = null;
  299. for (; pathIdx < entries.Length; pathIdx++)
  300. {
  301. string entry = entries[pathIdx];
  302. if (string.IsNullOrEmpty(entry))
  303. continue;
  304. // Not a scene object, break
  305. if (entry[0] != '!')
  306. {
  307. if (pathIdx > 0)
  308. {
  309. string prevEntry = entries[pathIdx - 1];
  310. soName = prevEntry.Substring(1, prevEntry.Length - 1);
  311. }
  312. break;
  313. }
  314. }
  315. if (pathIdx >= entries.Length)
  316. {
  317. compName = null;
  318. propertyPath = null;
  319. return;
  320. }
  321. // If path is referencing a component, find it
  322. {
  323. string entry = entries[pathIdx];
  324. if (entry[0] == ':')
  325. compName = entry.Substring(1, entry.Length - 1);
  326. else
  327. compName = null;
  328. }
  329. // Look for a field name
  330. if (compName != null)
  331. {
  332. pathIdx++;
  333. if (pathIdx >= entries.Length)
  334. {
  335. propertyPath = null;
  336. return;
  337. }
  338. }
  339. if (shortName)
  340. {
  341. if (pathIdx < entries.Length)
  342. propertyPath = entries[entries.Length - 1];
  343. else
  344. propertyPath = null;
  345. }
  346. else
  347. {
  348. StringBuilder pathBuilder = new StringBuilder();
  349. for (; pathIdx < entries.Length - 1; pathIdx++)
  350. pathBuilder.Append(entries[pathIdx] + "/");
  351. if (pathIdx < entries.Length)
  352. pathBuilder.Append(entries[pathIdx]);
  353. propertyPath = pathBuilder.ToString();
  354. }
  355. }
  356. }
  357. internal class GUIAnimSimpleEntry : GUIAnimFieldEntry
  358. {
  359. private GUILabel valueDisplay;
  360. private GUILayoutX underlayLayout;
  361. private GUILabel overlaySpacing;
  362. public GUIAnimSimpleEntry(GUIAnimFieldLayouts layouts, string path, Color color, bool child = false)
  363. : base(layouts, path, child, child ? 45 : 30)
  364. {
  365. valueDisplay = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  366. underlayLayout = layouts.underlay.AddLayoutX();
  367. underlayLayout.AddSpace(child ? 30 : 15);
  368. GUITexture colorSquare = new GUITexture(Builtin.WhiteTexture,
  369. GUIOption.FixedWidth(10), GUIOption.FixedHeight(10));
  370. colorSquare.SetTint(color);
  371. underlayLayout.AddElement(colorSquare);
  372. underlayLayout.AddFlexibleSpace();
  373. underlayLayout.AddElement(valueDisplay);
  374. underlayLayout.AddSpace(50);
  375. overlaySpacing = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  376. layouts.overlay.AddElement(overlaySpacing);
  377. }
  378. public override void Toggle(bool on)
  379. {
  380. underlayLayout.Active = on;
  381. overlaySpacing.Active = on;
  382. base.Toggle(on);
  383. }
  384. public override void SetValue(object value)
  385. {
  386. if (value == null)
  387. return;
  388. string strValue = value.ToString();
  389. valueDisplay.SetContent(strValue);
  390. }
  391. }
  392. internal class GUIAnimComplexEntry : GUIAnimFieldEntry
  393. {
  394. private GUILayout foldoutLayout;
  395. private GUIToggle foldout;
  396. private GUILabel underlaySpacing;
  397. protected GUIAnimSimpleEntry[] children;
  398. public GUIAnimComplexEntry(GUIAnimFieldLayouts layouts, string path, string[] childEntries, Color[] colors)
  399. : base(layouts, path, false, 20)
  400. {
  401. foldout = new GUIToggle("", EditorStyles.Expand);
  402. foldout.OnToggled += Toggle;
  403. GUILabel spacer = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  404. foldoutLayout = layouts.overlay.AddLayoutX();
  405. foldoutLayout.AddSpace(5);
  406. foldoutLayout.AddElement(foldout);
  407. foldoutLayout.AddElement(spacer);
  408. foldoutLayout.AddFlexibleSpace();
  409. underlaySpacing = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  410. layouts.underlay.AddElement(underlaySpacing);
  411. children = new GUIAnimSimpleEntry[childEntries.Length];
  412. for (int i = 0; i < childEntries.Length; i++)
  413. {
  414. Color color;
  415. if (i < colors.Length)
  416. color = colors[i];
  417. else
  418. color = Color.White;
  419. children[i] = new GUIAnimSimpleEntry(layouts, path + childEntries[i], color, true);
  420. children[i].OnEntrySelected += x => { OnEntrySelected?.Invoke(x); };
  421. }
  422. Toggle(false);
  423. }
  424. public override void Toggle(bool on)
  425. {
  426. foreach(var child in children)
  427. child.Toggle(on);
  428. }
  429. public override GUIAnimFieldEntry[] GetChildren()
  430. {
  431. return children;
  432. }
  433. }
  434. internal class GUIAnimVec2Entry : GUIAnimComplexEntry
  435. {
  436. public GUIAnimVec2Entry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  437. : base(layouts, path, new[] { ".x", ".y" }, colors)
  438. { }
  439. public override void SetValue(object value)
  440. {
  441. if (value == null)
  442. return;
  443. Vector2 vector = (Vector2)value;
  444. children[0].SetValue(vector.x);
  445. children[1].SetValue(vector.y);
  446. }
  447. }
  448. internal class GUIAnimVec3Entry : GUIAnimComplexEntry
  449. {
  450. public GUIAnimVec3Entry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  451. : base(layouts, path, new[] { ".x", ".y", ".z" }, colors)
  452. { }
  453. public override void SetValue(object value)
  454. {
  455. if (value == null)
  456. return;
  457. Vector3 vector = (Vector3)value;
  458. children[0].SetValue(vector.x);
  459. children[1].SetValue(vector.y);
  460. children[2].SetValue(vector.z);
  461. }
  462. }
  463. internal class GUIAnimVec4Entry : GUIAnimComplexEntry
  464. {
  465. public GUIAnimVec4Entry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  466. : base(layouts, path, new[] { ".x", ".y", ".z", ".w" }, colors)
  467. { }
  468. public override void SetValue(object value)
  469. {
  470. if (value == null)
  471. return;
  472. Vector4 vector = (Vector4)value;
  473. children[0].SetValue(vector.x);
  474. children[1].SetValue(vector.y);
  475. children[2].SetValue(vector.z);
  476. children[3].SetValue(vector.w);
  477. }
  478. }
  479. internal class GUIAnimColorEntry : GUIAnimComplexEntry
  480. {
  481. public GUIAnimColorEntry(GUIAnimFieldLayouts layouts, string path, Color[] colors)
  482. : base(layouts, path, new[] { ".r", ".g", ".b", ".a" }, colors)
  483. { }
  484. public override void SetValue(object value)
  485. {
  486. if (value == null)
  487. return;
  488. Color color = (Color)value;
  489. children[0].SetValue(color.r);
  490. children[1].SetValue(color.g);
  491. children[2].SetValue(color.b);
  492. children[3].SetValue(color.a);
  493. }
  494. }
  495. internal class GUIAnimMissingEntry : GUIAnimFieldEntry
  496. {
  497. private GUILabel missingLabel;
  498. private GUILayoutX underlayLayout;
  499. private GUILabel overlaySpacing;
  500. public GUIAnimMissingEntry(GUIAnimFieldLayouts layouts, string path)
  501. : base(layouts, path, false, 15)
  502. {
  503. missingLabel = new GUILabel("Missing property!", GUIOption.FixedHeight(GetEntryHeight()));
  504. underlayLayout = layouts.underlay.AddLayoutX();
  505. underlayLayout.AddFlexibleSpace();
  506. underlayLayout.AddElement(missingLabel);
  507. underlayLayout.AddSpace(50);
  508. overlaySpacing = new GUILabel("", GUIOption.FixedHeight(GetEntryHeight()));
  509. layouts.overlay.AddElement(overlaySpacing);
  510. }
  511. public override void Toggle(bool on)
  512. {
  513. underlayLayout.Active = on;
  514. overlaySpacing.Active = on;
  515. base.Toggle(on);
  516. }
  517. }
  518. internal struct AnimFieldInfo
  519. {
  520. public AnimFieldInfo(string path, FieldAnimCurves curveGroup, bool isUserCurve)
  521. {
  522. this.path = path;
  523. this.curveGroup = curveGroup;
  524. this.isUserCurve = isUserCurve;
  525. }
  526. public string path;
  527. public FieldAnimCurves curveGroup;
  528. public bool isUserCurve;
  529. }
  530. /** @} */
  531. }