diff options
Diffstat (limited to 'src/library_sdl.js')
-rw-r--r-- | src/library_sdl.js | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js index 580330e8..8cb8db72 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -350,11 +350,19 @@ var LibrarySDL = { event['movementX'] = event['mozMovementX']; event['movementY'] = event['mozMovementY']; // fall through - case 'keydown': case 'keyup': case 'mousedown': case 'mouseup': case 'DOMMouseScroll': - if (event.type == 'DOMMouseScroll') { - event = { + case 'keydown': case 'keyup': case 'mousedown': case 'mouseup': case 'DOMMouseScroll': case 'mousewheel': + if (event.type == 'DOMMouseScroll' || event.type == 'mousewheel') { + var button = (event.type == 'DOMMouseScroll' ? event.detail : -event.wheelDelta) > 0 ? 4 : 3; + var event2 = { type: 'mousedown', - button: event.detail > 0 ? 4 : 3, + button: button, + pageX: event.pageX, + pageY: event.pageY + }; + SDL.events.push(event2); + event = { + type: 'mouseup', + button: button, pageX: event.pageX, pageY: event.pageY }; @@ -371,11 +379,6 @@ var LibrarySDL = { Module.printErr('SDL event queue full, dropping earliest event'); SDL.events.shift(); } - if ((event.keyCode >= 37 && event.keyCode <= 40) || // arrow keys - event.keyCode == 32 || // space - event.keyCode == 33 || event.keyCode == 34) { // page up/down - event.preventDefault(); - } break; case 'mouseout': // Un-press all pressed mouse buttons, because we might miss the release outside of the canvas @@ -391,6 +394,11 @@ var LibrarySDL = { } } break; + case 'unload': + SDL.events.push(event); + // Force-run a main event loop, since otherwise this event will never be caught! + Browser.mainLoop.runner(); + return true; } return false; }, @@ -485,6 +493,10 @@ var LibrarySDL = { SDL.mouseY = y; break; } + case 'unload': { + {{{ makeSetValue('ptr', 'SDL.structs.KeyboardEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}}; + break; + } default: throw 'Unhandled SDL event: ' + event.type; } }, @@ -550,9 +562,11 @@ var LibrarySDL = { SDL_Init: function(what) { SDL.startTime = Date.now(); - ['keydown', 'keyup'].forEach(function(event) { - addEventListener(event, SDL.receiveEvent, true); - }); + // capture all key events. we just keep down and up, but also capture press to prevent default actions + document.onkeydown = SDL.receiveEvent; + document.onkeyup = SDL.receiveEvent; + document.onkeypress = SDL.receiveEvent; + window.onunload = SDL.receiveEvent; SDL.keyboardState = _malloc(0x10000); _memset(SDL.keyboardState, 0, 0x10000); // Initialize this structure carefully for closure @@ -561,6 +575,7 @@ var LibrarySDL = { SDL.DOMEventToSDLEvent['mousedown'] = 0x401 /* SDL_MOUSEBUTTONDOWN */; SDL.DOMEventToSDLEvent['mouseup'] = 0x402 /* SDL_MOUSEBUTTONUP */; SDL.DOMEventToSDLEvent['mousemove'] = 0x400 /* SDL_MOUSEMOTION */; + SDL.DOMEventToSDLEvent['unload'] = 0x100 /* SDL_QUIT */; return 0; // success }, @@ -619,7 +634,7 @@ var LibrarySDL = { }, SDL_SetVideoMode: function(width, height, depth, flags) { - ['mousedown', 'mouseup', 'mousemove', 'DOMMouseScroll', 'mouseout'].forEach(function(event) { + ['mousedown', 'mouseup', 'mousemove', 'DOMMouseScroll', 'mousewheel', 'mouseout'].forEach(function(event) { Module['canvas'].addEventListener(event, SDL.receiveEvent, true); }); Module['canvas'].width = width; |