Cleanup: Input code
File: input.js Tests Added: False (already present partially -- see below) Changes: - Fixed JSHint Errors - Converted to normal non-Crockford constructors NOTE: while there are tests for the actual key-detecting functionality, the tests do not cover the actual Keyboard and Mouse objects themselves.
This commit is contained in:
parent
d21cd6c164
commit
d6e281baf6
457
include/input.js
457
include/input.js
|
@ -5,60 +5,60 @@
|
||||||
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
|
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*jslint browser: true, white: false, bitwise: false */
|
/*jslint browser: true, white: false */
|
||||||
/*global window, Util */
|
/*global window, Util */
|
||||||
|
|
||||||
|
var Keyboard, Mouse;
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
//
|
//
|
||||||
// Keyboard event handler
|
// Keyboard event handler
|
||||||
//
|
//
|
||||||
|
|
||||||
function Keyboard(defaults) {
|
Keyboard = function (defaults) {
|
||||||
"use strict";
|
this._keyDownList = []; // List of depressed keys
|
||||||
|
|
||||||
var that = {}, // Public API methods
|
|
||||||
conf = {}, // Configuration attributes
|
|
||||||
|
|
||||||
keyDownList = []; // List of depressed keys
|
|
||||||
// (even if they are happy)
|
// (even if they are happy)
|
||||||
|
|
||||||
// Configuration attributes
|
Util.set_defaults(this, defaults, {
|
||||||
Util.conf_defaults(conf, that, defaults, [
|
'target': document,
|
||||||
['target', 'wo', 'dom', document, 'DOM element that captures keyboard input'],
|
'focused': true
|
||||||
['focused', 'rw', 'bool', true, 'Capture and send key events'],
|
});
|
||||||
|
|
||||||
['onKeyPress', 'rw', 'func', null, 'Handler for key press/release']
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// Private functions
|
|
||||||
//
|
|
||||||
|
|
||||||
/////// setup
|
|
||||||
|
|
||||||
function onRfbEvent(evt) {
|
|
||||||
if (conf.onKeyPress) {
|
|
||||||
Util.Debug("onKeyPress " + (evt.type == 'keydown' ? "down" : "up")
|
|
||||||
+ ", keysym: " + evt.keysym.keysym + "(" + evt.keysym.keyname + ")");
|
|
||||||
conf.onKeyPress(evt.keysym.keysym, evt.type == 'keydown');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the keyboard handler
|
// create the keyboard handler
|
||||||
var k = KeyEventDecoder(kbdUtil.ModifierSync(),
|
this._handler = new KeyEventDecoder(kbdUtil.ModifierSync(),
|
||||||
VerifyCharModifier(
|
VerifyCharModifier( /* jshint newcap: false */
|
||||||
TrackKeyState(
|
TrackKeyState(
|
||||||
EscapeModifiers(onRfbEvent)
|
EscapeModifiers(this._handleRfbEvent.bind(this))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
); /* jshint newcap: true */
|
||||||
|
|
||||||
function onKeyDown(e) {
|
// keep these here so we can refer to them later
|
||||||
if (! conf.focused) {
|
this._eventHandlers = {
|
||||||
return true;
|
'keyup': this._handleKeyUp.bind(this),
|
||||||
|
'keydown': this._handleKeyDown.bind(this),
|
||||||
|
'keypress': this._handleKeyPress.bind(this),
|
||||||
|
'blur': this._allKeysUp.bind(this)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Keyboard.prototype = {
|
||||||
|
// private methods
|
||||||
|
|
||||||
|
_handleRfbEvent: function (e) {
|
||||||
|
if (this._onKeyPress) {
|
||||||
|
Util.Debug("onKeyPress " + (e.type == 'keydown' ? "down" : "up") +
|
||||||
|
", keysym: " + e.keysym.keysym + "(" + e.keysym.keyname + ")");
|
||||||
|
this._onKeyPress(e.keysym.keysym, e.type == 'keydown');
|
||||||
}
|
}
|
||||||
if (k.keydown(e)) {
|
},
|
||||||
|
|
||||||
|
_handleKeyDown: function (e) {
|
||||||
|
if (!this._focused) { return true; }
|
||||||
|
|
||||||
|
if (this._handler.keydown(e)) {
|
||||||
// Suppress bubbling/default actions
|
// Suppress bubbling/default actions
|
||||||
Util.stopEvent(e);
|
Util.stopEvent(e);
|
||||||
return false;
|
return false;
|
||||||
|
@ -67,12 +67,12 @@ function onKeyDown(e) {
|
||||||
// will have the character code translated
|
// will have the character code translated
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
function onKeyPress(e) {
|
|
||||||
if (! conf.focused) {
|
_handleKeyPress: function (e) {
|
||||||
return true;
|
if (!this._focused) { return true; }
|
||||||
}
|
|
||||||
if (k.keypress(e)) {
|
if (this._handler.keypress(e)) {
|
||||||
// Suppress bubbling/default actions
|
// Suppress bubbling/default actions
|
||||||
Util.stopEvent(e);
|
Util.stopEvent(e);
|
||||||
return false;
|
return false;
|
||||||
|
@ -81,13 +81,12 @@ function onKeyPress(e) {
|
||||||
// will have the character code translated
|
// will have the character code translated
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
function onKeyUp(e) {
|
_handleKeyUp: function (e) {
|
||||||
if (! conf.focused) {
|
if (!this._focused) { return true; }
|
||||||
return true;
|
|
||||||
}
|
if (this._handler.keyup(e)) {
|
||||||
if (k.keyup(e)) {
|
|
||||||
// Suppress bubbling/default actions
|
// Suppress bubbling/default actions
|
||||||
Util.stopEvent(e);
|
Util.stopEvent(e);
|
||||||
return false;
|
return false;
|
||||||
|
@ -96,153 +95,147 @@ function onKeyUp(e) {
|
||||||
// will have the character code translated
|
// will have the character code translated
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
function onOther(e) {
|
_allKeysUp: function () {
|
||||||
k.syncModifiers(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
function allKeysUp() {
|
|
||||||
Util.Debug(">> Keyboard.allKeysUp");
|
Util.Debug(">> Keyboard.allKeysUp");
|
||||||
|
this._handler.releaseAll();
|
||||||
k.releaseAll();
|
|
||||||
Util.Debug("<< Keyboard.allKeysUp");
|
Util.Debug("<< Keyboard.allKeysUp");
|
||||||
}
|
},
|
||||||
|
|
||||||
//
|
// Public methods
|
||||||
// Public API interface functions
|
|
||||||
//
|
|
||||||
|
|
||||||
that.grab = function() {
|
grab: function () {
|
||||||
//Util.Debug(">> Keyboard.grab");
|
//Util.Debug(">> Keyboard.grab");
|
||||||
var c = conf.target;
|
var c = this._target;
|
||||||
|
|
||||||
Util.addEvent(c, 'keydown', onKeyDown);
|
Util.addEvent(c, 'keydown', this._eventHandlers.keydown);
|
||||||
Util.addEvent(c, 'keyup', onKeyUp);
|
Util.addEvent(c, 'keyup', this._eventHandlers.keyup);
|
||||||
Util.addEvent(c, 'keypress', onKeyPress);
|
Util.addEvent(c, 'keypress', this._eventHandlers.keypress);
|
||||||
|
|
||||||
// Release (key up) if window loses focus
|
// Release (key up) if window loses focus
|
||||||
Util.addEvent(window, 'blur', allKeysUp);
|
Util.addEvent(window, 'blur', this._eventHandlers.blur);
|
||||||
|
|
||||||
//Util.Debug("<< Keyboard.grab");
|
//Util.Debug("<< Keyboard.grab");
|
||||||
};
|
},
|
||||||
|
|
||||||
that.ungrab = function() {
|
ungrab: function () {
|
||||||
//Util.Debug(">> Keyboard.ungrab");
|
//Util.Debug(">> Keyboard.ungrab");
|
||||||
var c = conf.target;
|
var c = this._target;
|
||||||
|
|
||||||
Util.removeEvent(c, 'keydown', onKeyDown);
|
Util.removeEvent(c, 'keydown', this._eventHandlers.keydown);
|
||||||
Util.removeEvent(c, 'keyup', onKeyUp);
|
Util.removeEvent(c, 'keyup', this._eventHandlers.keyup);
|
||||||
Util.removeEvent(c, 'keypress', onKeyPress);
|
Util.removeEvent(c, 'keypress', this._eventHandlers.keypress);
|
||||||
Util.removeEvent(window, 'blur', allKeysUp);
|
Util.removeEvent(window, 'blur', this._eventHandlers.blur);
|
||||||
|
|
||||||
// Release (key up) all keys that are in a down state
|
// Release (key up) all keys that are in a down state
|
||||||
allKeysUp();
|
this._allKeysUp();
|
||||||
|
|
||||||
//Util.Debug(">> Keyboard.ungrab");
|
//Util.Debug(">> Keyboard.ungrab");
|
||||||
|
},
|
||||||
|
|
||||||
|
sync: function (e) {
|
||||||
|
this._handler.syncModifiers(e);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
that.sync = function(e) {
|
Util.make_properties(Keyboard, [
|
||||||
k.syncModifiers(e);
|
['target', 'wo', 'dom'], // DOM element that captures keyboard input
|
||||||
}
|
['focused', 'rw', 'bool'], // Capture and send key events
|
||||||
|
|
||||||
return that; // Return the public API interface
|
|
||||||
|
|
||||||
} // End of Keyboard()
|
|
||||||
|
|
||||||
|
['onKeyPress', 'rw', 'func'] // Handler for key press/release
|
||||||
|
]);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Mouse event handler
|
// Mouse event handler
|
||||||
//
|
//
|
||||||
|
|
||||||
function Mouse(defaults) {
|
Mouse = function (defaults) {
|
||||||
"use strict";
|
this._mouseCaptured = false;
|
||||||
|
|
||||||
var that = {}, // Public API methods
|
this._doubleClickTimer = null;
|
||||||
conf = {}, // Configuration attributes
|
this._lastTouchPos = null;
|
||||||
mouseCaptured = false;
|
|
||||||
|
|
||||||
var doubleClickTimer = null,
|
|
||||||
lastTouchPos = null;
|
|
||||||
|
|
||||||
// Configuration attributes
|
// Configuration attributes
|
||||||
Util.conf_defaults(conf, that, defaults, [
|
Util.set_defaults(this, defaults, {
|
||||||
['target', 'ro', 'dom', document, 'DOM element that captures mouse input'],
|
'target': document,
|
||||||
['notify', 'ro', 'func', null, 'Function to call to notify whenever a mouse event is received'],
|
'focused': true,
|
||||||
['focused', 'rw', 'bool', true, 'Capture and send mouse clicks/movement'],
|
'scale': 1.0,
|
||||||
['scale', 'rw', 'float', 1.0, 'Viewport scale factor 0.0 - 1.0'],
|
'touchButton': 1
|
||||||
|
});
|
||||||
|
|
||||||
['onMouseButton', 'rw', 'func', null, 'Handler for mouse button click/release'],
|
this._eventHandlers = {
|
||||||
['onMouseMove', 'rw', 'func', null, 'Handler for mouse movement'],
|
'mousedown': this._handleMouseDown.bind(this),
|
||||||
['touchButton', 'rw', 'int', 1, 'Button mask (1, 2, 4) for touch devices (0 means ignore clicks)']
|
'mouseup': this._handleMouseUp.bind(this),
|
||||||
]);
|
'mousemove': this._handleMouseMove.bind(this),
|
||||||
|
'mousewheel': this._handleMouseWheel.bind(this),
|
||||||
|
'mousedisable': this._handleMouseDisable.bind(this)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
function captureMouse() {
|
Mouse.prototype = {
|
||||||
|
// private methods
|
||||||
|
_captureMouse: function () {
|
||||||
// capturing the mouse ensures we get the mouseup event
|
// capturing the mouse ensures we get the mouseup event
|
||||||
if (conf.target.setCapture) {
|
if (this._target.setCapture) {
|
||||||
conf.target.setCapture();
|
this._target.setCapture();
|
||||||
}
|
}
|
||||||
|
|
||||||
// some browsers give us mouseup events regardless,
|
// some browsers give us mouseup events regardless,
|
||||||
// so if we never captured the mouse, we can disregard the event
|
// so if we never captured the mouse, we can disregard the event
|
||||||
mouseCaptured = true;
|
this._mouseCaptured = true;
|
||||||
|
},
|
||||||
|
|
||||||
|
_releaseMouse: function () {
|
||||||
|
if (this._target.releaseCapture) {
|
||||||
|
this._target.releaseCapture();
|
||||||
|
}
|
||||||
|
this._mouseCaptured = false;
|
||||||
|
},
|
||||||
|
|
||||||
|
_resetDoubleClickTimer: function () {
|
||||||
|
this._doubleClickTimer = null;
|
||||||
|
},
|
||||||
|
|
||||||
|
_handleMouseButton: function (e, down) {
|
||||||
|
if (!this._focused) { return true; }
|
||||||
|
|
||||||
|
if (this._notify) {
|
||||||
|
this._notify(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
function releaseMouse() {
|
var evt = (e ? e : window.event);
|
||||||
if (conf.target.releaseCapture) {
|
var pos = Util.getEventPosition(e, this._target, this._scale);
|
||||||
conf.target.releaseCapture();
|
|
||||||
}
|
|
||||||
mouseCaptured = false;
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// Private functions
|
|
||||||
//
|
|
||||||
|
|
||||||
function resetDoubleClickTimer() {
|
|
||||||
doubleClickTimer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function onMouseButton(e, down) {
|
|
||||||
var evt, pos, bmask;
|
|
||||||
if (! conf.focused) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conf.notify) {
|
|
||||||
conf.notify(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
evt = (e ? e : window.event);
|
|
||||||
pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
||||||
|
|
||||||
|
var bmask;
|
||||||
if (e.touches || e.changedTouches) {
|
if (e.touches || e.changedTouches) {
|
||||||
// Touch device
|
// Touch device
|
||||||
|
|
||||||
// When two touches occur within 500 ms of each other and are
|
// When two touches occur within 500 ms of each other and are
|
||||||
// closer than 20 pixels together a double click is triggered.
|
// closer than 20 pixels together a double click is triggered.
|
||||||
if (down == 1) {
|
if (down == 1) {
|
||||||
if (doubleClickTimer == null) {
|
if (this._doubleClickTimer === null) {
|
||||||
lastTouchPos = pos;
|
this._lastTouchPos = pos;
|
||||||
} else {
|
} else {
|
||||||
clearTimeout(doubleClickTimer);
|
clearTimeout(this._doubleClickTimer);
|
||||||
|
|
||||||
// When the distance between the two touches is small enough
|
// When the distance between the two touches is small enough
|
||||||
// force the position of the latter touch to the position of
|
// force the position of the latter touch to the position of
|
||||||
// the first.
|
// the first.
|
||||||
|
|
||||||
var xs = lastTouchPos.x - pos.x;
|
var xs = this._lastTouchPos.x - pos.x;
|
||||||
var ys = lastTouchPos.y - pos.y;
|
var ys = this._lastTouchPos.y - pos.y;
|
||||||
var d = Math.sqrt((xs * xs) + (ys * ys));
|
var d = Math.sqrt((xs * xs) + (ys * ys));
|
||||||
|
|
||||||
// The goal is to trigger on a certain physical width, the
|
// The goal is to trigger on a certain physical width, the
|
||||||
// devicePixelRatio brings us a bit closer but is not optimal.
|
// devicePixelRatio brings us a bit closer but is not optimal.
|
||||||
if (d < 20 * window.devicePixelRatio) {
|
if (d < 20 * window.devicePixelRatio) {
|
||||||
pos = lastTouchPos;
|
pos = this._lastTouchPos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doubleClickTimer = setTimeout(resetDoubleClickTimer, 500);
|
this._doubleClickTimer = setTimeout(this._resetDoubleClickTimer.bind(this), 500);
|
||||||
}
|
}
|
||||||
bmask = conf.touchButton;
|
bmask = this._touchButton;
|
||||||
// If bmask is set
|
// If bmask is set
|
||||||
} else if (evt.which) {
|
} else if (evt.which) {
|
||||||
/* everything except IE */
|
/* everything except IE */
|
||||||
|
@ -253,149 +246,143 @@ function onMouseButton(e, down) {
|
||||||
(evt.button & 0x2) * 2 + // Right
|
(evt.button & 0x2) * 2 + // Right
|
||||||
(evt.button & 0x4) / 2; // Middle
|
(evt.button & 0x4) / 2; // Middle
|
||||||
}
|
}
|
||||||
//Util.Debug("mouse " + pos.x + "," + pos.y + " down: " + down +
|
|
||||||
// " bmask: " + bmask + "(evt.button: " + evt.button + ")");
|
if (this._onMouseButton) {
|
||||||
if (conf.onMouseButton) {
|
|
||||||
Util.Debug("onMouseButton " + (down ? "down" : "up") +
|
Util.Debug("onMouseButton " + (down ? "down" : "up") +
|
||||||
", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask);
|
", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask);
|
||||||
conf.onMouseButton(pos.x, pos.y, down, bmask);
|
this._onMouseButton(pos.x, pos.y, down, bmask);
|
||||||
}
|
}
|
||||||
Util.stopEvent(e);
|
Util.stopEvent(e);
|
||||||
return false;
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
_handleMouseDown: function (e) {
|
||||||
|
this._captureMouse();
|
||||||
|
this._handleMouseButton(e, 1);
|
||||||
|
},
|
||||||
|
|
||||||
|
_handleMouseUp: function (e) {
|
||||||
|
if (!this._mouseCaptured) { return; }
|
||||||
|
|
||||||
|
this._handleMouseButton(e, 0);
|
||||||
|
this._releaseMouse();
|
||||||
|
},
|
||||||
|
|
||||||
|
_handleMouseWheel: function (e) {
|
||||||
|
if (!this._focused) { return true; }
|
||||||
|
|
||||||
|
if (this._notify) {
|
||||||
|
this._notify(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMouseDown(e) {
|
var evt = (e ? e : window.event);
|
||||||
captureMouse();
|
var pos = Util.getEventPosition(e, this._target, this._scale);
|
||||||
onMouseButton(e, 1);
|
var wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
|
||||||
}
|
var bmask;
|
||||||
|
|
||||||
function onMouseUp(e) {
|
|
||||||
if (!mouseCaptured) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onMouseButton(e, 0);
|
|
||||||
releaseMouse();
|
|
||||||
}
|
|
||||||
|
|
||||||
function onMouseWheel(e) {
|
|
||||||
var evt, pos, bmask, wheelData;
|
|
||||||
if (! conf.focused) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (conf.notify) {
|
|
||||||
conf.notify(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
evt = (e ? e : window.event);
|
|
||||||
pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
||||||
wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
|
|
||||||
if (wheelData > 0) {
|
if (wheelData > 0) {
|
||||||
bmask = 1 << 3;
|
bmask = 1 << 3;
|
||||||
} else {
|
} else {
|
||||||
bmask = 1 << 4;
|
bmask = 1 << 4;
|
||||||
}
|
}
|
||||||
//Util.Debug('mouse scroll by ' + wheelData + ':' + pos.x + "," + pos.y);
|
|
||||||
if (conf.onMouseButton) {
|
if (this._onMouseButton) {
|
||||||
conf.onMouseButton(pos.x, pos.y, 1, bmask);
|
this._onMouseButton(pos.x, pos.y, 1, bmask);
|
||||||
conf.onMouseButton(pos.x, pos.y, 0, bmask);
|
this._onMouseButton(pos.x, pos.y, 0, bmask);
|
||||||
}
|
}
|
||||||
Util.stopEvent(e);
|
Util.stopEvent(e);
|
||||||
return false;
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
_handleMouseMove: function (e) {
|
||||||
|
if (! this._focused) { return true; }
|
||||||
|
|
||||||
|
if (this._notify) {
|
||||||
|
this._notify(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMouseMove(e) {
|
var evt = (e ? e : window.event);
|
||||||
var evt, pos;
|
var pos = Util.getEventPosition(e, this._target, this._scale);
|
||||||
if (! conf.focused) {
|
if (this._onMouseMove) {
|
||||||
return true;
|
this._onMouseMove(pos.x, pos.y);
|
||||||
}
|
|
||||||
if (conf.notify) {
|
|
||||||
conf.notify(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
evt = (e ? e : window.event);
|
|
||||||
pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
||||||
//Util.Debug('mouse ' + evt.which + '/' + evt.button + ' up:' + pos.x + "," + pos.y);
|
|
||||||
if (conf.onMouseMove) {
|
|
||||||
conf.onMouseMove(pos.x, pos.y);
|
|
||||||
}
|
}
|
||||||
Util.stopEvent(e);
|
Util.stopEvent(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
|
|
||||||
|
_handleMouseDisable: function (e) {
|
||||||
|
if (!this._focused) { return true; }
|
||||||
|
|
||||||
|
var evt = (e ? e : window.event);
|
||||||
|
var pos = Util.getEventPosition(e, this._target, this._scale);
|
||||||
|
|
||||||
function onMouseDisable(e) {
|
|
||||||
var evt, pos;
|
|
||||||
if (! conf.focused) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
evt = (e ? e : window.event);
|
|
||||||
pos = Util.getEventPosition(e, conf.target, conf.scale);
|
|
||||||
/* Stop propagation if inside canvas area */
|
/* Stop propagation if inside canvas area */
|
||||||
if ((pos.realx >= 0) && (pos.realy >= 0) &&
|
if ((pos.realx >= 0) && (pos.realy >= 0) &&
|
||||||
(pos.realx < conf.target.offsetWidth) &&
|
(pos.realx < this._target.offsetWidth) &&
|
||||||
(pos.realy < conf.target.offsetHeight)) {
|
(pos.realy < this._target.offsetHeight)) {
|
||||||
//Util.Debug("mouse event disabled");
|
//Util.Debug("mouse event disabled");
|
||||||
Util.stopEvent(e);
|
Util.stopEvent(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//Util.Debug("mouse event not disabled");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
},
|
||||||
|
|
||||||
//
|
|
||||||
// Public API interface functions
|
|
||||||
//
|
|
||||||
|
|
||||||
that.grab = function() {
|
// Public methods
|
||||||
//Util.Debug(">> Mouse.grab");
|
grab: function () {
|
||||||
var c = conf.target;
|
var c = this._target;
|
||||||
|
|
||||||
if ('ontouchstart' in document.documentElement) {
|
if ('ontouchstart' in document.documentElement) {
|
||||||
Util.addEvent(c, 'touchstart', onMouseDown);
|
Util.addEvent(c, 'touchstart', this._eventHandlers.mousedown);
|
||||||
Util.addEvent(window, 'touchend', onMouseUp);
|
Util.addEvent(window, 'touchend', this._eventHandlers.mouseup);
|
||||||
Util.addEvent(c, 'touchend', onMouseUp);
|
Util.addEvent(c, 'touchend', this._eventHandlers.mouseup);
|
||||||
Util.addEvent(c, 'touchmove', onMouseMove);
|
Util.addEvent(c, 'touchmove', this._eventHandlers.mousemove);
|
||||||
} else {
|
} else {
|
||||||
Util.addEvent(c, 'mousedown', onMouseDown);
|
Util.addEvent(c, 'mousedown', this._eventHandlers.mousedown);
|
||||||
Util.addEvent(window, 'mouseup', onMouseUp);
|
Util.addEvent(window, 'mouseup', this._eventHandlers.mouseup);
|
||||||
Util.addEvent(c, 'mouseup', onMouseUp);
|
Util.addEvent(c, 'mouseup', this._eventHandlers.mouseup);
|
||||||
Util.addEvent(c, 'mousemove', onMouseMove);
|
Util.addEvent(c, 'mousemove', this._eventHandlers.mousemove);
|
||||||
Util.addEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
|
Util.addEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
|
||||||
onMouseWheel);
|
this._eventHandlers.mousewheel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Work around right and middle click browser behaviors */
|
/* Work around right and middle click browser behaviors */
|
||||||
Util.addEvent(document, 'click', onMouseDisable);
|
Util.addEvent(document, 'click', this._eventHandlers.mousedisable);
|
||||||
Util.addEvent(document.body, 'contextmenu', onMouseDisable);
|
Util.addEvent(document.body, 'contextmenu', this._eventHandlers.mousedisable);
|
||||||
|
},
|
||||||
|
|
||||||
//Util.Debug("<< Mouse.grab");
|
ungrab: function () {
|
||||||
};
|
var c = this._target;
|
||||||
|
|
||||||
that.ungrab = function() {
|
|
||||||
//Util.Debug(">> Mouse.ungrab");
|
|
||||||
var c = conf.target;
|
|
||||||
|
|
||||||
if ('ontouchstart' in document.documentElement) {
|
if ('ontouchstart' in document.documentElement) {
|
||||||
Util.removeEvent(c, 'touchstart', onMouseDown);
|
Util.removeEvent(c, 'touchstart', this._eventHandlers.mousedown);
|
||||||
Util.removeEvent(window, 'touchend', onMouseUp);
|
Util.removeEvent(window, 'touchend', this._eventHandlers.mouseup);
|
||||||
Util.removeEvent(c, 'touchend', onMouseUp);
|
Util.removeEvent(c, 'touchend', this._eventHandlers.mouseup);
|
||||||
Util.removeEvent(c, 'touchmove', onMouseMove);
|
Util.removeEvent(c, 'touchmove', this._eventHandlers.mousemove);
|
||||||
} else {
|
} else {
|
||||||
Util.removeEvent(c, 'mousedown', onMouseDown);
|
Util.removeEvent(c, 'mousedown', this._eventHandlers.mousedown);
|
||||||
Util.removeEvent(window, 'mouseup', onMouseUp);
|
Util.removeEvent(window, 'mouseup', this._eventHandlers.mouseup);
|
||||||
Util.removeEvent(c, 'mouseup', onMouseUp);
|
Util.removeEvent(c, 'mouseup', this._eventHandlers.mouseup);
|
||||||
Util.removeEvent(c, 'mousemove', onMouseMove);
|
Util.removeEvent(c, 'mousemove', this._eventHandlers.mousemove);
|
||||||
Util.removeEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
|
Util.removeEvent(c, (Util.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel',
|
||||||
onMouseWheel);
|
this._eventHandlers.mousewheel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Work around right and middle click browser behaviors */
|
/* Work around right and middle click browser behaviors */
|
||||||
Util.removeEvent(document, 'click', onMouseDisable);
|
Util.removeEvent(document, 'click', this._eventHandlers.mousedisable);
|
||||||
Util.removeEvent(document.body, 'contextmenu', onMouseDisable);
|
Util.removeEvent(document.body, 'contextmenu', this._eventHandlers.mousedisable);
|
||||||
|
|
||||||
//Util.Debug(">> Mouse.ungrab");
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return that; // Return the public API interface
|
Util.make_properties(Mouse, [
|
||||||
|
['target', 'ro', 'dom'], // DOM element that captures mouse input
|
||||||
|
['notify', 'ro', 'func'], // Function to call to notify whenever a mouse event is received
|
||||||
|
['focused', 'rw', 'bool'], // Capture and send mouse clicks/movement
|
||||||
|
['scale', 'rw', 'float'], // Viewport scale factor 0.0 - 1.0
|
||||||
|
|
||||||
} // End of Mouse()
|
['onMouseButton', 'rw', 'func'], // Handler for mouse button click/release
|
||||||
|
['onMouseMove', 'rw', 'func'], // Handler for mouse movement
|
||||||
|
['touchButton', 'rw', 'int'] // Button mask (1, 2, 4) for touch devices (0 means ignore clicks)
|
||||||
|
]);
|
||||||
|
})();
|
||||||
|
|
Loading…
Reference in New Issue