187 lines
6.4 KiB
C#
187 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using DDD.Managers;
|
|
using DDD.ScriptableObjects;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
|
|
namespace DDD.Uis.Tycoon
|
|
{
|
|
public class CraftRecipeUi : PopupUi
|
|
{
|
|
[Title("프리팹")]
|
|
[SerializeField]
|
|
private CraftRecipeButton _craftRecipeButtonPrefab;
|
|
|
|
[Title("컴포넌트")]
|
|
[SerializeField]
|
|
private GameObject _panel;
|
|
|
|
[SerializeField]
|
|
private GameObject _craftRecipesPanel;
|
|
|
|
[Title("클래스")]
|
|
[SerializeField]
|
|
private UiEventsController _uiEventsController;
|
|
|
|
[Title("실시간 데이터")]
|
|
[SerializeField]
|
|
private List<CraftRecipeButton> _craftRecipeButtons;
|
|
|
|
public CraftRecipeButton CurrentCraftRecipeButton { get; private set; }
|
|
private InputAction _interactionEAction;
|
|
private InputAction _cancelAction;
|
|
private InputAction _pressFAction;
|
|
|
|
private UnityAction _selectRecipeAction;
|
|
|
|
private void Awake()
|
|
{
|
|
// 더미 데이터 삭제
|
|
foreach (Transform element in _craftRecipesPanel.transform)
|
|
{
|
|
if (element)
|
|
{
|
|
Destroy(element.gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_interactionEAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.InteractionE);
|
|
_cancelAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.Cancel);
|
|
_pressFAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.PressF);
|
|
|
|
_craftRecipeButtons = new List<CraftRecipeButton>(ItemManager.Instance.CraftRecipeDataSo.GetDataCount());
|
|
foreach (CraftRecipeData craftRecipeData in ItemManager.Instance.AcquiredCraftRecipeDatas.Values)
|
|
{
|
|
CraftRecipeButton craftRecipeButton = Instantiate(_craftRecipeButtonPrefab, _craftRecipesPanel.transform);
|
|
craftRecipeButton.RegisterButtonListener(() => OnClickedCraftRecipe(craftRecipeButton));
|
|
craftRecipeButton.SetCraftRecipeData(craftRecipeData);
|
|
_craftRecipeButtons.Add(craftRecipeButton);
|
|
}
|
|
|
|
SetNavigationCraftRecipeButtons();
|
|
|
|
_panel.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_interactionEAction = null;
|
|
_cancelAction = null;
|
|
_pressFAction = null;
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
base.Open();
|
|
_panel.SetActive(true);
|
|
|
|
_uiEventsController.SetSelectObject(_craftRecipeButtons[0].gameObject);
|
|
EventSystem.current.SetSelectedGameObject(_uiEventsController.SelectObject);
|
|
}
|
|
|
|
public void OnClose(InputAction.CallbackContext context)
|
|
{
|
|
Close();
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
_panel.SetActive(false);
|
|
base.Close();
|
|
|
|
if (CurrentCraftRecipeButton)
|
|
{
|
|
_selectRecipeAction?.Invoke();
|
|
}
|
|
|
|
CurrentCraftRecipeButton = null;
|
|
}
|
|
|
|
public override void EnableInput()
|
|
{
|
|
_interactionEAction.performed += OnInteractionE;
|
|
_cancelAction.performed += OnClose;
|
|
_pressFAction.performed += OnClose;
|
|
_uiEventsController.EnableAutoNavigate();
|
|
}
|
|
|
|
public override void DisableInput()
|
|
{
|
|
_interactionEAction.performed -= OnInteractionE;
|
|
_cancelAction.performed -= OnClose;
|
|
_pressFAction.performed -= OnClose;
|
|
_uiEventsController.DisableAutoNavigate();
|
|
}
|
|
|
|
private void OnInteractionE(InputAction.CallbackContext context)
|
|
{
|
|
if (!EventSystem.current) return;
|
|
|
|
GameObject currentObject = EventSystem.current.currentSelectedGameObject;
|
|
if (!currentObject || !currentObject.GetComponent<CraftRecipeButton>()) return;
|
|
|
|
currentObject.GetComponent<Button>()?.onClick.Invoke();
|
|
}
|
|
|
|
private void OnClickedCraftRecipe(CraftRecipeButton clickedCraftRecipeButton)
|
|
{
|
|
CurrentCraftRecipeButton = clickedCraftRecipeButton;
|
|
Close();
|
|
}
|
|
|
|
private void SetNavigationCraftRecipeButtons()
|
|
{
|
|
int columnCount = 4;
|
|
int totalCount = ItemManager.Instance.AcquiredCraftRecipeDatas.Count;
|
|
|
|
for (int i = 0; i < totalCount; i++)
|
|
{
|
|
Navigation navigation = _craftRecipeButtons[i].Button.navigation;
|
|
navigation.mode = Navigation.Mode.Explicit;
|
|
|
|
// 상(Up) 버튼 설정
|
|
int upIndex = i - columnCount;
|
|
if (upIndex >= 0)
|
|
{
|
|
navigation.selectOnUp = _craftRecipeButtons[upIndex].Button;
|
|
}
|
|
|
|
// 하(Down) 버튼 설정
|
|
int downIndex = i + columnCount;
|
|
if (downIndex < totalCount)
|
|
{
|
|
navigation.selectOnDown = _craftRecipeButtons[downIndex].Button;
|
|
}
|
|
|
|
// 좌(Left) 버튼 설정
|
|
int leftIndex = i - 1;
|
|
// 같은 행(=i/columnCount)이면 연결, 그렇지 않다면 연결 X
|
|
if (leftIndex >= 0 && (leftIndex / columnCount) == (i / columnCount))
|
|
{
|
|
navigation.selectOnLeft = _craftRecipeButtons[leftIndex].Button;
|
|
}
|
|
|
|
// 우(Right) 버튼 설정
|
|
int rightIndex = i + 1;
|
|
// 같은 행(=i/columnCount)이면 연결, 그렇지 않다면 연결 X
|
|
if (rightIndex < totalCount && (rightIndex / columnCount) == (i / columnCount))
|
|
{
|
|
navigation.selectOnRight = _craftRecipeButtons[rightIndex].Button;
|
|
}
|
|
|
|
// 설정한 내비게이션 정보를 버튼에 반영
|
|
_craftRecipeButtons[i].Button.navigation = navigation;
|
|
}
|
|
}
|
|
|
|
public void SetSelectRecipeAction(UnityAction selectRecipeAction) => _selectRecipeAction = selectRecipeAction;
|
|
}
|
|
} |