GUIElement.cs 7.6 KB

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