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

Touchscreen do not work #59

Closed
mallumoSK opened this issue Dec 9, 2023 · 4 comments
Closed

Touchscreen do not work #59

mallumoSK opened this issue Dec 9, 2023 · 4 comments

Comments

@mallumoSK
Copy link

Hi,
I not sure why but touchscreen do not work on JS target (notebook with touchscreen)

I rewrite fallows, it is NOT the best solution but works:

// de.fabmax.kool.input.PlatformInputJs

    private fun Event.asTouchEvent(): TouchEvent? {
        return if (this is TouchEvent) this
        else null
    }

 private fun installInputHandlers(canvas: HTMLCanvasElement) {
        var isTouchActive = false

        fun Event.buildTouchPosition(): Vec2d? {
            preventDefault()
            return with(canvas.getBoundingClientRect()) {
                asTouchEvent()?.let { touch ->
                    if (touch.changedTouches.length == 0) null
                    else {
                        val item = touch.changedTouches.item(0)
                        Vec2d(
                            item.elementX * window.devicePixelRatio - left,
                            item.elementY * window.devicePixelRatio - top
                        )
                    }
                }
            }
        }

        fun Event.invokeMoveTouchAsMouse() {
            buildTouchPosition()?.also {
                virtualPointerPos.x = it.x
                virtualPointerPos.y = it.y
                PointerInput.handleMouseMove(virtualPointerPos.x, virtualPointerPos.y)
            }
        }

        canvas.onmousemove = { ev ->
            if (!isTouchActive) {
                val bounds = canvas.getBoundingClientRect()
                if (PointerLockState.hasPointerLock) {
                    // on active pointer lock, mouse event position is constant and only deltas are reported
                    //  -> use deltas to compute a virtual unbounded pointer position
                    virtualPointerPos.x += pointerMovementX(ev)
                    virtualPointerPos.y += pointerMovementY(ev)
                } else {
                    virtualPointerPos.x = (ev.clientX * window.devicePixelRatio - bounds.left)
                    virtualPointerPos.y = (ev.clientY * window.devicePixelRatio - bounds.top)
                }
                PointerInput.handleMouseMove(virtualPointerPos.x, virtualPointerPos.y)
            }
        }

        canvas.addEventListener("touchstart", { ev ->
            ev.preventDefault()
            isTouchActive = true
            ev.invokeMoveTouchAsMouse()
            PointerInput.handleMouseButtonEvent(0, true)
        }, false)

        canvas.addEventListener("touchend", { ev ->
            ev.preventDefault()
            ev.invokeMoveTouchAsMouse()
            PointerInput.handleMouseButtonEvent(0, false)
            isTouchActive = false
        }, false)

        canvas.addEventListener("touchcancel", { ev ->
            ev.preventDefault()
            isTouchActive = false
            ev.invokeMoveTouchAsMouse()
            PointerInput.handleMouseExit()
            isTouchActive = false
        }, false)

        canvas.addEventListener("touchmove", { ev ->
            ev.preventDefault()
            ev.asTouchEvent()?.apply {
                if (changedTouches.length == 1) {
                    ev.invokeMoveTouchAsMouse()
                }
            }
        }, false)

...

These modifications break OrbitInputTransform at touch mode
But can be easily fixed by storing initial position at touch start
and for camera transform use difference between current and initial position

@fabmax
Copy link
Collaborator

fabmax commented Dec 10, 2023

Can you make a pull request for this? By copy and pasting code it's really difficult to track the actual changes

@mallumoSK
Copy link
Author

Ok but how to do it ... i have no permission to create branch

@fabmax
Copy link
Collaborator

fabmax commented Jan 24, 2024

You have to fork the repository. Then, you can push your changes to your own fork / copy of the repo. Once you did that github should offer you an option to create a pull request.

@mallumoSK
Copy link
Author

thanx, good to known :)

#61

@fabmax fabmax closed this as completed Feb 25, 2025
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