Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Dev/controller refactor (#36)
Browse files Browse the repository at this point in the history
* Simplified the controller implementation a bit

* removed an unused namespace

* Fixed improper removal of item from collection
  • Loading branch information
StephenHodgson authored and SimonDarksideJ committed Sep 29, 2019
1 parent ac50e5b commit fbe840c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 109 deletions.
99 changes: 21 additions & 78 deletions Controllers/LuminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
using XRTK.Definitions.Devices;
using XRTK.Definitions.InputSystem;
using XRTK.Definitions.Utilities;
using XRTK.Extensions;
using XRTK.Interfaces.InputSystem;
using XRTK.Providers.Controllers;

#if PLATFORM_LUMIN
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using XRTK.Definitions.Controllers;
using XRTK.Services;
#endif

Expand Down Expand Up @@ -62,43 +62,49 @@ public override void SetupDefaultInteractions(Handedness controllerHandedness)
/// <summary>
/// Updates the controller's interaction mappings and ready the current input values.
/// </summary>
public void UpdateController()
public override void UpdateController()
{
if (!Enabled) { return; }

base.UpdateController();

UpdateControllerData();

if (Interactions == null)
{
Debug.LogError($"No interaction configuration for Windows Mixed Reality Motion Controller {ControllerHandedness}");
Debug.LogError($"No interaction configuration for {GetType().Name} {ControllerHandedness}");
Enabled = false;
}

for (int i = 0; i < Interactions?.Length; i++)
{
switch (Interactions[i].InputType)
var interactionMapping = Interactions[i];

switch (interactionMapping.InputType)
{
case DeviceInputType.SpatialPointer:
UpdatePoseData(Interactions[i]);
UpdatePoseData(interactionMapping);
break;
case DeviceInputType.ButtonPress:
UpdateButtonData(Interactions[i]);
UpdateButtonData(interactionMapping);
break;
case DeviceInputType.Select:
case DeviceInputType.Trigger:
case DeviceInputType.TriggerTouch:
case DeviceInputType.TriggerPress:
case DeviceInputType.TouchpadTouch:
case DeviceInputType.TouchpadPress:
UpdateSingleAxisData(Interactions[i]);
UpdateSingleAxisData(interactionMapping);
break;
case DeviceInputType.Touchpad:
UpdateDualAxisData(Interactions[i]);
UpdateDualAxisData(interactionMapping);
break;
default:
Debug.LogError($"Input [{Interactions[i].InputType}] is not handled for this controller [{GetType().Name}]");
Debug.LogError($"Input [{interactionMapping.InputType}] is not handled for this controller [{GetType().Name}]");
break;
}

interactionMapping.RaiseInputAction(InputSource, ControllerHandedness);
}
}

Expand Down Expand Up @@ -166,7 +172,6 @@ private void UpdateButtonData(MixedRealityInteractionMapping interactionMapping)

var isHomeButton = interactionMapping.Description.Contains("Home");

// Update the interaction data source
if (!isHomeButton)
{
interactionMapping.BoolData = MlControllerReference.State.ButtonState[(int)MLInputControllerButton.Bumper] > 0;
Expand All @@ -176,83 +181,35 @@ private void UpdateButtonData(MixedRealityInteractionMapping interactionMapping)
interactionMapping.BoolData = IsHomePressed;
IsHomePressed = false;
}

// If our value changed raise it.
if (interactionMapping.Changed)
{
// Raise input system Event if it enabled
if (interactionMapping.BoolData)
{
MixedRealityToolkit.InputSystem?.RaiseOnInputDown(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction);
}
else
{
MixedRealityToolkit.InputSystem?.RaiseOnInputUp(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction);
}
}

if (interactionMapping.Updated)
{
MixedRealityToolkit.InputSystem?.RaiseOnInputPressed(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction);
}
}

private void UpdateSingleAxisData(MixedRealityInteractionMapping interactionMapping)
{
Debug.Assert(interactionMapping.AxisType == AxisType.SingleAxis || interactionMapping.AxisType == AxisType.Digital);

float singleAxisValue = interactionMapping.Description.Contains("Touchpad")
interactionMapping.FloatData = interactionMapping.Description.Contains("Touchpad")
? MlControllerReference.Touch1PosAndForce.z
: MlControllerReference.TriggerValue;

switch (interactionMapping.InputType)
{
case DeviceInputType.Trigger:
// Do nothing.
break;
case DeviceInputType.Select:
case DeviceInputType.TriggerPress:
case DeviceInputType.TouchpadPress:
// Update the interaction data source
interactionMapping.BoolData = singleAxisValue.Equals(1f);
interactionMapping.BoolData = interactionMapping.FloatData.Equals(1f);
break;
case DeviceInputType.TriggerTouch:
case DeviceInputType.TouchpadTouch:
case DeviceInputType.TriggerNearTouch:
// Update the interaction data source
interactionMapping.BoolData = !singleAxisValue.Equals(0f);
interactionMapping.BoolData = !interactionMapping.FloatData.Equals(0f);
break;
case DeviceInputType.Trigger:
// Update the interaction data source
interactionMapping.FloatData = singleAxisValue;

// If our value changed raise it.
if (interactionMapping.Updated)
{
// Raise input system Event if it enabled
MixedRealityToolkit.InputSystem?.RaiseOnInputPressed(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction, interactionMapping.FloatData);
}
return;
default:
Debug.LogError($"Input [{interactionMapping.InputType}] is not handled for this controller [{GetType().Name}]");
return;
}

// If our value changed raise it.
if (interactionMapping.Changed)
{
// Raise input system Event if it enabled
if (interactionMapping.BoolData)
{
MixedRealityToolkit.InputSystem?.RaiseOnInputDown(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction);
}
else
{
MixedRealityToolkit.InputSystem?.RaiseOnInputUp(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction);
}
}

if (interactionMapping.Updated)
{
MixedRealityToolkit.InputSystem?.RaiseOnInputPressed(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction, singleAxisValue);
}
}

private void UpdateDualAxisData(MixedRealityInteractionMapping interactionMapping)
Expand All @@ -272,13 +229,6 @@ private void UpdateDualAxisData(MixedRealityInteractionMapping interactionMappin

// Update the interaction data source
interactionMapping.Vector2Data = dualAxisPosition;

// If our value changed raise it.
if (interactionMapping.Updated)
{
// Raise input system Event if it enabled
MixedRealityToolkit.InputSystem?.RaisePositionInputChanged(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction, interactionMapping.Vector2Data);
}
}

private void UpdatePoseData(MixedRealityInteractionMapping interactionMapping)
Expand All @@ -298,13 +248,6 @@ private void UpdatePoseData(MixedRealityInteractionMapping interactionMapping)

// Update the interaction data source
interactionMapping.PoseData = currentPointerPose;

// If our value changed raise it.
if (interactionMapping.Updated)
{
// Raise input system Event if it enabled
MixedRealityToolkit.InputSystem?.RaisePoseInputChanged(InputSource, ControllerHandedness, interactionMapping.MixedRealityInputAction, interactionMapping.PoseData);
}
}

#endif // PLATFORM_LUMIN
Expand Down
49 changes: 21 additions & 28 deletions Controllers/LuminControllerDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using UnityEngine.XR.MagicLeap;
using XRTK.Definitions.Devices;
using XRTK.Definitions.Utilities;
using XRTK.Interfaces.Providers.Controllers;
using XRTK.Services;

#endif // PLATFORM_LUMIN
Expand All @@ -38,19 +37,6 @@ public LuminControllerDataProvider(string name, uint priority, BaseMixedRealityC
/// </summary>
private readonly Dictionary<byte, LuminController> activeControllers = new Dictionary<byte, LuminController>();

/// <inheritdoc/>
public override IMixedRealityController[] GetActiveControllers()
{
var list = new List<IMixedRealityController>();

foreach (var controller in activeControllers.Values)
{
list.Add(controller);
}

return list.ToArray();
}

/// <inheritdoc />
public override void Enable()
{
Expand Down Expand Up @@ -96,6 +82,8 @@ public override void Enable()
/// <inheritdoc />
public override void Update()
{
base.Update();

foreach (var controller in activeControllers)
{
controller.Value?.UpdateController();
Expand All @@ -113,12 +101,7 @@ public override void Disable()

foreach (var activeController in activeControllers)
{
var controller = GetController(activeController.Key, false);

if (controller != null)
{
MixedRealityToolkit.InputSystem?.RaiseSourceLost(controller.InputSource, controller);
}
RemoveController(activeController.Key, false);
}

activeControllers.Clear();
Expand Down Expand Up @@ -175,9 +158,26 @@ private LuminController GetController(byte controllerId, bool addController = tr

detectedController.MlControllerReference = mlController;
activeControllers.Add(controllerId, detectedController);
AddController(detectedController);
return detectedController;
}

private void RemoveController(byte controllerId, bool removeFromRegistry = true)
{
var controller = GetController(controllerId, false);

if (controller != null)
{
MixedRealityToolkit.InputSystem?.RaiseSourceLost(controller.InputSource, controller);
RemoveController(controller);
}

if (removeFromRegistry)
{
activeControllers.Remove(controllerId);
}
}

#region Controller Events

private void OnControllerConnected(byte controllerId)
Expand All @@ -193,14 +193,7 @@ private void OnControllerConnected(byte controllerId)

private void OnControllerDisconnected(byte controllerId)
{
var controller = GetController(controllerId, false);

if (controller != null)
{
MixedRealityToolkit.InputSystem?.RaiseSourceLost(controller.InputSource, controller);
}

activeControllers.Remove(controllerId);
RemoveController(controllerId);
}

private void MlInputOnControllerButtonDown(byte controllerId, MLInputControllerButton button)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.xrtk.lumin",
"displayName": "XRTK.Lumin",
"description": "The Lumin platform components for the Mixed Reality Toolkit",
"tags":[
"tags": [
"xrtk",
"magic",
"leap",
Expand All @@ -13,13 +13,13 @@
"reality",
"lumin"
],
"version": "0.1.6",
"version": "0.1.7",
"unity": "2019.1",
"license": "MIT",
"src": "Assets/XRTK.Lumin",
"author": "XRTK Team (https://github.com/XRTK)",
"dependencies": {
"com.unity.xr.magicleap": "2.0.0-preview.19",
"com.xrtk.core": "0.1.19"
"com.xrtk.core": "0.1.21"
}
}

0 comments on commit fbe840c

Please sign in to comment.