2
0

GUIElement.cs 11 KB

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