forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseManager.js
More file actions
192 lines (155 loc) · 5.83 KB
/
Copy pathMouseManager.js
File metadata and controls
192 lines (155 loc) · 5.83 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
var Class = require('../../utils/Class');
var Features = require('../../device/Features');
var MouseEvents = require('./events');
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
var MouseManager = new Class({
initialize:
function MouseManager (inputManager)
{
this.manager = inputManager;
// @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propagate fully.
this.capture = true;
this.enabled = false;
this.target;
this.handler;
/**
* @property {boolean} locked - If the mouse has been pointer locked successfully this will
* be set to true.
*/
this.locked = false;
},
boot: function ()
{
var config = this.manager.config;
this.enabled = config.inputMouse;
this.target = config.inputMouseEventTarget;
this.capture = config.inputMouseCapture;
if (!this.target)
{
this.target = this.manager.game.canvas;
}
if (config.disableContextMenu)
{
this.disableContextMenu();
}
if (this.enabled)
{
this.startListeners();
}
},
disableContextMenu: function ()
{
document.body.addEventListener('contextmenu', function (event)
{
event.preventDefault();
return false;
});
return this;
},
/**
* If the browser supports it, you can request that the pointer be locked to the browser window.
* This is classically known as 'FPS controls', where the pointer can't leave the browser until
* the user presses an exit key. If the browser successfully enters a locked state, a
* 'POINTER_LOCK_CHANGE_EVENT' will be dispatched - from the game's input manager - with an
* `isPointerLocked` property.
* It is important to note that pointer lock can only be enabled after an 'engagement gesture',
* see: https://w3c.github.io/pointerlock/#dfn-engagement-gesture.
*/
requestPointerLock: function ()
{
if (Features.pointerLock)
{
var element = this.target;
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
}
},
/**
* Internal pointerLockChange handler.
*
* @param {Event} event - The native event from the browser.
*/
pointerLockChange: function (event)
{
var element = this.target;
this.locked = document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element
? true : false;
this.manager.queue.push(event);
},
/**
* If the browser supports pointer lock, this will request that the pointer lock is released. If
* the browser successfully enters a locked state, a 'POINTER_LOCK_CHANGE_EVENT' will be
* dispatched - from the game's input manager - with an `isPointerLocked` property.
*/
releasePointerLock: function ()
{
if (Features.pointerLock)
{
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
document.exitPointerLock();
}
},
startListeners: function ()
{
var queue = this.manager.queue;
var target = this.target;
var passive = { passive: true };
var nonPassive = { passive: false };
var handler;
if (this.capture)
{
handler = function (event)
{
if (event.defaultPrevented)
{
// Do nothing if event already handled
return;
}
// console.log('mouse', event);
queue.push(event);
event.preventDefault();
};
target.addEventListener('mousemove', handler, nonPassive);
target.addEventListener('mousedown', handler, nonPassive);
target.addEventListener('mouseup', handler, nonPassive);
}
else
{
handler = function (event)
{
if (event.defaultPrevented)
{
// Do nothing if event already handled
return;
}
queue.push(event);
};
target.addEventListener('mousemove', handler, passive);
target.addEventListener('mousedown', handler, passive);
target.addEventListener('mouseup', handler, passive);
}
this.handler = handler;
if (Features.pointerLock)
{
this.pointerLockChange = this.pointerLockChange.bind(this);
document.addEventListener('pointerlockchange', this.pointerLockChange, true);
document.addEventListener('mozpointerlockchange', this.pointerLockChange, true);
document.addEventListener('webkitpointerlockchange', this.pointerLockChange, true);
}
},
stopListeners: function ()
{
var target = this.target;
target.removeEventListener('mousemove', this.handler);
target.removeEventListener('mousedown', this.handler);
target.removeEventListener('mouseup', this.handler);
if (Features.pointerLock)
{
document.removeEventListener('pointerlockchange', this.pointerLockChange, true);
document.removeEventListener('mozpointerlockchange', this.pointerLockChange, true);
document.removeEventListener('webkitpointerlockchange', this.pointerLockChange, true);
}
}
});
module.exports = MouseManager;