EditorUtility.cs 4.6 KB

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