diff options
author | Michael Bishop <mbtyke@gmail.com> | 2013-10-18 11:04:08 -0400 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2014-04-13 16:57:12 +0300 |
commit | 248be33550961ffa88130f8815616cf61701ff1e (patch) | |
tree | b615e3bb138ad986ede61ee182077aaf63d19e0c /src/library_browser.js | |
parent | ce58885c11947bc4fb511646989c7d88212f69fa (diff) |
Added preliminary support for the SDL2 touch api. Missing items:
1. Timestamps
2. TouchID (the Device ID)
As inline with the SDL spec, we will pass a touch ID of
`SDL_TOUCH_MOUSEID` for touch events that are simulated by the
mouse so games can rely solely on touch events if they like.
Includes the SDL2 Copyright notice on the headers that contain SDL2
content.
Includes a fix to SDL_PeepEvents.
Diffstat (limited to 'src/library_browser.js')
-rw-r--r-- | src/library_browser.js | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 0808b9f0..9f4bed5c 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -482,6 +482,8 @@ mergeInto(LibraryManager.library, { mouseY: 0, mouseMovementX: 0, mouseMovementY: 0, + touches: {}, + lastTouches: {}, calculateMouseEvent: function(event) { // event should be mousemove, mousedown or mouseup if (Browser.pointerLock) { @@ -510,8 +512,9 @@ mergeInto(LibraryManager.library, { // Otherwise, calculate the movement based on the changes // in the coordinates. var rect = Module["canvas"].getBoundingClientRect(); - var x, y; - + var cw = Module["canvas"].width; + var ch = Module["canvas"].height; + // Neither .scrollX or .pageXOffset are defined in a spec, but // we prefer .scrollX because it is currently in a spec draft. // (see: http://www.w3.org/TR/2013/WD-cssom-view-20131217/) @@ -522,26 +525,37 @@ mergeInto(LibraryManager.library, { // and we have no viable fallback. assert((typeof scrollX !== 'undefined') && (typeof scrollY !== 'undefined'), 'Unable to retrieve scroll position, mouse positions likely broken.'); #endif - if (event.type == 'touchstart' || - event.type == 'touchend' || - event.type == 'touchmove') { - var t = event.touches.item(0); - if (t) { - x = t.pageX - (scrollX + rect.left); - y = t.pageY - (scrollY + rect.top); - } else { - return; + + if (event.type === 'touchstart' || event.type === 'touchend' || event.type === 'touchmove') { + var touch = event.touch; + if (touch === undefined) { + return; // the "touch" property is only defined in SDL + } - } else { - x = event.pageX - (scrollX + rect.left); - y = event.pageY - (scrollY + rect.top); + var adjustedX = touch.pageX - (scrollX + rect.left); + var adjustedY = touch.pageY - (scrollY + rect.top); + + adjustedX = adjustedX * (cw / rect.width); + adjustedY = adjustedY * (ch / rect.height); + + var coords = {x: adjustedX, y: adjustedY}; + + if (event.type === 'touchstart') { + Browser.lastTouches[touch.identifier] = coords; + Browser.touches[touch.identifier] = coords; + } else if (event.type === 'touchend' || event.type === 'touchmove') { + Browser.lastTouches[touch.identifier] = Browser.touches[touch.identifier]; + Browser.touches[touch.identifier] = { x: adjustedX, y: adjustedY }; + } + return; } + var x = event.pageX - (scrollX + rect.left); + var y = event.pageY - (scrollY + rect.top); + // the canvas might be CSS-scaled compared to its backbuffer; // SDL-using content will want mouse coordinates in terms // of backbuffer units. - var cw = Module["canvas"].width; - var ch = Module["canvas"].height; x = x * (cw / rect.width); y = y * (ch / rect.height); |