[Fix] Board Optimization, Fix Screen Loading

This commit is contained in:
2026-06-07 00:57:20 +07:00
parent e487795e6f
commit 285c11597a
9 changed files with 117 additions and 94 deletions
+50 -15
View File
@@ -23,6 +23,7 @@ namespace Minesweeper.Presentation.Views
[SerializeField] private MinesweeperUiConfig uiConfig;
private readonly Dictionary<int, CellView> cellsByCoordinate = new Dictionary<int, CellView>();
private readonly Stack<CellView> pooledCells = new Stack<CellView>();
private IReadOnlyList<BoardCellData> currentCells;
private bool inputEnabled = true;
private bool currentRevealUnflaggedMines;
@@ -83,14 +84,27 @@ namespace Minesweeper.Presentation.Views
currentBoardHeight = height;
currentCells = cells;
currentRevealUnflaggedMines = revealUnflaggedMines;
ConfigureGrid(width, height);
var layoutWasEnabled = gridLayoutGroup != null && gridLayoutGroup.enabled;
SetGridLayoutEnabled(false);
for (var i = 0; i < cells.Count; i++)
try
{
CreateCell(cells[i], cellViewFactory);
}
ConfigureGrid(width, height);
Refresh(cells, revealUnflaggedMines);
for (var i = 0; i < cells.Count; i++)
{
var cell = cells[i];
var view = CreateCell(cell, cellViewFactory);
if (view != null)
{
view.Render(cell, uiConfig, currentPixelsPerUnitMultiplier, currentContentPadding, revealUnflaggedMines);
}
}
}
finally
{
SetGridLayoutEnabled(layoutWasEnabled);
}
}
public void Refresh(IReadOnlyList<BoardCellData> cells, bool revealUnflaggedMines)
@@ -207,17 +221,17 @@ namespace Minesweeper.Presentation.Views
return Mathf.Max(uiConfig.MinimumCellSize, Mathf.Floor(Mathf.Min(cellByWidth, cellByHeight)));
}
private void CreateCell(BoardCellData cell, ICellViewFactory cellViewFactory)
private CellView CreateCell(BoardCellData cell, ICellViewFactory cellViewFactory)
{
if (gridLayoutGroup == null)
{
return;
return null;
}
var view = cellViewFactory.CreateCell(cell, gridLayoutGroup.transform);
var view = GetOrCreateCell(cell, cellViewFactory);
if (view == null)
{
return;
return null;
}
view.SetInputEnabled(inputEnabled);
@@ -226,6 +240,22 @@ namespace Minesweeper.Presentation.Views
view.PressStarted += OnCellPressStarted;
view.PressEnded += OnCellPressEnded;
cellsByCoordinate[ToKey(cell.X, cell.Y)] = view;
return view;
}
private CellView GetOrCreateCell(BoardCellData cell, ICellViewFactory cellViewFactory)
{
CellView view;
if (pooledCells.Count > 0)
{
view = pooledCells.Pop();
view.transform.SetParent(gridLayoutGroup.transform, false);
view.Initialize(cell.X, cell.Y);
view.gameObject.SetActive(true);
return view;
}
return cellViewFactory.CreateCell(cell, gridLayoutGroup.transform);
}
private void Clear()
@@ -238,19 +268,24 @@ namespace Minesweeper.Presentation.Views
cell.FlagRequested -= OnCellFlagRequested;
cell.PressStarted -= OnCellPressStarted;
cell.PressEnded -= OnCellPressEnded;
PoolCell(cell);
}
}
cellsByCoordinate.Clear();
}
if (gridLayoutGroup == null)
{
return;
}
private void PoolCell(CellView cell)
{
cell.gameObject.SetActive(false);
pooledCells.Push(cell);
}
for (var i = gridLayoutGroup.transform.childCount - 1; i >= 0; i--)
private void SetGridLayoutEnabled(bool enabled)
{
if (gridLayoutGroup != null)
{
Destroy(gridLayoutGroup.transform.GetChild(i).gameObject);
gridLayoutGroup.enabled = enabled;
}
}