GUIAnimFieldDisplay.cs 19 KB

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