TilemapGroupLayer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. namespace MonoGame.Extended.Tilemaps
  5. {
  6. /// <summary>
  7. /// Represents a layer that contains other layers as children.
  8. /// </summary>
  9. /// <remarks>
  10. /// Group layers allow for hierarchical organization of layers and can apply
  11. /// transformations and effects to all child layers collectively.
  12. /// </remarks>
  13. public class TilemapGroupLayer : TilemapLayer
  14. {
  15. private readonly List<TilemapLayer> _childLayers = new List<TilemapLayer>();
  16. /// <summary>
  17. /// Gets the collection of child layers.
  18. /// </summary>
  19. public IReadOnlyList<TilemapLayer> ChildLayers
  20. {
  21. get
  22. {
  23. return _childLayers;
  24. }
  25. }
  26. /// <inheritdoc/>
  27. public override Rectangle Bounds
  28. {
  29. get
  30. {
  31. if (_childLayers.Count == 0)
  32. {
  33. return Rectangle.Empty;
  34. }
  35. // Calculate bounds as the union of all child layer bounds
  36. Rectangle bounds = _childLayers[0].Bounds;
  37. for (int i = 1; i < _childLayers.Count; i++)
  38. {
  39. bounds = Rectangle.Union(bounds, _childLayers[i].Bounds);
  40. }
  41. return bounds;
  42. }
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="TilemapGroupLayer"/> class.
  46. /// </summary>
  47. /// <param name="name">The name of the layer.</param>
  48. public TilemapGroupLayer(string name) : base(name)
  49. {
  50. }
  51. /// <summary>
  52. /// Adds a child layer to this group.
  53. /// </summary>
  54. /// <param name="layer">The layer to add.</param>
  55. public void AddLayer(TilemapLayer layer)
  56. {
  57. _childLayers.Add(layer);
  58. }
  59. /// <summary>
  60. /// Removes a child layer from this group.
  61. /// </summary>
  62. /// <param name="layer">The layer to remove.</param>
  63. /// <returns><see langword="true"/> if the layer was removed; otherwise, <see langword="false"/>.</returns>
  64. public bool RemoveLayer(TilemapLayer layer)
  65. {
  66. return _childLayers.Remove(layer);
  67. }
  68. /// <summary>
  69. /// Gets the child layer with the specified name.
  70. /// </summary>
  71. /// <param name="name">The name of the layer.</param>
  72. /// <returns>The layer with the specified name, or <see langword="null"/> if not found.</returns>
  73. /// <remarks>
  74. /// This method searches recursively through all child layers and their descendants.
  75. /// </remarks>
  76. public TilemapLayer GetLayer(string name)
  77. {
  78. if (string.IsNullOrEmpty(name))
  79. {
  80. return null;
  81. }
  82. // Search immediate children first
  83. foreach (TilemapLayer layer in _childLayers)
  84. {
  85. if (layer.Name == name)
  86. {
  87. return layer;
  88. }
  89. }
  90. // Recursively search in child group layers
  91. foreach (TilemapLayer layer in _childLayers)
  92. {
  93. if (layer is TilemapGroupLayer groupLayer)
  94. {
  95. TilemapLayer found = groupLayer.GetLayer(name);
  96. if (found != null)
  97. {
  98. return found;
  99. }
  100. }
  101. }
  102. return null;
  103. }
  104. }
  105. }