GUILayout.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 void AddElementInternal(GUIElement element)
  14. {
  15. if (IsDestroyed())
  16. {
  17. Debug.LogWarning("Attempting to add an element to a destroyed layout. Ignoring operation.");
  18. return;
  19. }
  20. if (!children.Contains(element))
  21. element.SetParent(this);
  22. }
  23. internal override void SetParent(GUILayout layout)
  24. {
  25. if (parent != null)
  26. parent.Remove(this);
  27. parent = layout;
  28. if (parent != null)
  29. parent.children.Add(this);
  30. }
  31. public void AddElement(GUIElement element)
  32. {
  33. if (element.IsStatic())
  34. {
  35. Debug.LogWarning("You are trying to change parent of a static GUI element. Ignoring operation.");
  36. return;
  37. }
  38. AddElementInternal(element);
  39. }
  40. public GUIFixedSpace AddSpace(int size)
  41. {
  42. GUIFixedSpace fixedSpace = new GUIFixedSpace(this, size);
  43. AddElementInternal(fixedSpace);
  44. return fixedSpace;
  45. }
  46. public GUIFlexibleSpace AddFlexibleSpace()
  47. {
  48. GUIFlexibleSpace flexibleSpace = new GUIFlexibleSpace(this);
  49. AddElementInternal(flexibleSpace);
  50. return flexibleSpace;
  51. }
  52. public GUILayoutX AddLayoutX()
  53. {
  54. GUILayoutX layoutX = new GUILayoutX(this);
  55. AddElementInternal(layoutX);
  56. return layoutX;
  57. }
  58. public GUILayoutY AddLayoutY()
  59. {
  60. GUILayoutY layoutY = new GUILayoutY(this);
  61. AddElementInternal(layoutY);
  62. return layoutY;
  63. }
  64. public void Remove(GUIElement element)
  65. {
  66. if (children.Contains(element))
  67. element.SetParent(null);
  68. }
  69. public void Remove(int childIdx)
  70. {
  71. if (childIdx >= 0 && childIdx < children.Count)
  72. {
  73. GUIElement element = children[childIdx];
  74. element.SetParent(null);
  75. }
  76. }
  77. public int GetNumChildren()
  78. {
  79. return children.Count;
  80. }
  81. public GUIElement GetChild(int index)
  82. {
  83. if (index < 0 || index >= children.Count)
  84. return null;
  85. return children[index];
  86. }
  87. public override void Destroy()
  88. {
  89. GUIElement[] childArray = children.ToArray(); // Iterating over it will modify it so make a copy
  90. for (int i = 0; i < childArray.Length; i++)
  91. childArray[i].Destroy();
  92. children.Clear();
  93. base.Destroy();
  94. }
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. protected static extern void Internal_CreateInstanceXFromArea(GUILayout instance, GUIArea parentArea);
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. protected static extern void Internal_CreateInstanceXFromLayout(GUILayout instance, GUILayout parentLayout);
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. protected static extern void Internal_CreateInstanceYFromScrollArea(GUILayout instance, GUIScrollArea parentArea);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. protected static extern void Internal_CreateInstanceYFromLayout(GUILayout instance, GUILayout parentLayout);
  103. }
  104. }