ProjectDDD/Assets/_DDD/_Scripts/Restaurant/Ui/CookUi/CookUi.cs
2025-08-29 13:28:03 +09:00

185 lines
6.8 KiB
C#

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<RestaurantUiActions, CookViewModel>
{
[SerializeField] private TabGroupUi _addedCookTabGroup;
[SerializeField] private Transform _addedCookContent;
[SerializeField] private Transform _cookIngredientContent;
[SerializeField] private Image _cookwareImage;
[SerializeField] private Image _cookImage;
[SerializeField] private List<SelectedIngredient> _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 GameObject GetInitialSelected()
{
if (IsInitialized == false) return null;
var initialSelected = _viewModel.GetInitialSelected();
if (initialSelected)
{
return initialSelected;
}
return null;
}
protected override void OnCreatedInitializePopup()
{
_addedCookTabGroup.Initialize(OnAddedCookTabSelected);
foreach (var selectedIngredient in _selectedIngredients)
{
selectedIngredient.Initialize();
}
SetupBindings();
}
protected override void OnOpenedEventsPopup(OpenPopupUiEvent evt)
{
if (evt.Payload is CookwareType cookwareType)
{
_viewModel.SetCookwareType(cookwareType);
}
_viewModel.CreateAddedCookItemSlot(_addedCookContent);
_viewModel.CreateIngredientInventoryItemSlot(_cookIngredientContent);
_addedCookTabGroup.SelectFirstTab();
_viewModel.OnAddedIngredients += OnAddedIngredients;
}
protected override void OnClosedEventsPopup(ClosePopupUiEvent evt)
{
if (_viewModel)
{
_viewModel.OnAddedIngredients -= OnAddedIngredients;
}
}
protected override void OnInputStartedPopup(RestaurantUiActions actionEnum, InputAction.CallbackContext context)
{
}
protected override void OnInputPerformedPopup(RestaurantUiActions actionEnum, InputAction.CallbackContext context)
{
switch (actionEnum)
{
case RestaurantUiActions.Cancel:
Close();
break;
case RestaurantUiActions.Interact1:
HandleInteract1();
break;
case RestaurantUiActions.Interact2:
HandleInteract2();
break;
}
}
protected override void OnInputCanceledPopup(RestaurantUiActions actionEnum, InputAction.CallbackContext context)
{
}
private void SetupBindings()
{
BindingHelper.BindImage(_bindingContext, _cookwareImage, nameof(_viewModel.CookwareIcon));
BindingHelper.BindLocalizedStringEvent(_bindingContext, _cookwareName, nameof(_viewModel.CookwareName));
BindingHelper.BindLocalizedStringEvent<CookViewModel>(_bindingContext, _cookName, viewModel => viewModel.SelectedCookSnapshot.Name);
BindingHelper.BindLocalizedStringEvent<CookViewModel>(_bindingContext, _cookDescriptionName, viewModel => viewModel.SelectedCookSnapshot.Description);
BindingHelper.BindText<CookViewModel>(_bindingContext, _cookPrice, viewModel => viewModel.SelectedCookSnapshot.CookPrice);
BindingHelper.BindImage<CookViewModel>(_bindingContext, _cookImage, viewModel => viewModel.SelectedCookSnapshot.CookIcon);
}
private void HandleInteract1()
{
var selected = EventSystem.current.currentSelectedGameObject;
var interactable = selected?.GetComponent<IInteractableUi>();
interactable?.OnInteract();
}
private void HandleInteract2()
{
_viewModel.OnStartedCooking?.Invoke();
Close();
}
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);
}
}
}