GUIElement.cs 9.0 KB

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