GUIElement.cs 7.5 KB

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