From 455295011d1d04d2c9a87f22af4b8b7f99315c04 Mon Sep 17 00:00:00 2001 From: "Li, Zhen" Date: Tue, 20 Feb 2024 21:48:23 -0500 Subject: [PATCH] Partial fix issue-42: allow drag on canvas --- Sources/Grape/Views/ForceDirectedGraph+Gesture.swift | 6 ++++-- Sources/Grape/Views/ForceDirectedGraphModel.swift | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/Grape/Views/ForceDirectedGraph+Gesture.swift b/Sources/Grape/Views/ForceDirectedGraph+Gesture.swift index 781d8cc..ee42d7b 100644 --- a/Sources/Grape/Views/ForceDirectedGraph+Gesture.swift +++ b/Sources/Grape/Views/ForceDirectedGraph+Gesture.swift @@ -9,13 +9,15 @@ extension ForceDirectedGraph { internal func onDragChange( _ value: SwiftUI.DragGesture.Value ) { - if model.draggingNodeID == nil { + if !model.isDragStartStateRecorded { if let nodeID = model.findNode(at: value.startLocation) { model.draggingNodeID = nodeID } else { model.backgroundDragStart = value.location.simd } + assert(model.isDragStartStateRecorded == true) } + guard let nodeID = model.draggingNodeID else { if let dragStart = model.backgroundDragStart { let delta = value.location.simd - dragStart @@ -51,7 +53,7 @@ extension ForceDirectedGraph { if let dragStart = model.backgroundDragStart { let delta = value.location.simd - dragStart model.modelTransform.translate += delta - model.backgroundDragStart = value.location.simd + model.backgroundDragStart = nil } return } diff --git a/Sources/Grape/Views/ForceDirectedGraphModel.swift b/Sources/Grape/Views/ForceDirectedGraphModel.swift index 11fb74b..4d55bb8 100644 --- a/Sources/Grape/Views/ForceDirectedGraphModel.swift +++ b/Sources/Grape/Views/ForceDirectedGraphModel.swift @@ -50,6 +50,11 @@ public final class ForceDirectedGraphModel { @usableFromInline var backgroundDragStart: SIMD2? = nil + @inlinable + var isDragStartStateRecorded: Bool { + return draggingNodeID != nil || backgroundDragStart != nil + } + @usableFromInline let velocityDecay: Double