GUIUtility.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup GUI-Engine
  8. * @{
  9. */
  10. /// <summary>
  11. /// Various helper functionality that's useful when creating GUI elements.
  12. /// </summary>
  13. public class GUIUtility
  14. {
  15. /// <summary>
  16. /// Calculates optimal size of a GUI element.
  17. /// </summary>
  18. /// <param name="element">GUI element to calculate the optimal size for.</param>
  19. /// <returns>Size that allows the GUI element to properly display all of its content.</returns>
  20. public static Vector2I CalculateOptimalSize(GUIElement element)
  21. {
  22. Vector2I output;
  23. Internal_CalculateOptimalSize(element.GetCachedPtr(), out output);
  24. return output;
  25. }
  26. /// <summary>
  27. /// Calculates the bounds of a GUI element. This is similar to <see cref="GUIElement.Bounds"/> but allows you to
  28. /// returns bounds relative to a specific parent GUI panel.
  29. /// </summary>
  30. /// <param name="element">Elements to calculate the bounds for.</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
  33. /// first GUI panel parent instead.</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. /// <summary>
  45. /// Calculates optimal content size for the provided text using the provided font and size. Size is calculated
  46. /// without word wrap.
  47. /// </summary>
  48. /// <param name="text">Text to calculate the bounds for.</param>
  49. /// <param name="font">Font that will be used for rendering the text.</param>
  50. /// <param name="fontSize">Size of individual characters in the font, in points.</param>
  51. /// <returns>Width/height required to display the text, in pixels.</returns>
  52. public static Vector2I CalculateTextBounds(string text, Font font, int fontSize)
  53. {
  54. Vector2I output;
  55. if (font != null && text != null)
  56. {
  57. IntPtr fontPtr = font.GetCachedPtr();
  58. Internal_CalculateTextBounds(text, fontPtr, fontSize, out output);
  59. }
  60. else
  61. output = new Vector2I();
  62. return output;
  63. }
  64. [MethodImpl(MethodImplOptions.InternalCall)]
  65. private static extern void Internal_CalculateOptimalSize(IntPtr element, out Vector2I output);
  66. [MethodImpl(MethodImplOptions.InternalCall)]
  67. private static extern void Internal_CalculateBounds(IntPtr element, IntPtr relativeTo, out Rect2I output);
  68. [MethodImpl(MethodImplOptions.InternalCall)]
  69. private static extern void Internal_CalculateTextBounds(string text, IntPtr fontPtr, int fontSize, out Vector2I output);
  70. }
  71. /** @} */
  72. }