/
PointerLock.js
183 lines (157 loc) · 4.45 KB
/
PointerLock.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
/**
* The pointer lock input handler.
* @class
* @constructor
* @param {Phaser.Game} game
*/
Phaser.PointerLock = function (game)
{
/**
* The currently running game.
* @type {Phaser.Game}
*/
this.game = game;
/**
* The Input Manager.
* @type {Phaser.Input}
*/
this.input = game.input;
/**
* The element where event listeners are added.
* @type {HTMLElement}
*/
this.element = game.canvas;
/**
* Whether the input handler is active.
* @type {boolean}
* @readOnly
*/
this.active = false;
/**
* Whether the pointer is locked to the game canvas.
* @type {boolean}
*/
this.locked = false;
/**
* A signal dispatched when the pointer is locked or unlocked.
* Its arguments are {@link Phaser.PointerLock#locked} and the original event from the browser.
* @type {Phaser.Signal}
*/
this.onChange = new Phaser.Signal();
/**
* A signal dispatched when a request to lock or unlock the pointer fails.
* Its argument is the original event from the browser.
* @type {Phaser.Signal}
*/
this.onError = new Phaser.Signal();
/**
* The 'pointerlockchange' handler, bound to this instance.
* @type {function}
* @private
*/
this.boundOnChangeHandler = this.onChangeHandler.bind(this);
/**
* The 'pointerlockerror' handler, bound to this instance.
* @type {function}
* @private
*/
this.boundOnErrorHandler = this.onErrorHandler.bind(this);
var device = game.device;
/**
* The name of the 'pointerLockElement' property (or its equivalent) on this device.
* @type {?string}
* @private
*/
this.pointerLockElement = device.pointerLockElement;
/**
* The name of the 'pointerlockchange' event (or its equivalent) on this device.
* @type {?string}
* @private
*/
this.pointerlockchange = device.pointerlockchange;
/**
* The name of the 'pointerlockerror' event (or its equivalent) on this device.
* @type {?string}
* @private
*/
this.pointerlockerror = device.pointerlockerror;
};
/**
* Activates the handler, unless already active or Pointer Lock is unsupported on this device.
* @method Phaser.PointerLock#start
* @return {boolean} - True if the handler was started, otherwise false.
*/
Phaser.PointerLock.prototype.start = function ()
{
if (!this.game.device.pointerLock || this.active)
{
return false;
}
if (!this.element.requestPointerLock)
{
this.element.requestPointerLock = this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
}
if (!document.exitPointerLock)
{
document.exitPointerLock = document.mozExitPointerLock || document.webkitExitPointerLock;
}
document.addEventListener(this.pointerlockchange, this.boundOnChangeHandler, true);
document.addEventListener(this.pointerlockerror, this.boundOnErrorHandler, true);
this.active = true;
return true;
};
/**
* Deactivates the handler.
* @method Phaser.PointerLock#stop
*/
Phaser.PointerLock.prototype.stop = function ()
{
document.removeEventListener(this.pointerlockchange, this.boundOnChangeHandler, true);
document.removeEventListener(this.pointerlockerror, this.boundOnErrorHandler, true);
this.active = false;
};
/**
* Requests the browser to lock the pointer to the game canvas.
* Use onChange and onError to track the result of the request.
* @method Phaser.PointerLock#request
*/
Phaser.PointerLock.prototype.request = function ()
{
if (!this.active || this.locked)
{
return;
}
this.element.requestPointerLock();
};
/**
* Releases the locked pointer.
* Use onChange and onError to track the result of the request.
* @method Phaser.PointerLock#exit
*/
Phaser.PointerLock.prototype.exit = function ()
{
document.exitPointerLock();
};
/**
* Handles the 'pointerlockchange' event from the browser.
* @method Phaser.PointerLock#onChangeHandler
* @private
* @param {Event} event
* @emits Phaser.PointerLock#onChange
*/
Phaser.PointerLock.prototype.onChangeHandler = function (event)
{
this.locked = (document[this.pointerLockElement] === this.element);
this.onChange.dispatch(this.locked, event);
};
/**
* Handles the 'pointerlockerror' event from the browser.
* @method Phaser.PointerLock#onErrorHandler
* @private
* @param {Event} event
* @emits Phaser.PointerLock#onError
*/
Phaser.PointerLock.prototype.onErrorHandler = function (event)
{
this.onError.dispatch(event);
};