EditorUtility.cs 3.9 KB

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