GUILayout.cs 12 KB

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