From 2cdda231698a3e31eeeeaed67c8822dd467856af Mon Sep 17 00:00:00 2001 From: David Back Date: Wed, 27 Jun 2018 13:05:39 -0700 Subject: [PATCH 1/3] more robustness for optional properties --- scripts/system/edit.js | 1 - .../particle_explorer/hifi-entity-ui.js | 48 +++------- .../particle_explorer/particleExplorerTool.js | 93 +++++++++++++------ 3 files changed, 80 insertions(+), 62 deletions(-) diff --git a/scripts/system/edit.js b/scripts/system/edit.js index e9c7a49378..08e7a541d5 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -2373,7 +2373,6 @@ function selectParticleEntity(entityID) { particleExplorerTool.createWebView(); particleExplorerTool.setActiveParticleEntity(entityID); - particleExplorerTool.setActiveParticleProperties(properties); // Switch to particle explorer var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); diff --git a/scripts/system/particle_explorer/hifi-entity-ui.js b/scripts/system/particle_explorer/hifi-entity-ui.js index 36505a0b7f..62a0aadc86 100644 --- a/scripts/system/particle_explorer/hifi-entity-ui.js +++ b/scripts/system/particle_explorer/hifi-entity-ui.js @@ -61,12 +61,18 @@ function HifiEntityUI(parent) { this.parent = parent; var self = this; + this.sendPackage = {}; this.settingsUpdateLock = false; - this.webBridgeSync = _.debounce(function (id, val) { - if (self.EventBridge && !self.settingsUpdateLock) { - var sendPackage = {}; - sendPackage[id] = val; - self.submitChanges(sendPackage); + this.webBridgeSync = function(id, val) { + if (!this.settingsUpdateLock) { + this.sendPackage[id] = val; + this.webBridgeSyncDebounce(); + } + }; + this.webBridgeSyncDebounce = _.debounce(function () { + if (self.EventBridge) { + self.submitChanges(self.sendPackage); + self.sendPackage = {}; } }, DEBOUNCE_TIMEOUT); } @@ -159,7 +165,6 @@ HifiEntityUI.prototype = { var self = this; var fields = document.getElementsByTagName("input"); - self.settingsUpdateLock = true; if (!currentProperties.locked) { for (var i = 0; i < fields.length; i++) { fields[i].removeAttribute("disabled"); @@ -179,7 +184,7 @@ HifiEntityUI.prototype = { for (var e in keys) { if (keys.hasOwnProperty(e)) { var value = keys[e]; - + var property = currentProperties[value]; var field = self.builtRows[value]; if (field) { @@ -235,10 +240,6 @@ HifiEntityUI.prototype = { } } } - // Now unlocking settings Update lock for sending messages on callbacks. - setTimeout(function () { - self.settingsUpdateLock = false; - }, DEBOUNCE_TIMEOUT * 2.5); }, connect: function (EventBridge) { this.EventBridge = EventBridge; @@ -253,28 +254,9 @@ HifiEntityUI.prototype = { data = JSON.parse(data); if (data.messageType === 'particle_settings') { - // Update settings - var currentProperties = data.currentProperties; - // Update uninitialized variables - if (!currentProperties.alphaStart) { - currentProperties.alphaStart = currentProperties.alpha; - } - if (!currentProperties.alphaFinish) { - currentProperties.alphaFinish = currentProperties.alpha; - } - if (!currentProperties.radiusStart) { - currentProperties.radiusStart = currentProperties.particleRadius; - } - if (!currentProperties.radiusFinish) { - currentProperties.radiusFinish = currentProperties.particleRadius; - } - if (!currentProperties.colorStart || !currentProperties.colorStart.red) { - currentProperties.colorStart = currentProperties.color; - } - if (!currentProperties.colorFinish || !currentProperties.colorFinish.red) { - currentProperties.colorFinish = currentProperties.color; - } - self.fillFields(currentProperties); + self.settingsUpdateLock = true; + self.fillFields(data.currentProperties); + self.settingsUpdateLock = false; // Do expected property match with structure; } else if (data.messageType === 'particle_close') { self.disableFields(); diff --git a/scripts/system/particle_explorer/particleExplorerTool.js b/scripts/system/particle_explorer/particleExplorerTool.js index a1f06fda35..0de86fbf2f 100644 --- a/scripts/system/particle_explorer/particleExplorerTool.js +++ b/scripts/system/particle_explorer/particleExplorerTool.js @@ -9,7 +9,8 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -/* global window, alert, ParticleExplorerTool, EventBridge, dat, listenForSettingsUpdates,createVec3Folder,createQuatFolder,writeVec3ToInterface,writeDataToInterface*/ +/* global window, alert, ParticleExplorerTool, EventBridge, dat, listenForSettingsUpdates, createVec3Folder, + createQuatFolder, writeVec3ToInterface, writeDataToInterface */ var PARTICLE_EXPLORER_HTML_URL = Script.resolvePath('particleExplorer.html'); @@ -17,7 +18,7 @@ var PARTICLE_EXPLORER_HTML_URL = Script.resolvePath('particleExplorer.html'); ParticleExplorerTool = function() { var that = {}; that.activeParticleEntity = 0; - that.activeParticleProperties = {}; + that.updatedActiveParticleProperties = {}; that.createWebView = function() { that.webView = Tablet.getTablet("com.highfidelity.interface.tablet.system"); @@ -30,7 +31,7 @@ ParticleExplorerTool = function() { return; } that.activeParticleEntity = 0; - that.activeParticleProperties = {}; + that.updatedActiveParticleProperties = {}; var messageData = { messageType: "particle_close" @@ -38,46 +39,86 @@ ParticleExplorerTool = function() { that.webView.emitScriptEvent(JSON.stringify(messageData)); }; - function sendActiveParticleProperties() { + function sendParticleProperties(properties) { that.webView.emitScriptEvent(JSON.stringify({ messageType: "particle_settings", - currentProperties: that.activeParticleProperties + currentProperties: properties })); } + function sendActiveParticleProperties() { + var properties = Entities.getEntityProperties(that.activeParticleEntity); + if (properties.emitOrientation) { + properties.emitOrientation = Quat.safeEulerAngles(properties.emitOrientation); + } + // Update uninitialized variables + if (isNaN(properties.alphaStart)) { + properties.alphaStart = properties.alpha; + } + if (isNaN(properties.alphaFinish)) { + properties.alphaFinish = properties.alpha; + } + if (isNaN(properties.radiusStart)) { + properties.radiusStart = properties.particleRadius; + } + if (isNaN(properties.radiusFinish)) { + properties.radiusFinish = properties.particleRadius; + } + if (isNaN(properties.colorStart) || isNaN(properties.colorStart.red)) { + properties.colorStart = properties.color; + } + if (isNaN(properties.colorFinish) || isNaN(properties.colorFinish.red)) { + properties.colorFinish = properties.color; + } + sendParticleProperties(properties); + } + + function sendUpdatedActiveParticleProperties() { + sendParticleProperties(that.updatedActiveParticleProperties); + that.updatedActiveParticleProperties = {}; + } + that.webEventReceived = function(message) { var data = JSON.parse(message); if (data.messageType === "settings_update") { - if (data.updatedSettings.emitOrientation) { - data.updatedSettings.emitOrientation = Quat.fromVec3Degrees(data.updatedSettings.emitOrientation); - } - Entities.editEntity(that.activeParticleEntity, data.updatedSettings); - - for (var key in data.updatedSettings) { - if (that.activeParticleProperties.hasOwnProperty(key)) { - that.activeParticleProperties[key] = data.updatedSettings[key]; - } - } + var updatedSettings = data.updatedSettings; var optionalProps = ["alphaStart", "alphaFinish", "radiusStart", "radiusFinish", "colorStart", "colorFinish"]; var fallbackProps = ["alpha", "particleRadius", "color"]; - var entityProps = Entities.getEntityProperties(that.activeParticleProperties, optionalProps); + for (var i = 0; i < optionalProps.length; i++) { + var fallbackProp = fallbackProps[Math.floor(i / 2)]; + var optionalValue = updatedSettings[optionalProps[i]]; + var fallbackValue = updatedSettings[fallbackProp]; + if (optionalValue && fallbackValue) { + delete updatedSettings[optionalProps[i]]; + } + } + + if (updatedSettings.emitOrientation) { + updatedSettings.emitOrientation = Quat.fromVec3Degrees(updatedSettings.emitOrientation); + } + + Entities.editEntity(that.activeParticleEntity, updatedSettings); + + var entityProps = Entities.getEntityProperties(that.activeParticleEntity, optionalProps); + var needsUpdate = false; for (var i = 0; i < optionalProps.length; i++) { - var fallback = fallbackProps[Math.floor(i / 2)]; - if (data.updatedSettings[fallback]) { - var prop = optionalProps[i]; - if (!that.activeParticleProperties[prop] || (fallback === "color" && !that.activeParticleProperties[prop].red)) { - that.activeParticleProperties[prop] = entityProps[fallback]; + var fallbackProp = fallbackProps[Math.floor(i / 2)]; + var fallbackValue = updatedSettings[fallbackProp]; + if (fallbackValue) { + var optionalProp = optionalProps[i]; + if (isNaN(entityProps[optionalProp]) || (fallbackProp === "color" && isNaN(entityProps[optionalProp].red))) { + that.updatedActiveParticleProperties[optionalProp] = fallbackValue; needsUpdate = true; } } } if (needsUpdate) { - sendActiveParticleProperties(); + sendUpdatedActiveParticleProperties(); } - + } else if (data.messageType === "page_loaded") { sendActiveParticleProperties(); } @@ -85,12 +126,8 @@ ParticleExplorerTool = function() { that.setActiveParticleEntity = function(id) { that.activeParticleEntity = id; - }; - - that.setActiveParticleProperties = function(properties) { - that.activeParticleProperties = properties; sendActiveParticleProperties(); }; - + return that; }; From e74d30ce22788b41fdb802543cd01d576a0620a8 Mon Sep 17 00:00:00 2001 From: David Back Date: Wed, 27 Jun 2018 17:43:41 -0700 Subject: [PATCH 2/3] fix color fallbacks --- scripts/system/particle_explorer/particleExplorerTool.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/system/particle_explorer/particleExplorerTool.js b/scripts/system/particle_explorer/particleExplorerTool.js index 0de86fbf2f..b675d26ff6 100644 --- a/scripts/system/particle_explorer/particleExplorerTool.js +++ b/scripts/system/particle_explorer/particleExplorerTool.js @@ -108,7 +108,7 @@ ParticleExplorerTool = function() { var fallbackValue = updatedSettings[fallbackProp]; if (fallbackValue) { var optionalProp = optionalProps[i]; - if (isNaN(entityProps[optionalProp]) || (fallbackProp === "color" && isNaN(entityProps[optionalProp].red))) { + if ((fallbackProp !== "color" && isNaN(entityProps[optionalProp])) || (fallbackProp === "color" && isNaN(entityProps[optionalProp].red))) { that.updatedActiveParticleProperties[optionalProp] = fallbackValue; needsUpdate = true; } From fd4a8fdd833fea9e31a2ec39b28496a2290c7c32 Mon Sep 17 00:00:00 2001 From: David Back Date: Fri, 29 Jun 2018 15:33:55 -0700 Subject: [PATCH 3/3] fix color NaN check --- scripts/system/particle_explorer/particleExplorerTool.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/system/particle_explorer/particleExplorerTool.js b/scripts/system/particle_explorer/particleExplorerTool.js index b675d26ff6..80256a12e3 100644 --- a/scripts/system/particle_explorer/particleExplorerTool.js +++ b/scripts/system/particle_explorer/particleExplorerTool.js @@ -64,10 +64,10 @@ ParticleExplorerTool = function() { if (isNaN(properties.radiusFinish)) { properties.radiusFinish = properties.particleRadius; } - if (isNaN(properties.colorStart) || isNaN(properties.colorStart.red)) { + if (isNaN(properties.colorStart.red)) { properties.colorStart = properties.color; } - if (isNaN(properties.colorFinish) || isNaN(properties.colorFinish.red)) { + if (isNaN(properties.colorFinish.red)) { properties.colorFinish = properties.color; } sendParticleProperties(properties);