ProjectDDD/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemDetailView.cs
2025-07-30 03:18:16 +09:00

160 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Localization.Components;
using UnityEngine.UI;
namespace DDD
{
public class ItemDetailView : MonoBehaviour, IEventHandler<ItemSlotSelectedEvent>
{
[SerializeField] private Image _viewImage;
[SerializeField] private TextMeshProUGUI _nameLabel;
[SerializeField] private LocalizeStringEvent _labelLocalizer;
[SerializeField] private TextMeshProUGUI _descriptionLabel;
[SerializeField] private LocalizeStringEvent _descriptionLocalizer;
[SerializeField] private Transform _cookWarePanel;
[SerializeField] private Image _cookwareImage;
[SerializeField] private RectTransform _tasteHashTagContent1;
[SerializeField] private HorizontalLayoutGroup _tasteHashTagContentLayoutGroup;
[SerializeField] private RectTransform _tasteHashTagContent2;
private RestaurantManagementSo _restaurantManagementSo;
private List<TasteHashTagSlotUi> _tasteHashTagSlotUis = new();
private ItemViewModel _currentItemViewModel;
private TaskCompletionSource<bool> _isInitialized = new();
private const string CookwarePanel = "CookwarePanel";
private const string NotCookwarePanel = "NotCookwarePanel";
private async void Start()
{
_restaurantManagementSo = await AssetManager.LoadAsset<RestaurantManagementSo>(DataConstants.RestaurantManagementSo);
Debug.Assert(_restaurantManagementSo != null, "RestaurantManagementSo is null");
_nameLabel.text = string.Empty;
_descriptionLabel.text = string.Empty;
_cookwareImage.sprite = null;
ClearHashTags();
_isInitialized.SetResult(true);
}
private void OnEnable()
{
EventBus.Register<ItemSlotSelectedEvent>(this);
}
private void OnDisable()
{
EventBus.Unregister<ItemSlotSelectedEvent>(this);
}
public void Invoke(ItemSlotSelectedEvent evt)
{
Show(evt.Model);
}
public async void Show(ItemViewModel model)
{
await _isInitialized.Task;
_currentItemViewModel = model;
if (_currentItemViewModel == null) return;
string viewItemKey = null;
if (_currentItemViewModel.ItemType == ItemType.Recipe)
{
viewItemKey = _currentItemViewModel.GetRecipeResultKey;
}
else if (_currentItemViewModel.ItemType == ItemType.Ingredient)
{
viewItemKey = _currentItemViewModel.Id;
}
_labelLocalizer.StringReference = LocalizationManager.Instance.GetLocalizedName(viewItemKey);
_descriptionLocalizer.StringReference = LocalizationManager.Instance.GetLocalizedDescription(viewItemKey);
_cookwareImage.sprite = _currentItemViewModel.GetCookwareSprite;
UpdateTasteHashTags(_currentItemViewModel);
}
private void UpdateTasteHashTags(ItemViewModel model)
{
ClearHashTags();
if (model == null) return;
_tasteHashTagSlotUis.Clear();
List<TasteData> tasteDatas = model.GetTasteDatas;
if (tasteDatas == null || tasteDatas.Count <= 0) return;
var outlineColor = model.RecipeType switch
{
RecipeType.FoodRecipe => _restaurantManagementSo.FoodTasteOutlineColor,
RecipeType.DrinkRecipe => _restaurantManagementSo.DrinkTasteOutlineColor,
_ => throw new ArgumentOutOfRangeException()
};
float maxWidth = _tasteHashTagContent1.rect.width;
float currentLineWidth = 0f;
foreach (var tasteData in tasteDatas)
{
var newTasteHashTag = Instantiate(_restaurantManagementSo.TasteHashTagSlotUiPrefab, _tasteHashTagContent1, false);
newTasteHashTag.Initialize(outlineColor, tasteData);
LayoutRebuilder.ForceRebuildLayoutImmediate(newTasteHashTag.RectTransform);
float slotWidth = newTasteHashTag.RectTransform.rect.width;
if (currentLineWidth + slotWidth > maxWidth)
{
newTasteHashTag.transform.SetParent(_tasteHashTagContent2, false);
currentLineWidth = slotWidth + _tasteHashTagContentLayoutGroup.spacing;
}
else
{
currentLineWidth += slotWidth + _tasteHashTagContentLayoutGroup.spacing;
}
_tasteHashTagSlotUis.Add(newTasteHashTag);
}
LayoutRebuilder.ForceRebuildLayoutImmediate(_tasteHashTagContent1);
LayoutRebuilder.ForceRebuildLayoutImmediate(_tasteHashTagContent2);
}
public void UpdateCategory(InventoryCategoryType category)
{
if (category == InventoryCategoryType.Ingredient)
{
_viewImage.sprite = DataManager.Instance.GetSprite(NotCookwarePanel);
_cookWarePanel.gameObject.SetActive(false);
}
else
{
_viewImage.sprite = DataManager.Instance.GetSprite(CookwarePanel);
_cookWarePanel.gameObject.SetActive(true);
}
Canvas.ForceUpdateCanvases();
UpdateTasteHashTags(_currentItemViewModel);
}
private void ClearHashTags()
{
foreach (Transform content in _tasteHashTagContent1)
{
Destroy(content.gameObject);
}
foreach (Transform content in _tasteHashTagContent2)
{
Destroy(content.gameObject);
}
}
}
}