#nullable enable using System.Threading; using UnityEngine.EventSystems; using Timer = CCG.Bigfoot.Utility.Timer; using CCG.Bigfoot.InputSystem; namespace CCG.Bigfoot.CardScripts { /// <summary> /// LandingCard_InputHandling provides common input handling between LandingCards /// </summary> /// <remarks> /// Authors: CS /// Created: 2024-01-29 /// </remarks> public partial class LandingCard { private Timer? _holdTouchTimer = null; private float _defaultHoldTime = -1f; private CancellationTokenSource? _onHeldCancellation = null; protected InputManager? _inputManager = null; private bool _isHoldingDown = false; #region Callbacks public override void OnPointerDown(PointerEventData ped) { InitializeHoldTimer(); } public override void OnPointerUp(PointerEventData ped) { if (_holdTouchTimer != null && _holdTouchTimer.IsRunning) { _holdTouchTimer.Stop(); } } public override void OnPointerClick(PointerEventData ped) { //--Validate click if (!_isHoldingDown) { ExecuteOnPointerClickLogic(); } else { _isHoldingDown = false; } } private void OnHeld() { ExecuteOnHeldLogic(); } #endregion #region Methods protected virtual void ExecuteOnPointerClickLogic() { } protected virtual void ExecuteOnHeldLogic() { } private void InitializeHoldTimer() { if (_defaultHoldTime < 0 && _inputManager != null) { _defaultHoldTime = _inputManager.GetInputSettings().defaultHoldTime; //--This could change during runtime, so we always reference the value in InputManager. } _onHeldCancellation ??= new(); void onCompleted() { using (_onHeldCancellation) { OnHeld(); CompleteHoldTimer(); } _isHoldingDown = true; } void onCancelled() { using (_onHeldCancellation) { CompleteHoldTimer(); } } _holdTouchTimer ??= new(_defaultHoldTime, onCompleted, onCancelled, _onHeldCancellation.Token); _holdTouchTimer.Run(); } private void CompleteHoldTimer() { _onHeldCancellation?.Dispose(); _onHeldCancellation = null; } #endregion } }