EditorUtility.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 bs;
  7. namespace bs.Editor
  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. /// Calculates the center of all axis aligned boxes of the provided scene objects.
  43. /// Only certain components like <see cref="Renderable"/> will be included in center calculations.
  44. /// </summary>
  45. /// <param name="objects">Scene objects to calculate the center for.</param>
  46. /// <returns>Center of the objects group in world space.</returns>
  47. public static Vector3 CalculateCenter(SceneObject[] objects)
  48. {
  49. Vector3 center;
  50. Internal_CalculateArrayCenter(objects, out center);
  51. return center;
  52. }
  53. /// <summary>
  54. /// Converts a hierarchy of scene objects and their children into a flat array. Doesn't modify the scene object's
  55. /// themselves.
  56. /// </summary>
  57. /// <param name="so">Scene object whose hierarchy to flatten.</param>
  58. /// <returns>Array of flattened hierarchy in a depth-first manner.</returns>
  59. public static SceneObject[] FlattenHierarchy(SceneObject so)
  60. {
  61. Stack<SceneObject> todo = new Stack<SceneObject>();
  62. todo.Push(so);
  63. List<SceneObject> flattenedHierarchy = new List<SceneObject>();
  64. while (todo.Count > 0)
  65. {
  66. SceneObject cur = todo.Pop();
  67. flattenedHierarchy.Add(cur);
  68. int numChildren = cur.GetNumChildren();
  69. for (int i = 0; i < numChildren; i++)
  70. todo.Push(cur.GetChild(i));
  71. }
  72. return flattenedHierarchy.ToArray();
  73. }
  74. /// <summary>
  75. /// Find all resources that the provided resource depends on.
  76. /// </summary>
  77. /// <param name="resource">Resource whose dependencies to find.</param>
  78. /// <param name="recursive">Determines whether or not child objects will also be searched (if object has any
  79. /// children).</param>
  80. /// <returns>All resources the provided resource is dependant on (does not include itself).</returns>
  81. public static Resource[] FindDependencies(Resource resource, bool recursive = true)
  82. {
  83. return Internal_FindDependencies(resource, recursive);
  84. }
  85. /// <summary>
  86. /// Checks is the provided scene object internal (hidden from normal user, used by internal engine systems).
  87. /// </summary>
  88. /// <param name="so">Scene object to check.</param>
  89. /// <returns>True if internal, false otherwise. </returns>
  90. public static bool IsInternal(SceneObject so)
  91. {
  92. if (so == null)
  93. return false;
  94. IntPtr objPtr = so.GetCachedPtr();
  95. return Internal_IsInternal(objPtr);
  96. }
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. private static extern void Internal_CalculateBounds(SceneObject so, out AABox bounds);
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern void Internal_CalculateBoundsArray(SceneObject[] objects, out AABox bounds);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern void Internal_CalculateArrayCenter(SceneObject[] objects, out Vector3 center);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern Resource[] Internal_FindDependencies(Resource resource, bool recursive);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern bool Internal_IsInternal(IntPtr soPtr);
  107. }
  108. /** @} */
  109. }