Skip to content

Commit fb2c2c7

Browse files
committed
Updated passive and capture states of the input handlers
1 parent c328d1e commit fb2c2c7

3 files changed

Lines changed: 90 additions & 40 deletions

File tree

v3/src/boot/Config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var ValueToColor = require('../display/color/ValueToColor');
4646
* @property {object} [?input.mouse.target=null] - [description]
4747
* @property {boolean} [input.touch=true] - [description]
4848
* @property {object} [?input.touch.target=null] - [description]
49+
* @property {object} [?input.touch.capture=true] - [description]
4950
* @property {boolean} [input.gamepad=false] - [description]
5051
* @property {boolean} [disableContextMenu=false] - [description]
5152
* @property {boolean} [banner=false] - [description]
@@ -124,9 +125,11 @@ var Config = new Class({
124125

125126
this.inputMouse = GetValue(config, 'input.mouse', true);
126127
this.inputMouseEventTarget = GetValue(config, 'input.mouse.target', null);
128+
this.inputMouseCapture = GetValue(config, 'input.mouse.capture', true);
127129

128130
this.inputTouch = GetValue(config, 'input.touch', true);
129131
this.inputTouchEventTarget = GetValue(config, 'input.touch.target', null);
132+
this.inputTouchCapture = GetValue(config, 'input.touch.capture', true);
130133

131134
this.inputGamepad = GetValue(config, 'input.gamepad', false);
132135

v3/src/input/mouse/MouseManager.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var MouseManager = new Class({
1414
this.manager = inputManager;
1515

1616
// @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propagate fully.
17-
this.capture = false;
17+
this.capture = true;
1818

1919
this.enabled = false;
2020

@@ -35,6 +35,7 @@ var MouseManager = new Class({
3535

3636
this.enabled = config.inputMouse;
3737
this.target = config.inputMouseEventTarget;
38+
this.capture = config.inputMouseCapture;
3839

3940
if (!this.target)
4041
{
@@ -112,34 +113,56 @@ var MouseManager = new Class({
112113
startListeners: function ()
113114
{
114115
var queue = this.manager.queue;
116+
var target = this.target;
115117

116-
var _this = this;
118+
var passive = { passive: true };
119+
var nonPassive = { passive: false };
117120

118-
var handler = function (event)
121+
var handler;
122+
123+
if (this.capture)
119124
{
120-
if (event.preventDefaulted)
125+
handler = function (event)
121126
{
122-
// Do nothing if event already handled
123-
return;
124-
}
127+
if (event.preventDefaulted)
128+
{
129+
// Do nothing if event already handled
130+
return;
131+
}
125132

126-
queue.push(event);
133+
queue.push(event);
127134

128-
if (_this.capture)
129-
{
130135
event.preventDefault();
131-
}
132-
};
136+
};
133137

134-
this.handler = handler;
138+
target.addEventListener('mousemove', handler, nonPassive);
139+
target.addEventListener('mousedown', handler, nonPassive);
140+
target.addEventListener('mouseup', handler, nonPassive);
141+
}
142+
else
143+
{
144+
handler = function (event)
145+
{
146+
if (event.preventDefaulted)
147+
{
148+
// Do nothing if event already handled
149+
return;
150+
}
151+
152+
queue.push(event);
153+
};
154+
155+
target.addEventListener('mousemove', handler, passive);
156+
target.addEventListener('mousedown', handler, passive);
157+
target.addEventListener('mouseup', handler, passive);
158+
}
135159

136-
this.target.addEventListener('mousemove', handler, false);
137-
this.target.addEventListener('mousedown', handler, false);
138-
this.target.addEventListener('mouseup', handler, false);
160+
this.handler = handler;
139161

140162
if (Features.pointerLock)
141163
{
142164
this.pointerLockChange = this.pointerLockChange.bind(this);
165+
143166
document.addEventListener('pointerlockchange', this.pointerLockChange, true);
144167
document.addEventListener('mozpointerlockchange', this.pointerLockChange, true);
145168
document.addEventListener('webkitpointerlockchange', this.pointerLockChange, true);
@@ -148,9 +171,11 @@ var MouseManager = new Class({
148171

149172
stopListeners: function ()
150173
{
151-
this.target.removeEventListener('mousemove', this.handler);
152-
this.target.removeEventListener('mousedown', this.handler);
153-
this.target.removeEventListener('mouseup', this.handler);
174+
var target = this.target;
175+
176+
target.removeEventListener('mousemove', this.handler);
177+
target.removeEventListener('mousedown', this.handler);
178+
target.removeEventListener('mouseup', this.handler);
154179

155180
if (Features.pointerLock)
156181
{

v3/src/input/touch/TouchManager.js

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var TouchManager = new Class({
1313
this.manager = inputManager;
1414

1515
// @property {boolean} capture - If true the DOM events will have event.preventDefault applied to them, if false they will propagate fully.
16-
this.capture = false;
16+
this.capture = true;
1717

1818
this.enabled = false;
1919

@@ -28,6 +28,7 @@ var TouchManager = new Class({
2828

2929
this.enabled = config.inputTouch;
3030
this.target = config.inputTouchEventTarget;
31+
this.capture = config.inputTouchCapture;
3132

3233
if (!this.target)
3334
{
@@ -43,39 +44,60 @@ var TouchManager = new Class({
4344
startListeners: function ()
4445
{
4546
var queue = this.manager.queue;
47+
var target = this.target;
4648

47-
var _this = this;
49+
var passive = { passive: true };
50+
var nonPassive = { passive: false };
4851

49-
var handler = function (event)
52+
var handler;
53+
54+
if (this.capture)
5055
{
51-
if (event.preventDefaulted)
56+
handler = function (event)
5257
{
53-
// Do nothing if event already handled
54-
return;
55-
}
56-
57-
// console.log(event);
58+
if (event.preventDefaulted)
59+
{
60+
// Do nothing if event already handled
61+
return;
62+
}
5863

59-
queue.push(event);
64+
queue.push(event);
6065

61-
if (_this.capture)
62-
{
6366
event.preventDefault();
64-
}
65-
};
67+
};
6668

69+
target.addEventListener('touchstart', handler, nonPassive);
70+
target.addEventListener('touchmove', handler, nonPassive);
71+
target.addEventListener('touchend', handler, nonPassive);
72+
}
73+
else
74+
{
75+
handler = function (event)
76+
{
77+
if (event.preventDefaulted)
78+
{
79+
// Do nothing if event already handled
80+
return;
81+
}
82+
83+
queue.push(event);
84+
};
85+
86+
target.addEventListener('touchstart', handler, passive);
87+
target.addEventListener('touchmove', handler, passive);
88+
target.addEventListener('touchend', handler, passive);
89+
}
90+
6791
this.handler = handler;
68-
69-
this.target.addEventListener('touchstart', handler, false);
70-
this.target.addEventListener('touchmove', handler, false);
71-
this.target.addEventListener('touchend', handler, false);
7292
},
7393

7494
stopListeners: function ()
7595
{
76-
this.target.removeEventListener('touchstart', this.handler);
77-
this.target.removeEventListener('touchmove', this.handler);
78-
this.target.removeEventListener('touchend', this.handler);
96+
var target = this.target;
97+
98+
target.removeEventListener('touchstart', this.handler);
99+
target.removeEventListener('touchmove', this.handler);
100+
target.removeEventListener('touchend', this.handler);
79101
}
80102

81103
});

0 commit comments

Comments
 (0)