ProjectDDD/Assets/_DDD/_Scripts/Restaurant/Ui/RestaurantManagementUi/ItemDetailView.cs
2025-08-27 17:25:11 +09:00

168 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Components;
using UnityEngine.UI;
namespace DDD.Restaurant
{
public sealed class ItemDetailSnapshot
{
public Sprite BackgroundSprite { get; }
public bool ShowTastePanel { get; }
public bool ShowCookwarePanel { get; }
public Sprite CookwareSprite { get; }
public LocalizedString Name { get; }
public LocalizedString Description { get; }
public IReadOnlyList<TasteData> Tastes { get; }
public Material TasteMaterial { get; }
public ItemDetailSnapshot(
Sprite backgroundSprite,
bool showTastePanel,
bool showCookwarePanel,
Sprite cookwareSprite,
LocalizedString name,
LocalizedString description,
IReadOnlyList<TasteData> tastes,
Material tasteMaterial)
{
BackgroundSprite = backgroundSprite;
ShowTastePanel = showTastePanel;
ShowCookwarePanel = showCookwarePanel;
CookwareSprite = cookwareSprite;
Name = name;
Description = description;
Tastes = tastes;
TasteMaterial = tasteMaterial;
}
public static readonly ItemDetailSnapshot Empty = new(
backgroundSprite: null,
showTastePanel: false,
showCookwarePanel: false,
cookwareSprite: null,
name: null,
description: null,
tastes: System.Array.Empty<TasteData>(),
tasteMaterial: null);
}
public class ItemDetailView : MonoBehaviour, IUiView<RestaurantManagementViewModel>, IEventHandler<ItemSlotSelectedEvent>
{
[SerializeField] private Image _viewImage;
[SerializeField] private LocalizeStringEvent _nameLocalizeStringEvent;
[SerializeField] private LocalizeStringEvent _descriptionLocalizeStringEvent;
[SerializeField] private Transform _cookWarePanel;
[SerializeField] private Image _cookwareImage;
[SerializeField] private Transform _tasteHashTagPanel;
[SerializeField] private RectTransform _tasteHashTagContent1;
[SerializeField] private HorizontalLayoutGroup _tasteHashTagContentLayoutGroup;
[SerializeField] private RectTransform _tasteHashTagContent2;
private RestaurantManagementViewModel _viewModel;
private void OnDestroy()
{
EventBus.Unregister<ItemSlotSelectedEvent>(this);
}
public void Initialize(RestaurantManagementViewModel viewModel)
{
_viewModel = viewModel;
_cookwareImage.sprite = null;
_nameLocalizeStringEvent.OnUpdateString.Invoke(string.Empty);
_descriptionLocalizeStringEvent.OnUpdateString.Invoke(string.Empty);
EventBus.Register<ItemSlotSelectedEvent>(this);
}
public void SetupBindings(BindingContext bindingContext)
{
BindingHelper.BindImage<RestaurantManagementViewModel>(bindingContext, _viewImage, viewModel => viewModel.ItemDetail.BackgroundSprite);
BindingHelper.BindImage<RestaurantManagementViewModel>(bindingContext, _cookwareImage, viewModel => viewModel.ItemDetail.CookwareSprite);
BindingHelper.BindActive<RestaurantManagementViewModel>(bindingContext, _tasteHashTagPanel.gameObject, viewModel => viewModel.ItemDetail.ShowTastePanel);
BindingHelper.BindActive<RestaurantManagementViewModel>(bindingContext, _cookWarePanel.gameObject, viewModel => viewModel.ItemDetail.ShowCookwarePanel);
BindingHelper.BindLocalizedStringEvent<RestaurantManagementViewModel>(bindingContext, _nameLocalizeStringEvent, viewModel => viewModel.ItemDetail.Name);
BindingHelper.BindLocalizedStringEvent<RestaurantManagementViewModel>(bindingContext, _descriptionLocalizeStringEvent, viewModel => viewModel.ItemDetail.Description);
}
public void OnOpenedEvents()
{
UpdateView();
_viewModel.OnCategoryChanged += HandleCategoryChanged;
}
public void OnClosedEvents()
{
if (_viewModel)
{
_viewModel.OnCategoryChanged -= HandleCategoryChanged;
}
}
public void UpdateView()
{
if (_viewModel.SelectedItem == null)
{
ClearHashTags();
return;
}
UpdateTasteHashTags();
}
private void UpdateTasteHashTags()
{
ClearHashTags();
var tastes = _viewModel.GetTastes();
if (tastes == null || tastes.Count == 0) return;
var material = _viewModel.GetTasteMaterial();
float maxWidth = _tasteHashTagContent1.rect.width;
float currentLineWidth = 0f;
foreach (var taste in tastes)
{
var instance = _viewModel.CreateHashTag(_tasteHashTagContent1);
instance.Initialize(material, taste);
LayoutRebuilder.ForceRebuildLayoutImmediate(instance.RectTransform);
float w = instance.RectTransform.rect.width;
if (currentLineWidth + w > maxWidth)
{
instance.transform.SetParent(_tasteHashTagContent2, false);
currentLineWidth = w + _tasteHashTagContentLayoutGroup.spacing;
}
else
{
currentLineWidth += w + _tasteHashTagContentLayoutGroup.spacing;
}
}
LayoutRebuilder.ForceRebuildLayoutImmediate(_tasteHashTagContent1);
LayoutRebuilder.ForceRebuildLayoutImmediate(_tasteHashTagContent2);
}
public void HandleCategoryChanged(InventoryCategoryType category)
{
Canvas.ForceUpdateCanvases();
}
private void ClearHashTags()
{
Utils.DestroyAllChildren(_tasteHashTagContent1);
Utils.DestroyAllChildren(_tasteHashTagContent2);
}
public void HandleEvent(ItemSlotSelectedEvent evt)
{
_viewModel.SetSelectedItem(evt.Model);
UpdateView();
}
}
}