Skip to content

Commit

Permalink
Merge pull request #45 from li3zhen1/issue-42
Browse files Browse the repository at this point in the history
Partial fix issue-42: allow drag on canvas
  • Loading branch information
li3zhen1 committed Feb 21, 2024
2 parents e797789 + 4552950 commit e74a32e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/Grape/Views/ForceDirectedGraph+Gesture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/Grape/Views/ForceDirectedGraphModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public final class ForceDirectedGraphModel<Content: GraphContent> {
@usableFromInline
var backgroundDragStart: SIMD2<Double>? = nil

@inlinable
var isDragStartStateRecorded: Bool {
return draggingNodeID != nil || backgroundDragStart != nil
}

@usableFromInline
let velocityDecay: Double

Expand Down

0 comments on commit e74a32e

Please sign in to comment.