瀏覽代碼

Properly preventing default mouse events (right-click).

dev-tmpl
Bryan Miller 6 年之前
父節點
當前提交
a6af0f9dd8
共有 1 個檔案被更改,包括 61 行新增26 行删除
  1. +61
    -26
      app/js/ui/Input.js

+ 61
- 26
app/js/ui/Input.js 查看文件

this.__mouseLastAction = ""; this.__mouseLastAction = "";
this.__mouseButtons = []; this.__mouseButtons = [];


this.__keyboardEnabled = false;
this.__mouseEnabled = false;

this.enableKeyboardInput = (function(){ this.enableKeyboardInput = (function(){
var handle_keydown = (function(e){ var handle_keydown = (function(e){
if (AddToKeymap(e.keyCode, "keydown")){ if (AddToKeymap(e.keyCode, "keydown")){


return (function(enable){ return (function(enable){
enable = (enable !== false); enable = (enable !== false);
// NOTE: There shouldn't be any harm if the user repeatedly enables or disables keyboard.
if (enable){ if (enable){
this.__keyboardEnabled = true;
window.addEventListener("keydown", handle_keydown, false); window.addEventListener("keydown", handle_keydown, false);
//window.addEventListener("keypress", handle_keypress, false);
window.addEventListener("keyup", handle_keyup, false); window.addEventListener("keyup", handle_keyup, false);
} else { } else {
this.__keyboardEnabled = false;
window.removeEventListener("keydown", handle_keydown); window.removeEventListener("keydown", handle_keydown);
window.removeEventListener("keyup", handle_keyup); window.removeEventListener("keyup", handle_keyup);
} }
return diff; return diff;
}).bind(this); }).bind(this);


var handle_mousemove = (function(e){
if (this.__preventDefaults)
e.preventDefault();
var handle_mousemove = (function(e){
var pos = mousePosition(e); var pos = mousePosition(e);
if (pos.inbounds){ if (pos.inbounds){
if (this.__preventDefaults){
e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
e.cancelBubble = true;
}
this.__mousePosition = pos; this.__mousePosition = pos;
this.__mouseLastAction = "mousemove"; this.__mouseLastAction = "mousemove";
this.__emitter.emit("mousemove", { this.__emitter.emit("mousemove", {
action: "mousemove" action: "mousemove"
}); });
} }
return false;
}).bind(this); }).bind(this);


var handle_mousedown = (function(e){ var handle_mousedown = (function(e){
var button = buttonID(e); var button = buttonID(e);
var pos = mousePosition(e); var pos = mousePosition(e);
if (pos.inbounds){ if (pos.inbounds){
if (this.__preventDefaults)
if (this.__preventDefaults){
e.preventDefault(); e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
e.cancelBubble = true;
}
if (addMouseButton(button)){ if (addMouseButton(button)){
this.__mousePosition = pos; this.__mousePosition = pos;
this.__mouseLastButton = button; this.__mouseLastButton = button;
}); });
} }
} }
return false;
}).bind(this); }).bind(this);


var handle_mouseup = (function(e){
if (this.__preventDefaults)
e.preventDefault();
var handle_mouseup = (function(e){
var button = buttonID(e); var button = buttonID(e);
var pos = mousePosition(e); var pos = mousePosition(e);
// NOTE: I still want to check for button removal, even before testing if an event should // NOTE: I still want to check for button removal, even before testing if an event should
// fire, so that I don't have any phantom buttons listed as "pressed" in the mouseButtons list. // fire, so that I don't have any phantom buttons listed as "pressed" in the mouseButtons list.
var diff = removeMouseButton(button); var diff = removeMouseButton(button);
if (pos.inbounds && diff >= 0){
this.__mousePosition = pos;
this.__mouseLastButton = button;
this.__mouseLastAction = "mouseup";
var data = {
source: this,
lastX: pos.lastX,
lastY: pos.lastY,
x: pos.x,
y: pos.y,
button: button,
action: "mouseup"
if (pos.inbounds){
if (this.__preventDefaults){
e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
e.cancelBubble = true;
} }
this.__emitter.emit("mouseup", data);
if (diff <= MOUSECLICK_DELAY && this.__mouseButtons.length <= 0){
this.__emitter.emit("mouseclick", data);
if (diff >= 0){
this.__mousePosition = pos;
this.__mouseLastButton = button;
this.__mouseLastAction = "mouseup";
var data = {
source: this,
lastX: pos.lastX,
lastY: pos.lastY,
x: pos.x,
y: pos.y,
button: button,
action: "mouseup"
}
this.__emitter.emit("mouseup", data);
if (diff <= MOUSECLICK_DELAY && this.__mouseButtons.length <= 0){
this.__emitter.emit("mouseclick", data);
}
} }
} }
return false;
}).bind(this); }).bind(this);


var handle_mousewheel = (function(e){ var handle_mousewheel = (function(e){


// This event is purely for preventing Default behaviors on mouse events we're not using. // This event is purely for preventing Default behaviors on mouse events we're not using.
var handle_mouseprevdef = (function(e){ var handle_mouseprevdef = (function(e){
var pos = mousePosition(e);
if (this.__preventDefaults && pos.inbounds)
e.preventDefault();
var pos = mousePosition(e);;
if (this.__preventDefaults && (pos === null || pos.inbounds)){
e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
e.cancelBubble = true;
}
return false;
}).bind(this); }).bind(this);


return (function(enable){ return (function(enable){
enable = (enable !== false); enable = (enable !== false);
// NOTE: There shouldn't be any harm if the user repeatedly enables or disables mouse.
if (enable){ if (enable){
this.__mouseEnabled = true;
window.addEventListener("mousemove", handle_mousemove); window.addEventListener("mousemove", handle_mousemove);
window.addEventListener("mousedown", handle_mousedown); window.addEventListener("mousedown", handle_mousedown);
window.addEventListener("mouseup", handle_mouseup); window.addEventListener("mouseup", handle_mouseup);
window.addEventListener("mousewheel", handle_mousewheel); window.addEventListener("mousewheel", handle_mousewheel);
window.addEventListener("click", handle_mouseprevdef); window.addEventListener("click", handle_mouseprevdef);
window.addEventListener("dblclick", handle_mouseprevdef); window.addEventListener("dblclick", handle_mouseprevdef);
window.addEventListener("contextmenu", handle_mouseprevdef);
} else { } else {
this.__mouseEnabled = false;
window.removeEventListener("mousemove", handle_mousemove); window.removeEventListener("mousemove", handle_mousemove);
window.removeEventListener("mousedown", handle_mousedown); window.removeEventListener("mousedown", handle_mousedown);
window.removeEventListener("mouseup", handle_mouseup); window.removeEventListener("mouseup", handle_mouseup);
window.removeEventListener("mousewheel", handle_mousewheel); window.removeEventListener("mousewheel", handle_mousewheel);
window.removeEventListener("click", handle_mouseprevdef); window.removeEventListener("click", handle_mouseprevdef);
window.removeEventListener("dblclick", handle_mouseprevdef); window.removeEventListener("dblclick", handle_mouseprevdef);
window.removeEventListener("contextmenu", handle_mouseprevdef);
} }
}).bind(this); }).bind(this);
}).apply(this); }).apply(this);
this.enableMouseInput(); this.enableMouseInput();
} }


get mouseInputEnabled(){return this.__mouseEnabled;}
get keyboardInputEnabled(){return this.__keyboardEnabled;}

get lastkey(){ get lastkey(){
if (KEYMAP["lastcode"] !== null){ if (KEYMAP["lastcode"] !== null){
if (KEYMAP["lastcode"] in Object.keys(KEYBYCODE)){ if (KEYMAP["lastcode"] in Object.keys(KEYBYCODE)){

Loading…
取消
儲存