GUIFieldSelector.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 BansheeEngine;
  6. namespace BansheeEditor
  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. scrollArea.Layout.AddSpace(5);
  88. AddSceneObjectRows(rootElement);
  89. scrollArea.Layout.AddSpace(5);
  90. scrollArea.Layout.AddFlexibleSpace();
  91. }
  92. /// <summary>
  93. /// Registers a set of rows for all components in a <see cref="SceneObject"/>, as well as its transform and
  94. /// child objects.
  95. /// </summary>
  96. /// <param name="parent">Row element under which to create the new rows.</param>
  97. private void AddSceneObjectRows(Element parent)
  98. {
  99. string soName = "!" + parent.so.Name;
  100. Component[] components = parent.so.GetComponents();
  101. parent.children = new Element[components.Length + 2];
  102. SpriteTexture soIcon = EditorBuiltin.GetEditorIcon(EditorIcon.SceneObject);
  103. SpriteTexture compIcon = EditorBuiltin.GetEditorIcon(EditorIcon.Component);
  104. // Add transform
  105. parent.children[0] = AddFoldoutRow(parent.childLayout, soIcon, "Transform", parent.so,
  106. null, parent.path + "/" + soName, ToggleTransformFoldout);
  107. // Add components
  108. for (int i = 0; i < components.Length; i++)
  109. {
  110. Component childComponent = components[i];
  111. Action<Element, bool> toggleCallback =
  112. (toggleParent, expand) =>
  113. {
  114. SerializableObject componentObject = new SerializableObject(childComponent.GetType(), null);
  115. ToggleObjectFoldout(toggleParent, componentObject, expand);
  116. };
  117. string name = childComponent.GetType().Name;
  118. string path = parent.path + "/" + soName + "/:" + name;
  119. parent.children[i + 1] = AddFoldoutRow(parent.childLayout, compIcon, name, parent.so, childComponent, path,
  120. toggleCallback);
  121. }
  122. // Add children
  123. if (parent.so.GetNumChildren() > 0)
  124. {
  125. parent.children[parent.children.Length - 1] = AddFoldoutRow(parent.childLayout, soIcon, "Children",
  126. parent.so, null, parent.path + "/" + soName, ToggleChildFoldout);
  127. }
  128. }
  129. /// <summary>
  130. /// Registers a set of rows for all child fields of the provided object.
  131. /// </summary>
  132. /// <param name="parent">Parent foldout row to which to append the new elements.</param>
  133. /// <param name="serializableObject">Type of the object whose fields to display.</param>
  134. private void AddObjectRows(Element parent, SerializableObject serializableObject)
  135. {
  136. List<Element> elements = new List<Element>();
  137. foreach (var field in serializableObject.Fields)
  138. {
  139. if (!field.Animable)
  140. continue;
  141. string propertyPath = parent.path + "/" + field.Name;
  142. switch (field.Type)
  143. {
  144. case SerializableProperty.FieldType.Bool:
  145. case SerializableProperty.FieldType.Float:
  146. case SerializableProperty.FieldType.Int:
  147. case SerializableProperty.FieldType.Color:
  148. case SerializableProperty.FieldType.Vector2:
  149. case SerializableProperty.FieldType.Vector3:
  150. case SerializableProperty.FieldType.Vector4:
  151. elements.Add(AddFieldRow(parent.childLayout, field.Name, parent.so, parent.comp, propertyPath, field.Type));
  152. break;
  153. case SerializableProperty.FieldType.Object:
  154. Action<Element, bool> toggleCallback =
  155. (toggleParent, expand) =>
  156. {
  157. SerializableObject childObject = new SerializableObject(field.InternalType, null);
  158. ToggleObjectFoldout(toggleParent, childObject, expand);
  159. };
  160. elements.Add(AddFoldoutRow(parent.childLayout, null, field.Name, parent.so, parent.comp,
  161. propertyPath, toggleCallback));
  162. break;
  163. }
  164. }
  165. parent.children = elements.ToArray();
  166. }
  167. /// <summary>
  168. /// Registers a new row in the layout. The row cannot have any further children and can be selected as the output
  169. /// field.
  170. /// </summary>
  171. /// <param name="layout">Layout to append the row GUI elements to.</param>
  172. /// <param name="name">Name of the field.</param>
  173. /// <param name="so">Parent scene object of the field.</param>
  174. /// <param name="component">Parent component of the field. Can be null if field belongs to <see cref="SceneObject"/>.
  175. /// </param>
  176. /// <param name="path">Slash separated path to the field from its parent object.</param>
  177. /// <param name="type">Data type stored in the field.</param>
  178. /// <returns>Element object storing all information about the added field.</returns>
  179. private Element AddFieldRow(GUILayout layout, string name, SceneObject so, Component component, string path, SerializableProperty.FieldType type)
  180. {
  181. Element element = new Element(so, component, path);
  182. GUILayoutX elementLayout = layout.AddLayoutX();
  183. elementLayout.AddSpace(PADDING);
  184. elementLayout.AddSpace(foldoutWidth);
  185. GUILabel label = new GUILabel(new LocEdString(name));
  186. elementLayout.AddElement(label);
  187. GUIButton selectBtn = new GUIButton(new LocEdString("Select"));
  188. selectBtn.OnClick += () => { DoOnElementSelected(element, type); };
  189. elementLayout.AddFlexibleSpace();
  190. elementLayout.AddElement(selectBtn);
  191. element.path = path;
  192. return element;
  193. }
  194. /// <summary>
  195. /// Registers a new row in the layout. The row cannot be selected as the output field, but rather can be expanded
  196. /// so it displays child elements.
  197. /// </summary>
  198. /// <param name="layout">Layout to append the row GUI elements to.</param>
  199. /// <param name="icon">Optional icon to display next to the name. Can be null.</param>
  200. /// <param name="name">Name of the field.</param>
  201. /// <param name="so">Parent scene object of the field.</param>
  202. /// <param name="component">Parent component of the field. Can be null if field belongs to <see cref="SceneObject"/>.
  203. /// </param>
  204. /// <param name="path">Slash separated path to the field from its parent object.</param>
  205. /// <param name="toggleCallback">Callback to trigger when the user expands or collapses the foldout.</param>
  206. /// <returns>Element object storing all information about the added field.</returns>
  207. private Element AddFoldoutRow(GUILayout layout, SpriteTexture icon, string name, SceneObject so, Component component,
  208. string path, Action<Element, bool> toggleCallback)
  209. {
  210. Element element = new Element(so, component, path);
  211. GUILayoutY elementLayout = layout.AddLayoutY();
  212. GUILayoutX foldoutLayout = elementLayout.AddLayoutX();
  213. element.toggle = new GUIToggle("", EditorStyles.Expand);
  214. element.toggle.OnToggled += x => toggleCallback(element, x);
  215. foldoutLayout.AddSpace(PADDING);
  216. foldoutLayout.AddElement(element.toggle);
  217. if (icon != null)
  218. {
  219. GUITexture guiIcon = new GUITexture(icon, GUIOption.FixedWidth(16), GUIOption.FixedWidth(16));
  220. foldoutLayout.AddElement(guiIcon);
  221. }
  222. GUILabel label = new GUILabel(new LocEdString(name));
  223. foldoutLayout.AddElement(label);
  224. foldoutLayout.AddFlexibleSpace();
  225. element.indentLayout = elementLayout.AddLayoutX();
  226. element.indentLayout.AddSpace(INDENT_AMOUNT);
  227. element.childLayout = element.indentLayout.AddLayoutY();
  228. element.indentLayout.Active = false;
  229. return element;
  230. }
  231. /// <summary>
  232. /// Expands or collapses the set of rows displaying a <see cref="SceneObject"/>'s transform (position, rotation,
  233. /// scale).
  234. /// </summary>
  235. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  236. /// <param name="expand">True to expand, false to collapse.</param>
  237. private void ToggleTransformFoldout(Element parent, bool expand)
  238. {
  239. parent.childLayout.Clear();
  240. parent.children = null;
  241. parent.indentLayout.Active = expand;
  242. if (expand)
  243. {
  244. parent.children = new Element[3];
  245. parent.children[0] = AddFieldRow(parent.childLayout, "Position", parent.so, null, parent.path + "/Position", SerializableProperty.FieldType.Vector3);
  246. parent.children[1] = AddFieldRow(parent.childLayout, "Rotation", parent.so, null, parent.path + "/Rotation", SerializableProperty.FieldType.Vector3);
  247. parent.children[2] = AddFieldRow(parent.childLayout, "Scale", parent.so, null, parent.path + "/Scale", SerializableProperty.FieldType.Vector3);
  248. }
  249. }
  250. /// <summary>
  251. /// Expands or collapses the set of rows displaying all <see cref="SceneObject"/> children of a
  252. /// <see cref="SceneObject"/>.
  253. /// </summary>
  254. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  255. /// <param name="expand">True to expand, false to collapse.</param>
  256. private void ToggleChildFoldout(Element parent, bool expand)
  257. {
  258. parent.childLayout.Clear();
  259. parent.children = null;
  260. parent.indentLayout.Active = expand;
  261. if (expand)
  262. {
  263. int numChildren = parent.so.GetNumChildren();
  264. parent.children = new Element[numChildren];
  265. for (int i = 0; i < numChildren; i++)
  266. {
  267. SceneObject child = parent.so.GetChild(i);
  268. SpriteTexture soIcon = EditorBuiltin.GetEditorIcon(EditorIcon.SceneObject);
  269. parent.children[i] = AddFoldoutRow(parent.childLayout, soIcon, child.Name, child, null, parent.path,
  270. ToggleSceneObjectFoldout);
  271. }
  272. }
  273. }
  274. /// <summary>
  275. /// Expands or collapses the set of rows displaying all children of a <see cref="SceneObject"/>. This includes
  276. /// it's components, transform and child scene objects.
  277. /// </summary>
  278. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  279. /// <param name="expand">True to expand, false to collapse.</param>
  280. private void ToggleSceneObjectFoldout(Element parent, bool expand)
  281. {
  282. parent.childLayout.Clear();
  283. parent.children = null;
  284. parent.indentLayout.Active = expand;
  285. if (expand)
  286. {
  287. AddSceneObjectRows(parent);
  288. }
  289. }
  290. /// <summary>
  291. /// Expands or collapses the set of rows displaying all fields of a serializable object.
  292. /// </summary>
  293. /// <param name="parent">Parent row element whose children to expand/collapse.</param>
  294. /// <param name="obj">Object describing the type of the object whose fields to display.</param>
  295. /// <param name="expand">True to expand, false to collapse.</param>
  296. private void ToggleObjectFoldout(Element parent, SerializableObject obj,
  297. bool expand)
  298. {
  299. parent.childLayout.Clear();
  300. parent.children = null;
  301. parent.indentLayout.Active = expand;
  302. if (expand)
  303. AddObjectRows(parent, obj);
  304. }
  305. /// <summary>
  306. /// Triggered when the user selects a field.
  307. /// </summary>
  308. /// <param name="element">Row element for the field that was selected.</param>
  309. /// <param name="type">Data type stored in the field.</param>
  310. private void DoOnElementSelected(Element element, SerializableProperty.FieldType type)
  311. {
  312. if (OnElementSelected != null)
  313. OnElementSelected(element.so, element.comp, element.path, type);
  314. }
  315. }
  316. /** @} */
  317. }