GUIElement.cs 11 KB

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