CellGrid.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Drawing;
  18. namespace MobiusEditor.Model
  19. {
  20. public enum FacingType
  21. {
  22. None,
  23. North,
  24. NorthEast,
  25. East,
  26. SouthEast,
  27. South,
  28. SouthWest,
  29. West,
  30. NorthWest
  31. }
  32. public class CellChangedEventArgs<T> : EventArgs
  33. {
  34. public readonly int Cell;
  35. public readonly Point Location;
  36. public readonly T OldValue;
  37. public readonly T Value;
  38. public CellChangedEventArgs(CellMetrics metrics, int cell, T oldValue, T value)
  39. {
  40. Cell = cell;
  41. metrics.GetLocation(cell, out Location);
  42. OldValue = oldValue;
  43. Value = value;
  44. }
  45. public CellChangedEventArgs(CellMetrics metrics, Point location, T oldValue, T value)
  46. {
  47. Location = location;
  48. metrics.GetCell(location, out Cell);
  49. OldValue = oldValue;
  50. Value = value;
  51. }
  52. }
  53. public class CellGrid<T> : IEnumerable<(int Cell, T Value)>, IEnumerable
  54. {
  55. private readonly CellMetrics metrics;
  56. private readonly T[,] cells;
  57. public T this[int x, int y]
  58. {
  59. get => cells[y, x];
  60. set
  61. {
  62. if (!EqualityComparer<T>.Default.Equals(cells[y, x], value))
  63. {
  64. var lastValue = cells[y, x];
  65. cells[y, x] = value;
  66. OnCellChanged(new CellChangedEventArgs<T>(metrics, new Point(x, y), lastValue, cells[y, x]));
  67. }
  68. }
  69. }
  70. public T this[Point location] { get => this[location.X, location.Y]; set => this[location.X, location.Y] = value; }
  71. public T this[int cell] { get => this[cell % metrics.Width, cell / metrics.Width]; set => this[cell % metrics.Width, cell / metrics.Width] = value; }
  72. public Size Size => metrics.Size;
  73. public int Length => metrics.Length;
  74. public event EventHandler<CellChangedEventArgs<T>> CellChanged;
  75. public event EventHandler<EventArgs> Cleared;
  76. public CellGrid(CellMetrics metrics)
  77. {
  78. this.metrics = metrics;
  79. cells = new T[metrics.Height, metrics.Width];
  80. }
  81. public void Clear()
  82. {
  83. Array.Clear(cells, 0, cells.Length);
  84. OnCleared();
  85. }
  86. public T Adjacent(Point location, FacingType facing)
  87. {
  88. return metrics.Adjacent(location, facing, out Point adjacent) ? this[adjacent] : default;
  89. }
  90. public T Adjacent(int cell, FacingType facing)
  91. {
  92. if (!metrics.GetLocation(cell, out Point location))
  93. {
  94. return default;
  95. }
  96. return metrics.Adjacent(location, facing, out Point adjacent) ? this[adjacent] : default;
  97. }
  98. public bool CopyTo(CellGrid<T> other)
  99. {
  100. if (metrics.Length != other.metrics.Length)
  101. {
  102. return false;
  103. }
  104. for (var i = 0; i < metrics.Length; ++i)
  105. {
  106. other[i] = this[i];
  107. }
  108. return true;
  109. }
  110. protected virtual void OnCellChanged(CellChangedEventArgs<T> e)
  111. {
  112. CellChanged?.Invoke(this, e);
  113. }
  114. protected virtual void OnCleared()
  115. {
  116. Cleared?.Invoke(this, new EventArgs());
  117. }
  118. public IEnumerable<(int Cell, T Value)> IntersectsWith(ISet<int> cells)
  119. {
  120. foreach (var i in cells)
  121. {
  122. if (metrics.Contains(i))
  123. {
  124. var cell = this[i];
  125. if (cell != null)
  126. {
  127. yield return (i, cell);
  128. }
  129. }
  130. }
  131. }
  132. public IEnumerable<(int Cell, T Value)> IntersectsWith(ISet<Point> locations)
  133. {
  134. foreach (var location in locations)
  135. {
  136. if (metrics.Contains(location))
  137. {
  138. var cell = this[location];
  139. if (cell != null)
  140. {
  141. metrics.GetCell(location, out int i);
  142. yield return (i, cell);
  143. }
  144. }
  145. }
  146. }
  147. public IEnumerator<(int Cell, T Value)> GetEnumerator()
  148. {
  149. for (var i = 0; i < metrics.Length; ++i)
  150. {
  151. var cell = this[i];
  152. if (cell != null)
  153. {
  154. yield return (i, cell);
  155. }
  156. }
  157. }
  158. IEnumerator IEnumerable.GetEnumerator()
  159. {
  160. return GetEnumerator();
  161. }
  162. }
  163. }