/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System; using System.ComponentModel; using OmiLAXR.Components; using UnityEngine; namespace OmiLAXR.TrackingBehaviours { /// /// Monitors transform changes (position, rotation, scale) in GameObjects with TransformWatcher components. /// Supports both interval-based polling and real-time change detection modes. /// Can selectively ignore specific transform properties based on configuration. /// [AddComponentMenu("OmiLAXR / 3) Tracking Behaviours / Transform Tracking Behaviour")] [Description("Tracks position, rotation and scale changes in a game object holding component.")] public class TransformTrackingBehaviour : ScheduledTrackingBehaviour { public string scope; /// /// Enable real-time change detection by binding to TransformWatcher events. /// When false, only interval-based checking is performed. /// public bool detectOnChange = true; /// /// Flags to ignore specific transform components during tracking. /// Allows selective monitoring of only position, rotation, or scale changes. /// public TransformWatcher.TransformIgnore ignore; /// /// Event triggered when a tracked object's position changes. /// Provides both the TransformWatcher and detailed change information. /// [Gesture("Locomotion"), Action("Translation")] public readonly TrackingBehaviourEvent OnChangedPosition = new TrackingBehaviourEvent(); /// /// Event triggered when a tracked object's rotation changes. /// Includes old and new rotation values in the change data. /// [Gesture("Locomotion"), Action("Rotation")] public readonly TrackingBehaviourEvent OnChangedRotation = new TrackingBehaviourEvent(); /// /// Event triggered when a tracked object's scale changes. /// Captures scale modifications with before/after values. /// [Gesture("Locomotion"), Action("Scale")] public readonly TrackingBehaviourEvent OnChangedScale = new TrackingBehaviourEvent(); /// /// Event triggered when a tracked object's forward changes. /// Captures forward modifications with before/after values. /// [Gesture("Locomotion"), Action("Forward")] public readonly TrackingBehaviourEvent OnChangedForward = new TrackingBehaviourEvent(); /// /// Sets up real-time event bindings for detected TransformWatcher components. /// Binds to each watcher's change events for immediate notification of transform modifications. /// /// Array of TransformWatcher components to monitor protected override void AfterFilteredObjects(TransformWatcher[] transformWatchers) { } protected override void Run() { var arr = SelectedObjects; if (arr == null || arr.Count == 0) return; var scopeLocal = scope; var filterScope = !string.IsNullOrEmpty(scopeLocal); var ign = ignore; var detect = detectOnChange; for (var i = 0; i < arr.Count; i++) { var tw = arr[i]; if (tw == null) continue; if (filterScope && tw.scope != scopeLocal) continue; var state = tw.GetTransformChangeState(ign); if (!detect || state.Position.HasChanged) OnChangedPosition?.Invoke(this, tw, state.Position); if (!detect || state.Rotation.HasChanged) OnChangedRotation?.Invoke(this, tw, state.Rotation); if (!detect || state.Scale.HasChanged) OnChangedScale?.Invoke(this, tw, state.Scale); if (!detect || state.Forward.HasChanged) OnChangedForward?.Invoke(this, tw, state.Forward); } } } }