don't do a lot of processing during the mouse-move event handler

This commit is contained in:
Seth Alves 2017-07-05 11:27:13 -07:00
parent 1bcfe1eaba
commit ab340530b8

View file

@ -14,7 +14,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
/* global MyAvatar, Entities, Script, Camera, Vec3, Reticle, Overlays, getEntityCustomData, Messages, Quat, Controller */ /* global MyAvatar, Entities, Script, Camera, Vec3, Reticle, Overlays, getEntityCustomData, Messages, Quat, Controller,
isInEditMode, HMD */
(function() { // BEGIN LOCAL_SCOPE (function() { // BEGIN LOCAL_SCOPE
@ -22,6 +23,8 @@
Script.include("/~/system/libraries/utils.js"); Script.include("/~/system/libraries/utils.js");
var MAX_SOLID_ANGLE = 0.01; // objects that appear smaller than this can't be grabbed var MAX_SOLID_ANGLE = 0.01; // objects that appear smaller than this can't be grabbed
var DELAY_FOR_30HZ = 33; // milliseconds
var ZERO_VEC3 = { var ZERO_VEC3 = {
x: 0, x: 0,
y: 0, y: 0,
@ -415,6 +418,11 @@ Grabber.prototype.releaseEvent = function(event) {
return; return;
} }
if (this.moveEventTimer) {
Script.clearTimeout(this.moveEventTimer);
this.moveEventTimer = null;
}
if (this.isGrabbing) { if (this.isGrabbing) {
// this.deactivateEntity(this.entityID); // this.deactivateEntity(this.entityID);
this.isGrabbing = false; this.isGrabbing = false;
@ -440,11 +448,23 @@ Grabber.prototype.releaseEvent = function(event) {
}; };
Grabber.prototype.moveEvent = function(event) { Grabber.prototype.moveEvent = function(event) {
// during the handling of the event, do as little as possible. We save the updated mouse position,
// and start a timer to react to the change. If more changes arrive before the timer fires, only
// the last update will be considered. This is done to avoid backing-up Qt's event queue.
if (!this.isGrabbing) { if (!this.isGrabbing) {
return; return;
} }
mouse.updateDrag(event); mouse.updateDrag(event);
var _this = this;
if (!this.moveEventTimer) {
this.moveEventTimer = Script.setTimeout(function() {
_this.moveEventProcess();
} , DELAY_FOR_30HZ);
}
};
Grabber.prototype.moveEventProcess = function() {
// see if something added/restored gravity // see if something added/restored gravity
var entityProperties = Entities.getEntityProperties(this.entityID); var entityProperties = Entities.getEntityProperties(this.entityID);
if (!entityProperties || !entityProperties.gravity) { if (!entityProperties || !entityProperties.gravity) {
@ -538,6 +558,11 @@ Grabber.prototype.moveEvent = function(event) {
} else { } else {
Entities.updateAction(this.entityID, this.actionID, actionArgs); Entities.updateAction(this.entityID, this.actionID, actionArgs);
} }
var _this = this;
this.moveEventTimer = Script.setTimeout(function() {
_this.moveEventProcess();
}, DELAY_FOR_30HZ);
}; };
Grabber.prototype.keyReleaseEvent = function(event) { Grabber.prototype.keyReleaseEvent = function(event) {