174 lines
6.9 KiB
C#
174 lines
6.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 Transform _tasteHashTagPanel;
|
|
[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 CookwareDetailPanel = "CookwareDetailPanel";
|
|
private const string IngredientDetailPanel = "IngredientDetailPanel";
|
|
private const string RecipeDetailPanel = "RecipeDetailPanel";
|
|
|
|
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;
|
|
|
|
_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)
|
|
{
|
|
switch (category)
|
|
{
|
|
case InventoryCategoryType.Food:
|
|
case InventoryCategoryType.Drink:
|
|
_viewImage.sprite = DataManager.Instance.GetSprite(RecipeDetailPanel);
|
|
_tasteHashTagPanel.gameObject.SetActive(true);
|
|
_cookWarePanel.gameObject.SetActive(true);
|
|
break;
|
|
case InventoryCategoryType.Ingredient:
|
|
_viewImage.sprite = DataManager.Instance.GetSprite(IngredientDetailPanel);
|
|
_tasteHashTagPanel.gameObject.SetActive(true);
|
|
_cookWarePanel.gameObject.SetActive(false);
|
|
break;
|
|
case InventoryCategoryType.Cookware:
|
|
case InventoryCategoryType.Special:
|
|
_viewImage.sprite = DataManager.Instance.GetSprite(CookwareDetailPanel);
|
|
_tasteHashTagPanel.gameObject.SetActive(false);
|
|
_cookWarePanel.gameObject.SetActive(false);
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(category), category, null);
|
|
}
|
|
|
|
if (!_tasteHashTagPanel.gameObject.activeInHierarchy) return;
|
|
|
|
Canvas.ForceUpdateCanvases();
|
|
UpdateTasteHashTags(_currentItemViewModel);
|
|
}
|
|
|
|
private void ClearHashTags()
|
|
{
|
|
foreach (Transform content in _tasteHashTagContent1)
|
|
{
|
|
Destroy(content?.gameObject);
|
|
}
|
|
|
|
foreach (Transform content in _tasteHashTagContent2)
|
|
{
|
|
Destroy(content?.gameObject);
|
|
}
|
|
}
|
|
}
|
|
} |