EditorUtility.cs 5.3 KB

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