Skip to content

Commit

Permalink
set and enforce a maximum tilt (fixes #5177)
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Aug 17, 2023
1 parent b1ebdc7 commit d9d74c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import de.westnordost.streetcomplete.util.ktx.runImmediate
import kotlin.math.PI
import kotlin.math.min

/**
* Controls the camera of a Tangram MapController. Use in place of the
Expand Down Expand Up @@ -68,6 +69,8 @@ class CameraManager(private val c: MapController, private val contentResolver: C

val isAnimating: Boolean get() = lastAnimator != null

var maximumTilt: Float = PI.toFloat() / 8f // 45°

interface AnimationsListener {
@UiThread fun onAnimationsStarted()
@UiThread fun onAnimating()
Expand Down Expand Up @@ -115,7 +118,7 @@ class CameraManager(private val c: MapController, private val contentResolver: C
_tangramCamera.longitude = it.longitude
}
update.rotation?.let { _tangramCamera.rotation = it }
update.tilt?.let { _tangramCamera.tilt = it }
update.tilt?.let { _tangramCamera.tilt = min(it, maximumTilt) }
update.zoom?.let { _tangramCamera.zoom = it }
pushCameraPositionToController()
}
Expand All @@ -133,7 +136,8 @@ class CameraManager(private val c: MapController, private val contentResolver: C
assignAnimation("rotation", animator)
}
update.tilt?.let {
propValues.add(PropertyValuesHolder.ofFloat(TangramTiltProperty, it))
val tilt = min(it, maximumTilt)
propValues.add(PropertyValuesHolder.ofFloat(TangramTiltProperty, tilt))
assignAnimation("tilt", animator)
}
update.zoom?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.math.PI
import kotlin.math.log10
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -207,6 +208,12 @@ class KtMapController(private val c: MapController, contentResolver: ContentReso
set(value) { c.maximumZoomLevel = value }
get() = c.maximumZoomLevel

var maximumTilt: Float
set(value) {
cameraManager.maximumTilt = value
}
get() = cameraManager.maximumTilt

fun screenPositionToLatLon(screenPosition: PointF): LatLon? = c.screenPositionToLngLat(screenPosition)?.toLatLon()
fun latLonToScreenPosition(latLon: LatLon): PointF = c.lngLatToScreenPosition(latLon.toLngLat())
fun latLonToScreenPosition(latLon: LatLon, screenPositionOut: PointF, clipToViewport: Boolean) =
Expand Down Expand Up @@ -329,7 +336,18 @@ class KtMapController(private val c: MapController, contentResolver: ContentReso

/* -------------------------------------- Touch input --------------------------------------- */

fun setShoveResponder(responder: TouchInput.ShoveResponder?) { gestureManager.setShoveResponder(responder) }
fun setShoveResponder(responder: TouchInput.ShoveResponder?) {
// enforce maximum tilt
gestureManager.setShoveResponder(object : TouchInput.ShoveResponder {
override fun onShoveBegin() = responder?.onShoveBegin() ?: false
override fun onShoveEnd() = responder?.onShoveEnd() ?: false

override fun onShove(distance: Float): Boolean {
if (cameraPosition.tilt >= maximumTilt && distance < 0) return true
return responder?.onShove(distance) ?: false
}
})
}
fun setScaleResponder(responder: TouchInput.ScaleResponder?) { gestureManager.setScaleResponder(responder) }
fun setRotateResponder(responder: TouchInput.RotateResponder?) { gestureManager.setRotateResponder(responder) }
fun setPanResponder(responder: TouchInput.PanResponder?) { gestureManager.setPanResponder(responder) }
Expand Down

0 comments on commit d9d74c9

Please sign in to comment.