mirror of
https://github.com/overte-org/overte.git
synced 2025-04-27 04:56:27 +02:00
133 lines
5.1 KiB
JavaScript
133 lines
5.1 KiB
JavaScript
//
|
|
// particleExplorerTool.js
|
|
//
|
|
// Created by Eric Levin on 2/15/16
|
|
// Copyright 2016 High Fidelity, Inc.
|
|
// Adds particleExplorer tool to the edit panel when a user selects a particle entity from the edit tool window
|
|
// This is an example of a new, easy way to do two way bindings between dynamically created GUI and in-world entities.
|
|
//
|
|
// 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 */
|
|
|
|
|
|
var PARTICLE_EXPLORER_HTML_URL = Script.resolvePath('particleExplorer.html');
|
|
|
|
ParticleExplorerTool = function() {
|
|
var that = {};
|
|
that.activeParticleEntity = 0;
|
|
that.updatedActiveParticleProperties = {};
|
|
|
|
that.createWebView = function() {
|
|
that.webView = Tablet.getTablet("com.highfidelity.interface.tablet.system");
|
|
that.webView.setVisible = function(value) {};
|
|
that.webView.webEventReceived.connect(that.webEventReceived);
|
|
};
|
|
|
|
that.destroyWebView = function() {
|
|
if (!that.webView) {
|
|
return;
|
|
}
|
|
that.activeParticleEntity = 0;
|
|
that.updatedActiveParticleProperties = {};
|
|
|
|
var messageData = {
|
|
messageType: "particle_close"
|
|
};
|
|
that.webView.emitScriptEvent(JSON.stringify(messageData));
|
|
};
|
|
|
|
function sendParticleProperties(properties) {
|
|
that.webView.emitScriptEvent(JSON.stringify({
|
|
messageType: "particle_settings",
|
|
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.red)) {
|
|
properties.colorStart = properties.color;
|
|
}
|
|
if (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") {
|
|
var updatedSettings = data.updatedSettings;
|
|
|
|
var optionalProps = ["alphaStart", "alphaFinish", "radiusStart", "radiusFinish", "colorStart", "colorFinish"];
|
|
var fallbackProps = ["alpha", "particleRadius", "color"];
|
|
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 fallbackProp = fallbackProps[Math.floor(i / 2)];
|
|
var fallbackValue = updatedSettings[fallbackProp];
|
|
if (fallbackValue) {
|
|
var optionalProp = optionalProps[i];
|
|
if ((fallbackProp !== "color" && isNaN(entityProps[optionalProp])) || (fallbackProp === "color" && isNaN(entityProps[optionalProp].red))) {
|
|
that.updatedActiveParticleProperties[optionalProp] = fallbackValue;
|
|
needsUpdate = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (needsUpdate) {
|
|
sendUpdatedActiveParticleProperties();
|
|
}
|
|
|
|
} else if (data.messageType === "page_loaded") {
|
|
sendActiveParticleProperties();
|
|
}
|
|
};
|
|
|
|
that.setActiveParticleEntity = function(id) {
|
|
that.activeParticleEntity = id;
|
|
sendActiveParticleProperties();
|
|
};
|
|
|
|
return that;
|
|
};
|