aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-05-13 18:14:10 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-05-13 18:15:27 -0700
commitd7c7e70c6c54cb8dd3dea208a53f01f2404fa61d (patch)
treed6c90a3de9491107e5132956416e50ad75a1224b /src
parent48456b549b1262def4d84a2d058aa46abb22242e (diff)
use Browser mouse calculations in glut
Diffstat (limited to 'src')
-rw-r--r--src/library_browser.js2
-rw-r--r--src/library_glut.js41
-rw-r--r--src/library_sdl.js2
3 files changed, 20 insertions, 25 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index 0d0a1ee4..97233c36 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -379,7 +379,7 @@ mergeInto(LibraryManager.library, {
mouseMovementX: 0,
mouseMovementY: 0,
- calculateMouseMove: function(event) {
+ calculateMouseEvent: function(event) { // event should be mousemove, mousedown or mouseup
if (Browser.pointerLock) {
// When the pointer is locked, calculate the coordinates
// based on the movement of the mouse.
diff --git a/src/library_glut.js b/src/library_glut.js
index 49380367..35348028 100644
--- a/src/library_glut.js
+++ b/src/library_glut.js
@@ -12,8 +12,6 @@ var LibraryGLUT = {
motionFunc: null,
passiveMotionFunc: null,
mouseFunc: null,
- lastX: 0,
- lastY: 0,
buttons: 0,
modifiers: 0,
initWindowWidth: 256,
@@ -24,12 +22,6 @@ var LibraryGLUT = {
windowWidth: 0,
windowHeight: 0,
- savePosition: function(event) {
- /* TODO maybe loop here ala http://www.quirksmode.org/js/findpos.html */
- GLUT.lastX = event['clientX'] - Module['canvas'].offsetLeft;
- GLUT.lastY = event['clientY'] - Module['canvas'].offsetTop;
- },
-
saveModifiers: function(event) {
GLUT.modifiers = 0;
if (event['shiftKey'])
@@ -45,20 +37,21 @@ var LibraryGLUT = {
* spamming our app with uncessary callback call. It does happen in
* Chrome on Windows.
*/
- var newX = event['clientX'] - Module['canvas'].offsetLeft;
- var newY = event['clientY'] - Module['canvas'].offsetTop;
- if (newX == GLUT.lastX && newY == GLUT.lastY)
- return;
+ var lastX = Browser.mouseX;
+ var lastY = Browser.mouseY;
+ Browser.calculateMouseEvent(event);
+ var newX = Browser.mouseX;
+ var newY = Browser.mouseY;
+ if (newX == lastX && newY == lastY) return;
- GLUT.savePosition(event);
if (GLUT.buttons == 0 && event.target == Module["canvas"] && GLUT.passiveMotionFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
- Runtime.dynCall('vii', GLUT.passiveMotionFunc, [GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('vii', GLUT.passiveMotionFunc, [lastX, lastY]);
} else if (GLUT.buttons != 0 && GLUT.motionFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
- Runtime.dynCall('vii', GLUT.motionFunc, [GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('vii', GLUT.motionFunc, [lastX, lastY]);
}
},
@@ -159,7 +152,7 @@ var LibraryGLUT = {
if( GLUT.specialFunc ) {
event.preventDefault();
GLUT.saveModifiers(event);
- Runtime.dynCall('viii', GLUT.specialFunc, [key, GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('viii', GLUT.specialFunc, [key, Browser.mouseX, Browser.mouseY]);
}
}
else
@@ -168,7 +161,7 @@ var LibraryGLUT = {
if( key !== null && GLUT.keyboardFunc ) {
event.preventDefault();
GLUT.saveModifiers(event);
- Runtime.dynCall('viii', GLUT.keyboardFunc, [key, GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('viii', GLUT.keyboardFunc, [key, Browser.mouseX, Browser.mouseY]);
}
}
}
@@ -181,7 +174,7 @@ var LibraryGLUT = {
if(GLUT.specialUpFunc) {
event.preventDefault ();
GLUT.saveModifiers(event);
- Runtime.dynCall('viii', GLUT.specialUpFunc, [key, GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('viii', GLUT.specialUpFunc, [key, Browser.mouseX, Browser.mouseY]);
}
}
else
@@ -190,14 +183,15 @@ var LibraryGLUT = {
if( key !== null && GLUT.keyboardUpFunc ) {
event.preventDefault ();
GLUT.saveModifiers(event);
- Runtime.dynCall('viii', GLUT.keyboardUpFunc, [key, GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('viii', GLUT.keyboardUpFunc, [key, Browser.mouseX, Browser.mouseY]);
}
}
}
},
onMouseButtonDown: function(event){
- GLUT.savePosition(event);
+ Browser.calculateMouseEvent(event);
+
GLUT.buttons |= (1 << event['button']);
if(event.target == Module["canvas"] && GLUT.mouseFunc){
@@ -206,18 +200,19 @@ var LibraryGLUT = {
} catch (e) {}
event.preventDefault();
GLUT.saveModifiers(event);
- Runtime.dynCall('viiii', GLUT.mouseFunc, [event['button'], 0/*GLUT_DOWN*/, GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('viiii', GLUT.mouseFunc, [event['button'], 0/*GLUT_DOWN*/, Browser.mouseX, Browser.mouseY]);
}
},
onMouseButtonUp: function(event){
- GLUT.savePosition(event);
+ Browser.calculateMouseEvent(event);
+
GLUT.buttons &= ~(1 << event['button']);
if(GLUT.mouseFunc) {
event.preventDefault();
GLUT.saveModifiers(event);
- Runtime.dynCall('viiii', GLUT.mouseFunc, [event['button'], 1/*GLUT_UP*/, GLUT.lastX, GLUT.lastY]);
+ Runtime.dynCall('viiii', GLUT.mouseFunc, [event['button'], 1/*GLUT_UP*/, Browser.mouseX, Browser.mouseY]);
}
},
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 0e02ef6c..9c5e9805 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -513,7 +513,7 @@ var LibrarySDL = {
}
// fall through
case 'mousemove': {
- Browser.calculateMouseMove(event);
+ Browser.calculateMouseEvent(event);
if (event.type != 'mousemove') {
var down = event.type === 'mousedown';
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};