Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exploration] Keep rotation center when note or form is open #3122

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,12 @@ class MainFragment : Fragment(R.layout.fragment_main),
tilt = if (isFlat) PI.toFloat() / 5f else 0f
}
} else {
val pos = mapFragment.getCurrentCenter(mapOffsetWithOpenBottomSheet)!!
val offsetPos = mapFragment.getPostrotationPositionThatCentersPosition(pos, mapOffsetWithOpenBottomSheet)
mapFragment.updateCameraPosition(300) {
rotation = 0f
tilt = 0f
position = offsetPos
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,18 @@ open class MapFragment : Fragment(),
}
}

fun getCurrentCenter(padding: RectF): LatLon? {
return controller?.screenCenterToLatLon(padding)
}

fun getPositionThatCentersPosition(pos: LatLon, offset: RectF): LatLon? {
return controller?.getLatLonThatCentersLatLon(pos, offset)
}

fun getPostrotationPositionThatCentersPosition(pos: LatLon, offset: RectF): LatLon? {
return controller?.getLatLonThatCentersLatLonAfterRotation(pos, offset)
}

fun getDisplayedArea(): BoundingBox? = controller?.screenAreaToBoundingBox(RectF())

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,27 @@ class KtMapController(private val c: MapController, contentResolver: ContentReso
return position.translate(distanceAfterZoom, angle)
}

fun getLatLonThatCentersLatLonAfterRotation(position: LatLon, padding: RectF, zoom: Float = cameraPosition.zoom): LatLon? {
val view = glViewHolder?.view ?: return null
val w = view.width
val h = view.height
if (w == 0 || h == 0) return null

val screenCenter = screenPositionToLatLon(PointF(w / 2f, h / 2f)) ?: return null
val offsetScreenCenter = screenPositionToLatLon(
PointF(
padding.left + (w - padding.left - padding.right) / 2,
padding.top + (h - padding.top - padding.bottom) / 2
)
) ?: return null

val zoomDelta = zoom.toDouble() - cameraPosition.zoom
val distance = offsetScreenCenter.distanceTo(screenCenter)
val angle = 0.0
val distanceAfterZoom = distance * (2.0).pow(-zoomDelta)
return position.translate(-distanceAfterZoom, angle)
Comment on lines +282 to +300
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure at the beginning how much of getLatLonThatCentersLatLon I'd have to change to get the desired behavior, so I opted to add a copy of it to modify instead of trying to add a flag, and figure out what the right interface ought to be, afterward.

In the end, all I ended up changing was the angle (offset from north) and negating distanceAfterZoom on the final line (which I probably wouldn't even need if I chose the correct angle).

}

/* -------------------------------------- Data Layers --------------------------------------- */

fun addDataLayer(name: String, generateCentroid: Boolean = false): MapData =
Expand Down