EditorUtility.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /** @addtogroup Utility-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Contains various utility methods used throughout the editor.
  13. /// </summary>
  14. public class EditorUtility
  15. {
  16. /// <summary>
  17. /// Calculates the axis aligned box that encapsulated the provided scene object and its components and children.
  18. /// Only certain components like <see cref="Renderable"/> will be included in bound calculations.
  19. /// </summary>
  20. /// <param name="so">Scene object to calculate the bounds for.</param>
  21. /// <returns>Axis aligned box encompassing all scene object elements, in world space.</returns>
  22. public static AABox CalculateBounds(SceneObject so)
  23. {
  24. AABox bounds;
  25. Internal_CalculateBounds(so, out bounds);
  26. return bounds;
  27. }
  28. /// <summary>
  29. /// Calculates the axis aligned box that encapsulated the provided scene objects and their components and children.
  30. /// Only certain components like <see cref="Renderable"/> will be included in bound calculations.
  31. /// </summary>
  32. /// <param name="objects">Scene objects to calculate the bounds for.</param>
  33. /// <returns>Axis aligned box encompassing all scene object elements, in world space.</returns>
  34. public static AABox CalculateBounds(SceneObject[] objects)
  35. {
  36. AABox bounds;
  37. Internal_CalculateBoundsArray(objects, out bounds);
  38. return bounds;
  39. }
  40. /// <summary>
  41. /// Converts a hierarchy of scene objects and their children into a flat array. Doesn't modify the scene object's
  42. /// themselves.
  43. /// </summary>
  44. /// <param name="so">Scene object whose hierarchy to flatten.</param>
  45. /// <returns>Array of flattened hierarchy in a depth-first manner.</returns>
  46. public static SceneObject[] FlattenHierarchy(SceneObject so)
  47. {
  48. Stack<SceneObject> todo = new Stack<SceneObject>();
  49. todo.Push(so);
  50. List<SceneObject> flattenedHierarchy = new List<SceneObject>();
  51. while (todo.Count > 0)
  52. {
  53. SceneObject cur = todo.Pop();
  54. flattenedHierarchy.Add(cur);
  55. int numChildren = cur.GetNumChildren();
  56. for (int i = 0; i < numChildren; i++)
  57. todo.Push(cur.GetChild(i));
  58. }
  59. return flattenedHierarchy.ToArray();
  60. }
  61. /// <summary>
  62. /// Find all resources that the provided resource depends on.
  63. /// </summary>
  64. /// <param name="resource">Resource whose dependencies to find.</param>
  65. /// <param name="recursive">Determines whether or not child objects will also be searched (if object has any
  66. /// children).</param>
  67. /// <returns>All resources the provided resource is dependant on (does not include itself).</returns>
  68. public static Resource[] FindDependencies(Resource resource, bool recursive = true)
  69. {
  70. return Internal_FindDependencies(resource, recursive);
  71. }
  72. [MethodImpl(MethodImplOptions.InternalCall)]
  73. private static extern void Internal_CalculateBounds(SceneObject so, out AABox bounds);
  74. [MethodImpl(MethodImplOptions.InternalCall)]
  75. private static extern void Internal_CalculateBoundsArray(SceneObject[] objects, out AABox bounds);
  76. [MethodImpl(MethodImplOptions.InternalCall)]
  77. private static extern Resource[] Internal_FindDependencies(Resource resource, bool recursive);
  78. }
  79. /** @} */
  80. }