From fbe840cf9630f98afc6cdc16645fce9bc12c45bb Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Sun, 29 Sep 2019 17:04:09 -0400 Subject: [PATCH] Dev/controller refactor (#36) * Simplified the controller implementation a bit * removed an unused namespace * Fixed improper removal of item from collection --- Controllers/LuminController.cs | 99 +++++----------------- Controllers/LuminControllerDataProvider.cs | 49 +++++------ package.json | 6 +- 3 files changed, 45 insertions(+), 109 deletions(-) diff --git a/Controllers/LuminController.cs b/Controllers/LuminController.cs index e62f3ec..abfb585 100644 --- a/Controllers/LuminController.cs +++ b/Controllers/LuminController.cs @@ -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 @@ -62,27 +62,31 @@ public override void SetupDefaultInteractions(Handedness controllerHandedness) /// /// Updates the controller's interaction mappings and ready the current input values. /// - 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: @@ -90,15 +94,17 @@ public void UpdateController() 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); } } @@ -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; @@ -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) @@ -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) @@ -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 diff --git a/Controllers/LuminControllerDataProvider.cs b/Controllers/LuminControllerDataProvider.cs index af6d09e..8113796 100644 --- a/Controllers/LuminControllerDataProvider.cs +++ b/Controllers/LuminControllerDataProvider.cs @@ -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 @@ -38,19 +37,6 @@ public LuminControllerDataProvider(string name, uint priority, BaseMixedRealityC /// private readonly Dictionary activeControllers = new Dictionary(); - /// - public override IMixedRealityController[] GetActiveControllers() - { - var list = new List(); - - foreach (var controller in activeControllers.Values) - { - list.Add(controller); - } - - return list.ToArray(); - } - /// public override void Enable() { @@ -96,6 +82,8 @@ public override void Enable() /// public override void Update() { + base.Update(); + foreach (var controller in activeControllers) { controller.Value?.UpdateController(); @@ -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(); @@ -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) @@ -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) diff --git a/package.json b/package.json index 4ace034..c0f1636 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } \ No newline at end of file