GUIElement.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup GUI_Engine
  9. * @{
  10. */
  11. /// <summary>
  12. /// Base class for all GUI elements. Every GUI element can at least be positioned in it's parent layout/panel and be
  13. /// hidden/visible, focused/unfocused and assigned a context menu.
  14. /// </summary>
  15. public abstract class GUIElement : ScriptObject
  16. {
  17. /// <summary>
  18. /// Triggered when a GUI element receives keyboard focus.
  19. /// </summary>
  20. public Action OnFocusGained;
  21. /// <summary>
  22. /// Triggered when a GUI element loses keyboard focus.
  23. /// </summary>
  24. public Action OnFocusLost;
  25. /// <summary>
  26. /// Returns the layout this element belongs to, if any.
  27. /// </summary>
  28. public GUILayout Parent
  29. {
  30. get { return Internal_GetParent(mCachedPtr); }
  31. }
  32. /// <summary>
  33. /// Name of the style that determines the appearance of this GUI element.
  34. /// </summary>
  35. public string Style
  36. {
  37. get { return Internal_GetStyle(mCachedPtr); }
  38. set { Internal_SetStyle(mCachedPtr, value); }
  39. }
  40. /// <summary>
  41. /// Gets or sets non-clipped bounds of the GUI element. Relative to a parent GUI panel.
  42. /// </summary>
  43. public Rect2I Bounds
  44. {
  45. get
  46. {
  47. Rect2I bounds;
  48. Internal_GetBounds(mCachedPtr, out bounds);
  49. return bounds;
  50. }
  51. set { Internal_SetBounds(mCachedPtr, ref value); }
  52. }
  53. /// <summary>
  54. /// Returns the non-clipped bounds of the GUI element in screen space.
  55. /// </summary>
  56. public Rect2I ScreenBounds
  57. {
  58. get
  59. {
  60. Rect2I bounds;
  61. Internal_GetScreenBounds(mCachedPtr, out bounds);
  62. return bounds;
  63. }
  64. }
  65. /// <summary>
  66. /// Returns the non-clipped bounds of the GUI element including the margins. Relative to a parent GUI panel.
  67. /// </summary>
  68. public Rect2I VisualBounds
  69. {
  70. get
  71. {
  72. Rect2I bounds;
  73. Internal_GetVisualBounds(mCachedPtr, out bounds);
  74. return bounds;
  75. }
  76. }
  77. /// <summary>
  78. /// Makes the element hidden or visible. This will not affect the layout as the room for the element will still
  79. /// be reserved in the parent layout, use <see cref="Active"/> if you need to affect the layout as well.
  80. /// </summary>
  81. public bool Visible
  82. {
  83. set { Internal_SetVisible(mCachedPtr, value); }
  84. get { return Internal_GetVisible(mCachedPtr); }
  85. }
  86. /// <summary>
  87. /// Activates or deactivates the element, making it hidden or visible. When disabled it is essentially removed from
  88. /// the parent achieving the similar effect as if the element was destroyed.
  89. /// </summary>
  90. public bool Active
  91. {
  92. set { Internal_SetActive(mCachedPtr, value); }
  93. get { return Internal_GetActive(mCachedPtr); }
  94. }
  95. /// <summary>
  96. /// Disables or enables the element. Disabled elements cannot be interacted with and have a faded out appearance.
  97. /// </summary>
  98. public bool Disabled
  99. {
  100. set { Internal_SetDisabled(mCachedPtr, value); }
  101. get { return Internal_GetDisabled(mCachedPtr); }
  102. }
  103. /// <summary>
  104. /// Assigns or removes keyboard focus on this element.
  105. /// </summary>
  106. public bool Focus
  107. {
  108. set { Internal_SetFocus(mCachedPtr, value); }
  109. }
  110. /// <summary>
  111. /// Determines will this element block elements underneath it from receiving events like pointer click, hover
  112. /// on/off or be able to gain focus. True by default.
  113. /// </summary>
  114. public bool Blocking
  115. {
  116. get { return Internal_GetBlocking(mCachedPtr); }
  117. set { Internal_SetBlocking(mCachedPtr, value); }
  118. }
  119. /// <summary>
  120. /// Determines if the element can be navigated to by using keys/buttons (e.g. the 'Tab' button on the keyboard.
  121. /// </summary>
  122. public bool AcceptsKeyFocus
  123. {
  124. get { return Internal_GetAcceptsKeyFocus(mCachedPtr); }
  125. set { Internal_SetAcceptsKeyFocus(mCachedPtr, value); }
  126. }
  127. /// <summary>
  128. /// Destroys this element and all its children. Removes the element from parent layout/panel.
  129. /// </summary>
  130. /// <remarks>
  131. /// Calling methods on a destroyed element is a no-operation. Destroyed elements aren't allowed to be assigned as
  132. /// children of other elements, or be parents of other elements.
  133. /// </remarks>
  134. public virtual void Destroy()
  135. {
  136. Internal_Destroy(mCachedPtr);
  137. }
  138. /// <summary>
  139. /// Sets element position relative to parent GUI panel.
  140. /// </summary>
  141. /// <param name="x">X position of the element in pixels, relative to parent GUI panel.</param>
  142. /// <param name="y">Y position of the element in pixels, relative to parent GUI panel.</param>
  143. /// <remarks>
  144. /// Be aware that this value will be ignored if GUI element is part of a layout because the layout controls placement
  145. /// of child elements.
  146. /// </remarks>
  147. public void SetPosition(int x, int y)
  148. {
  149. Internal_SetPosition(mCachedPtr, x, y);
  150. }
  151. /// <summary>
  152. /// Sets a fixed element width.
  153. /// </summary>
  154. /// <param name="width">Width in pixels.</param>
  155. public void SetWidth(int width)
  156. {
  157. Internal_SetWidth(mCachedPtr, width);
  158. }
  159. /// <summary>
  160. /// Sets a flexible element width. Element will be resized according to its contents and parent layout but will
  161. /// always stay within the provided range.
  162. /// </summary>
  163. /// <param name="minWidth">Minimum width in pixels. Element will never be smaller than this width.</param>
  164. /// <param name="maxWidth">Maximum width in pixels. Element will never be larger than this width. Specify zero for
  165. /// unlimited width.</param>
  166. public void SetFlexibleWidth(int minWidth, int maxWidth)
  167. {
  168. Internal_SetFlexibleWidth(mCachedPtr, minWidth, maxWidth);
  169. }
  170. /// <summary>
  171. /// Sets a fixed element height.
  172. /// </summary>
  173. /// <param name="height">Height in pixels.</param>
  174. public void SetHeight(int height)
  175. {
  176. Internal_SetHeight(mCachedPtr, height);
  177. }
  178. /// <summary>
  179. /// Sets a flexible element height. Element will be resized according to its contents and parent layout but will
  180. /// always stay within the provided range.
  181. /// </summary>
  182. /// <param name="minHeight">Minimum height in pixels. Element will never be smaller than this height.</param>
  183. /// <param name="maxHeight">Maximum height in pixels. Element will never be larger than this height. Specify zero
  184. /// for unlimited height.</param>
  185. public void SetFlexibleHeight(int minHeight, int maxHeight)
  186. {
  187. Internal_SetFlexibleHeight(mCachedPtr, minHeight, maxHeight);
  188. }
  189. /// <summary>
  190. /// Resets element bounds to their initial values dictated by the element's style.
  191. /// </summary>
  192. public void ResetDimensions()
  193. {
  194. Internal_ResetDimensions(mCachedPtr);
  195. }
  196. /// <summary>
  197. /// Assigns a new context menu that will be opened when the element is right clicked.
  198. /// </summary>
  199. /// <param name="menu">Object containing context menu contents. Can be null if no menu is wanted.</param>
  200. public void SetContextMenu(ContextMenu menu)
  201. {
  202. IntPtr menuPtr = IntPtr.Zero;
  203. if (menu != null)
  204. menuPtr = menu.GetCachedPtr();
  205. Internal_SetContextMenu(mCachedPtr, menuPtr);
  206. }
  207. /// <summary>
  208. /// Triggered by the native interop object when the element gains keyboard focus.
  209. /// </summary>
  210. private void Internal_OnFocusGained()
  211. {
  212. if (OnFocusGained != null)
  213. OnFocusGained();
  214. }
  215. /// <summary>
  216. /// Triggered by the native interop object when the element loses keyboard focus.
  217. /// </summary>
  218. private void Internal_OnFocusLost()
  219. {
  220. if (OnFocusLost != null)
  221. OnFocusLost();
  222. }
  223. [MethodImpl(MethodImplOptions.InternalCall)]
  224. private static extern bool Internal_GetVisible(IntPtr nativeInstance);
  225. [MethodImpl(MethodImplOptions.InternalCall)]
  226. private static extern void Internal_SetVisible(IntPtr nativeInstance, bool visible);
  227. [MethodImpl(MethodImplOptions.InternalCall)]
  228. private static extern bool Internal_GetActive(IntPtr nativeInstance);
  229. [MethodImpl(MethodImplOptions.InternalCall)]
  230. private static extern void Internal_SetActive(IntPtr nativeInstance, bool enabled);
  231. [MethodImpl(MethodImplOptions.InternalCall)]
  232. private static extern bool Internal_GetDisabled(IntPtr nativeInstance);
  233. [MethodImpl(MethodImplOptions.InternalCall)]
  234. private static extern void Internal_SetDisabled(IntPtr nativeInstance, bool disabled);
  235. [MethodImpl(MethodImplOptions.InternalCall)]
  236. private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
  237. [MethodImpl(MethodImplOptions.InternalCall)]
  238. private static extern bool Internal_GetBlocking(IntPtr nativeInstance);
  239. [MethodImpl(MethodImplOptions.InternalCall)]
  240. private static extern void Internal_SetBlocking(IntPtr nativeInstance, bool blocking);
  241. [MethodImpl(MethodImplOptions.InternalCall)]
  242. private static extern bool Internal_GetAcceptsKeyFocus(IntPtr nativeInstance);
  243. [MethodImpl(MethodImplOptions.InternalCall)]
  244. private static extern void Internal_SetAcceptsKeyFocus(IntPtr nativeInstance, bool accepts);
  245. [MethodImpl(MethodImplOptions.InternalCall)]
  246. private static extern GUILayout Internal_GetParent(IntPtr nativeInstance);
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. private static extern void Internal_SetPosition(IntPtr nativeInstance, int x, int y);
  249. [MethodImpl(MethodImplOptions.InternalCall)]
  250. private static extern void Internal_SetWidth(IntPtr nativeInstance, int width);
  251. [MethodImpl(MethodImplOptions.InternalCall)]
  252. private static extern void Internal_SetFlexibleWidth(IntPtr nativeInstance, int minWidth, int maxWidth);
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. private static extern void Internal_SetHeight(IntPtr nativeInstance, int height);
  255. [MethodImpl(MethodImplOptions.InternalCall)]
  256. private static extern void Internal_SetFlexibleHeight(IntPtr nativeInstance, int minHeight, int maxHeight);
  257. [MethodImpl(MethodImplOptions.InternalCall)]
  258. private static extern void Internal_ResetDimensions(IntPtr nativeInstance);
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I value);
  261. [MethodImpl(MethodImplOptions.InternalCall)]
  262. private static extern void Internal_SetBounds(IntPtr nativeInstance, ref Rect2I value);
  263. [MethodImpl(MethodImplOptions.InternalCall)]
  264. private static extern void Internal_GetVisualBounds(IntPtr nativeInstance, out Rect2I value);
  265. [MethodImpl(MethodImplOptions.InternalCall)]
  266. private static extern void Internal_GetScreenBounds(IntPtr nativeInstance, out Rect2I value);
  267. [MethodImpl(MethodImplOptions.InternalCall)]
  268. private static extern void Internal_SetContextMenu(IntPtr nativeInstance, IntPtr contextMenu);
  269. [MethodImpl(MethodImplOptions.InternalCall)]
  270. private static extern string Internal_GetStyle(IntPtr nativeInstance);
  271. [MethodImpl(MethodImplOptions.InternalCall)]
  272. private static extern void Internal_SetStyle(IntPtr nativeInstance, string style);
  273. [MethodImpl(MethodImplOptions.InternalCall)]
  274. private static extern void Internal_Destroy(IntPtr nativeInstance);
  275. }
  276. /** @} */
  277. }