GUILayout.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 void Remove(GUIElement element)
  124. {
  125. if (children.Contains(element))
  126. element.SetParent(null);
  127. }
  128. public void Remove(int childIdx)
  129. {
  130. if (childIdx >= 0 && childIdx < children.Count)
  131. {
  132. GUIElement element = children[childIdx];
  133. element.SetParent(null);
  134. }
  135. }
  136. public int GetNumChildren()
  137. {
  138. return children.Count;
  139. }
  140. public GUIElement GetChild(int index)
  141. {
  142. if (index < 0 || index >= children.Count)
  143. return null;
  144. return children[index];
  145. }
  146. public override void Destroy()
  147. {
  148. GUIElement[] childArray = children.ToArray(); // Iterating over it will modify it so make a copy
  149. for (int i = 0; i < childArray.Length; i++)
  150. childArray[i].Destroy();
  151. children.Clear();
  152. base.Destroy();
  153. }
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. protected static extern void Internal_CreateInstanceXFromArea(GUILayout instance, GUIArea parentArea);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. protected static extern void Internal_CreateInstanceYFromScrollArea(GUILayout instance, GUIScrollArea parentArea);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. protected static extern void Internal_CreateInstanceXFromLayoutAdd(GUILayout instance, GUILayout parentLayout);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. protected static extern void Internal_CreateInstanceYFromLayoutAdd(GUILayout instance, GUILayout parentLayout);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. protected static extern void Internal_CreateInstanceXFromLayoutInsert(GUILayout instance, GUILayout parentLayout, int index);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. protected static extern void Internal_CreateInstanceYFromLayoutInsert(GUILayout instance, GUILayout parentLayout, int index);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. protected static extern void Internal_AddElement(IntPtr instance, IntPtr element);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. protected static extern void Internal_InsertElement(IntPtr instance, int index, IntPtr element);
  170. }
  171. }