Skip to content

Commit

Permalink
fixed teleport component (#724)
Browse files Browse the repository at this point in the history
* fixed teleport component

* updated examples submodule

* updated examples checkout

* renamed IMixedRealityTeleportComponentHandler -> IMixedRealityTeleportProvider

* updated examples

* fixed unit tests
  • Loading branch information
StephenHodgson committed Dec 18, 2020
1 parent 99e939b commit b706686
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 42 deletions.
19 changes: 13 additions & 6 deletions Editor/Profiles/MixedRealityTeleportSystemProfileInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,40 @@

using UnityEditor;
using XRTK.Definitions.TeleportSystem;
using XRTK.Services;

namespace XRTK.Editor.Profiles
{
[CustomEditor(typeof(MixedRealityTeleportSystemProfile))]
public class MixedRealityTeleportSystemProfileInspector : MixedRealityServiceProfileInspector
{
private SerializedProperty teleportHandlerComponent;
private SerializedProperty teleportProvider;

protected override void OnEnable()
{
base.OnEnable();

teleportHandlerComponent = serializedObject.FindProperty(nameof(teleportHandlerComponent));
teleportProvider = serializedObject.FindProperty(nameof(teleportProvider));
}

public override void OnInspectorGUI()
{
RenderHeader("The teleport system profile defines default behaviour for the teleport system.");

serializedObject.Update();
EditorGUI.BeginChangeCheck();

EditorGUILayout.PropertyField(teleportHandlerComponent);

EditorGUILayout.Space();
base.OnInspectorGUI();
EditorGUILayout.PropertyField(teleportProvider);

serializedObject.ApplyModifiedProperties();

if (EditorGUI.EndChangeCheck() &&
MixedRealityToolkit.IsInitialized)
{
EditorApplication.delayCall += () => MixedRealityToolkit.Instance.ResetProfile(MixedRealityToolkit.Instance.ActiveProfile);
}

base.OnInspectorGUI();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ namespace XRTK.Definitions.TeleportSystem
public class MixedRealityTeleportSystemProfile : BaseMixedRealityServiceProfile<IMixedRealityTeleportDataProvider>
{
[SerializeField]
[Implements(typeof(IMixedRealityTeleportComponentHandler), TypeGrouping.ByNamespaceFlat)]
[Tooltip("The concrete teleport handler component to use for teleportation.")]
private SystemType teleportHandlerComponent = null;
[Tooltip("The concrete teleport provider to use for teleportation.")]
[Implements(typeof(IMixedRealityTeleportProvider), TypeGrouping.ByNamespaceFlat)]
private SystemType teleportProvider;

/// <summary>
/// The concrete teleport handler component to use for teleportation.
/// The concrete teleport provider to use for teleportation.
/// </summary>
public SystemType TeleportHandlerComponent => teleportHandlerComponent;
public SystemType TeleportProvider
{
get => teleportProvider;
internal set => teleportProvider = value;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ namespace XRTK.Interfaces.TeleportSystem.Handlers
/// Interface to implement for handling teleport events by the <see cref="IMixedRealityTeleportSystem"/>
/// in <see cref="UnityEngine.MonoBehaviour"/> components.
/// </summary>
public interface IMixedRealityTeleportComponentHandler : IMixedRealityTeleportHandler { }
public interface IMixedRealityTeleportProvider : IMixedRealityTeleportHandler { }
}
49 changes: 23 additions & 26 deletions Runtime/Services/TeleportSystem/MixedRealityTeleportSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using XRTK.Definitions.TeleportSystem;
Expand All @@ -11,6 +12,7 @@
using XRTK.Interfaces.TeleportSystem;
using XRTK.Interfaces.TeleportSystem.Handlers;
using XRTK.Utilities;
using Object = UnityEngine.Object;

namespace XRTK.Services.Teleportation
{
Expand All @@ -27,12 +29,18 @@ public class MixedRealityTeleportSystem : BaseEventSystem, IMixedRealityTeleport
public MixedRealityTeleportSystem(MixedRealityTeleportSystemProfile profile)
: base(profile)
{
teleportHandlerComponent = profile.TeleportHandlerComponent;
if (profile.TeleportProvider?.Type == null)
{
throw new Exception($"The {nameof(MixedRealityTeleportSystemProfile)} is missing the required {teleportProvider}!");
}

teleportProvider = profile.TeleportProvider;
}

private readonly SystemType teleportProvider;

private TeleportEventData teleportEventData;
private bool isTeleporting = false;
private readonly SystemType teleportHandlerComponent;

#region IMixedRealityService Implementation

Expand All @@ -41,38 +49,27 @@ public override void Initialize()
{
base.Initialize();

var checkedHandlerComponent = false;
if (!Application.isPlaying)
{
VerifyHandlerComponent();
checkedHandlerComponent = true;
}
else
if (Application.isPlaying)
{
teleportEventData = new TeleportEventData(EventSystem.current);
}

if (!checkedHandlerComponent)
{
VerifyHandlerComponent();
}
CameraCache.Main.gameObject.EnsureComponent(teleportProvider.Type);
}

private void VerifyHandlerComponent()
/// <inheritdoc />
public override void Disable()
{
var activeHandler = (Object)CameraCache.Main.GetComponent<IMixedRealityTeleportComponentHandler>();
if (activeHandler.IsNull())
{
// No teleport handler attached to the camera yet, we can safely
// add the configured handler.
CameraCache.Main.gameObject.EnsureComponent(teleportHandlerComponent.Type);
}
else if (activeHandler.GetType() != teleportHandlerComponent.Type)
base.Disable();

if (!Application.isPlaying)
{
// There is handler attached to the camera but it's not the one configured
// in the profile.
Debug.LogWarning($"There is a {activeHandler.GetType().Name} attached to the camera but the active teleport system configuration requests a {teleportHandlerComponent.Type.Name}. " +
$"Likely you want to check your teleport system configuration.");
var component = CameraCache.Main.GetComponent<IMixedRealityTeleportProvider>() as Component;

if (!component.IsNull())
{
Object.DestroyImmediate(component);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions Tests/Services/TestTeleportProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using UnityEngine;
using XRTK.EventDatum.Teleport;
using XRTK.Interfaces.TeleportSystem.Handlers;

public class TestTeleportProvider : MonoBehaviour, IMixedRealityTeleportProvider
{
#region Implementation of IMixedRealityTeleportHandler

/// <inheritdoc />
public void OnTeleportRequest(TeleportEventData eventData)
{
throw new System.NotImplementedException();
}

/// <inheritdoc />
public void OnTeleportStarted(TeleportEventData eventData)
{
throw new System.NotImplementedException();
}

/// <inheritdoc />
public void OnTeleportCompleted(TeleportEventData eventData)
{
throw new System.NotImplementedException();
}

/// <inheritdoc />
public void OnTeleportCanceled(TeleportEventData eventData)
{
throw new System.NotImplementedException();
}

#endregion
}
11 changes: 11 additions & 0 deletions Tests/Services/TestTeleportProvider.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions Tests/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ public static void InitializeMixedRealityToolkitScene(bool useDefaultProfile)
Assert.IsNotNull(MixedRealityToolkit.Instance);
Assert.IsFalse(MixedRealityToolkit.HasActiveProfile);

var configuration = useDefaultProfile
? GetDefaultMixedRealityProfile<MixedRealityToolkitRootProfile>()
: ScriptableObject.CreateInstance<MixedRealityToolkitRootProfile>();
MixedRealityToolkitRootProfile configuration;
if (useDefaultProfile)
{
configuration = GetDefaultMixedRealityProfile<MixedRealityToolkitRootProfile>();
Debug.Assert(configuration.TeleportSystemProfile != null);
configuration.TeleportSystemProfile.TeleportProvider = typeof(TestTeleportProvider);
}
else
{
configuration = ScriptableObject.CreateInstance<MixedRealityToolkitRootProfile>();
}

Assert.IsTrue(configuration != null, "Failed to find the Default Mixed Reality Root Profile");
MixedRealityToolkit.Instance.ResetProfile(configuration);
Expand Down

0 comments on commit b706686

Please sign in to comment.