134 lines
3.7 KiB
C#
134 lines
3.7 KiB
C#
//============= Copyright (c) Ludic GmbH, All rights reserved. ==============
|
|
//
|
|
// Purpose: Part of the My Behaviour Tree Controller Code
|
|
//
|
|
//=============================================================================
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using MyBT;
|
|
using UnityEngine.XR.Interaction.Toolkit;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
[CustomEditor(typeof(NamedGrabEvent))]
|
|
public class NamedGrabEventInspector : ComponentHandlerInspector {
|
|
}
|
|
#endif
|
|
|
|
public class NamedGrabEvent : ComponentHandler {
|
|
public override string TypeLabel () {
|
|
return "NamedGrabEvent";
|
|
}
|
|
|
|
public override string ContentLabel() {
|
|
UpdateComponent();
|
|
return objName;
|
|
}
|
|
|
|
public override void UpdateComponent() {
|
|
base.UpdateComponent();
|
|
|
|
if (GetComponent<XRGrabInteractable>())
|
|
{
|
|
GetComponent<XRGrabInteractable>().selectEntered.AddListener(SelectEnterEventHandler);
|
|
GetComponent<XRGrabInteractable>().selectExited.AddListener(SelectExitEventHandler);
|
|
}
|
|
else {
|
|
Debug.LogWarning($"NamedGrabEvent '{gameObject.name}' could not attach grab event (missing XR Grab Interactable?)");
|
|
}
|
|
}
|
|
|
|
public string objName = "GrabObjXY";
|
|
private bool selectEnterTriggered = false;
|
|
private bool selectExitTriggered = false;
|
|
private bool _abortEventListener = false;
|
|
|
|
private void SelectEnterEventHandler(SelectEnterEventArgs args)
|
|
{
|
|
selectEnterTriggered = true;
|
|
}
|
|
|
|
private void SelectExitEventHandler(SelectExitEventArgs args)
|
|
{
|
|
selectExitTriggered = true;
|
|
}
|
|
|
|
public override string titleText {
|
|
get {
|
|
return "Run: Will Succeed on Select Enter and Exit Event";
|
|
}
|
|
}
|
|
|
|
public override string[][] helpText {
|
|
get {
|
|
return new string[][] {
|
|
new string[] {"Run", "Return Success on Select Enter and Exit Event", $"BTC.Run(\"{roomId}\", \"{gameObject.name}\")"},
|
|
};
|
|
}
|
|
}
|
|
|
|
public override void Run(MyBT.NodeState nodeState) {
|
|
if (nodeState == NodeState.FirstRun) {
|
|
// reset event trigger at start
|
|
selectEnterTriggered = false;
|
|
}
|
|
|
|
if (nodeState == NodeState.Aborting) {
|
|
selectEnterTriggered = false;
|
|
}
|
|
|
|
if (selectEnterTriggered) {
|
|
Debug.Log($"NamedGrabEvent: Select entered event from {gameObject.name} fired.");
|
|
Task.SetSucceeded();
|
|
return;
|
|
}
|
|
|
|
if (_abortEventListener)
|
|
{
|
|
Task.SetFailed();
|
|
Debug.Log($"NamedGrabEvent on {gameObject.name} failed.");
|
|
_abortEventListener = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
public override void ListenToEvent(MyBT.NodeState nodeState)
|
|
{
|
|
if (nodeState == NodeState.FirstRun)
|
|
{
|
|
// reset event trigger at start
|
|
selectExitTriggered = false;
|
|
Debug.Log("Enter ListenToEvent");
|
|
}
|
|
|
|
if (nodeState == NodeState.Aborting)
|
|
{
|
|
selectExitTriggered = false;
|
|
}
|
|
|
|
if (nodeState == NodeState.Running)
|
|
{
|
|
if (selectExitTriggered)
|
|
{
|
|
Debug.Log($"NamedGrabEvent: Select exited event from {gameObject.name} fired.");
|
|
Task.SetSucceeded();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void AbortEventListener(MyBT.NodeState nodeState)
|
|
{
|
|
if (nodeState == NodeState.FirstRun)
|
|
{
|
|
_abortEventListener = true;
|
|
Task.SetSucceeded();
|
|
return;
|
|
}
|
|
}
|
|
}
|