94 lines
2.5 KiB
C#
94 lines
2.5 KiB
C#
using System;
|
|
using Minesweeper.Core;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Minesweeper.Presentation.Views
|
|
{
|
|
public sealed class CellView : MonoBehaviour, IPointerClickHandler
|
|
{
|
|
[SerializeField] private Button button;
|
|
[SerializeField] private Image image;
|
|
[SerializeField] private TMP_Text label;
|
|
|
|
private int x;
|
|
private int y;
|
|
private bool inputEnabled = true;
|
|
|
|
public event Action<int, int> OpenRequested;
|
|
public event Action<int, int> FlagRequested;
|
|
|
|
public void Bind(Button button, Image image, TMP_Text label)
|
|
{
|
|
this.button = button;
|
|
this.image = image;
|
|
this.label = label;
|
|
}
|
|
|
|
public void Initialize(int x, int y)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
public void SetInputEnabled(bool enabled)
|
|
{
|
|
inputEnabled = enabled;
|
|
if (button != null)
|
|
{
|
|
button.interactable = enabled;
|
|
}
|
|
}
|
|
|
|
public void Render(BoardCellData cell, float pixelsPerUnitMultiplier)
|
|
{
|
|
gameObject.name = $"bt_{cell.X}_{cell.Y}_{cell.DisplayValue}";
|
|
|
|
if (image != null)
|
|
{
|
|
image.pixelsPerUnitMultiplier = pixelsPerUnitMultiplier;
|
|
image.color = cell.IsOpened ? new Color(0.78f, 0.78f, 0.78f) : Color.white;
|
|
}
|
|
|
|
if (label != null)
|
|
{
|
|
if (cell.IsFlagged)
|
|
{
|
|
label.text = "F";
|
|
}
|
|
else if (!cell.IsOpened)
|
|
{
|
|
label.text = string.Empty;
|
|
}
|
|
else if (cell.IsMine)
|
|
{
|
|
label.text = "M";
|
|
}
|
|
else
|
|
{
|
|
label.text = cell.NeighborMines == 0 ? string.Empty : cell.NeighborMines.ToString();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (!inputEnabled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (eventData.button == PointerEventData.InputButton.Left)
|
|
{
|
|
OpenRequested?.Invoke(x, y);
|
|
}
|
|
else if (eventData.button == PointerEventData.InputButton.Right)
|
|
{
|
|
FlagRequested?.Invoke(x, y);
|
|
}
|
|
}
|
|
}
|
|
}
|