GUIElement.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 or loses keyboard focus.
  14. /// </summary>
  15. public Action<bool> OnFocusChanged;
  16. /// <summary>
  17. /// Gets or sets non-clipped bounds of the GUI element. Relative to a parent GUI panel.
  18. /// </summary>
  19. public Rect2I Bounds
  20. {
  21. get { return Internal_GetBounds(mCachedPtr); }
  22. set { Internal_SetBounds(mCachedPtr, value); }
  23. }
  24. /// <summary>
  25. /// Gets or sets non-clipped bounds of the GUI element including the margins. Relative to a parent GUI panel.
  26. /// </summary>
  27. public Rect2I VisualBounds
  28. {
  29. get { return Internal_GetVisualBounds(mCachedPtr); }
  30. }
  31. /// <summary>
  32. /// Makes the element hidden or visible. This will not affect the layout as the room for the element will still
  33. /// be reserved in the parent layout, use <see cref="Enabled"/> if you need to affect the layout as well.
  34. /// </summary>
  35. public bool Visible
  36. {
  37. set { Internal_SetVisible(mCachedPtr, value); }
  38. }
  39. /// <summary>
  40. /// Disables or enables an element, making it hidden or visible. When disabled it is essentially removed from the
  41. /// parent achieving the similar effect as if the element was destroyed.
  42. /// </summary>
  43. public bool Enabled
  44. {
  45. set { Internal_SetEnabled(mCachedPtr, value); }
  46. }
  47. /// <summary>
  48. /// Assigns or removes keyboard focus on this element.
  49. /// </summary>
  50. public bool Focus
  51. {
  52. set { Internal_SetFocus(mCachedPtr, value); }
  53. }
  54. /// <summary>
  55. /// Destroys this element and all its children. Removes the element from parent layout/panel.
  56. /// </summary>
  57. /// <remarks>
  58. /// Calling methods on a destroyed element is a no-operation. Destroyed elements aren't allowed to be assigned as
  59. /// children of other elements, or be parents of other elements.
  60. /// </remarks>
  61. public virtual void Destroy()
  62. {
  63. Internal_Destroy(mCachedPtr);
  64. }
  65. /// <summary>
  66. /// Sets element position relative to parent GUI panel.
  67. /// </summary>
  68. /// <param name="x">X position of the element in pixels, relative to parent GUI panel.</param>
  69. /// <param name="y">Y position of the element in pixels, relative to parent GUI panel.</param>
  70. /// <remarks>
  71. /// Be aware that this value will be ignored if GUI element is part of a layout because the layout controls placement
  72. /// of child elements.
  73. /// </remarks>
  74. public void SetPosition(int x, int y)
  75. {
  76. Internal_SetPosition(mCachedPtr, x, y);
  77. }
  78. /// <summary>
  79. /// Sets a fixed element width.
  80. /// </summary>
  81. /// <param name="width">Width in pixels.</param>
  82. public void SetWidth(int width)
  83. {
  84. Internal_SetWidth(mCachedPtr, width);
  85. }
  86. /// <summary>
  87. /// Sets a flexible element width. Element will be resized according to its contents and parent layout but will
  88. /// always stay within the provided range.
  89. /// </summary>
  90. /// <param name="minWidth">Minimum width in pixels. Element will never be smaller than this width.</param>
  91. /// <param name="maxWidth">Maximum width in pixels. Element will never be larger than this width. Specify zero for
  92. /// unlimited width.</param>
  93. public void SetFlexibleWidth(int minWidth, int maxWidth)
  94. {
  95. Internal_SetFlexibleWidth(mCachedPtr, minWidth, maxWidth);
  96. }
  97. /// <summary>
  98. /// Sets a fixed element height.
  99. /// </summary>
  100. /// <param name="height">Height in pixels.</param>
  101. public void SetHeight(int height)
  102. {
  103. Internal_SetHeight(mCachedPtr, height);
  104. }
  105. /// <summary>
  106. /// Sets a flexible element height. Element will be resized according to its contents and parent layout but will
  107. /// always stay within the provided range.
  108. /// </summary>
  109. /// <param name="minHeight">Minimum height in pixels. Element will never be smaller than this height.</param>
  110. /// <param name="maxHeight">Maximum height in pixels. Element will never be larger than this height. Specify zero
  111. /// for unlimited height.</param>
  112. public void SetFlexibleHeight(int minHeight, int maxHeight)
  113. {
  114. Internal_SetFlexibleHeight(mCachedPtr, minHeight, maxHeight);
  115. }
  116. /// <summary>
  117. /// Resets element bounds to their initial values dictated by the element's style.
  118. /// </summary>
  119. public void ResetDimensions()
  120. {
  121. Internal_ResetDimensions(mCachedPtr);
  122. }
  123. /// <summary>
  124. /// Assigns a new context menu that will be opened when the element is right clicked.
  125. /// </summary>
  126. /// <param name="menu">Object containing context menu contents. Can be null if no menu is wanted.</param>
  127. public void SetContextMenu(ContextMenu menu)
  128. {
  129. IntPtr menuPtr = IntPtr.Zero;
  130. if (menu != null)
  131. menuPtr = menu.GetCachedPtr();
  132. Internal_SetContextMenu(mCachedPtr, menuPtr);
  133. }
  134. /// <summary>
  135. /// Triggered by the native interop object when the keyboard focus of this element changes.
  136. /// </summary>
  137. private void InternalOnFocusChanged(bool focus)
  138. {
  139. if (OnFocusChanged != null)
  140. OnFocusChanged(focus);
  141. }
  142. [MethodImpl(MethodImplOptions.InternalCall)]
  143. private static extern void Internal_SetVisible(IntPtr nativeInstance, bool visible);
  144. [MethodImpl(MethodImplOptions.InternalCall)]
  145. private static extern void Internal_SetEnabled(IntPtr nativeInstance, bool enabled);
  146. [MethodImpl(MethodImplOptions.InternalCall)]
  147. private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. private static extern void Internal_SetPosition(IntPtr nativeInstance, int x, int y);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern void Internal_SetWidth(IntPtr nativeInstance, int width);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern void Internal_SetFlexibleWidth(IntPtr nativeInstance, int minWidth, int maxWidth);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. private static extern void Internal_SetHeight(IntPtr nativeInstance, int height);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. private static extern void Internal_SetFlexibleHeight(IntPtr nativeInstance, int minHeight, int maxHeight);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. private static extern void Internal_ResetDimensions(IntPtr nativeInstance);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. private static extern Rect2I Internal_GetBounds(IntPtr nativeInstance);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern void Internal_SetBounds(IntPtr nativeInstance, Rect2I value);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern Rect2I Internal_GetVisualBounds(IntPtr nativeInstance);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. private static extern void Internal_SetContextMenu(IntPtr nativeInstance, IntPtr contextMenu);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. private static extern void Internal_Destroy(IntPtr nativeInstance);
  170. }
  171. }