GUIFieldSelector.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup AnimationEditor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Renders GUI elements that display a Scene Object, its transform, components and child objects, as well as all of
  13. /// their fields. User can then select one of the fields and the class will output a path to the selected field, as
  14. /// well as its parent scene object and component.
  15. /// </summary>
  16. public class GUIFieldSelector
  17. {
  18. private const int INDENT_AMOUNT = 5;
  19. private const int PADDING = 5;
  20. private GUIScrollArea scrollArea;
  21. private int foldoutWidth;
  22. private SceneObject rootSO;
  23. private Element rootElement;
  24. /// <summary>
  25. /// Stores a single entry in the field hierarchy.
  26. /// </summary>
  27. private struct Element
  28. {
  29. public Element(SceneObject so, Component comp, string path)
  30. {
  31. this.so = so;
  32. this.comp = comp;
  33. this.path = path;
  34. toggle = null;
  35. childLayout = null;
  36. indentLayout = null;
  37. children = null;
  38. }
  39. public SceneObject so;
  40. public Component comp;
  41. public string path;
  42. public GUIToggle toggle;
  43. public GUILayout childLayout;
  44. public GUILayout indentLayout;
  45. public Element[] children;
  46. }
  47. /// <summary>
  48. /// Triggers when the user selects a field. The subscriber will be receive a scene object the field is part of,
  49. /// component the field is part of, and a path to the field, each entry separated by "/". Component can be null
  50. /// in which case it is assumed the field is part of the SceneObject itself. Scene object names are always prefixed
  51. /// with "!", components are always prefixed with ":", while field entries have no prefix.
  52. ///
  53. /// For example: !My Scene Object/:Camera/path/to/field
  54. /// </summary>
  55. public Action<SceneObject, Component, string, SerializableProperty.FieldType> OnElementSelected;
  56. /// <summary>
  57. /// Creates a new GUIFieldSelector and registers its GUI elements in the provided layout.
  58. /// </summary>
  59. /// <param name="layout">Layout into which to add the selector GUI hierarchy.</param>
  60. /// <param name="so">Scene object to inspect the fields for.</param>
  61. /// <param name="width">Width of the selector area, in pixels.</param>
  62. /// <param name="height">Height of the selector area, in pixels.</param>
  63. public GUIFieldSelector(GUILayout layout, SceneObject so, int width, int height)
  64. {
  65. rootSO = so;
  66. scrollArea = new GUIScrollArea();
  67. scrollArea.SetWidth(width);
  68. scrollArea.SetHeight(height);
  69. layout.AddElement(scrollArea);
  70. GUISkin skin = EditorBuiltin.GUISkin;
  71. GUIElementStyle style = skin.GetStyle(EditorStyles.Expand);
  72. foldoutWidth = style.Width;
  73. Rebuild();
  74. }
  75. /// <summary>
  76. /// Rebuilds all of the selection GUI.
  77. /// </summary>
  78. private void Rebuild()
  79. {
  80. scrollArea.Layout.Clear();
  81. rootElement = new Element();
  82. if (rootSO == null)
  83. return;
  84. rootElement.so = rootSO;
  85. rootElement.childLayout = scrollArea.Layout;
  86. rootElement.indentLayout = null;
  87. GUILabel header = new GUILabel(new LocEdString("Select a property"), EditorStyles.Header);
  88. scrollArea.Layout.AddElement(header);
  89. scrollArea.Layout.AddSpace(5);
  90. AddSceneObjectRows(rootElement);
  91. scrollArea.Layout.AddSpace(5);
  92. scrollArea.Layout.AddFlexibleSpace();
  93. }
  94. /// <summary>
  95. /// Registers a set of rows for all components in a <see cref="SceneObject"/>, as well as its transform and
  96. /// child objects.
  97. /// </summary>
  98. /// <param name="parent">Row element under which to create the new rows.</param>
  99. private void AddSceneObjectRows(Element parent)
  100. {
  101. string soName = "!" + parent.so.Name;
  102. Component[] components = parent.so.GetComponents();
  103. parent.children = new Element[components.Length + 2];
  104. SpriteTexture soIcon = EditorBuiltin.GetEditorIcon(EditorIcon.SceneObject);
  105. SpriteTexture compIcon = EditorBuiltin.GetEditorIcon(EditorIcon.Component);
  106. // Add transform
  107. parent.children[0] = AddFoldoutRow(parent.childLayout, soIcon, "Transform", parent.so,
  108. null, parent.path + "/" + soName, ToggleTransformFoldout);
  109. // Add components
  110. for (int i = 0; i < components.Length; i++)
  111. {
  112. Component childComponent = components[i];
  113. Action<Element, bool> toggleCallback =
  114. (toggleParent, expand) =>
  115. {
  116. SerializableObject componentObject = new SerializableObject(childComponent.GetType(), childComponent);
  117. ToggleObjectFoldout(toggleParent, componentObject, expand);
  118. };
  119. string name = childComponent.GetType().Name;
  120. string path = parent.path + "/" + soName + "/:" + name;
  121. parent.children[i + 1] = AddFoldoutRow(parent.childLayout, compIcon, name, parent.so, childComponent, path,
  122. toggleCallback);
  123. }
  124. // Add children
  125. if (parent.so.GetNumChildren() > 0)
  126. {
  127. parent.children[parent.children.Length - 1] = AddFoldoutRow(parent.childLayout, soIcon, "Children",
  128. parent.so, null, parent.path + "/" + soName, ToggleChildFoldout);
  129. }
  130. }
  131. /// <summary>
  132. /// Registers a new row in the layout for the provided property. The type of row is determined by the property type.
  133. /// </summary>
  134. /// <param name="parent">Parent foldout row to which to append the new elements.</param>
  135. /// <param name="name">Name of the field the property belongs to.</param>
  136. /// <param name="path">Slash separated path to the field from its parent object.</param>
  137. /// <param name="property">Property to create the row for.</param>
  138. /// <param name="element">Element containing data for the newly created row. Only valid if method returns true.
  139. /// </param>
  140. /// <returns>Returns true if the row was successfully added, false otherwise.</returns>
  141. private bool AddPropertyRow(Element parent, string name, string path, SerializableProperty property,
  142. out Element element)
  143. {
  144. switch (property.Type)
  145. {
  146. case SerializableProperty.FieldType.Bool:
  147. case SerializableProperty.FieldType.Float:
  148. case SerializableProperty.FieldType.Int:
  149. case SerializableProperty.FieldType.Color:
  150. case SerializableProperty.FieldType.Vector2:
  151. case SerializableProperty.FieldType.Vector3:
  152. case SerializableProperty.FieldType.Vector4:
  153. element = AddFieldRow(parent.childLayout, name, parent.so, parent.comp, path, property.Type);
  154. return true;
  155. case SerializableProperty.FieldType.Object:
  156. {
  157. Action<Element, bool> toggleCallback =
  158. (toggleParent, expand) =>
  159. {
  160. SerializableObject childObject = new SerializableObject(property.InternalType, null);
  161. ToggleObjectFoldout(toggleParent, childObject, expand);
  162. };
  163. element = AddFoldoutRow(parent.childLayout, null, name, parent.so, parent.comp, path, toggleCallback);
  164. }
  165. return true;
  166. case SerializableProperty.FieldType.Array:
  167. {
  168. Action<Element, bool> toggleCallback =
  169. (toggleParent, expand) =>
  170. {
  171. SerializableArray childObject = property.GetArray();
  172. if (childObject != null)
  173. ToggleArrayFoldout(toggleParent, childObject, expand);
  174. };
  175. element = AddFoldoutRow(parent.childLayout, null, name, parent.so, parent.comp, path, toggleCallback);
  176. }
  177. return true;
  178. case SerializableProperty.FieldType.List:
  179. {
  180. Action<Element, bool> toggleCallback =
  181. (toggleParent, expand) =>
  182. {
  183. SerializableList childObject = property.GetList();
  184. if (childObject != null)
  185. ToggleListFoldout(toggleParent, childObject, expand);
  186. };
  187. element = AddFoldoutRow(parent.childLayout, null, name, parent.so, parent.comp, path, toggleCallback);
  188. }
  189. return true;
  190. }
  191. element = new Element();
  192. return false;
  193. }
  194. /// <summary>
  195. /// Registers a set of rows for all child fields of the provided object.
  196. /// </summary>
  197. /// <param name="parent">Parent foldout row to which to append the new elements.</param>
  198. /// <param name="serializableObject">Type of the object whose fields to display.</param>
  199. private void AddObjectRows(Element parent, SerializableObject serializableObject)
  200. {
  201. List<Element> elements = new List<Element>();
  202. foreach (var field in serializableObject.Fields)
  203. {
  204. if (!field.Flags.HasFlag(SerializableFieldAttributes.Animable))
  205. continue;
  206. string propertyPath = parent.path + "/" + field.Name;
  207. Element element;
  208. if(AddPropertyRow(parent, field.Name, propertyPath, field.GetProperty(), out element))
  209. elements.Add(element);
  210. }
  211. // Handle special fields
  212. if (serializableObject.Type == typeof(Animation))
  213. {
  214. Animation anim = serializableObject.Object as Animation;
  215. MorphShapes morphShapes = anim?.SceneObject.GetComponent<Renderable>()?.Mesh.Value?.MorphShapes;
  216. if (morphShapes != null)
  217. {
  218. string propertyPath = parent.path + "/MorphShapes";
  219. Action<Element, bool> toggleCallback =
  220. (toggleParent, expand) =>
  221. {
  222. toggleParent.childLayout.Clear();
  223. toggleParent.children = null;
  224. toggleParent.indentLayout.Active = expand;
  225. if (expand)
  226. {
  227. List<Element> childElements = new List<Element>();
  228. MorphChannel[] channels = morphShapes.Channels;
  229. for (int i = 0; i < channels.Length; i++)
  230. {
  231. string channelName = channels[i].Name;
  232. string framePropertyPath = parent.path + "/MorphShapes/Frames/" + channelName;
  233. string weightPropertyPath = parent.path + "/MorphShapes/Weight/" + channelName;
  234. childElements.Add(AddFieldRow(toggleParent.childLayout, channelName + " (Frames)",
  235. toggleParent.so, toggleParent.comp, framePropertyPath,
  236. SerializableProperty.FieldType.Float));
  237. childElements.Add(AddFieldRow(toggleParent.childLayout, channelName + " (Weight)",
  238. toggleParent.so, toggleParent.comp, weightPropertyPath,
  239. SerializableProperty.FieldType.Float));
  240. }
  241. toggleParent.children = childElements.ToArray();
  242. }
  243. };
  244. elements.Add(AddFoldoutRow(parent.childLayout, null, "MorphShapes", parent.so, parent.comp,
  245. propertyPath, toggleCallback));
  246. }
  247. }
  248. parent.children = elements.ToArray();
  249. }
  250. /// <summary>
  251. /// Registers a set of rows for all entries of the provided array.
  252. /// </summary>
  253. /// <param name="parent">Parent foldout row to which to append the new elements.</param>
  254. /// <param name="serializableArray">Array whose fields to display.</param>
  255. private void AddArrayRows(Element parent, SerializableArray serializableArray)
  256. {
  257. List<Element> elements = new List<Element>();
  258. int length = serializableArray.GetLength();
  259. for (int i = 0; i < length; i++)
  260. {
  261. string name = "[" + i + "]";
  262. string propertyPath = parent.path + name;
  263. Element element;
  264. if (AddPropertyRow(parent, name, propertyPath, serializableArray.GetProperty(i), out element))
  265. elements.Add(element);
  266. }
  267. parent.children = elements.ToArray();
  268. }
  269. /// <summary>
  270. /// Registers a set of rows for all entries of the provided list.
  271. /// </summary>
  272. /// <param name="parent">Parent foldout row to which to append the new elements.</param>
  273. /// <param name="serializableList">List whose fields to display.</param>
  274. private void AddListRows(Element parent, SerializableList serializableList)
  275. {
  276. List<Element> elements = new List<Element>();
  277. int length = serializableList.GetLength();
  278. for (int i = 0; i < length; i++)
  279. {
  280. string name = "[" + i + "]";
  281. string propertyPath = parent.path + name;
  282. Element element;
  283. if (AddPropertyRow(parent, name, propertyPath, serializableList.GetProperty(i), out element))
  284. elements.Add(element);
  285. }
  286. parent.children = elements.ToArray();
  287. }
  288. /// <summary>
  289. /// Registers a new row in the layout. The row cannot have any further children and can be selected as the output
  290. /// field.
  291. /// </summary>
  292. /// <param name="layout">Layout to append the row GUI elements to.</param>
  293. /// <param name="name">Name of the field.</param>
  294. /// <param name="so">Parent scene object of the field.</param>
  295. /// <param name="component">Parent component of the field. Can be null if field belongs to <see cref="SceneObject"/>.
  296. /// </param>
  297. /// <param name="path">Slash separated path to the field from its parent object.</param>
  298. /// <param name="type">Data type stored in the field.</param>
  299. /// <returns>Element object storing all information about the added field.</returns>
  300. private Element AddFieldRow(GUILayout layout, string name, SceneObject so, Component component, string path, SerializableProperty.FieldType type)
  301. {
  302. Element element = new Element(so, component, path);
  303. GUILayoutX elementLayout = layout.AddLayoutX();
  304. elementLayout.AddSpace(PADDING);
  305. elementLayout.AddSpace(foldoutWidth);
  306. GUILabel label = new GUILabel(new LocEdString(name));
  307. elementLayout.AddElement(label);
  308. GUIButton selectBtn = new GUIButton(new LocEdString("Select"));
  309. selectBtn.OnClick += () => { DoOnElementSelected(element, type); };
  310. elementLayout.AddFlexibleSpace();
  311. elementLayout.AddElement(selectBtn);
  312. elementLayout.AddSpace(5);
  313. element.path = path;
  314. return element;
  315. }
  316. /// <summary>
  317. /// Registers a new row in the layout. The row cannot be selected as the output field, but rather can be expanded
  318. /// so it displays child elements.
  319. /// </summary>
  320. /// <param name="layout">Layout to append the row GUI elements to.</param>
  321. /// <param name="icon">Optional icon to display next to the name. Can be null.</param>
  322. /// <param name="name">Name of the field.</param>
  323. /// <param name="so">Parent scene object of the field.</param>
  324. /// <param name="component">Parent component of the field. Can be null if field belongs to <see cref="SceneObject"/>.
  325. /// </param>
  326. /// <param name="path">Slash separated path to the field from its parent object.</param>
  327. /// <param name="toggleCallback">Callback to trigger when the user expands or collapses the foldout.</param>
  328. /// <returns>Element object storing all information about the added field.</returns>
  329. private Element AddFoldoutRow(GUILayout layout, SpriteTexture icon, string name, SceneObject so, Component component,
  330. string path, Action<Element, bool> toggleCallback)
  331. {
  332. Element element = new Element(so, component, path);
  333. GUILayoutY elementLayout = layout.AddLayoutY();
  334. GUILayoutX foldoutLayout = elementLayout.AddLayoutX();
  335. element.toggle = new GUIToggle("", EditorStyles.Expand);
  336. element.toggle.OnToggled += x => toggleCallback(element, x);
  337. foldoutLayout.AddSpace(PADDING);
  338. foldoutLayout.AddElement(element.toggle);
  339. if (icon != null)
  340. {
  341. GUITexture guiIcon = new GUITexture(icon, GUIOption.FixedWidth(16), GUIOption.FixedWidth(16));
  342. foldoutLayout.AddElement(guiIcon);
  343. }
  344. GUILabel label = new GUILabel(new LocEdString(name));
  345. foldoutLayout.AddElement(label);
  346. foldoutLayout.AddFlexibleSpace();
  347. element.indentLayout = elementLayout.AddLayoutX();
  348. element.indentLayout.AddSpace(INDENT_AMOUNT);
  349. element.childLayout = element.indentLayout.AddLayoutY();
  350. element.indentLayout.Active = false;
  351. return element;
  352. }
  353. /// <summary>
  354. /// Expands or collapses the set of rows displaying a <see cref="SceneObject"/>'s transform (position, rotation,
  355. /// scale).
  356. /// </summary>
  357. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  358. /// <param name="expand">True to expand, false to collapse.</param>
  359. private void ToggleTransformFoldout(Element parent, bool expand)
  360. {
  361. parent.childLayout.Clear();
  362. parent.children = null;
  363. parent.indentLayout.Active = expand;
  364. if (expand)
  365. {
  366. parent.children = new Element[3];
  367. parent.children[0] = AddFieldRow(parent.childLayout, "Position", parent.so, null, parent.path + "/Position", SerializableProperty.FieldType.Vector3);
  368. parent.children[1] = AddFieldRow(parent.childLayout, "Rotation", parent.so, null, parent.path + "/Rotation", SerializableProperty.FieldType.Vector3);
  369. parent.children[2] = AddFieldRow(parent.childLayout, "Scale", parent.so, null, parent.path + "/Scale", SerializableProperty.FieldType.Vector3);
  370. }
  371. }
  372. /// <summary>
  373. /// Expands or collapses the set of rows displaying all <see cref="SceneObject"/> children of a
  374. /// <see cref="SceneObject"/>.
  375. /// </summary>
  376. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  377. /// <param name="expand">True to expand, false to collapse.</param>
  378. private void ToggleChildFoldout(Element parent, bool expand)
  379. {
  380. parent.childLayout.Clear();
  381. parent.children = null;
  382. parent.indentLayout.Active = expand;
  383. if (expand)
  384. {
  385. int numChildren = parent.so.GetNumChildren();
  386. parent.children = new Element[numChildren];
  387. for (int i = 0; i < numChildren; i++)
  388. {
  389. SceneObject child = parent.so.GetChild(i);
  390. SpriteTexture soIcon = EditorBuiltin.GetEditorIcon(EditorIcon.SceneObject);
  391. parent.children[i] = AddFoldoutRow(parent.childLayout, soIcon, child.Name, child, null, parent.path,
  392. ToggleSceneObjectFoldout);
  393. }
  394. }
  395. }
  396. /// <summary>
  397. /// Expands or collapses the set of rows displaying all children of a <see cref="SceneObject"/>. This includes
  398. /// it's components, transform and child scene objects.
  399. /// </summary>
  400. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  401. /// <param name="expand">True to expand, false to collapse.</param>
  402. private void ToggleSceneObjectFoldout(Element parent, bool expand)
  403. {
  404. parent.childLayout.Clear();
  405. parent.children = null;
  406. parent.indentLayout.Active = expand;
  407. if (expand)
  408. AddSceneObjectRows(parent);
  409. }
  410. /// <summary>
  411. /// Expands or collapses the set of rows displaying all fields of a serializable object.
  412. /// </summary>
  413. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  414. /// <param name="obj">Object describing the type of the object whose fields to display.</param>
  415. /// <param name="expand">True to expand, false to collapse.</param>
  416. private void ToggleObjectFoldout(Element parent, SerializableObject obj,
  417. bool expand)
  418. {
  419. parent.childLayout.Clear();
  420. parent.children = null;
  421. parent.indentLayout.Active = expand;
  422. if (expand)
  423. AddObjectRows(parent, obj);
  424. }
  425. /// <summary>
  426. /// Expands or collapses the set of rows displaying all entries of a serializable array.
  427. /// </summary>
  428. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  429. /// <param name="obj">Object describing the array whose entries to display.</param>
  430. /// <param name="expand">True to expand, false to collapse.</param>
  431. private void ToggleArrayFoldout(Element parent, SerializableArray obj,
  432. bool expand)
  433. {
  434. parent.childLayout.Clear();
  435. parent.children = null;
  436. parent.indentLayout.Active = expand;
  437. if (expand)
  438. AddArrayRows(parent, obj);
  439. }
  440. /// <summary>
  441. /// Expands or collapses the set of rows displaying all entries of a serializable list.
  442. /// </summary>
  443. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  444. /// <param name="obj">Object describing the list whose entries to display.</param>
  445. /// <param name="expand">True to expand, false to collapse.</param>
  446. private void ToggleListFoldout(Element parent, SerializableList obj,
  447. bool expand)
  448. {
  449. parent.childLayout.Clear();
  450. parent.children = null;
  451. parent.indentLayout.Active = expand;
  452. if (expand)
  453. AddListRows(parent, obj);
  454. }
  455. /// <summary>
  456. /// Triggered when the user selects a field.
  457. /// </summary>
  458. /// <param name="element">Row element for the field that was selected.</param>
  459. /// <param name="type">Data type stored in the field.</param>
  460. private void DoOnElementSelected(Element element, SerializableProperty.FieldType type)
  461. {
  462. if (OnElementSelected != null)
  463. OnElementSelected(element.so, element.comp, element.path, type);
  464. }
  465. }
  466. /** @} */
  467. }