GUILayoutUtility.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. /// <summary>
  9. /// Helper class that performs various operations related to
  10. /// GUILayout and GUI element sizing/placement.
  11. /// </summary>
  12. public class GUILayoutUtility
  13. {
  14. /// <summary>
  15. /// Calculates optimal size of a GUI element.
  16. /// </summary>
  17. /// <param name="element">GUI element to calculate the optimal size for.</param>
  18. /// <returns>Size that allows the GUI element to properly display all of its content.</returns>
  19. public static Vector2I CalculateOptimalSize(GUIElement element)
  20. {
  21. Vector2I output;
  22. Internal_CalculateOptimalSize(element.GetCachedPtr(), out output);
  23. return output;
  24. }
  25. /// <summary>
  26. /// Calculates the bounds of a GUI element. This is similar to <see cref="GUIElement.Bounds"/> but allows you to
  27. /// returns bounds relative to a specific parent GUI panel.
  28. /// </summary>
  29. /// <param name="element">Elements to calculate the bounds for.
  30. /// </param>
  31. /// <param name="relativeTo">GUI panel the bounds will be relative to. If this is null or the provided panel is not
  32. /// a parent of the provided GUI element, the returned bounds will be relative to the first GUI panel parent instead.
  33. /// </param>
  34. /// <returns>Bounds of a GUI element relative to the provided GUI panel.</returns>
  35. public static Rect2I CalculateBounds(GUIElement element, GUIPanel relativeTo = null)
  36. {
  37. IntPtr relativeToNative = IntPtr.Zero;
  38. if (relativeTo != null)
  39. relativeToNative = relativeTo.GetCachedPtr();
  40. Rect2I output;
  41. Internal_CalculateBounds(element.GetCachedPtr(), relativeToNative, out output);
  42. return output;
  43. }
  44. [MethodImpl(MethodImplOptions.InternalCall)]
  45. private static extern void Internal_CalculateOptimalSize(IntPtr element, out Vector2I output);
  46. [MethodImpl(MethodImplOptions.InternalCall)]
  47. private static extern void Internal_CalculateBounds(IntPtr element, IntPtr relativeTo, out Rect2I output);
  48. }
  49. }