GUIFieldSelector.cs 16 KB

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