142 lines
4.7 KiB
C#
142 lines
4.7 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);
|
|
}
|
|
|
|
_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)
|
|
{
|
|
print("레시피 선택 E키");
|
|
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)
|
|
{
|
|
print("레시피 선택 버튼 클릭");
|
|
CurrentCraftRecipeButton = clickedCraftRecipeButton;
|
|
Close();
|
|
}
|
|
|
|
public void SetSelectRecipeAction(UnityAction selectRecipeAction) => _selectRecipeAction = selectRecipeAction;
|
|
}
|
|
} |