GUILayout.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 override bool IsStatic()
  10. {
  11. return true;
  12. }
  13. internal bool AddElementInternal(GUIElement element)
  14. {
  15. if (IsDestroyed())
  16. {
  17. Debug.LogWarning("Attempting to add an element to a destroyed layout. Ignoring operation.");
  18. return false;
  19. }
  20. if (!children.Contains(element))
  21. {
  22. element.SetParent(this);
  23. children.Add(element);
  24. return true;
  25. }
  26. return false;
  27. }
  28. internal bool InsertElementInternal(int index, GUIElement element)
  29. {
  30. if (IsDestroyed())
  31. {
  32. Debug.LogWarning("Attempting to add an element to a destroyed layout. Ignoring operation.");
  33. return false;
  34. }
  35. if (!children.Contains(element))
  36. {
  37. element.SetParent(this);
  38. children.Insert(index, element);
  39. return true;
  40. }
  41. return false;
  42. }
  43. internal void RemoveInternal(GUIElement element)
  44. {
  45. children.Remove(element);
  46. }
  47. internal override void SetParent(GUILayout layout)
  48. {
  49. if (parent != null)
  50. parent.RemoveInternal(this);
  51. parent = layout;
  52. if (parent != null)
  53. parent.children.Add(this);
  54. }
  55. public void AddElement(GUIElement element)
  56. {
  57. if (element.IsStatic())
  58. {
  59. Debug.LogWarning("You are trying to change parent of a static GUI element. Ignoring operation.");
  60. return;
  61. }
  62. if(AddElementInternal(element))
  63. Internal_AddElement(mCachedPtr, element.mCachedPtr);
  64. }
  65. public void InsertElement(int index, GUIElement element)
  66. {
  67. if (element.IsStatic())
  68. {
  69. Debug.LogWarning("You are trying to change parent of a static GUI element. Ignoring operation.");
  70. return;
  71. }
  72. if (InsertElementInternal(index, element))
  73. Internal_InsertElement(mCachedPtr, index, element.mCachedPtr);
  74. }
  75. public GUIFixedSpace AddSpace(int size)
  76. {
  77. GUIFixedSpace fixedSpace = new GUIFixedSpace(this, size);
  78. AddElementInternal(fixedSpace);
  79. return fixedSpace;
  80. }
  81. public GUIFixedSpace InsertSpace(int index, int size)
  82. {
  83. GUIFixedSpace fixedSpace = new GUIFixedSpace(this, index, size);
  84. InsertElementInternal(index, fixedSpace);
  85. return fixedSpace;
  86. }
  87. public GUIFlexibleSpace AddFlexibleSpace()
  88. {
  89. GUIFlexibleSpace flexibleSpace = new GUIFlexibleSpace(this);
  90. AddElementInternal(flexibleSpace);
  91. return flexibleSpace;
  92. }
  93. public GUIFlexibleSpace InsertFlexibleSpace(int index)
  94. {
  95. GUIFlexibleSpace flexibleSpace = new GUIFlexibleSpace(this, index);
  96. InsertElementInternal(index, flexibleSpace);
  97. return flexibleSpace;
  98. }
  99. public GUILayoutX AddLayoutX()
  100. {
  101. GUILayoutX layoutX = new GUILayoutX(this);
  102. AddElementInternal(layoutX);
  103. return layoutX;
  104. }
  105. public GUILayoutX InsertLayoutX(int index)
  106. {
  107. GUILayoutX layoutX = new GUILayoutX(this, index);
  108. InsertElementInternal(index, layoutX);
  109. return layoutX;
  110. }
  111. public GUILayoutY AddLayoutY()
  112. {
  113. GUILayoutY layoutY = new GUILayoutY(this);
  114. AddElementInternal(layoutY);
  115. return layoutY;
  116. }
  117. public GUILayoutY InsertLayoutY(int index)
  118. {
  119. GUILayoutY layoutY = new GUILayoutY(this, index);
  120. InsertElementInternal(index, layoutY);
  121. return layoutY;
  122. }
  123. public GUILayoutExplicit AddLayoutExplicit()
  124. {
  125. GUILayoutExplicit layoutY = new GUILayoutExplicit(this);
  126. AddElementInternal(layoutY);
  127. return layoutY;
  128. }
  129. public GUILayoutExplicit InsertLayoutExplicit(int index)
  130. {
  131. GUILayoutExplicit layoutY = new GUILayoutExplicit(this, index);
  132. InsertElementInternal(index, layoutY);
  133. return layoutY;
  134. }
  135. public void Remove(GUIElement element)
  136. {
  137. if (children.Contains(element))
  138. element.SetParent(null);
  139. }
  140. public void Remove(int childIdx)
  141. {
  142. if (childIdx >= 0 && childIdx < children.Count)
  143. {
  144. GUIElement element = children[childIdx];
  145. element.SetParent(null);
  146. }
  147. }
  148. public int GetNumChildren()
  149. {
  150. return children.Count;
  151. }
  152. public GUIElement GetChild(int index)
  153. {
  154. if (index < 0 || index >= children.Count)
  155. return null;
  156. return children[index];
  157. }
  158. public override void Destroy()
  159. {
  160. GUIElement[] childArray = children.ToArray(); // Iterating over it will modify it so make a copy
  161. for (int i = 0; i < childArray.Length; i++)
  162. childArray[i].Destroy();
  163. children.Clear();
  164. base.Destroy();
  165. }
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. protected static extern void Internal_CreateInstanceXFromArea(GUILayout instance, GUIArea parentArea);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. protected static extern void Internal_CreateInstanceYFromScrollArea(GUILayout instance, GUIScrollArea parentArea);
  170. [MethodImpl(MethodImplOptions.InternalCall)]
  171. protected static extern void Internal_CreateInstanceXFromLayoutAdd(GUILayout instance, GUILayout parentLayout);
  172. [MethodImpl(MethodImplOptions.InternalCall)]
  173. protected static extern void Internal_CreateInstanceYFromLayoutAdd(GUILayout instance, GUILayout parentLayout);
  174. [MethodImpl(MethodImplOptions.InternalCall)]
  175. protected static extern void Internal_CreateInstanceExplicitFromLayoutAdd(GUILayout instance, GUILayout parentLayout);
  176. [MethodImpl(MethodImplOptions.InternalCall)]
  177. protected static extern void Internal_CreateInstanceXFromLayoutInsert(GUILayout instance, GUILayout parentLayout, int index);
  178. [MethodImpl(MethodImplOptions.InternalCall)]
  179. protected static extern void Internal_CreateInstanceYFromLayoutInsert(GUILayout instance, GUILayout parentLayout, int index);
  180. [MethodImpl(MethodImplOptions.InternalCall)]
  181. protected static extern void Internal_CreateInstanceExplicitFromLayoutInsert(GUILayout instance, GUILayout parentLayout, int index);
  182. [MethodImpl(MethodImplOptions.InternalCall)]
  183. protected static extern void Internal_AddElement(IntPtr instance, IntPtr element);
  184. [MethodImpl(MethodImplOptions.InternalCall)]
  185. protected static extern void Internal_InsertElement(IntPtr instance, int index, IntPtr element);
  186. }
  187. }