Stratasys-450mc-VR/Assets/UI/MainMenuLayout.cs
2023-11-01 10:51:29 -04:00

193 lines
7.1 KiB
C#
Executable File

using SO;
using SO.Channels;
using UnityEngine;
using UnityEngine.UIElements;
using Button = UnityEngine.UIElements.Button;
namespace UI
{
public class MainMenuLayout : MonoBehaviour
{
[SerializeField] private SimpleChannelSO nextButtonChannel;
[SerializeField] private SimpleChannelSO previousButtonChannel;
[SerializeField] private SimpleChannelSO videoButtonChannel;
[SerializeField] private SimpleChannelSO documentButtonChannel;
[SerializeField] private SimpleChannelSO animationButtonChannel;
[SerializeField] private StepChangeChannelSO stepChangeChannel;
[SerializeField] private MachineChangeChannelSO machineChannel;
[SerializeField] private SimpleChannelSO playButtonChannel;
[SerializeField] private SimpleChannelSO stopButtonChannel;
[SerializeField] private VisualTreeAsset documentPanelAsset;
[SerializeField] private VisualTreeAsset videoPanelAsset;
private VisualElement switchingArea;
private Button nextButton;
private Button previousButton;
private Button videoButton;
private Button documentButton;
private Button animationButton;
private Label machineName;
private Label processName;
private Label stepName;
private Label stepNameText;
private Label stepDescriptionText;
private VisualElement videoScreen;
private VisualElement documentScreen;
private VisualElement documentPanel;
private Texture currentDocument;
private Button playButton;
private Button stopButton;
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
machineName = root.Q<Label>("MachineName");
processName = root.Q<Label>("ProcessName");
stepName = root.Q<Label>("StepName");
stepNameText = root.Q<Label>("StepNameText");
stepDescriptionText = root.Q<Label>("StepDescriptionText");
switchingArea = root.Q<VisualElement>("SwitchingArea");
nextButton = root.Q<Button>("NextButton");
previousButton = root.Q<Button>("PreviousButton");
videoButton = root.Q<Button>("VideoButton");
documentButton = root.Q<Button>("DocumentButton");
animationButton = root.Q<Button>("AnimationButton");
videoScreen = videoPanelAsset.CloneTree();
playButton = videoScreen.Q<Button>("PlayButton");
stopButton = videoScreen.Q<Button>("StopButton");
documentScreen = documentPanelAsset.CloneTree();
documentPanel = documentScreen.Q<VisualElement>("DocumentPanel");
nextButton.clicked += OnNextButtonClicked;
previousButton.clicked += OnPreviousButtonClicked;
videoButton.clicked += OnVideoButtonClicked;
documentButton.clicked += OnDocumentButtonClicked;
animationButton.clicked += OnAnimationButtonClicked;
playButton.clicked += OnPlayButtonClicked;
stopButton.clicked += OnStopButtonClicked;
stepChangeChannel.OnStepChanged += OnStepChanged;
machineChannel.OnMachineChange += OnMachineChanged;
playButtonChannel.OnEventRaised += OnPlayButtonClicked;
stopButtonChannel.OnEventRaised += OnStopButtonClicked;
animationButtonChannel.OnEventRaised += OnAnimationButtonClicked;
}
private void OnDisable()
{
nextButton.clicked -= OnNextButtonClicked;
previousButton.clicked -= OnPreviousButtonClicked;
videoButton.clicked -= OnVideoButtonClicked;
documentButton.clicked -= OnDocumentButtonClicked;
animationButton.clicked -= OnAnimationButtonClicked;
playButton.clicked -= OnPlayButtonClicked;
stopButton.clicked -= OnStopButtonClicked;
stepChangeChannel.OnStepChanged -= OnStepChanged;
machineChannel.OnMachineChange -= OnMachineChanged;
playButtonChannel.OnEventRaised -= OnPlayButtonClicked;
stopButtonChannel.OnEventRaised -= OnStopButtonClicked;
animationButtonChannel.OnEventRaised -= OnAnimationButtonClicked;
}
private void OnMachineChanged(MachineSO machine)
{
machineName.text = machine.MachineName;
processName.text = machine.Process.ProcessName;
OnStepChanged(machine.Process.Steps[0]);
}
private void OnStepChanged(StepSO step)
{
stepName.text = step.StepName;
stepNameText.text = step.StepName;
stepDescriptionText.text = step.Description;
currentDocument = step.Document;
}
private void OnNextButtonClicked()
{
switchingArea.Clear();
nextButtonChannel.RaiseEvent();
}
private void OnPreviousButtonClicked()
{
switchingArea.Clear();
previousButtonChannel.RaiseEvent();
}
private void OnVideoButtonClicked()
{
videoButtonChannel.RaiseEvent();
if (switchingArea.childCount.Equals(0) || switchingArea.Contains(documentScreen))
{
switchingArea.Clear();
switchingArea.Add(videoScreen);
return;
}
switchingArea.Clear();
}
private void OnDocumentButtonClicked()
{
documentButtonChannel.RaiseEvent();
if (switchingArea.childCount.Equals(0) || switchingArea.Contains(videoScreen))
{
switchingArea.Clear();
switchingArea.Add(documentScreen);
documentPanel.style.backgroundImage = new StyleBackground((Background)currentDocument);
return;
}
switchingArea.Clear();
}
// Debugging, grabs all the children of the root element and prints them to the console
private void InspectChildren(VisualElement element, int depth)
{
string indent = new string('\t', depth);
Debug.Log($"{indent}Element: {element.name}");
for (int i = 0; i < element.childCount; i++)
{
VisualElement child = element[i];
InspectChildren(child, depth + 1);
}
}
private void OnAnimationButtonClicked()
{
Debug.Log("triggered animation");
animationButtonChannel.RaiseEvent();
return;
}
private void OnStopButtonClicked()
{
Debug.Log("triggered");
stopButtonChannel.RaiseEvent();
}
private void OnPlayButtonClicked()
{
playButtonChannel.RaiseEvent();
}
}
}