GUILayout.cs 3.6 KB

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