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

Add a hotspot with event #247

Open
MahmoudBashir97 opened this issue May 30, 2023 · 1 comment
Open

Add a hotspot with event #247

MahmoudBashir97 opened this issue May 30, 2023 · 1 comment

Comments

@MahmoudBashir97
Copy link

MahmoudBashir97 commented May 30, 2023

Hi dear , I need to know how i can convert from the clicked touch event x,y to a panorama coordinates to add a new Hotspot in android kotlin , please if you can help!

@MahmoudBashir97 MahmoudBashir97 changed the title Attach a hotspot Add a hotspot with event May 30, 2023
@gouravide
Copy link

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants