Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/scene/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var registerComponent = require('../../core/component').registerComponent;
var shouldCaptureKeyEvent = require('../../utils/').shouldCaptureKeyEvent;
var THREE = require('../../lib/three');

var controls = new THREE.VRControls(new THREE.Object3D());
Expand All @@ -14,6 +15,7 @@ module.exports.Component = registerComponent('keyboard-shortcuts', {
var scene = this.el;

this.listener = window.addEventListener('keyup', function (event) {
if (!shouldCaptureKeyEvent(event)) { return; }
if (self.enterVREnabled && event.keyCode === 70) { // f.
scene.enterVR();
}
Expand Down
51 changes: 43 additions & 8 deletions src/components/wasd-controls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var registerComponent = require('../core/component').registerComponent;
var shouldCaptureKeyEvent = require('../utils/').shouldCaptureKeyEvent;
var THREE = require('../lib/three');

var MAX_DELTA = 0.2;
Expand All @@ -24,8 +25,12 @@ module.exports.Component = registerComponent('wasd-controls', {
this.velocity = new THREE.Vector3();
// To keep track of the pressed keys
this.keys = {};
this.onBlur = this.onBlur.bind(this);
this.onFocus = this.onFocus.bind(this);
this.onVisibilityChange = this.onVisibilityChange.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.attachVisibilityEventListeners();
},

update: function (previousData) {
Expand Down Expand Up @@ -81,11 +86,12 @@ module.exports.Component = registerComponent('wasd-controls', {
},

play: function () {
this.attachEventListeners();
this.attachKeyEventListeners();
},

pause: function () {
this.removeEventListeners();
this.keys = {};
this.removeKeyEventListeners();
},

tick: function (t) {
Expand All @@ -94,25 +100,54 @@ module.exports.Component = registerComponent('wasd-controls', {

remove: function () {
this.pause();
this.removeVisibilityEventListeners();
},

attachVisibilityEventListeners: function () {
window.addEventListener('blur', this.onBlur);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have a listener for the symmetric event. window.addEventListener("focus", this.onFocus);?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, very good call.

hmm, the more I think about it we probably shouldn't call pause on blur because that removes the event listeners. and then we'd have to reattach them when play gets called on focus. seems like maybe a waste. thoughts?

window.addEventListener('focus', this.onFocus);
document.addEventListener('visibilitychange', this.onVisibilityChange);
},

attachEventListeners: function () {
// Keyboard events
window.addEventListener('keydown', this.onKeyDown, false);
window.addEventListener('keyup', this.onKeyUp, false);
removeVisibilityEventListeners: function () {
window.removeEventListener('blur', this.onBlur);
window.removeEventListener('focus', this.onFocus);
document.removeEventListener('visibilitychange', this.onVisibilityChange);
},

removeEventListeners: function () {
// Keyboard events
attachKeyEventListeners: function () {
window.addEventListener('keydown', this.onKeyDown);
window.addEventListener('keyup', this.onKeyUp);
},

removeKeyEventListeners: function () {
window.removeEventListener('keydown', this.onKeyDown);
window.removeEventListener('keyup', this.onKeyUp);
},

onBlur: function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to the component module and always call pause on blur for every component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting idea; probably actually, yeah. you think that's good?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is the blur event exactly triggered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the current tab is no longer the focused tab - or if the window loses focus

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to say whether we want to force pause on blur. Seems too prescriptive at the moment.

this.pause();
},

onFocus: function () {
this.play();
},

onVisibilityChange: function () {
if (document.hidden) {
this.onBlur();
} else {
this.onFocus();
}
},

onKeyDown: function (event) {
if (!shouldCaptureKeyEvent(event)) { return; }
this.keys[event.keyCode] = true;
},

onKeyUp: function (event) {
if (!shouldCaptureKeyEvent(event)) { return; }
this.keys[event.keyCode] = false;
},

Expand Down
16 changes: 14 additions & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,25 @@ var isIOS = module.exports.isIOS = function () {
};

/**
* Checks mobile device orientation
* @return {Boolean} True if landscape orientation
* Checks mobile device orientation.
* @return {Boolean} True if landscape orientation.
*/
module.exports.isLandscape = function () {
return window.orientation === 90 || window.orientation === -90;
};

/**
* Returns whether we should capture this keyboard event for keyboard shortcuts.
* @param {Event} event Event object.
* @returns {Boolean} Whether the key event should be captured.
*/
module.exports.shouldCaptureKeyEvent = function (event) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed per @dmarcos' suggestion in the previous PR: #1064

if (event.shiftKey || event.metaKey || event.altKey || event.ctrlKey) {
return false;
}
return document.activeElement === document.body;
};

/**
* Splits a string into an array based on a delimiter.
*
Expand Down