using System; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.Localization.Components; using UnityEngine.UI; namespace DDD.Restaurant { [RequireComponent(typeof(CookViewModel))] public class CookUi : PopupUi { [SerializeField] private TabGroupUi _addedCookTabGroup; [SerializeField] private Transform _addedCookContent; [SerializeField] private Transform _cookIngredientContent; [SerializeField] private Image _cookwareImage; [SerializeField] private Image _cookImage; [SerializeField] private List _selectedIngredients = new(6); [SerializeField] private LocalizeStringEvent _cookwareName; [SerializeField] private LocalizeStringEvent _cookName; [SerializeField] private LocalizeStringEvent _cookDescriptionName; [SerializeField] private TextMeshProUGUI _cookPrice; [SerializeField] private RectTransform _tasteHashTagContent1; [SerializeField] private HorizontalLayoutGroup _tasteHashTagContentLayoutGroup; [SerializeField] private RectTransform _tasteHashTagContent2; protected override void OnCreatedInitialize() { base.OnCreatedInitialize(); _addedCookTabGroup.Initialize(OnAddedCookTabSelected); foreach (var selectedIngredient in _selectedIngredients) { selectedIngredient.Initialize(); } } public override void Open(OpenPopupUiEvent evt) { base.Open(evt); if (evt.Payload is CookwareType cookwareType) { _viewModel.SetCookwareType(cookwareType); } _viewModel.CreateAddedCookItemSlot(_addedCookContent); _viewModel.CreateIngredientInventoryItemSlot(_cookIngredientContent); _addedCookTabGroup.SelectFirstTab(); IsInitialized = true; } protected override void OnOpenedEvents() { base.OnOpenedEvents(); _viewModel.OnAddedIngredients += OnAddedIngredients; } protected override void OnClosedEvents() { base.OnClosedEvents(); if (_viewModel) { _viewModel.OnAddedIngredients -= OnAddedIngredients; } } protected override void SetupBindings() { base.SetupBindings(); BindingHelper.BindImage(_bindingContext, _cookwareImage, nameof(_viewModel.CookwareIcon)); BindingHelper.BindLocalizedStringEvent(_bindingContext, _cookwareName, nameof(_viewModel.CookwareName)); BindingHelper.BindLocalizedStringEvent(_bindingContext, _cookName, viewModel => viewModel.SelectedCookSnapshot.Name); BindingHelper.BindLocalizedStringEvent(_bindingContext, _cookDescriptionName, viewModel => viewModel.SelectedCookSnapshot.Description); BindingHelper.BindText(_bindingContext, _cookPrice, viewModel => viewModel.SelectedCookSnapshot.CookPrice); BindingHelper.BindImage(_bindingContext, _cookImage, viewModel => viewModel.SelectedCookSnapshot.CookIcon); } protected override GameObject GetInitialSelected() { if (IsInitialized == false) return null; var initialSelected = _viewModel.GetInitialSelected(); if (initialSelected) { return initialSelected; } return null; } protected override bool OnInputPerformed(RestaurantUiActions actionEnum, InputAction.CallbackContext context) { if (base.OnInputPerformed(actionEnum, context) == false) return false; switch (actionEnum) { case RestaurantUiActions.Cancel: Close(); break; case RestaurantUiActions.Interact1: HandleInteract1(); break; } return true; } private void HandleInteract1() { var selected = EventSystem.current.currentSelectedGameObject; var interactable = selected?.GetComponent(); interactable?.OnInteract(); } private void OnAddedCookTabSelected(int index) { _viewModel.SetAddedCook(index); UpdateTasteHashTags(); } private void OnAddedIngredients() { var ingredients = _viewModel.SelectedCookSlot.Model.GetIngredients; for (int i = 0; i < _selectedIngredients.Count; i++) { IngredientEntry ingredient = null; if (ingredients != null && i < ingredients.Count) { ingredient = ingredients[i]; } _selectedIngredients[i].SetIngredientEntry(ingredient); } } private void UpdateTasteHashTags() { ClearHashTags(); var tastes = _viewModel.SelectedCookSnapshot.Tastes; if (tastes == null || tastes.Count == 0) return; var material = _viewModel.SelectedCookSnapshot.TasteMaterial; 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); } private void ClearHashTags() { Utils.DestroyAllChildren(_tasteHashTagContent1); Utils.DestroyAllChildren(_tasteHashTagContent2); } } }