hannesa2 / panoramaGL

PanoramaGL Android
Apache License 2.0
169 stars 60 forks source link

Hotspot on Click #155

Open mohdDanishCode opened 2 years ago

mohdDanishCode commented 2 years ago

Not Able to get the actual touch coordinate of the panorama image create a hotspot with GestureDetector - on long press on the Screen

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!

gouravide commented 6 months ago

override fun onLongPress(e: MotionEvent) {

    val x = e.x
    val y = e.y
    val screenWidth = resources.displayMetrics.widthPixels
    val screenHeight = resources.displayMetrics.heightPixels
    val normalizedX = x / screenWidth
    val normalizedY = y / screenHeight

    addHotspotAt(normalizedX, normalizedY, screenWidth, screenHeight)
    Toast.makeText(this, "${normalizedX}, ${screenHeight}", Toast.LENGTH_SHORT).show()
}

private fun addHotspotAt(
    normalizedX: Float, normalizedY: Float, screenWidth: Int, screenHeight: Int
) {
    // These values would ideally come from your panorama viewer or camera settings
    val currentYaw = plManager.panorama.camera.yaw
    val currentPitch = plManager.panorama.camera.pitch
    val fov = plManager.panorama.camera.fov  // Current FOV considering zoom
    val aspectRatio = screenWidth.toFloat() / screenHeight

    // Convert normalized screen coordinates to changes in yaw and pitch
    val yawChange = (normalizedX - 0.5f) * fov
    val pitchChange = (0.5f - normalizedY) * (fov / aspectRatio)

    // Calculate new yaw and pitch for the hotspot
    val hotspotYaw = (currentYaw + yawChange) % 360
    val hotspotPitch = currentPitch + pitchChange
    // Ensure pitch stays within bounds [-90, 90]
    val clampedPitch = hotspotPitch.coerceIn(-90f, 90f)

    // Now, create your hotspot at this new yaw and pitch
    createHotspot(hotspotPitch = clampedPitch, hotspotYaw = hotspotYaw)
}

private fun createHotspot(hotspotPitch: Float, hotspotYaw: Float) {
    val hotspot = ActionPLHotspot(
        this,
        System.currentTimeMillis(),
        PLImage(BitmapFactory.decodeResource(resources, R.raw.hotspot)),
        hotspotPitch,
        hotspotYaw,
        PLConstants.kDefaultHotspotSize,
        PLConstants.kDefaultHotspotSize
    )
    plManager.panorama.addHotspot(hotspot)
}