Krzysztof
06/18/2021, 9:47 PMKrzysztof
06/18/2021, 10:32 PMspierce7
06/18/2021, 11:52 PMI am not yet sure whether it is possible to handle mouse scroll wheel, as it is not mentioned hereI haven’t tried this at all, but compose for desktop is basically just a Java Swing application drawing on a canvas (Maybe that’s an oversimplification?) My main point is, you have access to all the same stuff that a normal Java GUI application has access too. i.e. Mouse scroll wheel support. I’d look at something like this if you just need to detect the event https://docs.oracle.com/javase/tutorial/uiswing/events/mousewheellistener.html
Michael Paus
06/19/2021, 7:21 AM.mouseScrollFilter(onMouseScroll = { ev, _ ->
when (ev.delta) {
is MouseScrollUnit.Line -> {
val deltaValue = (ev.delta as MouseScrollUnit.Line).value
val zoomOut = deltaValue < 0.0
pointerLocation?.let { setScaleBy(if (zoomOut) 1.2 else 1/1.2, it) }
}
}
true})
I use that to zoom in and out to my canvas.Krzysztof
06/19/2021, 6:36 PMVadim Kapustin
06/21/2021, 8:58 AMModifier
.size(maxWidth, maxHeight)
.clip(RoundedCornerShape(8.dp))
.mouseScrollFilter { event, _ -> // make a map zoom
val scrollUnit = event.delta
val multiplier = if (scrollUnit is MouseScrollUnit.Line) {
if (scrollUnit.value > 0f) 1 / scrollStep else scrollStep
} else 1.0
currentArea = currentArea.zoom(multiplier, currentPoint)
true
}
.pointerMoveFilter( // change position of mouse pointer
onMove = { offset ->
currentPoint = MapPoint(offset)
previousDragPoint = null
true
}
)
.pointerInput(Unit) { // make a map zoom by double clicking
detectTapGestures(
onDoubleTap = { offset ->
currentArea = currentArea.zoom(sqrt(2.0), currentPoint)
},
)
}
.pointerInput(Unit) { // make a map shift
detectDragGestures(
onDrag = { change, _ ->
currentArea = currentArea.shift(
change.position - (previousDragPoint ?: change.previousPosition)
)
previousDragPoint = change.position
currentPoint = MapPoint(change.position)
}
)
}
currentPoint and currentArea - mutable states containing coordinates of the mouse pointer and displayed area of the map in georgraphical coordinatesVadim Kapustin
06/21/2021, 9:10 AMLaunchedEffect(currentArea) {
if (currentArea.isValid && currentArea != currentImage?.area) {
if (currentImage != null) delay(100) // pause to accumulate changes
progress = true
currentImage = MapRenderer().render(currentArea) // render map image for currentArea
progress = false
}
}
MapRenderer creates an image of the current area from tiles of the appropriate scaleVadim Kapustin
06/21/2021, 9:17 AMKrzysztof
06/21/2021, 6:54 PM