Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Allow Digital and SingleAxis input actions for teleport #217

Merged
merged 12 commits into from
Dec 21, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,36 @@ public class TeleportPointerInspector : LinePointerInspector
{
private readonly GUIContent teleportFoldoutHeader = new GUIContent("Teleport Pointer Settings");

private SerializedProperty teleportAction;
private SerializedProperty inputThreshold;
private SerializedProperty angleOffset;
private SerializedProperty teleportActivationAngle;
private SerializedProperty rotateActivationAngle;
private SerializedProperty rotationAmount;
private SerializedProperty backStrafeActivationAngle;
private SerializedProperty strafeAmount;
private SerializedProperty upDirectionThreshold;
private SerializedProperty lineColorHotSpot;
private SerializedProperty validLayers;
private SerializedProperty invalidLayers;

protected override void OnEnable()
{
DrawBasePointerActions = false;
base.OnEnable();

teleportAction = serializedObject.FindProperty(nameof(teleportAction));
inputThreshold = serializedObject.FindProperty(nameof(inputThreshold));
angleOffset = serializedObject.FindProperty(nameof(angleOffset));
teleportActivationAngle = serializedObject.FindProperty(nameof(teleportActivationAngle));
rotateActivationAngle = serializedObject.FindProperty(nameof(rotateActivationAngle));
rotationAmount = serializedObject.FindProperty(nameof(rotationAmount));
backStrafeActivationAngle = serializedObject.FindProperty(nameof(backStrafeActivationAngle));
strafeAmount = serializedObject.FindProperty(nameof(strafeAmount));
upDirectionThreshold = serializedObject.FindProperty(nameof(upDirectionThreshold));
lineColorHotSpot = serializedObject.FindProperty(nameof(lineColorHotSpot));
validLayers = serializedObject.FindProperty(nameof(validLayers));
invalidLayers = serializedObject.FindProperty(nameof(invalidLayers));
}

public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();

if (teleportAction.FoldoutWithBoldLabelPropertyField(teleportFoldoutHeader))
if (inputThreshold.FoldoutWithBoldLabelPropertyField(teleportFoldoutHeader))
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(inputThreshold);
Expand All @@ -60,10 +52,7 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(rotationAmount);
EditorGUILayout.PropertyField(backStrafeActivationAngle);
EditorGUILayout.PropertyField(strafeAmount);
EditorGUILayout.PropertyField(upDirectionThreshold);
EditorGUILayout.PropertyField(lineColorHotSpot);
EditorGUILayout.PropertyField(validLayers);
EditorGUILayout.PropertyField(invalidLayers);
EditorGUI.indentLevel--;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ LineRenderer:
m_MotionVectors: 0
m_LightProbeUsage: 0
m_ReflectionProbeUsage: 0
m_RayTracingMode: 0
m_RenderingLayerMask: 4294967295
m_RendererPriority: 0
m_Materials:
Expand All @@ -123,6 +124,7 @@ LineRenderer:
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
Expand Down Expand Up @@ -801,7 +803,6 @@ MonoBehaviour:
rotationAmount: 90
backStrafeActivationAngle: 45
strafeAmount: 0.25
upDirectionThreshold: 0.2
lineColorHotSpot:
serializedVersion: 2
key0: {r: 0, g: 0, b: 1, a: 0}
Expand Down Expand Up @@ -831,12 +832,6 @@ MonoBehaviour:
m_Mode: 0
m_NumColorKeys: 2
m_NumAlphaKeys: 4
validLayers:
serializedVersion: 2
m_Bits: 1
invalidLayers:
serializedVersion: 2
m_Bits: 4
minParabolaVelocity: 5
maxParabolaVelocity: 20
minDistanceModifier: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ MonoBehaviour:
profile: {fileID: 11400000, guid: f3f7a928ca0f70649a77a0f17e7465ff, type: 2}
teleportProvider:
reference:
teleportAction:
profileGuid: 399335ccc89c4d12b79f3539ca7771db
id: 5
description: Teleport Direction
axisConstraint: 4
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ MonoBehaviour:
m_Bits: 4
upDirectionThreshold: 0.2
maxDistance: 10
maxHeightDistance: 10
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
using XRTK.Definitions.InputSystem;
using XRTK.Definitions.Physics;
using XRTK.EventDatum.Input;
using XRTK.EventDatum.Teleport;
Expand All @@ -17,9 +16,6 @@ namespace XRTK.SDK.UX.Pointers
{
public class TeleportPointer : LinePointer
{
[SerializeField]
private MixedRealityInputAction teleportAction = MixedRealityInputAction.None;

[SerializeField]
[Range(0f, 1f)]
[Tooltip("The threshold amount for joystick input (Dead Zone)")]
Expand Down Expand Up @@ -64,8 +60,8 @@ protected Gradient LineColorHotSpot
set => lineColorHotSpot = value;
}

private Vector2 currentInputPosition = Vector2.zero;

private bool currentDigitalInputState = false;
private Vector2 currentDualAxisInputPosition = Vector2.zero;
private bool teleportEnabled = false;

private bool canTeleport = false;
Expand Down Expand Up @@ -212,24 +208,156 @@ public override void OnPostRaycast()

#region IMixedRealityInputHandler Implementation

/// <inheritdoc />
public override void OnInputDown(InputEventData eventData)
{
// Don't process input if we've got an active teleport request in progress.
if (eventData.used || IsTeleportRequestActive || !IsTeleportSystemEnabled)
{
return;
}

if (eventData.SourceId == InputSourceParent.SourceId &&
eventData.Handedness == Handedness &&
eventData.MixedRealityInputAction == MixedRealityToolkit.TeleportSystem.TeleportAction)
{
eventData.Use();
ProcessDigitalTeleportInput(true);
}
}

/// <inheritdoc />
public override void OnInputUp(InputEventData eventData)
{
if (eventData.SourceId == InputSourceParent.SourceId &&
eventData.Handedness == Handedness &&
eventData.MixedRealityInputAction == MixedRealityToolkit.TeleportSystem.TeleportAction)
{
eventData.Use();
ProcessDigitalTeleportInput(false);
}
}

/// <inheritdoc />
public override void OnInputChanged(InputEventData<float> eventData)
{
// Don't process input if we've got an active teleport request in progress.
if (eventData.used || IsTeleportRequestActive || !IsTeleportSystemEnabled)
{
return;
}

if (eventData.SourceId == InputSourceParent.SourceId &&
eventData.Handedness == Handedness &&
eventData.MixedRealityInputAction == MixedRealityToolkit.TeleportSystem.TeleportAction)
{
eventData.Use();
ProcessSingleAxisTeleportInput(eventData);
}
}

/// <inheritdoc />
public override void OnInputChanged(InputEventData<Vector2> eventData)
{
// Don't process input if we've got an active teleport request in progress.
if (IsTeleportRequestActive || !IsTeleportSystemEnabled) { return; }
if (eventData.used || IsTeleportRequestActive || !IsTeleportSystemEnabled)
{
return;
}

if (eventData.SourceId == InputSourceParent.SourceId &&
eventData.Handedness == Handedness &&
eventData.MixedRealityInputAction == teleportAction)
eventData.MixedRealityInputAction == MixedRealityToolkit.TeleportSystem.TeleportAction)
{
eventData.Use();
ProcessDualAxisTeleportInput(eventData);
}
}

#endregion IMixedRealityInputHandler Implementation

#region IMixedRealityTeleportHandler Implementation

/// <inheritdoc />
public override void OnTeleportRequest(TeleportEventData eventData)
{
// Only turn off the pointer if we're not the one sending the request
if (eventData.Pointer.PointerId == PointerId)
{
IsTeleportRequestActive = false;
}
else
{
IsTeleportRequestActive = true;
BaseCursor?.SetVisibility(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do a traditional null check on the unity engine object types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll change that in another PR as this code was not introduced in this PR and I don't want to trigger another build on this one. Need this to release to continue testing.

}
}

/// <inheritdoc />
public override void OnTeleportCompleted(TeleportEventData eventData)
{
IsTeleportRequestActive = false;
BaseCursor?.SetVisibility(false);
}

/// <inheritdoc />
public override void OnTeleportCanceled(TeleportEventData eventData)
{
IsTeleportRequestActive = false;
BaseCursor?.SetVisibility(false);
}

#endregion IMixedRealityTeleportHandler Implementation

private void ProcessDigitalTeleportInput(bool isPressed)
{
currentDigitalInputState = isPressed;

if (currentDigitalInputState && !teleportEnabled)
{
teleportEnabled = true;
MixedRealityToolkit.TeleportSystem?.RaiseTeleportRequest(this, TeleportHotSpot);
}
else if (!currentDigitalInputState)
{
currentInputPosition = eventData.InputData;
var canTeleport = false;

if (teleportEnabled &&
TeleportValidationResult == TeleportValidationResult.Valid ||
TeleportValidationResult == TeleportValidationResult.HotSpot)
{
canTeleport = true;
}

if (canTeleport)
{
teleportEnabled = false;

if (TeleportValidationResult == TeleportValidationResult.Valid ||
TeleportValidationResult == TeleportValidationResult.HotSpot)
{
MixedRealityToolkit.TeleportSystem?.RaiseTeleportStarted(this, TeleportHotSpot);
}
}
else if (teleportEnabled)
{
teleportEnabled = false;
MixedRealityToolkit.TeleportSystem?.RaiseTeleportCanceled(this, TeleportHotSpot);
}
}
}

private void ProcessSingleAxisTeleportInput(InputEventData<float> eventData) => ProcessDigitalTeleportInput(eventData.InputData > inputThreshold);

private void ProcessDualAxisTeleportInput(InputEventData<Vector2> eventData)
{
currentDualAxisInputPosition = eventData.InputData;

if (Mathf.Abs(currentInputPosition.y) > inputThreshold ||
Mathf.Abs(currentInputPosition.x) > inputThreshold)
if (Mathf.Abs(currentDualAxisInputPosition.y) > inputThreshold ||
Mathf.Abs(currentDualAxisInputPosition.x) > inputThreshold)
{
// Get the angle of the pointer input
float angle = Mathf.Atan2(currentInputPosition.x, currentInputPosition.y) * Mathf.Rad2Deg;
float angle = Mathf.Atan2(currentDualAxisInputPosition.x, currentDualAxisInputPosition.y) * Mathf.Rad2Deg;

// Offset the angle so it's 'forward' facing
angle += angleOffset;
Expand Down Expand Up @@ -333,40 +461,5 @@ public override void OnInputChanged(InputEventData<Vector2> eventData)
canTeleport = true;
}
}

#endregion IMixedRealityInputHandler Implementation

#region IMixedRealityTeleportHandler Implementation

/// <inheritdoc />
public override void OnTeleportRequest(TeleportEventData eventData)
{
// Only turn off the pointer if we're not the one sending the request
if (eventData.Pointer.PointerId == PointerId)
{
IsTeleportRequestActive = false;
}
else
{
IsTeleportRequestActive = true;
BaseCursor?.SetVisibility(false);
}
}

/// <inheritdoc />
public override void OnTeleportCompleted(TeleportEventData eventData)
{
IsTeleportRequestActive = false;
BaseCursor?.SetVisibility(false);
}

/// <inheritdoc />
public override void OnTeleportCanceled(TeleportEventData eventData)
{
IsTeleportRequestActive = false;
BaseCursor?.SetVisibility(false);
}

#endregion IMixedRealityTeleportHandler Implementation
}
}