-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmouse.js.html
More file actions
300 lines (268 loc) · 11 KB
/
mouse.js.html
File metadata and controls
300 lines (268 loc) · 11 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: mouse.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: mouse.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
var platform = require("./platform");
// prevent springy scrolling on ios
document.ontouchmove = function(e) {
e.preventDefault();
};
// prevent right-click on desktop
window.oncontextmenu = function() {
return false;
};
var relMouseCoords = function(canvas, event) {
var x = event.pageX - canvas.offsetLeft + document.body.scrollLeft;
var y = event.pageY - canvas.offsetTop + document.body.scrollTop;
// scale based on ratio of canvas internal dimentions to css dimensions
if (canvas.style.width.length) {
x *= canvas.width / canvas.style.width.substring(0, canvas.style.width.indexOf("p"));
}
if (canvas.style.height.length) {
y *= canvas.height / canvas.style.height.substring(0, canvas.style.height.indexOf("p"));
}
return {x:x, y:y};
};
function relMouseCoordsEjecta(canvas, event) {
var ratioX = canvas.width / window.innerWidth;
var ratioY = canvas.height / window.innerHeight;
var x = event.pageX * ratioX;
var y = event.pageY * ratioY;
return {x:x, y:y};
}
if (platform.isEjecta()) {
relMouseCoords = relMouseCoordsEjecta;
}
/**
* Mouse and touch input handling. An instance of Mouse is available as {@link Splat.Game#mouse}.
*
* The first touch will emulates a mouse press with button 0.
* This means you can use the mouse ({@link Mouse#isPressed}/{@link Mouse#consumePressed}) APIs and your game will work on touch screens (as long as you only need the left button.
*
* A mouse press will emulate a touch if the device does not support touch.
* This means you can use {@link Mouse#touches}, and your game will still work on a PC with a mouse.
* Also, if you call {@link Mouse#consumePressed} with button 0, it will add a `consumed:true` field to all current touches. This will help you prevent processing a touch multiple times.
*
* @constructor
* @param {external:canvas} canvas The canvas to listen for events on.
*/
function Mouse(canvas) {
/**
* The x coordinate of the cursor relative to the left side of the canvas.
* @member {number}
*/
this.x = 0;
/**
* The y coordinate of the cursor relative to the top of the canvas.
* @member {number}
*/
this.y = 0;
/**
* The current button states.
* @member {Array}
* @private
*/
this.buttons = [0, 0, 0];
/**
* An array of the current touches on a touch screen device. Each touch has a `x`, `y`, and `id` field.
* @member {Array}
*/
this.touches = [];
/**
* A function that is called when a mouse button or touch is released.
* @callback onmouseupHandler
* @param {number} x The x coordinate of the mouse or touch that was released.
* @param {number} y The y coordinate of the mouse or touch that was released.
*/
/**
* A function that will be called when a mouse button is released, or a touch has stopped.
* This is useful for opening a URL with {@link Splat.openUrl} to avoid popup blockers.
* @member {onmouseupHandler}
*/
this.onmouseup = undefined;
var self = this;
canvas.addEventListener("mousedown", function(event) {
var m = relMouseCoords(canvas, event);
self.x = m.x;
self.y = m.y;
self.buttons[event.button] = 2;
updateTouchFromMouse();
});
canvas.addEventListener("mouseup", function(event) {
var m = relMouseCoords(canvas, event);
self.x = m.x;
self.y = m.y;
self.buttons[event.button] = 0;
updateTouchFromMouse();
if (self.onmouseup) {
self.onmouseup(self.x, self.y);
}
});
canvas.addEventListener("mousemove", function(event) {
var m = relMouseCoords(canvas, event);
self.x = m.x;
self.y = m.y;
updateTouchFromMouse();
});
function updateTouchFromMouse() {
if (self.supportsTouch()) {
return;
}
var idx = touchIndexById("mouse");
if (self.isPressed(0)) {
if (idx !== undefined) {
var touch = self.touches[idx];
touch.x = self.x;
touch.y = self.y;
} else {
self.touches.push({
id: "mouse",
x: self.x,
y: self.y
});
}
} else if (idx !== undefined) {
self.touches.splice(idx, 1);
}
}
function updateMouseFromTouch(touch) {
self.x = touch.x;
self.y = touch.y;
if (self.buttons[0] === 0) {
self.buttons[0] = 2;
}
}
function touchIndexById(id) {
for (var i = 0; i < self.touches.length; i++) {
if (self.touches[i].id === id) {
return i;
}
}
return undefined;
}
function eachChangedTouch(event, onChangeFunc) {
var touches = event.changedTouches;
for (var i = 0; i < touches.length; i++) {
onChangeFunc(touches[i]);
}
}
canvas.addEventListener("touchstart", function(event) {
eachChangedTouch(event, function(touch) {
var t = relMouseCoords(canvas, touch);
t.id = touch.identifier;
if (self.touches.length === 0) {
t.isMouse = true;
updateMouseFromTouch(t);
}
self.touches.push(t);
});
});
canvas.addEventListener("touchmove", function(event) {
eachChangedTouch(event, function(touch) {
var idx = touchIndexById(touch.identifier);
var t = self.touches[idx];
var coords = relMouseCoords(canvas, touch);
t.x = coords.x;
t.y = coords.y;
if (t.isMouse) {
updateMouseFromTouch(t);
}
});
});
canvas.addEventListener("touchend", function(event) {
eachChangedTouch(event, function(touch) {
var idx = touchIndexById(touch.identifier);
var t = self.touches.splice(idx, 1)[0];
if (t.isMouse) {
if (self.touches.length === 0) {
self.buttons[0] = 0;
} else {
self.touches[0].isMouse = true;
updateMouseFromTouch(self.touches[0]);
}
}
if (self.onmouseup) {
self.onmouseup(t.x, t.y);
}
});
});
}
/**
* Test whether the device supports touch events. This is useful to customize messages to say either "click" or "tap".
* @returns {boolean}
*/
Mouse.prototype.supportsTouch = function() {
return "ontouchstart" in window || navigator.msMaxTouchPoints;
};
/**
* Test if a mouse button is currently pressed.
* @param {number} button The button number to test. Button 0 is typically the left mouse button, as well as the first touch location.
* @param {number} [x] The left edge of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @param {number} [y] The top edge of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @param {number} [width] The width of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @param {number} [height] The height of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @returns {boolean}
*/
Mouse.prototype.isPressed = function(button, x, y, width, height) {
var b = this.buttons[button] >= 1;
if (arguments.length > 1 && (this.x < x || this.x > x + width || this.y < y || this.y > y + height)) {
b = false;
}
return b;
};
/**
* Test if a mouse button is currently pressed, and was newly pressed down since the last call to consumePressed.
* If you call this with button 0, it will add a `consumed:true` field to all current touches. This will help you prevent processing a touch multiple times.
* @param {number} button The button number to test.
* @param {number} [x] The left edge of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @param {number} [y] The top edge of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @param {number} [width] The width of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @param {number} [height] The height of a rectangle to restrict the test to. If the mouse position is outside of this rectangle, the button will not be considered pressed.
* @returns {boolean}
*/
Mouse.prototype.consumePressed = function(button, x, y, width, height) {
var b = this.buttons[button] === 2;
if (arguments.length > 1 && (this.x < x || this.x > x + width || this.y < y || this.y > y + height)) {
b = false;
}
if (b) {
this.buttons[button] = 1;
if (button === 0) {
for (var i = 0; i < this.touches.length; i++) {
this.touches[i].consumed = true;
}
}
}
return b;
};
module.exports = Mouse;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-buffer.html">buffer</a></li><li><a href="module-KeyMap.html">KeyMap</a></li></ul><h3>Externals</h3><ul><li><a href="external-AudioContext.html">AudioContext</a></li><li><a href="external-canvas.html">canvas</a></li><li><a href="external-CanvasRenderingContext2D.html">CanvasRenderingContext2D</a></li><li><a href="external-image.html">image</a></li></ul><h3>Classes</h3><ul><li><a href="Accelerometer.html">Accelerometer</a></li><li><a href="Animation.html">Animation</a></li><li><a href="AnimationLoader.html">AnimationLoader</a></li><li><a href="FontLoader.html">FontLoader</a></li><li><a href="ImageLoader.html">ImageLoader</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="SceneManager.html">SceneManager</a></li><li><a href="SoundLoader.html">SoundLoader</a></li><li><a href="Splat.AnimatedEntity.html">AnimatedEntity</a></li><li><a href="Splat.AStar.html">AStar</a></li><li><a href="Splat.BinaryHeap.html">BinaryHeap</a></li><li><a href="Splat.Camera.html">Camera</a></li><li><a href="Splat.Entity.html">Entity</a></li><li><a href="Splat.EntityBoxCamera.html">EntityBoxCamera</a></li><li><a href="Splat.Game.html">Game</a></li><li><a href="Splat.math.Random.html">Random</a></li><li><a href="Splat.NinePatch.html">NinePatch</a></li><li><a href="Splat.Scene.html">Scene</a></li><li><a href="Splat.Timer.html">Timer</a></li></ul><h3>Namespaces</h3><ul><li><a href="Splat.html">Splat</a></li><li><a href="Splat.ads.html">ads</a></li><li><a href="Splat.leaderboards.html">leaderboards</a></li><li><a href="Splat.math.html">math</a></li><li><a href="Splat.saveData.html">saveData</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Thu Sep 17 2015 23:22:42 GMT-0400 (EDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>