-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInputEventHandler.js
More file actions
79 lines (49 loc) · 1.46 KB
/
Copy pathInputEventHandler.js
File metadata and controls
79 lines (49 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var InputEvent = function() {
this.x = -1;
this.y = -1;
this.shift = false;
this.control = false;
this.keyCode = 0;
this.button = -1;
};
InputEvent.Button = {
LEFT: 0,
MIDDLE: 1,
RIGHT: 2
};
var InputEventHandler = function( document ) {
EventHandler.call( this );
this.registerHandlers( document );
};
InputEventHandler.prototype = Object.create( EventHandler.prototype );
InputEventHandler.prototype._fireEvent = function(event, value) {
if(this._eventListeners.has(event)) {
var listeners = this._eventListeners.get(event);
for(var i = 0; i < listeners.length; i++) {
if(typeof listeners[i] == "function") {
if( listeners[i](value) === true ) {
break;
}
}
}
}
};
InputEventHandler.prototype.translateMouseEvent = function( mouseEvent ) {
var e = new InputEvent;
e.x = mouseEvent.clientX;
e.y = mouseEvent.clientY;
e.button = mouseEvent.button;
e.shift = mouseEvent.shiftKey;
return e;
};
InputEventHandler.prototype.registerHandlers = function( document ) {
document.addEventListener("mousedown", (function( e ) {
this._fireEvent("mousedown", this.translateMouseEvent( e ) );
}).bind( this ));
document.addEventListener("mouseup", (function( e ) {
this._fireEvent("mouseup", this.translateMouseEvent( e ) );
}).bind( this ));
document.addEventListener("mousemove", (function( e ) {
this._fireEvent("mousemove", this.translateMouseEvent( e ) );
}).bind( this ));
};