This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mouse.js
200 lines (181 loc) · 5.15 KB
/
Mouse.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var EventDispatcher = require('./EventDispatcher');
var MouseEvent = require('./MouseEvent');
/**
* EventDispatcher for mouse events
* @class
*
* @example
* var Window = require('pex-sys').Window;
* var MouseEvent = require('pex-sys').MouseEvent;
*
* Window.create({
* init: function() {
* var mouse = this.getMouse()
* mouse.addEventListener(MouseEvent.MOUSE_DOWN, function(e){ });
* mouse.addEventListener(MouseEvent.MOUSE_UP, function(e){ });
* mouse.addEventListener(MouseEvent.MOUSE_MOVE, function(e){ });
* mouse.addEventListener(MouseEvent.MOUSE_DRAG, function(e){ });
* mouse.addEventListener(MouseEvent.MOUSE_SCROLL, function(e){ });
* }
* })
*/
function Mouse() {
EventDispatcher.call(this);
this._x = 0;
this._y = 0;
this._prevX = 0;
this._prevY = 0;
this._deltaX = 0;
this._deltaY = 0;
this._isDown = false;
}
Mouse.prototype = Object.create(EventDispatcher.prototype);
Mouse.prototype.constructor = Mouse;
/**
* @return {Number} mouse x position
*/
Mouse.prototype.getPosX = function() {
return this._x;
};
/**
* @return {Number} mouse y position
*/
Mouse.prototype.getPosY = function() {
return this._y;
};
/**
* @return {Array} mouse position as [x, y]
*/
Mouse.prototype.getPos = function(out){
if(out === undefined){
return [this._x,this._y];
}
out[0] = this._x;
out[1] = this._y;
return out;
};
/**
* @return {Number} previous mouse x position
*/
Mouse.prototype.getPrevPosX = function() {
return this._prevX;
};
/**
* @return {Number} previous mouse y position
*/
Mouse.prototype.getPrevPosY = function() {
return this._prevY;
};
/**
* @return {Array} previous mouse position as [x, y]
*/
Mouse.prototype.getPrevPos = function(out){
if(out === undefined){
return [this._prevX,this._prevY];
}
out[0] = this._prevX;
out[1] = this._prevY;
return out;
};
/**
* @return {Number} the difference between current and the last position on the x axis
*/
Mouse.prototype.getDeltaX = function() {
return this._deltaX;
};
/**
* @return {Number} the difference between current and the last position on the y axis
*/
Mouse.prototype.getDeltaY = function() {
return this._deltaY;
};
/**
* @return {Array} the difference between current and the last position as [x,y]
*/
Mouse.prototype.getDelta = function(out){
if(out === undefined){
return [this._deltaX,this._deltaY];
}
out[0] = this._deltaX;
out[1] = this._deltaY;
return out;
};
/**
* Fires MOUSE_DOWN event
* @protected
*
* @param {Object} e - event data
* @param {String} e.x - mouse x position
* @param {Number} e.y - mouse y position
* @param {Boolean} e.altKey - is alt key pressed?
* @param {Boolean} e.shiftKey - is shift key pressed?
* @param {Boolean} e.ctrlKey - is ctrl key pressed?
* @param {Boolean} e.metaKey - is meta (apple/win) key pressed?
*/
Mouse.prototype.handleMouseDown = function(e) {
this._isDown = true;
e.mouse = this;
this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, e));
}
/**
* Fires MOUSE_UP event
* @protected
*
* @param {Object} e - event data
* @param {String} e.x - mouse x position
* @param {Number} e.y - mouse y position
* @param {Boolean} e.altKey - is alt key pressed?
* @param {Boolean} e.shiftKey - is shift key pressed?
* @param {Boolean} e.ctrlKey - is ctrl key pressed?
* @param {Boolean} e.metaKey - is meta (apple/win) key pressed?
*/
Mouse.prototype.handleMouseUp = function(e) {
this._isDown = false;
e.mouse = this;
this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP, e));
}
/**
* Fires MOUSE_MOVE event
* @protected
*
* @param {Object} e - event data
* @param {String} e.x - mouse x position
* @param {Number} e.y - mouse y position
* @param {Boolean} e.altKey - is alt key pressed?
* @param {Boolean} e.shiftKey - is shift key pressed?
* @param {Boolean} e.ctrlKey - is ctrl key pressed?
* @param {Boolean} e.metaKey - is meta (apple/win) key pressed?
*/
Mouse.prototype.handleMouseMove = function(e) {
this._prevX = this._x;
this._prevY = this._y;
this._x = e.x;
this._y = e.y;
this._deltaX = this._x - this._prevX;
this._deltaY = this._y - this._prevY;
e.mouse = this;
//don't fire mouse move events while dragging
if (this._isDown) {
this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DRAG, e));
}
else {
this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE, e));
}
}
/**
* Fires MOUSE_SCROLL event
* @protected
*
* @param {Object} e - event data
* @param {String} e.dx - amount of scroll in x direction
* @param {Number} e.dy - amount of scroll in y direction
* @param {Boolean} e.altKey - is alt key pressed?
* @param {Boolean} e.shiftKey - is shift key pressed?
* @param {Boolean} e.ctrlKey - is ctrl key pressed?
* @param {Boolean} e.metaKey - is meta (apple/win) key pressed?
*/
Mouse.prototype.handleMouseScroll = function(e) {
e.mouse = this;
this.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_SCROLL, e));
}
module.exports = Mouse;