GUILayoutUtility.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.</param>
  30. /// <param name="relativeTo">GUI panel the bounds will be relative to. If this is null or the provided panel is not
  31. /// a parent of the provided GUI element, the returned bounds will be relative to the
  32. /// first GUI panel parent instead.</param>
  33. /// <returns>Bounds of a GUI element relative to the provided GUI panel.</returns>
  34. public static Rect2I CalculateBounds(GUIElement element, GUIPanel relativeTo = null)
  35. {
  36. IntPtr relativeToNative = IntPtr.Zero;
  37. if (relativeTo != null)
  38. relativeToNative = relativeTo.GetCachedPtr();
  39. Rect2I output;
  40. Internal_CalculateBounds(element.GetCachedPtr(), relativeToNative, out output);
  41. return output;
  42. }
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. private static extern void Internal_CalculateOptimalSize(IntPtr element, out Vector2I output);
  45. [MethodImpl(MethodImplOptions.InternalCall)]
  46. private static extern void Internal_CalculateBounds(IntPtr element, IntPtr relativeTo, out Rect2I output);
  47. }
  48. }