aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-05-24 01:42:15 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-05-24 01:42:15 -0700
commite6228eb1692bcf04c9f47f0af1655cca452d3132 (patch)
tree3e33b988af0c0c042c1fd0e0b86a6734cf99aedf
parent226e89cf2dad4fa96603ebd6f2255c4cbeb89bdd (diff)
parenta431ee57217c68ecbf47eb8932d09718c38e0cc9 (diff)
Merge pull request #435 from ehsan/sdlmouse
Fix the SDL mouse motion events
-rw-r--r--src/library_sdl.js28
-rw-r--r--tests/sdl_mouse.c1
2 files changed, 20 insertions, 9 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index e2e199e5..85a04d27 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -27,6 +27,7 @@ var LibrarySDL = {
startTime: null,
mouseX: 0,
mouseY: 0,
+ buttonState: 0,
DOMEventToSDLEvent: {},
@@ -307,7 +308,7 @@ var LibrarySDL = {
}
return false;
},
-
+
makeCEvent: function(event, ptr) {
if (typeof event === 'number') {
// This is a pointer to a native C event that was SDL_PushEvent'ed
@@ -349,7 +350,17 @@ var LibrarySDL = {
break;
}
- case 'mousedown': case 'mouseup': case 'mousemove': {
+ case 'mousedown': case 'mouseup':
+ if (event.type == 'mousedown') {
+ // SDL_BUTTON(x) is defined as (1 << ((x)-1)). SDL buttons are 1-3,
+ // and DOM buttons are 0-2, so this means that the below formula is
+ // correct.
+ SDL.buttonState |= 1 << event.button;
+ } else if (event.type == 'mouseup') {
+ SDL.buttonState = 0;
+ }
+ // fall through
+ case 'mousemove': {
var x = event.pageX - Module['canvas'].offsetLeft;
var y = event.pageY - Module['canvas'].offsetTop;
if (event.type != 'mousemove') {
@@ -361,8 +372,7 @@ var LibrarySDL = {
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.y', 'y', 'i32') }}};
} else {
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
- {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.button', 'event.button', 'i8') }}};
- {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.state', 'down ? 1 : 0', 'i8') }}};
+ {{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.state', 'SDL.buttonState', 'i8') }}};
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.x', 'x', 'i32') }}};
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.y', 'y', 'i32') }}};
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.xrel', 'Browser.getMovementX(x - SDL.mouseX, event)', 'i32') }}};
@@ -434,11 +444,11 @@ var LibrarySDL = {
SDL.keyboardState = _malloc(0x10000);
_memset(SDL.keyboardState, 0, 0x10000);
// Initialize this structure carefully for closure
- SDL.DOMEventToSDLEvent['keydown'] = 0x300;
- SDL.DOMEventToSDLEvent['keyup'] = 0x301;
- SDL.DOMEventToSDLEvent['mousedown'] = 0x401;
- SDL.DOMEventToSDLEvent['mouseup'] = 0x402;
- SDL.DOMEventToSDLEvent['mousemove'] = 0x400;
+ SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */;
+ SDL.DOMEventToSDLEvent['keyup'] = 0x301 /* SDL_KEYUP */;
+ SDL.DOMEventToSDLEvent['mousedown'] = 0x401 /* SDL_MOUSEBUTTONDOWN */;
+ SDL.DOMEventToSDLEvent['mouseup'] = 0x402 /* SDL_MOUSEBUTTONUP */;
+ SDL.DOMEventToSDLEvent['mousemove'] = 0x400 /* SDL_MOUSEMOTION */;
return 0; // success
},
diff --git a/tests/sdl_mouse.c b/tests/sdl_mouse.c
index eb4aa425..8af87c8c 100644
--- a/tests/sdl_mouse.c
+++ b/tests/sdl_mouse.c
@@ -12,6 +12,7 @@ void one() {
switch(event.type) {
case SDL_MOUSEMOTION: {
SDL_MouseMotionEvent *m = (SDL_MouseMotionEvent*)&event;
+ assert(m->state == 0);
int x, y;
SDL_GetMouseState(&x, &y);
assert(x == m->x && y == m->y);