GUILayout.cs 13 KB

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