GUILayout.cs 13 KB

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