Files
2026-03-18 09:13:48 +07:00

108 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using YachtDice.Categories;
namespace YachtDice.UI
{
public class ScoreCardView : MonoBehaviour
{
[Header("Category Rows (порядок соответствует каталогу)")]
[SerializeField] private List<CategoryRowView> categoryRows = new();
[Header("Summary")]
[SerializeField] private TMP_Text totalScoreText;
public event Action<CategoryDefinition> OnCategorySelected;
private CategoryCatalog _catalog;
private Dictionary<CategoryDefinition, int> _categoryToRowIndex;
/// <summary>
/// Инициализирует скоркарту из каталога категорий.
/// Вызывается из презентационного слоя после DI.
/// </summary>
public void Initialize(CategoryCatalog categoryCatalog)
{
_catalog = categoryCatalog;
_categoryToRowIndex = new Dictionary<CategoryDefinition, int>();
var all = _catalog.All;
int count = Mathf.Min(categoryRows.Count, all.Count);
for (int i = 0; i < count; i++)
{
categoryRows[i].Initialize(all[i]);
categoryRows[i].OnCategorySelected += HandleCategorySelected;
_categoryToRowIndex[all[i]] = i;
}
UpdateTotalDisplay(0);
}
public void UpdatePreviews(Dictionary<CategoryDefinition, int> previews)
{
foreach (var kvp in previews)
{
if (_categoryToRowIndex.TryGetValue(kvp.Key, out int rowIndex))
{
categoryRows[rowIndex].ShowPreview(kvp.Value);
categoryRows[rowIndex].SetInteractable(true);
}
}
}
public void ClearAllPreviews()
{
foreach (var t in categoryRows)
{
t.HidePreview();
t.SetInteractable(false);
}
}
public void SetCategoryScored(CategoryDefinition category, int score)
{
if (_categoryToRowIndex.TryGetValue(category, out int index))
categoryRows[index].SetRecordedScore(score);
}
public void SetAllInteractable(bool interactable)
{
foreach (var t in categoryRows)
t.SetInteractable(interactable);
}
public void SetCategoryInteractable(CategoryDefinition category, bool interactable)
{
if (_categoryToRowIndex != null && _categoryToRowIndex.TryGetValue(category, out int index))
categoryRows[index].SetInteractable(interactable);
}
public void UpdateTotalDisplay(int totalScore)
{
totalScoreText.text = totalScore.ToString();
}
public void ResetAll()
{
foreach (var t in categoryRows)
t.ResetRow();
UpdateTotalDisplay(0);
}
private void HandleCategorySelected(CategoryDefinition category)
{
OnCategorySelected?.Invoke(category);
}
private void OnDestroy()
{
foreach (var t in categoryRows)
t.OnCategorySelected -= HandleCategorySelected;
}
}
}