GUILayout.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.
  89. /// The depth value will be clamped if outside of the depth range of the parent GUI panel.</param>
  90. /// <param name="depthRangeMin">Smallest depth offset allowed by any child GUI panels. If a child panel has a depth
  91. /// offset lower than this value it will be clamped.</param>
  92. /// <param name="depthRangeMax">Largest depth offset allowed by any child GUI panels. If a child panel has a depth
  93. /// offset higher than this value it will be clamped.</param>
  94. /// <param name="options">Options that allow you to control how is the panel positioned and sized.</param>
  95. /// <returns>Newly created GUI panel.</returns>
  96. public GUIPanel AddPanel(Int16 depth = 0, ushort depthRangeMin = ushort.MaxValue,
  97. ushort depthRangeMax = ushort.MaxValue, params GUIOption[] options)
  98. {
  99. GUIPanel layout = new GUIPanel(depth, depthRangeMin, depthRangeMax, options);
  100. AddElement(layout);
  101. return layout;
  102. }
  103. /// <summary>
  104. /// Adds a new flexible space as a child of this layout. Flexible space expands
  105. /// to fill all available space in the layout. Space is inserted after all existing elements.
  106. /// </summary>
  107. /// <returns>Newly created flexible space.</returns>
  108. public GUIFlexibleSpace AddFlexibleSpace()
  109. {
  110. GUIFlexibleSpace space = new GUIFlexibleSpace();
  111. AddElement(space);
  112. return space;
  113. }
  114. /// <summary>
  115. /// Adds a new fixed space object. Fixed space inserts a blank space with specific
  116. /// width or height (depending on layout type) in the layout. Space is inserted after all existing elements.
  117. /// </summary>
  118. /// <param name="size">Size of the space in pixels. This will represent either width or height depending whether the
  119. /// layout is vertical or horizontal.</param>
  120. /// <returns>Newly created fixed space.</returns>
  121. public GUIFixedSpace AddSpace(int size)
  122. {
  123. GUIFixedSpace space = new GUIFixedSpace(size);
  124. AddElement(space);
  125. return space;
  126. }
  127. /// <summary>
  128. /// Adds a new horizontal layout as a child of this layout. Layout is inserted
  129. /// before the element at the specified index.
  130. /// </summary>
  131. /// <param name="idx">Index to insert the layout at. This must be in range [0, GetNumChildren()).</param>
  132. /// <param name="options">Options that allow you to control how is the layout positioned and sized.</param>
  133. /// <returns>Newly created horizontal layout.</returns>
  134. public GUILayoutX InsertLayoutX(int idx, params GUIOption[] options)
  135. {
  136. GUILayoutX layout = new GUILayoutX(options);
  137. InsertElement(idx, layout);
  138. return layout;
  139. }
  140. /// <summary>
  141. /// Adds a new vertical layout as a child of this layout. Layout is inserted
  142. /// before the element at the specified index.
  143. /// </summary>
  144. /// <param name="idx">Index to insert the layout at. This must be in range [0, GetNumChildren()).</param>
  145. /// <param name="options">Options that allow you to control how is the layout positioned and sized.</param>
  146. /// <returns>Newly created vertical layout.</returns>
  147. public GUILayoutY InsertLayoutY(int idx, params GUIOption[] options)
  148. {
  149. GUILayoutY layout = new GUILayoutY(options);
  150. InsertElement(idx, layout);
  151. return layout;
  152. }
  153. /// <summary>
  154. /// Adds a new GUI panel as a child of this layout. Panel is inserted before the element at the specified index.
  155. /// </summary>
  156. /// <param name="idx">Index to insert the panel at. This must be in range [0, GetNumChildren()).</param>
  157. /// <param name="options">Options that allow you to control how is the panel positioned and sized.</param>
  158. /// <returns>Newly created GUI panel.</returns>
  159. public GUIPanel InsertPanel(int idx, params GUIOption[] options)
  160. {
  161. GUIPanel layout = new GUIPanel(options);
  162. InsertElement(idx, layout);
  163. return layout;
  164. }
  165. /// <summary>
  166. /// Adds a new GUI panel as a child of this layout. Panel is inserted before the element at the specified index.
  167. /// </summary>
  168. /// <param name="depth">Depth at which to position the panel. Panels with lower depth will be displayed in front of
  169. /// panels with higher depth. Provided depth is relative to the depth of the parent GUI panel.
  170. /// The depth value will be clamped if outside of the depth range of the parent GUI panel.</param>
  171. /// <param name="depthRangeMin">Smallest depth offset allowed by any child GUI panels. If a child panel has a depth
  172. /// offset lower than this value it will be clamped.</param>
  173. /// <param name="depthRangeMax">Largest depth offset allowed by any child GUI panels. If a child panel has a depth
  174. /// offset higher than this value it will be clamped.</param>
  175. /// <param name="options">Options that allow you to control how is the panel positioned and sized.</param>
  176. /// <returns>Newly created GUI panel.</returns>
  177. public GUIPanel InsertPanel(int idx, Int16 depth = 0, ushort depthRangeMin = ushort.MaxValue,
  178. ushort depthRangeMax = ushort.MaxValue, params GUIOption[] options)
  179. {
  180. GUIPanel layout = new GUIPanel(depth, depthRangeMin, depthRangeMax, options);
  181. InsertElement(idx, layout);
  182. return layout;
  183. }
  184. /// <summary>
  185. /// Adds a new flexible space as a child of this layout. Flexible space expands to fill all available space in the
  186. /// layout. is inserted before the element at the specified index.
  187. /// </summary>
  188. /// <returns>Newly created flexible space.</returns>
  189. public GUIFlexibleSpace InsertFlexibleSpace(int idx)
  190. {
  191. GUIFlexibleSpace space = new GUIFlexibleSpace();
  192. InsertElement(idx, space);
  193. return space;
  194. }
  195. /// <summary>
  196. /// Adds a new fixed space object. Fixed space inserts a blank space with specific width or height (depending on
  197. /// layout type) in the layout. Space is inserted after all existing elements.
  198. /// </summary>
  199. /// <param name="size">Size of the space in pixels. This will represent either width or height depending whether the
  200. /// layout is vertical or horizontal.</param>
  201. /// <returns>Newly created fixed space.</returns>
  202. public GUIFixedSpace InsertSpace(int idx, int size)
  203. {
  204. GUIFixedSpace space = new GUIFixedSpace(size);
  205. InsertElement(idx, space);
  206. return space;
  207. }
  208. [MethodImpl(MethodImplOptions.InternalCall)]
  209. protected static extern void Internal_CreateInstanceYFromScrollArea(GUILayout instance, GUIScrollArea parentArea);
  210. [MethodImpl(MethodImplOptions.InternalCall)]
  211. protected static extern void Internal_CreateInstanceX(GUILayout instance, GUIOption[] options);
  212. [MethodImpl(MethodImplOptions.InternalCall)]
  213. protected static extern void Internal_CreateInstanceY(GUILayout instance, GUIOption[] options);
  214. [MethodImpl(MethodImplOptions.InternalCall)]
  215. protected static extern void Internal_CreateInstancePanel(GUILayout instance, Int16 depth, ushort depthRangeMin,
  216. ushort depthRangeMax, GUIOption[] options);
  217. [MethodImpl(MethodImplOptions.InternalCall)]
  218. protected static extern void Internal_AddElement(IntPtr instance, IntPtr element);
  219. [MethodImpl(MethodImplOptions.InternalCall)]
  220. protected static extern void Internal_InsertElement(IntPtr instance, int index, IntPtr element);
  221. [MethodImpl(MethodImplOptions.InternalCall)]
  222. protected static extern int Internal_GetChildCount(IntPtr instance);
  223. [MethodImpl(MethodImplOptions.InternalCall)]
  224. protected static extern GUIElement Internal_GetChild(IntPtr instance, int index);
  225. }
  226. }