GUIElement.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. This will not affect the layout as the room for the element will still
  33. /// be reserved in the parent layout, use <see cref="Active"/> if you need to affect the layout as well.
  34. /// </summary>
  35. public bool Visible
  36. {
  37. set { Internal_SetVisible(mCachedPtr, value); }
  38. }
  39. /// <summary>
  40. /// Activates or deactivates the element, making it hidden or visible. When disabled it is essentially removed from
  41. /// the parent achieving the similar effect as if the element was destroyed.
  42. /// </summary>
  43. public bool Active
  44. {
  45. set { Internal_SetActive(mCachedPtr, value); }
  46. }
  47. /// <summary>
  48. /// Disables or enables the element. Disabled elements cannot be interacted with and have a faded out appearance.
  49. /// </summary>
  50. public bool Disabled
  51. {
  52. set { Internal_SetDisabled(mCachedPtr, value); }
  53. }
  54. /// <summary>
  55. /// Assigns or removes keyboard focus on this element.
  56. /// </summary>
  57. public bool Focus
  58. {
  59. set { Internal_SetFocus(mCachedPtr, value); }
  60. }
  61. /// <summary>
  62. /// Destroys this element and all its children. Removes the element from parent layout/panel.
  63. /// </summary>
  64. /// <remarks>
  65. /// Calling methods on a destroyed element is a no-operation. Destroyed elements aren't allowed to be assigned as
  66. /// children of other elements, or be parents of other elements.
  67. /// </remarks>
  68. public virtual void Destroy()
  69. {
  70. Internal_Destroy(mCachedPtr);
  71. }
  72. /// <summary>
  73. /// Sets element position relative to parent GUI panel.
  74. /// </summary>
  75. /// <param name="x">X position of the element in pixels, relative to parent GUI panel.</param>
  76. /// <param name="y">Y position of the element in pixels, relative to parent GUI panel.</param>
  77. /// <remarks>
  78. /// Be aware that this value will be ignored if GUI element is part of a layout because the layout controls placement
  79. /// of child elements.
  80. /// </remarks>
  81. public void SetPosition(int x, int y)
  82. {
  83. Internal_SetPosition(mCachedPtr, x, y);
  84. }
  85. /// <summary>
  86. /// Sets a fixed element width.
  87. /// </summary>
  88. /// <param name="width">Width in pixels.</param>
  89. public void SetWidth(int width)
  90. {
  91. Internal_SetWidth(mCachedPtr, width);
  92. }
  93. /// <summary>
  94. /// Sets a flexible element width. Element will be resized according to its contents and parent layout but will
  95. /// always stay within the provided range.
  96. /// </summary>
  97. /// <param name="minWidth">Minimum width in pixels. Element will never be smaller than this width.</param>
  98. /// <param name="maxWidth">Maximum width in pixels. Element will never be larger than this width. Specify zero for
  99. /// unlimited width.</param>
  100. public void SetFlexibleWidth(int minWidth, int maxWidth)
  101. {
  102. Internal_SetFlexibleWidth(mCachedPtr, minWidth, maxWidth);
  103. }
  104. /// <summary>
  105. /// Sets a fixed element height.
  106. /// </summary>
  107. /// <param name="height">Height in pixels.</param>
  108. public void SetHeight(int height)
  109. {
  110. Internal_SetHeight(mCachedPtr, height);
  111. }
  112. /// <summary>
  113. /// Sets a flexible element height. Element will be resized according to its contents and parent layout but will
  114. /// always stay within the provided range.
  115. /// </summary>
  116. /// <param name="minHeight">Minimum height in pixels. Element will never be smaller than this height.</param>
  117. /// <param name="maxHeight">Maximum height in pixels. Element will never be larger than this height. Specify zero
  118. /// for unlimited height.</param>
  119. public void SetFlexibleHeight(int minHeight, int maxHeight)
  120. {
  121. Internal_SetFlexibleHeight(mCachedPtr, minHeight, maxHeight);
  122. }
  123. /// <summary>
  124. /// Resets element bounds to their initial values dictated by the element's style.
  125. /// </summary>
  126. public void ResetDimensions()
  127. {
  128. Internal_ResetDimensions(mCachedPtr);
  129. }
  130. /// <summary>
  131. /// Assigns a new context menu that will be opened when the element is right clicked.
  132. /// </summary>
  133. /// <param name="menu">Object containing context menu contents. Can be null if no menu is wanted.</param>
  134. public void SetContextMenu(ContextMenu menu)
  135. {
  136. IntPtr menuPtr = IntPtr.Zero;
  137. if (menu != null)
  138. menuPtr = menu.GetCachedPtr();
  139. Internal_SetContextMenu(mCachedPtr, menuPtr);
  140. }
  141. /// <summary>
  142. /// Triggered by the native interop object when the keyboard focus of this element changes.
  143. /// </summary>
  144. private void InternalOnFocusChanged(bool focus)
  145. {
  146. if (OnFocusChanged != null)
  147. OnFocusChanged(focus);
  148. }
  149. [MethodImpl(MethodImplOptions.InternalCall)]
  150. private static extern void Internal_SetVisible(IntPtr nativeInstance, bool visible);
  151. [MethodImpl(MethodImplOptions.InternalCall)]
  152. private static extern void Internal_SetActive(IntPtr nativeInstance, bool enabled);
  153. [MethodImpl(MethodImplOptions.InternalCall)]
  154. private static extern void Internal_SetDisabled(IntPtr nativeInstance, bool disabled);
  155. [MethodImpl(MethodImplOptions.InternalCall)]
  156. private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
  157. [MethodImpl(MethodImplOptions.InternalCall)]
  158. private static extern void Internal_SetPosition(IntPtr nativeInstance, int x, int y);
  159. [MethodImpl(MethodImplOptions.InternalCall)]
  160. private static extern void Internal_SetWidth(IntPtr nativeInstance, int width);
  161. [MethodImpl(MethodImplOptions.InternalCall)]
  162. private static extern void Internal_SetFlexibleWidth(IntPtr nativeInstance, int minWidth, int maxWidth);
  163. [MethodImpl(MethodImplOptions.InternalCall)]
  164. private static extern void Internal_SetHeight(IntPtr nativeInstance, int height);
  165. [MethodImpl(MethodImplOptions.InternalCall)]
  166. private static extern void Internal_SetFlexibleHeight(IntPtr nativeInstance, int minHeight, int maxHeight);
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. private static extern void Internal_ResetDimensions(IntPtr nativeInstance);
  169. [MethodImpl(MethodImplOptions.InternalCall)]
  170. private static extern Rect2I Internal_GetBounds(IntPtr nativeInstance);
  171. [MethodImpl(MethodImplOptions.InternalCall)]
  172. private static extern void Internal_SetBounds(IntPtr nativeInstance, Rect2I value);
  173. [MethodImpl(MethodImplOptions.InternalCall)]
  174. private static extern Rect2I Internal_GetVisualBounds(IntPtr nativeInstance);
  175. [MethodImpl(MethodImplOptions.InternalCall)]
  176. private static extern void Internal_SetContextMenu(IntPtr nativeInstance, IntPtr contextMenu);
  177. [MethodImpl(MethodImplOptions.InternalCall)]
  178. private static extern void Internal_Destroy(IntPtr nativeInstance);
  179. }
  180. }