More snappy tap animation

This commit is contained in:
Michal Pikulski
2025-11-06 18:43:06 +01:00
parent a9b9fb87b5
commit d01859cec0
9 changed files with 397 additions and 47 deletions

View File

@@ -190,13 +190,20 @@ namespace UI.DragAndDrop.Core
if (eventData.button != PointerEventData.InputButton.Left)
return;
Debug.Log($"[DraggableObject] OnBeginDrag called on {name}. _isDraggingEnabled={_isDraggingEnabled}");
// Check if dragging is enabled BEFORE setting any state
if (!_isDraggingEnabled)
{
Debug.Log($"[DraggableObject] OnBeginDrag blocked - dragging disabled on {name}");
return;
}
_isDragging = true;
_wasDragged = true;
Debug.Log($"[DraggableObject] Drag started on {name}");
// ...existing code...
if (_canvas != null && _canvas.renderMode == RenderMode.ScreenSpaceOverlay && RectTransform != null)
{
@@ -498,7 +505,26 @@ namespace UI.DragAndDrop.Core
/// </summary>
public virtual void SetDraggingEnabled(bool enabled)
{
Debug.Log($"[DraggableObject] SetDraggingEnabled({enabled}) called on {name}. _isDragging={_isDragging}, _isDraggingEnabled={_isDraggingEnabled}");
_isDraggingEnabled = enabled;
// If disabling dragging while actively dragging, stop the drag
if (!enabled && _isDragging)
{
Debug.Log($"[DraggableObject] Stopping active drag on {name}");
_isDragging = false;
// Re-enable raycasting
if (_raycaster != null)
_raycaster.enabled = true;
if (_imageComponent != null)
_imageComponent.raycastTarget = true;
if (_canvasGroup != null)
_canvasGroup.blocksRaycasts = true;
}
Debug.Log($"[DraggableObject] After SetDraggingEnabled: _isDragging={_isDragging}, _isDraggingEnabled={_isDraggingEnabled}");
}
#endregion

View File

@@ -1,4 +1,5 @@
using Pixelplacement;
using Pixelplacement.TweenSystem;
using UnityEngine;
using UnityEngine.InputSystem; // Added for new Input System
@@ -46,9 +47,17 @@ namespace UI.DragAndDrop.Core
protected int _savedSlotIndex;
protected Vector3 _lastPosition;
// Effect tracking
protected bool _effectsSuppressed;
protected bool _isPlayingPlacementTween; // Tracks if a one-time placement tween is active
protected TweenBase _placementPositionTween;
protected TweenBase _placementScaleTween;
protected TweenBase _activeTweenId; // Track active scale tween for cancellation
// Properties
public DraggableObject ParentDraggable => _parentDraggable;
public bool IsInitialized => _isInitialized;
public bool EffectsSuppressed => _effectsSuppressed;
/// <summary>
/// Initialize the visual with its parent draggable object
@@ -136,11 +145,18 @@ namespace UI.DragAndDrop.Core
{
if (!_isInitialized || _parentDraggable == null)
return;
UpdateFollowPosition();
UpdateFollowRotation(); // Track base rotation changes
UpdateRotation();
UpdateTilt();
// Skip follow position/rotation when effects are suppressed (e.g., in opening slot)
// UNLESS we're currently playing a placement tween (allow it to complete)
// The visual should stay locked in place after placement tween completes
if (!_effectsSuppressed || _isPlayingPlacementTween)
{
UpdateFollowPosition();
UpdateFollowRotation(); // Track base rotation changes
UpdateRotation();
UpdateTilt();
}
UpdateVisualContent();
}
@@ -275,7 +291,10 @@ namespace UI.DragAndDrop.Core
: _parentDraggable.GetSlotIndex();
// Idle animation (sine/cosine wobble with different frequencies)
// Disabled if effects are suppressed
float idleMultiplier = _parentDraggable.IsHovering ? 0.2f : 1f;
idleMultiplier = _effectsSuppressed ? 0f : idleMultiplier;
float time = Time.time * idleAnimationSpeed + _savedSlotIndex;
// Use sine for X wobble, cosine for Y wobble (different phases)
@@ -346,7 +365,10 @@ namespace UI.DragAndDrop.Core
{
if (useScaleAnimations)
{
Tween.LocalScale(transform, Vector3.one * scaleOnDrag, scaleTransitionDuration, 0f, Tween.EaseOutBack);
if (_activeTweenId != null)
_activeTweenId.Stop();
_activeTweenId = Tween.LocalScale(transform, Vector3.one * scaleOnDrag, scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
if (canvas != null)
@@ -382,7 +404,11 @@ namespace UI.DragAndDrop.Core
// Only reset scale if NOT in a slot (let slots handle their own scaling)
if (draggable.CurrentSlot == null)
{
Tween.LocalScale(transform, Vector3.one, scaleTransitionDuration, 0f, Tween.EaseOutBack);
// TODO: Fix this repetetive stuff
if (_activeTweenId != null)
_activeTweenId.Stop();
_activeTweenId = Tween.LocalScale(transform, Vector3.one, scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
@@ -405,7 +431,10 @@ namespace UI.DragAndDrop.Core
{
if (useScaleAnimations)
{
Tween.LocalScale(transform, Vector3.one * scaleOnHover, scaleTransitionDuration, 0f, Tween.EaseOutBack);
if (_activeTweenId != null)
_activeTweenId.Stop();
_activeTweenId = Tween.LocalScale(transform, Vector3.one * scaleOnHover, scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
// Punch rotation effect
@@ -421,7 +450,10 @@ namespace UI.DragAndDrop.Core
{
if (!draggable.WasDragged && useScaleAnimations)
{
Tween.LocalScale(transform, Vector3.one, scaleTransitionDuration, 0f, Tween.EaseOutBack);
if (_activeTweenId != null)
_activeTweenId.Stop();
_activeTweenId = Tween.LocalScale(transform, Vector3.one, scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
OnPointerExitVisual();
@@ -431,7 +463,10 @@ namespace UI.DragAndDrop.Core
{
if (useScaleAnimations)
{
Tween.LocalScale(transform, Vector3.one * scaleOnDrag, scaleTransitionDuration, 0f, Tween.EaseOutBack);
if (_activeTweenId != null)
_activeTweenId.Stop();
_activeTweenId = Tween.LocalScale(transform, Vector3.one * scaleOnDrag, scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
OnPointerDownVisual();
@@ -443,7 +478,10 @@ namespace UI.DragAndDrop.Core
if (useScaleAnimations)
{
Tween.LocalScale(transform, Vector3.one * targetScale, scaleTransitionDuration, 0f, Tween.EaseOutBack);
if (_activeTweenId != null)
_activeTweenId.Stop();
_activeTweenId = Tween.LocalScale(transform, Vector3.one * targetScale, scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
OnPointerUpVisual(longPress);
@@ -462,7 +500,11 @@ namespace UI.DragAndDrop.Core
if (useScaleAnimations)
{
float targetScale = selected ? scaleOnDrag : scaleOnHover;
Tween.LocalScale(transform, Vector3.one * targetScale, scaleTransitionDuration, 0f, Tween.EaseOutBack);
if (_activeTweenId != null)
_activeTweenId.Stop();
_activeTweenId = Tween.LocalScale(transform, Vector3.one * targetScale, scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
OnSelectionVisual(selected);
@@ -520,6 +562,146 @@ namespace UI.DragAndDrop.Core
#endregion
#region Effect Management API
/// <summary>
/// Suppress all ongoing visual effects (idle animations, glow, etc.)
/// This does NOT affect tweens initiated externally (like slot placement).
/// Subclasses should override this to stop their specific effects.
/// </summary>
public virtual void SuppressEffects()
{
_effectsSuppressed = true;
// Cancel any active scale tween we're tracking
if (_activeTweenId != null)
{
_activeTweenId.Stop();
_activeTweenId = null;
}
// Cancel ALL tweens on this transform to prevent slot scale tween from conflicting
Tween.Cancel(transform.GetInstanceID());
OnEffectsSuppressed();
}
/// <summary>
/// Resume all visual effects that were suppressed
/// </summary>
public virtual void ResumeEffects()
{
_effectsSuppressed = false;
OnEffectsResumed();
}
/// <summary>
/// Reset all visual effects to their default state
/// </summary>
public virtual void ResetEffects()
{
_effectsSuppressed = false;
// Cancel any active scale tween we're tracking
if (_activeTweenId != null)
{
_activeTweenId.Stop();
_activeTweenId = null;
}
// Reset scale to default
transform.localScale = Vector3.one;
// Reset shake parent rotation
if (shakeParent != null)
{
shakeParent.localRotation = Quaternion.identity;
}
// Reset tilt parent rotation
if (tiltParent != null)
{
tiltParent.localRotation = Quaternion.identity;
}
OnEffectsReset();
}
/// <summary>
/// Play a one-time placement tween to move visual to target position and scale.
/// This works even when effects are suppressed, and allows the visual to follow
/// the parent during the tween, then locks in place after completion.
/// </summary>
/// <param name="duration">Duration of the tween</param>
/// <param name="targetScale">Target scale (if null, uses current parent scale)</param>
public virtual void PlayPlacementTween(float duration = 0.3f, Vector3? targetScale = null)
{
// Cancel any existing placement tweens
StopPlacementTween();
_isPlayingPlacementTween = true;
// The position tween is handled by UpdateFollowPosition naturally
// We just need to wait for the duration, then lock in place
// If target scale provided, tween the scale
if (targetScale.HasValue)
{
_placementScaleTween = Tween.LocalScale(transform, targetScale.Value, duration, 0f, Tween.EaseOutBack);
}
// Use a dummy tween to track completion
_placementPositionTween = Tween.Value(0f, 1f, (val) => { }, duration, 0f, Tween.EaseOutBack,
completeCallback: () =>
{
_isPlayingPlacementTween = false;
_placementPositionTween = null;
OnPlacementTweenComplete();
});
}
/// <summary>
/// Stop any active placement tween and immediately lock in place
/// </summary>
public virtual void StopPlacementTween()
{
if (_placementPositionTween != null)
{
_placementPositionTween.Stop();
_placementPositionTween = null;
}
if (_placementScaleTween != null)
{
_placementScaleTween.Stop();
_placementScaleTween = null;
}
_isPlayingPlacementTween = false;
}
/// <summary>
/// Called when placement tween completes. Override for custom behavior.
/// </summary>
protected virtual void OnPlacementTweenComplete() { }
/// <summary>
/// Called when effects are suppressed. Override to stop specific effects.
/// </summary>
protected virtual void OnEffectsSuppressed() { }
/// <summary>
/// Called when effects are resumed. Override to restart specific effects.
/// </summary>
protected virtual void OnEffectsResumed() { }
/// <summary>
/// Called when effects are reset. Override to reset specific effects.
/// </summary>
protected virtual void OnEffectsReset() { }
#endregion
protected virtual void OnDestroy()
{
UnsubscribeFromParentEvents();