using System; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Localization.Components; using UnityEngine.UI; namespace DDD { public class ItemDetailView : MonoBehaviour, IEventHandler { [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 _tasteHashTagSlotUis = new(); private ItemViewModel _currentItemViewModel; private const string CookwarePanel = "CookwarePanel"; private const string NotCookwarePanel = "NotCookwarePanel"; private async void Start() { _restaurantManagementSo = await AssetManager.LoadAsset(DataConstants.RestaurantManagementSo); Debug.Assert(_restaurantManagementSo != null, "RestaurantManagementSo is null"); _nameLabel.text = string.Empty; _descriptionLabel.text = string.Empty; _cookwareImage.sprite = null; ClearHashTags(); } private void OnEnable() { EventBus.Register(this); } private void OnDisable() { EventBus.Unregister(this); } public void Invoke(ItemSlotSelectedEvent evt) { Show(evt.Model); } public void Show(ItemViewModel model) { _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 = DataManager.Instance.GetSprite(viewItemKey); UpdateTasteHashTags(_currentItemViewModel); } private void UpdateTasteHashTags(ItemViewModel model) { ClearHashTags(); if (model == null) return; _tasteHashTagSlotUis.Clear(); List 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); } } } }