GUILayout.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup GUI_Engine
  9. * @{
  10. */
  11. /// <summary>
  12. /// Base class for GUI layout implementations. GUI layouts serve as containers
  13. /// for GUI elements that position and resize the elements automatically with
  14. /// respect to layout rules set on the elements.
  15. /// </summary>
  16. public abstract class GUILayout : GUIElement
  17. {
  18. /// <summary>
  19. /// Returns number of child elements in the layout.
  20. /// </summary>
  21. public int ChildCount
  22. {
  23. get { return Internal_GetChildCount(mCachedPtr); }
  24. }
  25. /// <summary>
  26. /// Adds a new element to the layout after all existing elements.
  27. /// </summary>
  28. /// <param name="element">GUI element to add.</param>
  29. public void AddElement(GUIElement element)
  30. {
  31. if(element != null)
  32. Internal_AddElement(mCachedPtr, element.mCachedPtr);
  33. }
  34. /// <summary>
  35. /// Inserts a GUI element before the element at the specified index.
  36. /// </summary>
  37. /// <param name="index">Index to insert the GUI element at. This must be in range [0, GetNumChildren()).</param>
  38. /// <param name="element">GUI element to insert.</param>
  39. public void InsertElement(int index, GUIElement element)
  40. {
  41. if(index < 0 || index > ChildCount)
  42. throw new ArgumentOutOfRangeException("index", index, "Index out of range.");
  43. if (element != null)
  44. Internal_InsertElement(mCachedPtr, index, element.mCachedPtr);
  45. }
  46. /// <summary>
  47. /// Gets a child elements at the specified index in the layout.
  48. /// </summary>
  49. /// <param name="index">Index of the element to retrieve. This must be in range [0, GetNumChildren()).</param>
  50. /// <returns>GUI element at the specified index, or null if the index is invalid.</returns>
  51. public GUIElement GetChild(int index)
  52. {
  53. return Internal_GetChild(mCachedPtr, index);
  54. }
  55. /// <summary>
  56. /// Adds a new horizontal layout as a child of this layout. Layout is inserted after all existing elements.
  57. /// </summary>
  58. /// <param name="options">Options that allow you to control how is the layout positioned and sized.</param>
  59. /// <returns>Newly created horizontal layout.</returns>
  60. public GUILayoutX AddLayoutX(params GUIOption[] options)
  61. {
  62. GUILayoutX layout = new GUILayoutX(options);
  63. AddElement(layout);
  64. return layout;
  65. }
  66. /// <summary>
  67. /// Adds a new vertical layout as a child of this layout. Layout is inserted after all existing elements.
  68. /// </summary>
  69. /// <param name="options">Options that allow you to control how is the layout positioned and sized.</param>
  70. /// <returns>Newly created vertical layout.</returns>
  71. public GUILayoutY AddLayoutY(params GUIOption[] options)
  72. {
  73. GUILayoutY layout = new GUILayoutY(options);
  74. AddElement(layout);
  75. return layout;
  76. }
  77. /// <summary>
  78. /// Adds a new GUI panel as a child of this layout. Panel is inserted after all existing elements.
  79. /// </summary>
  80. /// <param name="options">Options that allow you to control how is the panel positioned and sized.</param>
  81. /// <returns>Newly created GUI panel.</returns>
  82. public GUIPanel AddPanel(params GUIOption[] options)
  83. {
  84. GUIPanel layout = new GUIPanel(options);
  85. AddElement(layout);
  86. return layout;
  87. }
  88. /// <summary>
  89. /// Adds a new GUI panel as a child of this layout. Panel is inserted after all existing elements.
  90. /// </summary>
  91. /// <param name="depth">Depth at which to position the panel. Panels with lower depth will be displayed in front of
  92. /// panels with higher depth. Provided depth is relative to the depth of the parent GUI panel.
  93. /// The depth value will be clamped if outside of the depth range of the parent GUI panel.</param>
  94. /// <param name="depthRangeMin">Smallest depth offset allowed by any child GUI panels. If a child panel has a depth
  95. /// offset lower than this value it will be clamped.</param>
  96. /// <param name="depthRangeMax">Largest depth offset allowed by any child GUI panels. If a child panel has a depth
  97. /// offset higher than this value it will be clamped.</param>
  98. /// <param name="options">Options that allow you to control how is the panel positioned and sized.</param>
  99. /// <returns>Newly created GUI panel.</returns>
  100. public GUIPanel AddPanel(Int16 depth = 0, ushort depthRangeMin = ushort.MaxValue,
  101. ushort depthRangeMax = ushort.MaxValue, params GUIOption[] options)
  102. {
  103. GUIPanel layout = new GUIPanel(depth, depthRangeMin, depthRangeMax, options);
  104. AddElement(layout);
  105. return layout;
  106. }
  107. /// <summary>
  108. /// Adds a new flexible space as a child of this layout. Flexible space expands
  109. /// to fill all available space in the layout. Space is inserted after all existing elements.
  110. /// </summary>
  111. /// <returns>Newly created flexible space.</returns>
  112. public GUIFlexibleSpace AddFlexibleSpace()
  113. {
  114. GUIFlexibleSpace space = new GUIFlexibleSpace();
  115. AddElement(space);
  116. return space;
  117. }
  118. /// <summary>
  119. /// Adds a new fixed space object. Fixed space inserts a blank space with specific
  120. /// width or height (depending on layout type) in the layout. Space is inserted after all existing elements.
  121. /// </summary>
  122. /// <param name="size">Size of the space in pixels. This will represent either width or height depending whether the
  123. /// layout is vertical or horizontal.</param>
  124. /// <returns>Newly created fixed space.</returns>
  125. public GUIFixedSpace AddSpace(int size)
  126. {
  127. GUIFixedSpace space = new GUIFixedSpace(size);
  128. AddElement(space);
  129. return space;
  130. }
  131. /// <summary>
  132. /// Adds a new horizontal layout as a child of this layout. Layout is inserted
  133. /// before the element at the specified index.
  134. /// </summary>
  135. /// <param name="idx">Index to insert the layout at. This must be in range [0, GetNumChildren()).</param>
  136. /// <param name="options">Options that allow you to control how is the layout positioned and sized.</param>
  137. /// <returns>Newly created horizontal layout.</returns>
  138. public GUILayoutX InsertLayoutX(int idx, params GUIOption[] options)
  139. {
  140. GUILayoutX layout = new GUILayoutX(options);
  141. InsertElement(idx, layout);
  142. return layout;
  143. }
  144. /// <summary>
  145. /// Adds a new vertical layout as a child of this layout. Layout is inserted
  146. /// before the element at the specified index.
  147. /// </summary>
  148. /// <param name="idx">Index to insert the layout at. This must be in range [0, GetNumChildren()).</param>
  149. /// <param name="options">Options that allow you to control how is the layout positioned and sized.</param>
  150. /// <returns>Newly created vertical layout.</returns>
  151. public GUILayoutY InsertLayoutY(int idx, params GUIOption[] options)
  152. {
  153. GUILayoutY layout = new GUILayoutY(options);
  154. InsertElement(idx, layout);
  155. return layout;
  156. }
  157. /// <summary>
  158. /// Adds a new GUI panel as a child of this layout. Panel is inserted before the element at the specified index.
  159. /// </summary>
  160. /// <param name="idx">Index to insert the panel at. This must be in range [0, GetNumChildren()).</param>
  161. /// <param name="options">Options that allow you to control how is the panel positioned and sized.</param>
  162. /// <returns>Newly created GUI panel.</returns>
  163. public GUIPanel InsertPanel(int idx, params GUIOption[] options)
  164. {
  165. GUIPanel layout = new GUIPanel(options);
  166. InsertElement(idx, layout);
  167. return layout;
  168. }
  169. /// <summary>
  170. /// Adds a new GUI panel as a child of this layout. Panel is inserted before the element at the specified index.
  171. /// </summary>
  172. /// <param name="idx">Index at which to insert the panel.</param>
  173. /// <param name="depth">Depth at which to position the panel. Panels with lower depth will be displayed in front of
  174. /// panels with higher depth. Provided depth is relative to the depth of the parent GUI panel.
  175. /// The depth value will be clamped if outside of the depth range of the parent GUI panel.</param>
  176. /// <param name="depthRangeMin">Smallest depth offset allowed by any child GUI panels. If a child panel has a depth
  177. /// offset lower than this value it will be clamped.</param>
  178. /// <param name="depthRangeMax">Largest depth offset allowed by any child GUI panels. If a child panel has a depth
  179. /// offset higher than this value it will be clamped.</param>
  180. /// <param name="options">Options that allow you to control how is the panel positioned and sized.</param>
  181. /// <returns>Newly created GUI panel.</returns>
  182. public GUIPanel InsertPanel(int idx, Int16 depth = 0, ushort depthRangeMin = ushort.MaxValue,
  183. ushort depthRangeMax = ushort.MaxValue, params GUIOption[] options)
  184. {
  185. GUIPanel layout = new GUIPanel(depth, depthRangeMin, depthRangeMax, options);
  186. InsertElement(idx, layout);
  187. return layout;
  188. }
  189. /// <summary>
  190. /// Adds a new flexible space as a child of this layout. Flexible space expands to fill all available space in the
  191. /// layout. is inserted before the element at the specified index.
  192. /// </summary>
  193. /// <returns>Newly created flexible space.</returns>
  194. public GUIFlexibleSpace InsertFlexibleSpace(int idx)
  195. {
  196. GUIFlexibleSpace space = new GUIFlexibleSpace();
  197. InsertElement(idx, space);
  198. return space;
  199. }
  200. /// <summary>
  201. /// Adds a new fixed space object. Fixed space inserts a blank space with specific width or height (depending on
  202. /// layout type) in the layout. Space is inserted after all existing elements.
  203. /// </summary>
  204. /// <param name="idx">Index at which to insert the space.</param>
  205. /// <param name="size">Size of the space in pixels. This will represent either width or height depending whether the
  206. /// layout is vertical or horizontal.</param>
  207. /// <returns>Newly created fixed space.</returns>
  208. public GUIFixedSpace InsertSpace(int idx, int size)
  209. {
  210. GUIFixedSpace space = new GUIFixedSpace(size);
  211. InsertElement(idx, space);
  212. return space;
  213. }
  214. /// <summary>
  215. /// Destroys all children of this layout.
  216. /// </summary>
  217. public void Clear()
  218. {
  219. Internal_Clear(mCachedPtr);
  220. }
  221. [MethodImpl(MethodImplOptions.InternalCall)]
  222. protected static extern void Internal_CreateInstanceYFromScrollArea(GUILayout instance, GUIScrollArea parentArea);
  223. [MethodImpl(MethodImplOptions.InternalCall)]
  224. protected static extern void Internal_CreateInstanceX(GUILayout instance, GUIOption[] options);
  225. [MethodImpl(MethodImplOptions.InternalCall)]
  226. protected static extern void Internal_CreateInstanceY(GUILayout instance, GUIOption[] options);
  227. [MethodImpl(MethodImplOptions.InternalCall)]
  228. protected static extern void Internal_CreateInstancePanel(GUILayout instance, Int16 depth, ushort depthRangeMin,
  229. ushort depthRangeMax, GUIOption[] options);
  230. [MethodImpl(MethodImplOptions.InternalCall)]
  231. protected static extern void Internal_AddElement(IntPtr instance, IntPtr element);
  232. [MethodImpl(MethodImplOptions.InternalCall)]
  233. protected static extern void Internal_InsertElement(IntPtr instance, int index, IntPtr element);
  234. [MethodImpl(MethodImplOptions.InternalCall)]
  235. protected static extern int Internal_GetChildCount(IntPtr instance);
  236. [MethodImpl(MethodImplOptions.InternalCall)]
  237. protected static extern GUIElement Internal_GetChild(IntPtr instance, int index);
  238. [MethodImpl(MethodImplOptions.InternalCall)]
  239. protected static extern void Internal_Clear(IntPtr instance);
  240. }
  241. /** @} */
  242. }