GUIElement.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. namespace BansheeEngine
  5. {
  6. /// <summary>
  7. /// Base class for all GUI elements. Every GUI element can at least be positioned in it's parent layout/panel and be
  8. /// hidden/visible, focused/unfocused and assigned a context menu.
  9. /// </summary>
  10. public abstract class GUIElement : ScriptObject
  11. {
  12. /// <summary>
  13. /// Triggered when a GUI element receives or loses keyboard focus.
  14. /// </summary>
  15. public Action<bool> OnFocusChanged;
  16. /// <summary>
  17. /// Gets or sets non-clipped bounds of the GUI element. Relative to a parent GUI panel.
  18. /// </summary>
  19. public Rect2I Bounds
  20. {
  21. get { return Internal_GetBounds(mCachedPtr); }
  22. set { Internal_SetBounds(mCachedPtr, value); }
  23. }
  24. /// <summary>
  25. /// Gets or sets non-clipped bounds of the GUI element including the margins. Relative to a parent GUI panel.
  26. /// </summary>
  27. public Rect2I VisualBounds
  28. {
  29. get { return Internal_GetVisualBounds(mCachedPtr); }
  30. }
  31. /// <summary>
  32. /// Makes the element hidden or visible.
  33. /// </summary>
  34. public bool Visible
  35. {
  36. set { Internal_SetVisible(mCachedPtr, value); }
  37. }
  38. /// <summary>
  39. /// Assigns or removes keyboard focus on this element.
  40. /// </summary>
  41. public bool Focus
  42. {
  43. set { Internal_SetFocus(mCachedPtr, value); }
  44. }
  45. /// <summary>
  46. /// Destroys this element and all its children. Removes the element from parent layout/panel.
  47. /// </summary>
  48. /// <remarks>
  49. /// Calling methods on a destroyed element is a no-operation. Destroyed elements aren't allowed to be assigned as
  50. /// children of other elements, or be parents of other elements.
  51. /// </remarks>
  52. public virtual void Destroy()
  53. {
  54. Internal_Destroy(mCachedPtr);
  55. }
  56. /// <summary>
  57. /// Sets element position relative to parent GUI panel.
  58. /// </summary>
  59. /// <param name="x">X position of the element in pixels, relative to parent GUI panel.</param>
  60. /// <param name="y">Y position of the element in pixels, relative to parent GUI panel.</param>
  61. /// <remarks>
  62. /// Be aware that this value will be ignored if GUI element is part of a layout because the layout controls placement
  63. /// of child elements.
  64. /// </remarks>
  65. public void SetPosition(int x, int y)
  66. {
  67. Internal_SetPosition(mCachedPtr, x, y);
  68. }
  69. /// <summary>
  70. /// Sets a fixed element width.
  71. /// </summary>
  72. /// <param name="width">Width in pixels.</param>
  73. public void SetWidth(int width)
  74. {
  75. Internal_SetWidth(mCachedPtr, width);
  76. }
  77. /// <summary>
  78. /// Sets a flexible element width. Element will be resized according to its contents and parent layout but will always
  79. /// stay within the provided range.
  80. /// </summary>
  81. /// <param name="minWidth">Minimum width in pixels. Element will never be smaller than this width.
  82. /// </param>
  83. /// <param name="maxWidth">Maximum width in pixels. Element will never be larger than this width. Specify zero for
  84. /// unlimited width.
  85. /// </param>
  86. public void SetFlexibleWidth(int minWidth, int maxWidth)
  87. {
  88. Internal_SetFlexibleWidth(mCachedPtr, minWidth, maxWidth);
  89. }
  90. /// <summary>
  91. /// Sets a fixed element height.
  92. /// </summary>
  93. /// <param name="height">Height in pixels.</param>
  94. public void SetHeight(int height)
  95. {
  96. Internal_SetHeight(mCachedPtr, height);
  97. }
  98. /// <summary>
  99. /// Sets a flexible element height. Element will be resized according to its contents and parent layout but will
  100. /// always stay within the provided range.
  101. /// </summary>
  102. /// <param name="minHeight">Minimum height in pixels. Element will never be smaller than this height.
  103. /// </param>
  104. /// <param name="maxHeight">Maximum height in pixels. Element will never be larger than this height. Specify zero for
  105. /// unlimited height.
  106. /// </param>
  107. public void SetFlexibleHeight(int minHeight, int maxHeight)
  108. {
  109. Internal_SetFlexibleHeight(mCachedPtr, minHeight, maxHeight);
  110. }
  111. /// <summary>
  112. /// Resets element bounds to their initial values dictated by the element's style.
  113. /// </summary>
  114. public void ResetDimensions()
  115. {
  116. Internal_ResetDimensions(mCachedPtr);
  117. }
  118. /// <summary>
  119. /// Assigns a new context menu that will be opened when the element is right clicked.
  120. /// </summary>
  121. /// <param name="menu">Object containing context menu contents. Can be null if no menu is wanted.</param>
  122. public void SetContextMenu(ContextMenu menu)
  123. {
  124. IntPtr menuPtr = IntPtr.Zero;
  125. if (menu != null)
  126. menuPtr = menu.GetCachedPtr();
  127. Internal_SetContextMenu(mCachedPtr, menuPtr);
  128. }
  129. /// <summary>
  130. /// Triggered by the native interop object when the keyboard focus of this element changes.
  131. /// </summary>
  132. private void InternalOnFocusChanged(bool focus)
  133. {
  134. if (OnFocusChanged != null)
  135. OnFocusChanged(focus);
  136. }
  137. [MethodImpl(MethodImplOptions.InternalCall)]
  138. private static extern void Internal_SetVisible(IntPtr nativeInstance, bool visible);
  139. [MethodImpl(MethodImplOptions.InternalCall)]
  140. private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
  141. [MethodImpl(MethodImplOptions.InternalCall)]
  142. private static extern void Internal_SetPosition(IntPtr nativeInstance, int x, int y);
  143. [MethodImpl(MethodImplOptions.InternalCall)]
  144. private static extern void Internal_SetWidth(IntPtr nativeInstance, int width);
  145. [MethodImpl(MethodImplOptions.InternalCall)]
  146. private static extern void Internal_SetFlexibleWidth(IntPtr nativeInstance, int minWidth, int maxWidth);
  147. [MethodImpl(MethodImplOptions.InternalCall)]
  148. private static extern void Internal_SetHeight(IntPtr nativeInstance, int height);
  149. [MethodImpl(MethodImplOptions.InternalCall)]
  150. private static extern void Internal_SetFlexibleHeight(IntPtr nativeInstance, int minHeight, int maxHeight);
  151. [MethodImpl(MethodImplOptions.InternalCall)]
  152. private static extern void Internal_ResetDimensions(IntPtr nativeInstance);
  153. [MethodImpl(MethodImplOptions.InternalCall)]
  154. private static extern Rect2I Internal_GetBounds(IntPtr nativeInstance);
  155. [MethodImpl(MethodImplOptions.InternalCall)]
  156. private static extern void Internal_SetBounds(IntPtr nativeInstance, Rect2I value);
  157. [MethodImpl(MethodImplOptions.InternalCall)]
  158. private static extern Rect2I Internal_GetVisualBounds(IntPtr nativeInstance);
  159. [MethodImpl(MethodImplOptions.InternalCall)]
  160. private static extern void Internal_SetContextMenu(IntPtr nativeInstance, IntPtr contextMenu);
  161. [MethodImpl(MethodImplOptions.InternalCall)]
  162. private static extern void Internal_Destroy(IntPtr nativeInstance);
  163. }
  164. }