[Fix] Refactoring (Remove GameView) TEMP

This commit is contained in:
2026-06-07 00:18:30 +07:00
parent a9767c5301
commit 79a928ae52
34 changed files with 965 additions and 805 deletions
@@ -1,18 +1,12 @@
using Minesweeper.Config;
using Minesweeper.Core;
using Minesweeper.Presentation.Views;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Minesweeper.Presentation.Factories
{
public sealed class CellViewFactory : ICellViewFactory
{
private const string ContentImagePath = "Content/Image";
private const string ContentLabelPath = "Content/Text (TMP)";
private const string ContentPath = "Content";
private readonly MinesweeperUiConfig uiConfig;
public CellViewFactory(MinesweeperUiConfig uiConfig)
@@ -23,22 +17,23 @@ namespace Minesweeper.Presentation.Factories
public CellView CreateCell(BoardCellData cell, Transform parent)
{
var prefab = uiConfig.CellButtonPrefab;
var go = prefab != null ? Object.Instantiate(prefab, parent) : CreateFallbackCell(parent);
if (prefab == null)
{
Debug.LogError("CellViewFactory failed: CellButtonPrefab is not assigned.");
return null;
}
var go = Object.Instantiate(prefab, parent);
go.name = BuildCellName(cell.X, cell.Y, cell.DisplayValue);
var view = go.GetComponent<CellView>();
if (view == null)
{
view = go.AddComponent<CellView>();
Debug.LogError($"CellViewFactory failed: '{prefab.name}' is missing CellView.");
Object.Destroy(go);
return null;
}
var button = go.GetComponent<Button>();
var backgroundImage = go.GetComponent<Image>();
var contentRoot = FindComponent<RectTransform>(go.transform, ContentPath);
var contentImage = FindComponent<Image>(go.transform, ContentImagePath);
var label = FindComponent<TMP_Text>(go.transform, ContentLabelPath);
view.Bind(button, backgroundImage, contentRoot, contentImage, label);
view.AutoBind();
view.Initialize(cell.X, cell.Y);
return view;
}
@@ -53,41 +48,5 @@ namespace Minesweeper.Presentation.Factories
return $"bt_{x}_{y}_{displayValue}";
}
private static T FindComponent<T>(Transform root, string path) where T : Component
{
var child = root.Find(path);
return child != null ? child.GetComponent<T>() : null;
}
private static GameObject CreateFallbackCell(Transform parent)
{
var go = new GameObject("Cell", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image), typeof(Button));
go.transform.SetParent(parent, false);
var content = new GameObject("Content", typeof(RectTransform));
content.transform.SetParent(go.transform, false);
Stretch(content.GetComponent<RectTransform>());
var image = new GameObject("Image", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image));
image.transform.SetParent(content.transform, false);
Stretch(image.GetComponent<RectTransform>());
var text = new GameObject("Text (TMP)", typeof(RectTransform), typeof(CanvasRenderer), typeof(TextMeshProUGUI));
text.transform.SetParent(content.transform, false);
Stretch(text.GetComponent<RectTransform>());
var label = text.GetComponent<TextMeshProUGUI>();
label.alignment = TextAlignmentOptions.Center;
label.enableAutoSizing = true;
return go;
}
private static void Stretch(RectTransform rectTransform)
{
rectTransform.anchorMin = Vector2.zero;
rectTransform.anchorMax = Vector2.one;
rectTransform.offsetMin = Vector2.zero;
rectTransform.offsetMax = Vector2.zero;
}
}
}
@@ -1,8 +1,6 @@
using Minesweeper.Config;
using Minesweeper.Presentation.Views;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Minesweeper.Presentation.Factories
{
@@ -12,10 +10,6 @@ namespace Minesweeper.Presentation.Factories
private const string BoardGridName = "BoardGrid";
private const string PausePanelName = "PausePanel";
private const string ResultPanelName = "ResultPanel";
private const string RestartButtonPath = "RestartButton";
private const string ContinueButtonPath = "ContinueButton";
private const string MainMenuButtonPath = "MainMenuButton";
private const string ResultTextPath = "ResultText";
public MinesweeperScreenRefs Spawn(Transform contentRoot, MinesweeperScreenCatalog catalog)
{
@@ -39,20 +33,20 @@ namespace Minesweeper.Presentation.Factories
var result = SpawnScreen(catalog.ResultPanelPrefab, contentRoot, ResultPanelName, 3);
var mainMenuView = RequireComponent<MainMenuView>(mainMenu.transform, MainMenuPanelName);
mainMenuView.BindRoot(mainMenu);
if (mainMenuView != null)
{
mainMenuView.BindRoot(mainMenu);
}
var boardView = RequireComponent<BoardView>(board.transform, BoardGridName);
var pauseView = RequireComponent<PauseView>(pause.transform, PausePanelName);
var resultView = RequireComponent<ResultView>(result.transform, ResultPanelName);
var refs = new MinesweeperScreenRefs(
mainMenuView,
RequireComponent<RectTransform>(board.transform, BoardGridName),
RequireComponent<GridLayoutGroup>(board.transform, BoardGridName),
pause,
RequireChildComponent<Button>(pause.transform, RestartButtonPath),
RequireChildComponent<Button>(pause.transform, ContinueButtonPath),
RequireChildComponent<Button>(pause.transform, MainMenuButtonPath),
result,
RequireChildComponent<Button>(result.transform, RestartButtonPath),
RequireChildComponent<Button>(result.transform, MainMenuButtonPath),
RequireChildComponent<TMP_Text>(result.transform, ResultTextPath));
boardView,
pauseView,
resultView);
mainMenu.SetActive(false);
board.SetActive(false);
@@ -92,24 +86,6 @@ namespace Minesweeper.Presentation.Factories
return component;
}
private static T RequireChildComponent<T>(Transform root, string path) where T : Component
{
var child = root.Find(path);
if (child == null)
{
Debug.LogError($"Minesweeper screen bootstrap failed: '{root.name}/{path}' was not found.");
return null;
}
var component = child.GetComponent<T>();
if (component == null)
{
Debug.LogError($"Minesweeper screen bootstrap failed: '{root.name}/{path}' is missing {typeof(T).Name}.");
}
return component;
}
private static void Stretch(RectTransform rectTransform)
{
if (rectTransform == null)