kniEngine / kni

KNI is a cross-platform C# game framework.
Other
92 stars 4 forks source link

Touch gestures #742

Open davidhesselbom opened 6 months ago

davidhesselbom commented 6 months ago

I'm trying to get gestures working, i.e. dragging with a finger a smartphone screen.

Using the default template, the game ends up with a file named Window.6.0.2.js that defines a bunch of window events, and using the browser's debugger and "device emulation" mode, I can see the events window.onmousedown and window.onmouseup are firing. However, window.ontouchstart never fires...

So, I added this in index.html:

        window.addEventListener('touchstart', function (e) {
            console.log("event start")
            var touchobj = e.changedTouches[0];
            startx = parseInt(touchobj.clientX);
            starty = parseInt(touchobj.clientY);
            console.log("event start done", startx, starty)
        }, false);

and then I get the expected output in the console when clicking around in "device emulation" mode, but window.ontouchstart still isn't firing.

Also, I added this to the Update method of the game:

            var state = Mouse.GetState();
            Console.WriteLine($"{new Point(state.X, state.Y)} {state.LeftButton}");

and when using the browser's "device emulation" mode, the button is always "Released", never "Pressed", despite the fact that, as mentioned earlier, the events window.onmousedown and window.onmouseup in Window.6.0.2.js are both firing.

Maybe I'm missing something obvious?

nkast commented 6 months ago

Touch is problematic. Chrome fix and break it on every version, If you google it you will see that it's a problem for over a decade now. What the device emulator does on the desktop is completely irrelevant.

If i remember correctly the mobile version of firefox worked as expected.

andrewandrepowell commented 1 week ago

If it's not too late to give a response, but I recently figured out adding the event listener to the document object, i.e. winder.document.addEventListener(...) worked for me.

nkast commented 1 week ago

Interesting.

Did you modify events in the 'Window.8.0.0.js' ?

https://github.com/nkast/Wasm/blob/main/Wasm.Dom/wwwroot/js/Window.8.0.0.js#L73-L106