Skip to content

Commit 48456b5

Browse files
committed
refactor mouse movement handling from SDL into Browser
1 parent 1ad859b commit 48456b5

File tree

2 files changed

+50
-43
lines changed

2 files changed

+50
-43
lines changed

src/library_browser.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,47 @@ mergeInto(LibraryManager.library, {
374374
0;
375375
},
376376

377+
mouseX: 0,
378+
mouseY: 0,
379+
mouseMovementX: 0,
380+
mouseMovementY: 0,
381+
382+
calculateMouseMove: function(event) {
383+
if (Browser.pointerLock) {
384+
// When the pointer is locked, calculate the coordinates
385+
// based on the movement of the mouse.
386+
// Workaround for Firefox bug 764498
387+
if (event.type != 'mousemove' &&
388+
('mozMovementX' in event)) {
389+
Browser.mouseMovementX = Browser.mouseMovementY = 0;
390+
} else {
391+
Browser.mouseMovementX = Browser.getMovementX(event);
392+
Browser.mouseMovementY = Browser.getMovementY(event);
393+
}
394+
Browser.mouseX = SDL.mouseX + Browser.mouseMovementX;
395+
Browser.mouseY = SDL.mouseY + Browser.mouseMovementY;
396+
} else {
397+
// Otherwise, calculate the movement based on the changes
398+
// in the coordinates.
399+
var rect = Module["canvas"].getBoundingClientRect();
400+
var x = event.pageX - (window.scrollX + rect.left);
401+
var y = event.pageY - (window.scrollY + rect.top);
402+
403+
// the canvas might be CSS-scaled compared to its backbuffer;
404+
// SDL-using content will want mouse coordinates in terms
405+
// of backbuffer units.
406+
var cw = Module["canvas"].width;
407+
var ch = Module["canvas"].height;
408+
x = x * (cw / rect.width);
409+
y = y * (ch / rect.height);
410+
411+
Browser.mouseMovementX = x - Browser.mouseX;
412+
Browser.mouseMovementY = y - Browser.mouseY;
413+
Browser.mouseX = x;
414+
Browser.mouseY = y;
415+
}
416+
},
417+
377418
xhrLoad: function(url, onload, onerror) {
378419
var xhr = new XMLHttpRequest();
379420
xhr.open('GET', url, true);

src/library_sdl.js

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ var LibrarySDL = {
4646
textInput: false,
4747

4848
startTime: null,
49-
mouseX: 0,
50-
mouseY: 0,
5149
buttonState: 0,
5250
DOMButtons: [0, 0, 0],
5351

@@ -515,54 +513,22 @@ var LibrarySDL = {
515513
}
516514
// fall through
517515
case 'mousemove': {
518-
if (Browser.pointerLock) {
519-
// When the pointer is locked, calculate the coordinates
520-
// based on the movement of the mouse.
521-
// Workaround for Firefox bug 764498
522-
if (event.type != 'mousemove' &&
523-
('mozMovementX' in event)) {
524-
var movementX = 0, movementY = 0;
525-
} else {
526-
var movementX = Browser.getMovementX(event);
527-
var movementY = Browser.getMovementY(event);
528-
}
529-
var x = SDL.mouseX + movementX;
530-
var y = SDL.mouseY + movementY;
531-
} else {
532-
// Otherwise, calculate the movement based on the changes
533-
// in the coordinates.
534-
var rect = Module["canvas"].getBoundingClientRect();
535-
var x = event.pageX - (window.scrollX + rect.left);
536-
var y = event.pageY - (window.scrollY + rect.top);
537-
538-
// the canvas might be CSS-scaled compared to its backbuffer;
539-
// SDL-using content will want mouse coordinates in terms
540-
// of backbuffer units.
541-
var cw = Module["canvas"].width;
542-
var ch = Module["canvas"].height;
543-
x = x * (cw / rect.width);
544-
y = y * (ch / rect.height);
545-
546-
var movementX = x - SDL.mouseX;
547-
var movementY = y - SDL.mouseY;
548-
}
516+
Browser.calculateMouseMove(event);
549517
if (event.type != 'mousemove') {
550518
var down = event.type === 'mousedown';
551519
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
552520
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.button', 'event.button+1', 'i8') }}}; // DOM buttons are 0-2, SDL 1-3
553521
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.state', 'down ? 1 : 0', 'i8') }}};
554-
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.x', 'x', 'i32') }}};
555-
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.y', 'y', 'i32') }}};
522+
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.x', 'Browser.mouseX', 'i32') }}};
523+
{{{ makeSetValue('ptr', 'SDL.structs.MouseButtonEvent.y', 'Browser.mouseY', 'i32') }}};
556524
} else {
557525
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}};
558526
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.state', 'SDL.buttonState', 'i8') }}};
559-
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.x', 'x', 'i32') }}};
560-
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.y', 'y', 'i32') }}};
561-
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.xrel', 'movementX', 'i32') }}};
562-
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.yrel', 'movementY', 'i32') }}};
527+
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.x', 'Browser.mouseX', 'i32') }}};
528+
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.y', 'Browser.mouseY', 'i32') }}};
529+
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.xrel', 'Browser.mouseMovementX', 'i32') }}};
530+
{{{ makeSetValue('ptr', 'SDL.structs.MouseMotionEvent.yrel', 'Browser.mouseMovementY', 'i32') }}};
563531
}
564-
SDL.mouseX = x;
565-
SDL.mouseY = y;
566532
break;
567533
}
568534
case 'unload': {
@@ -924,8 +890,8 @@ var LibrarySDL = {
924890
},
925891

926892
SDL_GetMouseState: function(x, y) {
927-
if (x) {{{ makeSetValue('x', '0', 'SDL.mouseX', 'i32') }}};
928-
if (y) {{{ makeSetValue('y', '0', 'SDL.mouseY', 'i32') }}};
893+
if (x) {{{ makeSetValue('x', '0', 'Browser.mouseX', 'i32') }}};
894+
if (y) {{{ makeSetValue('y', '0', 'Browser.mouseY', 'i32') }}};
929895
return SDL.buttonState;
930896
},
931897

0 commit comments

Comments
 (0)