aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEhsan Akhgari <ehsan.akhgari@gmail.com>2012-05-17 17:24:59 -0400
committerEhsan Akhgari <ehsan.akhgari@gmail.com>2012-05-22 15:44:16 -0400
commit1be33be6cab36fd056feb812bcc4999682b3425f (patch)
tree9e06d4d554efaa8a2e8941cb9a789e543733cfdb
parentb7ce870dd4b1352e308e212e77cd6161c1ec904e (diff)
Fix the SDL mouse motion events
-rw-r--r--src/library_sdl.js23
-rw-r--r--tests/sdl_mouse.c1
2 files changed, 16 insertions, 8 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index e2e199e5..eac68600 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: {},
@@ -292,6 +293,13 @@ var LibrarySDL = {
pageX: event.pageX,
pageY: event.pageY
};
+ } else 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;
}
SDL.events.push(event);
if (SDL.events.length >= 10000) {
@@ -307,7 +315,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
@@ -361,8 +369,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 +441,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);