GUILayout.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. namespace BansheeEngine
  5. {
  6. public abstract class GUILayout : GUIElement
  7. {
  8. internal List<GUIElement> children = new List<GUIElement>();
  9. internal bool AddElementInternal(GUIElement element)
  10. {
  11. if (IsDestroyed())
  12. {
  13. Debug.LogWarning("Attempting to add an element to a destroyed layout. Ignoring operation.");
  14. return false;
  15. }
  16. if (!children.Contains(element))
  17. {
  18. element.SetParent(this);
  19. children.Add(element);
  20. return true;
  21. }
  22. return false;
  23. }
  24. internal bool InsertElementInternal(int index, GUIElement element)
  25. {
  26. if (IsDestroyed())
  27. {
  28. Debug.LogWarning("Attempting to add an element to a destroyed layout. Ignoring operation.");
  29. return false;
  30. }
  31. if (!children.Contains(element))
  32. {
  33. element.SetParent(this);
  34. children.Insert(index, element);
  35. return true;
  36. }
  37. return false;
  38. }
  39. internal void RemoveInternal(GUIElement element)
  40. {
  41. children.Remove(element);
  42. }
  43. internal override void SetParent(GUILayout layout)
  44. {
  45. if (parent != null)
  46. parent.RemoveInternal(this);
  47. parent = layout;
  48. if (parent != null)
  49. parent.children.Add(this);
  50. }
  51. public void AddElement(GUIElement element)
  52. {
  53. if(AddElementInternal(element))
  54. Internal_AddElement(mCachedPtr, element.mCachedPtr);
  55. }
  56. public void InsertElement(int index, GUIElement element)
  57. {
  58. if (InsertElementInternal(index, element))
  59. Internal_InsertElement(mCachedPtr, index, element.mCachedPtr);
  60. }
  61. public void Remove(GUIElement element)
  62. {
  63. if (children.Contains(element))
  64. element.SetParent(null);
  65. }
  66. public void Remove(int childIdx)
  67. {
  68. if (childIdx >= 0 && childIdx < children.Count)
  69. {
  70. GUIElement element = children[childIdx];
  71. element.SetParent(null);
  72. }
  73. }
  74. public int GetNumChildren()
  75. {
  76. return children.Count;
  77. }
  78. public GUIElement GetChild(int index)
  79. {
  80. if (index < 0 || index >= children.Count)
  81. return null;
  82. return children[index];
  83. }
  84. public override void Destroy()
  85. {
  86. GUIElement[] childArray = children.ToArray(); // Iterating over it will modify it so make a copy
  87. for (int i = 0; i < childArray.Length; i++)
  88. childArray[i].Destroy();
  89. children.Clear();
  90. base.Destroy();
  91. }
  92. public GUILayoutX AddLayoutX(params GUIOption[] options)
  93. {
  94. GUILayoutX layout = new GUILayoutX(options);
  95. AddElement(layout);
  96. return layout;
  97. }
  98. public GUILayoutY AddLayoutY(params GUIOption[] options)
  99. {
  100. GUILayoutY layout = new GUILayoutY(options);
  101. AddElement(layout);
  102. return layout;
  103. }
  104. public GUIPanel AddPanel(params GUIOption[] options)
  105. {
  106. GUIPanel layout = new GUIPanel(options);
  107. AddElement(layout);
  108. return layout;
  109. }
  110. public GUIPanel AddPanel(Int16 depth = 0, ushort depthRangeMin = ushort.MaxValue, ushort depthRangeMax = ushort.MaxValue, params GUIOption[] options)
  111. {
  112. GUIPanel layout = new GUIPanel(depth, depthRangeMin, depthRangeMax, options);
  113. AddElement(layout);
  114. return layout;
  115. }
  116. public GUIFlexibleSpace AddFlexibleSpace()
  117. {
  118. GUIFlexibleSpace space = new GUIFlexibleSpace();
  119. AddElement(space);
  120. return space;
  121. }
  122. public GUIFixedSpace AddSpace(int size)
  123. {
  124. GUIFixedSpace space = new GUIFixedSpace(size);
  125. AddElement(space);
  126. return space;
  127. }
  128. public GUILayoutX InsertLayoutX(int idx, params GUIOption[] options)
  129. {
  130. GUILayoutX layout = new GUILayoutX(options);
  131. InsertElement(idx, layout);
  132. return layout;
  133. }
  134. public GUILayoutY InsertLayoutY(int idx, params GUIOption[] options)
  135. {
  136. GUILayoutY layout = new GUILayoutY(options);
  137. InsertElement(idx, layout);
  138. return layout;
  139. }
  140. public GUIPanel InsertPanel(int idx, params GUIOption[] options)
  141. {
  142. GUIPanel layout = new GUIPanel(options);
  143. InsertElement(idx, layout);
  144. return layout;
  145. }
  146. public GUIPanel InsertPanel(int idx, Int16 depth = 0, ushort depthRangeMin = ushort.MaxValue,
  147. ushort depthRangeMax = ushort.MaxValue, params GUIOption[] options)
  148. {
  149. GUIPanel layout = new GUIPanel(depth, depthRangeMin, depthRangeMax, options);
  150. InsertElement(idx, layout);
  151. return layout;
  152. }
  153. public GUIFlexibleSpace InsertFlexibleSpace(int idx)
  154. {
  155. GUIFlexibleSpace space = new GUIFlexibleSpace();
  156. InsertElement(idx, space);
  157. return space;
  158. }
  159. public GUIFixedSpace InsertSpace(int idx, int size)
  160. {
  161. GUIFixedSpace space = new GUIFixedSpace(size);
  162. InsertElement(idx, space);
  163. return space;
  164. }
  165. [MethodImpl(MethodImplOptions.InternalCall)]
  166. protected static extern void Internal_CreateInstanceYFromScrollArea(GUILayout instance, GUIScrollArea parentArea);
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. protected static extern void Internal_CreateInstanceX(GUILayout instance, GUIOption[] options);
  169. [MethodImpl(MethodImplOptions.InternalCall)]
  170. protected static extern void Internal_CreateInstanceY(GUILayout instance, GUIOption[] options);
  171. [MethodImpl(MethodImplOptions.InternalCall)]
  172. protected static extern void Internal_CreateInstancePanel(GUILayout instance, Int16 depth, ushort depthRangeMin, ushort depthRangeMax, GUIOption[] options);
  173. [MethodImpl(MethodImplOptions.InternalCall)]
  174. protected static extern void Internal_AddElement(IntPtr instance, IntPtr element);
  175. [MethodImpl(MethodImplOptions.InternalCall)]
  176. protected static extern void Internal_InsertElement(IntPtr instance, int index, IntPtr element);
  177. }
  178. }