GUIElement.cs 12 KB

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