GUIElement.cs 9.4 KB

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