Example #1
0
        /// <inheritdoc />
        public override void Enable()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            if (!inputHandle.IsValid)
            {
                if (MlInput.MLInputCreate(inputConfiguration, ref inputHandle).IsOk)
                {
                    if (MlInput.MLInputGetControllerState(inputHandle, statePointer).IsOk)
                    {
                        MlInput.MLInputControllerState.GetControllerStates(statePointer, ref controllerStates);
                    }
                    else
                    {
                        Debug.LogError("Failed to update the controller input state!");
                    }

                    MlInput.MLInputControllerCallbacksEx.OnConnect    += MlInputControllerCallbacksEx_OnConnect;
                    MlInput.MLInputControllerCallbacksEx.OnDisconnect += MlInputControllerCallbacksEx_OnDisconnect;

                    if (!MlInput.MLInputSetControllerCallbacksEx(inputHandle, in controllerCallbacks, IntPtr.Zero).IsOk)
                    {
                        Debug.LogError("Failed to set controller callbacks!");
                    }
                }
                else
                {
                    Debug.LogError("Failed to create input tracker!");
                }
            }

            if (!controllerHandle.IsValid)
            {
                if (!MlController.MLControllerCreateEx(controllerConfiguration, ref controllerHandle).IsOk)
                {
                    Debug.LogError("Failed to create controller tracker!");
                }
                else
                {
                    MlController.MLControllerGetState(controllerHandle, ref controllerSystemState);
                }
            }
        }
Example #2
0
        /// <inheritdoc />
        public override void Update()
        {
            base.Update();

            if (!Application.isPlaying)
            {
                return;
            }
            if (!inputHandle.IsValid)
            {
                return;
            }
            if (!controllerHandle.IsValid)
            {
                return;
            }

            if (MlInput.MLInputGetControllerState(inputHandle, statePointer).IsOk)
            {
                MlInput.MLInputControllerState.GetControllerStates(statePointer, ref controllerStates);
            }
            else
            {
                Debug.LogError("Failed to update the controller input state!");
            }

            if (!MlController.MLControllerGetState(controllerHandle, ref controllerSystemState).IsOk)
            {
                Debug.LogError("Failed to get the controller system state!");
            }

            for (int i = 0; i < controllerStates.Length; i++)
            {
                if (controllerStates[i].is_connected &&
                    !activeControllers.ContainsKey(controllerStates[i].hardware_index))
                {
                    GetController(controllerStates[i].hardware_index);
                }
            }

            foreach (var controller in activeControllers)
            {
                controller.Value?.UpdateController(controllerStates[controller.Key], controllerSystemState.controller_state[controller.Key]);
            }
        }
Example #3
0
        /// <inheritdoc />
        public override void Disable()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            if (controllerHandle.IsValid)
            {
                MlInput.MLInputControllerCallbacksEx.OnConnect    -= MlInputControllerCallbacksEx_OnConnect;
                MlInput.MLInputControllerCallbacksEx.OnDisconnect -= MlInputControllerCallbacksEx_OnDisconnect;

                if (!MlInput.MLInputSetControllerCallbacksEx(inputHandle, default, IntPtr.Zero).IsOk)
                {
                    Debug.LogError("Failed to clear controller callbacks!");
                }

                if (!MlController.MLControllerDestroy(controllerHandle).IsOk)
                {
                    Debug.LogError($"Failed to destroy {nameof(MlController)} tracker!");
                }
            }

            if (inputHandle.IsValid)
            {
                if (!MlInput.MLInputDestroy(inputHandle).IsOk)
                {
                    Debug.LogError("Failed to destroy the input tracker!");
                }
            }

            foreach (var activeController in activeControllers)
            {
                RemoveController(activeController.Key, false);
            }

            activeControllers.Clear();
        }