Files
HDRobotics/Assets/Scripts/View/ProgramInfoView.cs

156 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class ProgramInfoView : MonoBehaviour
{
// --- 프로그램 정보 패널 ---
[SerializeField] private Transform leftControllerTransform;
[SerializeField] private GameObject infoPanel;
public InputActionReference showInfoPanel;
public InputActionReference hideInfoPanel;
public InputActionReference[] playProgram;
private bool isPressingX = false;
private bool isPressingAorB = false;
[SerializeField] public TextMeshProUGUI jobName;
[SerializeField] public TextMeshProUGUI jobSpeed;
[SerializeField] private GameObject orderPrefab;
[SerializeField] private GameObject endPrefab;
[SerializeField] private Transform contentParent;
[SerializeField] private Button resetBtn;
[SerializeField] private Button startBtn;
[SerializeField] private Button stopBtn;
[SerializeField] private Button minusBtn;
[SerializeField] private Button plusBtn;
private List<StepInfoItem> instantiatedStepItems = new List<StepInfoItem>();
public event Action OnResetClicked;
public event Action OnStartClicked;
public event Action OnStopClicked;
public event Action<int> OnMinusClicked;
public event Action<int> OnPlusClicked;
void Start()
{
infoPanel.SetActive(false);
if (showInfoPanel != null)
{
showInfoPanel.action.Enable();
showInfoPanel.action.performed += ShowProgramInfoPanel;
}
if (hideInfoPanel != null)
{
hideInfoPanel.action.Enable();
hideInfoPanel.action.performed += HideProgramInfoPanel;
}
if (playProgram != null)
{
for (int i = 0; i < playProgram.Length; i++)
{
playProgram[i].action.Enable();
playProgram[i].action.performed += PlayProgram;
}
}
resetBtn.onClick.AddListener(() => OnResetClicked?.Invoke());
startBtn.onClick.AddListener(() => OnStartClicked?.Invoke());
stopBtn.onClick.AddListener(() => OnStopClicked?.Invoke());
minusBtn.onClick.AddListener(() => OnMinusClicked?.Invoke(-1));
plusBtn.onClick.AddListener(() => OnPlusClicked?.Invoke(1));
}
private void OnDestroy()
{
if (showInfoPanel != null)
{
showInfoPanel.action.performed -= ShowProgramInfoPanel;
}
if (hideInfoPanel != null)
{
hideInfoPanel.action.performed -= HideProgramInfoPanel;
}
if (playProgram != null)
{
for (int i = 0; i < playProgram.Length; i++)
{
playProgram[i].action.performed -= PlayProgram;
}
}
}
public void UpdateProgramInfo(RobotProgram program, List<RobotMoveStep> steps)
{
if (program == null) return;
foreach (Transform child in contentParent)
{
Destroy(child.gameObject);
}
instantiatedStepItems.Clear();
// 새 스텝 목록 생성
if (steps == null) return;
foreach (RobotMoveStep step in steps)
{
GameObject newPrefab = Instantiate(orderPrefab, contentParent);
StepInfoItem stepItemUI = newPrefab.GetComponent<StepInfoItem>();
if (stepItemUI != null)
{
stepItemUI.SetData(step);
instantiatedStepItems.Add(stepItemUI);
}
else
{
Debug.LogError("orderPrefab에 StepInfoItem.cs 스크립트가 없습니다");
}
if (jobSpeed.text == "0%")
jobSpeed.text = $"{step.Speed}%";
}
Instantiate(endPrefab, contentParent);
}
private void ShowProgramInfoPanel(InputAction.CallbackContext obj)
{
isPressingX = !isPressingX;
if (isPressingX)
{
infoPanel.transform.SetParent(leftControllerTransform);
infoPanel.transform.localPosition = new Vector3(0f, 0.2f, 0f);
infoPanel.transform.localRotation = Quaternion.Euler(0, 180, 0);
infoPanel.SetActive(true);
}
else
{
infoPanel.SetActive(false);
}
}
private void HideProgramInfoPanel(InputAction.CallbackContext obj)
{
infoPanel.SetActive(false);
}
private void PlayProgram(InputAction.CallbackContext obj)
{
isPressingAorB = !isPressingAorB;
if (isPressingAorB)
{
OnStartClicked?.Invoke();
}
else
{
OnStopClicked?.Invoke();
}
}
}