GUIElement.cs 10.0 KB

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