From cb21f31f9e65722677be86523ac5ba9e47b8d1c6 Mon Sep 17 00:00:00 2001 From: CG-Tespy Date: Thu, 17 Jun 2021 06:17:04 -0400 Subject: [PATCH] [Bugfix] Drag Event Handlers Executing for Draggables not Really Being Dragged (#968) * Draggables are now only treated as dragged based on the state of a bool Before, when a draggable is being moved even in the editor (or by other things like scroll rects), they can still invoke events. With this fix, we can have the events only execute when they actually are being dragged, be it by the user or some code meant to do said dragging * Moved Drag event handlers to their own folder --- Assets/Fungus/Docs/CHANGELOG.txt | 1 + Assets/Fungus/Scripts/Components/Draggable2D.cs | 12 ++++++++++++ Assets/Fungus/Scripts/EventHandlers/Drag.meta | 8 ++++++++ .../EventHandlers/{ => Drag}/DragCancelled.cs | 0 .../EventHandlers/{ => Drag}/DragCancelled.cs.meta | 0 .../EventHandlers/{ => Drag}/DragCompleted.cs | 6 ++++-- .../EventHandlers/{ => Drag}/DragCompleted.cs.meta | 0 .../Scripts/EventHandlers/{ => Drag}/DragEntered.cs | 3 ++- .../EventHandlers/{ => Drag}/DragEntered.cs.meta | 0 .../Scripts/EventHandlers/{ => Drag}/DragExited.cs | 3 ++- .../EventHandlers/{ => Drag}/DragExited.cs.meta | 0 .../Scripts/EventHandlers/{ => Drag}/DragStarted.cs | 0 .../EventHandlers/{ => Drag}/DragStarted.cs.meta | 0 13 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 Assets/Fungus/Scripts/EventHandlers/Drag.meta rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragCancelled.cs (100%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragCancelled.cs.meta (100%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragCompleted.cs (97%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragCompleted.cs.meta (100%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragEntered.cs (97%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragEntered.cs.meta (100%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragExited.cs (97%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragExited.cs.meta (100%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragStarted.cs (100%) rename Assets/Fungus/Scripts/EventHandlers/{ => Drag}/DragStarted.cs.meta (100%) diff --git a/Assets/Fungus/Docs/CHANGELOG.txt b/Assets/Fungus/Docs/CHANGELOG.txt index fc5d813f..9be1df7a 100644 --- a/Assets/Fungus/Docs/CHANGELOG.txt +++ b/Assets/Fungus/Docs/CHANGELOG.txt @@ -16,6 +16,7 @@ Unreleased - WriterAudio.GetSecondsRemaining reports 0 if not playing. Thanks to KVinS. - Add special case that skips variation logic for sequences that contains no elements, making no change. - Add NotifyEnd to Writer WaitForInputAndClear logic. Thanks to wolfrug. + - Drag event handlers now only trigger from direct drags, not just movement. Thanks to CG-Tespy. v3.13.6 ====== diff --git a/Assets/Fungus/Scripts/Components/Draggable2D.cs b/Assets/Fungus/Scripts/Components/Draggable2D.cs index 73f8b0a7..b375073c 100644 --- a/Assets/Fungus/Scripts/Components/Draggable2D.cs +++ b/Assets/Fungus/Scripts/Components/Draggable2D.cs @@ -36,6 +36,14 @@ namespace Fungus [Tooltip("Use the UI Event System to check for drag events. Clicks that hit an overlapping UI object will be ignored. Camera must have a PhysicsRaycaster component, or a Physics2DRaycaster for 2D colliders.")] [SerializeField] protected bool useEventSystem; + [SerializeField] protected bool beingDragged; + + public virtual bool BeingDragged + { + get { return beingDragged; } + set { beingDragged = value; } + } + protected Vector3 startingPosition; protected bool updatePosition = false; protected Vector3 newPosition; @@ -95,6 +103,8 @@ namespace Fungus protected virtual void DoBeginDrag() { + beingDragged = true; + // Offset the object so that the drag is anchored to the exact point where the user clicked it float x = Input.mousePosition.x; float y = Input.mousePosition.y; @@ -161,6 +171,8 @@ namespace Fungus { LeanTween.move(gameObject, startingPosition, returnDuration).setEase(LeanTweenType.easeOutExpo); } + + beingDragged = false; } protected virtual void DoPointerEnter() diff --git a/Assets/Fungus/Scripts/EventHandlers/Drag.meta b/Assets/Fungus/Scripts/EventHandlers/Drag.meta new file mode 100644 index 00000000..eab00bc9 --- /dev/null +++ b/Assets/Fungus/Scripts/EventHandlers/Drag.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 21248040e66eae44e925a1352ef3add9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs b/Assets/Fungus/Scripts/EventHandlers/Drag/DragCancelled.cs similarity index 100% rename from Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragCancelled.cs diff --git a/Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs.meta b/Assets/Fungus/Scripts/EventHandlers/Drag/DragCancelled.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/EventHandlers/DragCancelled.cs.meta rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragCancelled.cs.meta diff --git a/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs b/Assets/Fungus/Scripts/EventHandlers/Drag/DragCompleted.cs similarity index 97% rename from Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragCompleted.cs index 510cc266..775e584d 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs +++ b/Assets/Fungus/Scripts/EventHandlers/Drag/DragCompleted.cs @@ -159,7 +159,8 @@ namespace Fungus /// public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) { - if (this.targetObjects != null && this.draggableObjects != null && + if (draggableObject.BeingDragged && + this.targetObjects != null && this.draggableObjects != null && this.draggableObjects.Contains(draggableObject) && this.targetObjects.Contains(targetObject)) { @@ -173,7 +174,8 @@ namespace Fungus /// public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) { - if (this.targetObjects != null && this.draggableObjects != null && + if (draggableObject.BeingDragged && + this.targetObjects != null && this.draggableObjects != null && this.draggableObjects.Contains(draggableObject) && this.targetObjects.Contains(targetObject)) { diff --git a/Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs.meta b/Assets/Fungus/Scripts/EventHandlers/Drag/DragCompleted.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/EventHandlers/DragCompleted.cs.meta rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragCompleted.cs.meta diff --git a/Assets/Fungus/Scripts/EventHandlers/DragEntered.cs b/Assets/Fungus/Scripts/EventHandlers/Drag/DragEntered.cs similarity index 97% rename from Assets/Fungus/Scripts/EventHandlers/DragEntered.cs rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragEntered.cs index 4d6ccb54..38df0dd4 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragEntered.cs +++ b/Assets/Fungus/Scripts/EventHandlers/Drag/DragEntered.cs @@ -117,7 +117,8 @@ namespace Fungus /// public virtual void OnDragEntered(Draggable2D draggableObject, Collider2D targetObject) { - if (this.targetObjects != null && this.draggableObjects != null && + if (draggableObject.BeingDragged && + this.targetObjects != null && this.draggableObjects != null && this.draggableObjects.Contains(draggableObject) && this.targetObjects.Contains(targetObject)) { diff --git a/Assets/Fungus/Scripts/EventHandlers/DragEntered.cs.meta b/Assets/Fungus/Scripts/EventHandlers/Drag/DragEntered.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/EventHandlers/DragEntered.cs.meta rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragEntered.cs.meta diff --git a/Assets/Fungus/Scripts/EventHandlers/DragExited.cs b/Assets/Fungus/Scripts/EventHandlers/Drag/DragExited.cs similarity index 97% rename from Assets/Fungus/Scripts/EventHandlers/DragExited.cs rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragExited.cs index d99ff271..2f2a575e 100644 --- a/Assets/Fungus/Scripts/EventHandlers/DragExited.cs +++ b/Assets/Fungus/Scripts/EventHandlers/Drag/DragExited.cs @@ -117,7 +117,8 @@ namespace Fungus /// public virtual void OnDragExited(Draggable2D draggableObject, Collider2D targetObject) { - if (this.targetObjects != null && this.draggableObjects != null && + if (draggableObject.BeingDragged && + this.targetObjects != null && this.draggableObjects != null && this.draggableObjects.Contains(draggableObject) && this.targetObjects.Contains(targetObject)) { diff --git a/Assets/Fungus/Scripts/EventHandlers/DragExited.cs.meta b/Assets/Fungus/Scripts/EventHandlers/Drag/DragExited.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/EventHandlers/DragExited.cs.meta rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragExited.cs.meta diff --git a/Assets/Fungus/Scripts/EventHandlers/DragStarted.cs b/Assets/Fungus/Scripts/EventHandlers/Drag/DragStarted.cs similarity index 100% rename from Assets/Fungus/Scripts/EventHandlers/DragStarted.cs rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragStarted.cs diff --git a/Assets/Fungus/Scripts/EventHandlers/DragStarted.cs.meta b/Assets/Fungus/Scripts/EventHandlers/Drag/DragStarted.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/EventHandlers/DragStarted.cs.meta rename to Assets/Fungus/Scripts/EventHandlers/Drag/DragStarted.cs.meta