Skip to content

Commit

Permalink
i tucked everything into gesture detector, wheel-scrolling too
Browse files Browse the repository at this point in the history
  • Loading branch information
pier-bezuhoff committed Jan 16, 2024
1 parent 177af6b commit b7763aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
7 changes: 4 additions & 3 deletions composeApp/src/commonMain/kotlin/ui/EditClusterScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ fun EditClusterContent(
}
else { // only long-press drag in other modes
// navigate canvas
// offset = offset + pan // ought to be something more sophisticated
offset = (offset + centroid/scale) - (centroid/(scale*zoom) + pan/scale)
scale *= zoom
scale *= zoom // MAYBE: add zoom slider for non-mobile
}
}

},
onVerticalScroll = { yDelta ->
1 // maybe scale as onPanZoom
},
onDragStart = {
// draggables = circles
// grab circle or handle
Expand All @@ -244,7 +246,6 @@ fun EditClusterContent(
)
.graphicsLayer(
scaleX = scale, scaleY = scale,
// translationX = offset.x, translationY = offset.y,
translationX = -scale*offset.x, translationY = -scale*offset.y,
transformOrigin = TransformOrigin(0f, 0f)
)
Expand Down
70 changes: 41 additions & 29 deletions composeApp/src/commonMain/kotlin/ui/Gestures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,65 @@ import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.gestures.waitForUpOrCancellation
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.pointerInput

inline fun Modifier.reactiveCanvas(
vararg keys: Any?,
crossinline onPanZoom: (pan: Offset, centroid: Offset, zoom: Float) -> Unit,
crossinline onVerticalScroll: (yDelta: Float) -> Unit = { },
crossinline onDown: (position: Offset) -> Unit = { },
crossinline onTap: (position: Offset) -> Unit = { },
crossinline onDragStart: (position: Offset) -> Unit = { },
crossinline onDrag: (delta: Offset) -> Unit,
crossinline onDragEnd: () -> Unit = { },
): Modifier =
pointerInput(*keys) {
detectTransformGestures { centroid, pan, zoom, rotation ->
this
.pointerInput(*keys) {
awaitEachGesture {
val event = awaitPointerEvent(PointerEventPass.Initial)
if (event.type == PointerEventType.Scroll) {
val yDelta = event.changes.map { it.scrollDelta.y }.sum()
// println("scroll")
onVerticalScroll(yDelta)
}
}
}
.pointerInput(*keys) {
detectTransformGestures { centroid, pan, zoom, rotation ->
// println("transform")
onPanZoom(pan, centroid, zoom)
}
}
.pointerInput(*keys) {
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)
onPanZoom(pan, centroid, zoom)
}
}
.pointerInput(*keys) {
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)
// println("down")
onDown(down.position)
onDown(down.position)
}
}
}
.pointerInput(*keys) {
detectTapGestures { position ->
.pointerInput(*keys) {
detectTapGestures { position ->
// println("tap")
onTap(position)
onTap(position)
}
}
}
// NOTE: the later pointInput-s *can* consume events before passing it higher
.pointerInput(*keys) {
detectDragGesturesAfterLongPress(
onDragStart = { position ->
// NOTE: the later pointInput-s *can* consume events before passing it higher
.pointerInput(*keys) {
detectDragGesturesAfterLongPress(
onDragStart = { position ->
// println("long drag start")
onDragStart(position)
},
onDrag = { change, dragAmount ->
onDrag(dragAmount)
},
onDragEnd = {
onDragStart(position)
},
onDrag = { change, dragAmount ->
onDrag(dragAmount)
},
onDragEnd = {
// println("long drag end")
onDragEnd()
}
)
}
onDragEnd()
}
)
}

0 comments on commit b7763aa

Please sign in to comment.