/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System.ComponentModel; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.Controls; namespace OmiLAXR.TrackingBehaviours.Learner { /// /// Tracks XR device input events using Unity's Input System. /// Monitors button presses and releases across all connected input devices. /// [AddComponentMenu("OmiLAXR / 3) Tracking Behaviours / Input System Tracking Behaviour"), Description("Tracks XR device inputs by using instance.")] public class InputSystemTrackingBehaviour : TrackingBehaviour { /// /// Contains information about an input tracking event including device and button details. /// public struct InputTrackingBehaviourArgs { /// /// Unique identifier of the input device. /// public int DeviceId; /// /// Display name of the input device. /// public string DeviceName; /// /// Name of the button that was pressed or released. /// public string ButtonName; } /// /// Event triggered when any button is pressed on any tracked device. /// [Gesture("XRController"), Action("Press")] public readonly TrackingBehaviourEvent OnPressedAnyButton = new TrackingBehaviourEvent(); /// /// Event triggered when any button is released on any tracked device. /// [Gesture("XRController"), Action("Release")] public readonly TrackingBehaviourEvent OnReleasedAnyButton = new TrackingBehaviourEvent(); /// /// Updates each frame to check for input events across all connected devices. /// protected virtual void Update() { // Check all devices foreach (var device in InputSystem.devices) { // Check all controls on the device foreach (var control in device.allControls) { // If it's a button control and is pressed var button = control as ButtonControl; if (button == null || !button.wasPressedThisFrame) continue; // Fire the event OnPressedAnyButton?.Invoke(this, new InputTrackingBehaviourArgs() { ButtonName = control.name, DeviceId = device.deviceId, DeviceName = device.displayName }); // TODO: Add invoking of OnReleasedAnyButton } } } } }