From ee2481254775f59bef2b2f43e2647af77732cf90 Mon Sep 17 00:00:00 2001 From: Aref Ariyapour Date: Mon, 3 Aug 2015 17:32:46 +0200 Subject: [PATCH] Add userDefindeSystemNode to XML3D data adapter. Not Working Add attributeChangedCallback() to xml3dDataAdapter. Everything is working. Some special cases should be discussed. Fix a problem if a node is removed from a data node. Everything is working properly --- src/data/adapter/data.js | 41 +++---- src/data/adapter/values.js | 9 +- src/data/adapter/xml3d.js | 167 ++++++++++++++++++++++++--- src/renderer/renderer/scene/scene.js | 7 +- src/renderer/webgl/scene/glscene.js | 9 +- tests/scenes/scripts/animateColor.js | 2 +- tests/scenes/system-parameters.html | 7 +- 7 files changed, 179 insertions(+), 63 deletions(-) diff --git a/src/data/adapter/data.js b/src/data/adapter/data.js index e7b54759..504e882b 100644 --- a/src/data/adapter/data.js +++ b/src/data/adapter/data.js @@ -76,10 +76,10 @@ DataAdapter.prototype.onXflowLoadEvent = function(node, newLevel, oldLevel){ } }; DataAdapter.prototype.getDataComplete = function(){ - return this.xflowDataNode.getProgressLevel() == Infinity; + return this.xflowDataNode._children[0].getProgressLevel() == Infinity; }; DataAdapter.prototype.getDataProgressLevel = function(){ - return this.xflowDataNode.getProgressLevel(); + return this.xflowDataNode._children[0].getProgressLevel(); }; /** Recursively passing platform information to children of a data node @@ -114,7 +114,7 @@ function recursiveDataAdapterConstruction(adapter, systemDataAdapter) { */ - var filterNames = []; // We store the node names with sys flag set + var filterNames = []; // Where we store the node names with sys flag set var xflowDataNode = new DataNode(false); // Data node to keep the visible dom nodes for (var child = adapter.node.firstElementChild; child !== null; child = child.nextElementSibling) { @@ -137,7 +137,6 @@ function recursiveDataAdapterConstruction(adapter, systemDataAdapter) { //Here we check for data nodes with sys flag set if (child.sys != undefined){ //Check if a system parameter with this name exists - // TODO: consider both, user defined and internal sys data node here! if (systemDataAdapter.xflowDataNode.getOutputNames().indexOf(child.name)>-1) { filterNames.push(child.name); } else { @@ -148,15 +147,8 @@ function recursiveDataAdapterConstruction(adapter, systemDataAdapter) { } - // TODO: Move this to the XML3D DataAdapter - //check if this node is the user defined system node, then we should not add system node to it - var userDefinedSystemDataUri = Resource.getAbsoluteURI( systemDataAdapter.node.ownerDocument.URL, systemDataAdapter.node.getAttribute("sys")); - if (userDefinedSystemDataUri && adapter.node.getAttribute("id") == userDefinedSystemDataUri.fragment){ - adapter.xflowDataNode._children = xflowDataNode._children; - } - else{ - addSystemNodeToAdapter(filterNames,adapter,systemDataAdapter.xflowDataNode,xflowDataNode); - } + addSystemNodeToAdapter(filterNames,adapter,systemDataAdapter.xflowDataNode,xflowDataNode); + // Passes _platform values to children nodes starting from the node // where these attributes are first defined if (adapter.xflowDataNode._platform !== null) { @@ -167,27 +159,23 @@ function recursiveDataAdapterConstruction(adapter, systemDataAdapter) { function addSystemNodeToAdapter(filterNames,adapter,systemDataNode,xflowDataNode){ var filter = "keep("+filterNames.join(",")+")"; - var sysData = new DataNode(false); + var sysDataAndUserData = new DataNode(false); + sysData.sourceNode = systemDataNode; + sysData.systemDataNode = true; sysData.systemDataAdapter = true; sysData.setFilter(filter); - - - var sysDataAndUserData = new DataNode(false); sysDataAndUserData.appendChild(xflowDataNode); sysDataAndUserData.appendChild(sysData); - if (adapter.getXflowNode()._children.length){ - XML3D.debug.assert(!adapter.getXflowNode()._children[0]._children[1].systemDataAdapter); - } - adapter.xflowDataNode.appendChild(sysDataAndUserData); - return; + adapter.xflowDataNode.appendChild(sysDataAndUserData); + return; } /** * The notifyChanged() method is called by the XML3D data structure to - * notify the DataAdapter about data changes (DOM mustation events) in its + * notify the DataAdapter about data changes (DOM mutation events) in its * associating node. When this method is called, all observers of the * DataAdapter are notified about data changes via their notifyDataChanged() * method. @@ -217,9 +205,9 @@ DataAdapter.prototype.notifyChanged = function (evt) { // Make sure to use the actual dom node! if (followUpAdapter) { - this.xflowDataNode.insertBefore(insertedXflowNode, followUpAdapter.getXflowNode()); + this.xflowDataNode._children[0].insertBefore(insertedXflowNode, followUpAdapter.getXflowNode()); } else { - this.xflowDataNode.appendChild(insertedXflowNode); + this.xflowDataNode._children[0].appendChild(insertedXflowNode); } } else if (evt.type === Events.NODE_REMOVED) { @@ -229,7 +217,7 @@ DataAdapter.prototype.notifyChanged = function (evt) { } var removedXflowNode = adapter.getXflowNode(); - this.xflowDataNode.removeChild(removedXflowNode); + this.xflowDataNode._children[0].removeChild(removedXflowNode); } else if (evt.type === Events.THIS_REMOVED) { this.clearAdapterHandles(); @@ -248,6 +236,7 @@ DataAdapter.prototype.attributeChangedCallback = function (name, oldValue, newVa } }; +// should I also chenge here: adapter.getXflowNode()._children[0] ? DataAdapter.prototype.connectedAdapterChanged = function (key, adapter /*, status */) { if (key === "src") { this.xflowDataNode.sourceNode = adapter ? adapter.getXflowNode() : null; diff --git a/src/data/adapter/values.js b/src/data/adapter/values.js index c32021c0..40673f47 100644 --- a/src/data/adapter/values.js +++ b/src/data/adapter/values.js @@ -70,13 +70,16 @@ ValueDataAdapter.prototype.attributeChangedCallback = function (name, oldValue, this.xflowInputNode.paramName = newValue ? this.node.name : null; }else if (name == "sys"){ var parentDataAdapter = this.factory.getAdapter(this.node.parentNode); - var filterMapping = parentDataAdapter.getXflowNode()._children[0]._children[1]._filterMapping; + if (parentDataAdapter.getXflowNode()._children[0]._children.length >1 && parentDataAdapter.getXflowNode()._children[0]._children[1].systemDataNode == true) + var filterMapping = parentDataAdapter.getXflowNode()._children[0]._children[1]._filterMapping; + if (!this.node.attributes["sys"]){ //If the sys flag is removed we update the filter in system data node filterMapping.removeName(filterMapping._names.indexOf(this.node.name)); }else { - //If sys flag is set - filterMapping.setName(filterMapping.length,this.node.name); + //If sys flag is set, we add the node name to the filter + if (filterMapping) + filterMapping.setName(filterMapping.length,this.node.name); } } }; diff --git a/src/data/adapter/xml3d.js b/src/data/adapter/xml3d.js index 8edd0d46..c76bab7c 100644 --- a/src/data/adapter/xml3d.js +++ b/src/data/adapter/xml3d.js @@ -1,11 +1,13 @@ var DataNode = require("../../xflow/interface/graph.js").DataNode; var dispatchCustomEvent = require("../../utils/misc.js").dispatchCustomEvent; +var URIResolver = require("../../utils/uri.js").URIResolver; var NodeAdapter = require("../../base/adapter.js").NodeAdapter; var createClass = XML3D.createClass; var AdapterHandle = require("../../base/adapterhandle.js"); var InputNode = require("../../xflow/interface/graph.js").InputNode; var BufferEntry = require("../../xflow/interface/data.js").BufferEntry; var XC = require("../../xflow/interface/constants.js"); +var Events = require("../../interface/notification.js"); // FIXME: Remove copied code! Rename to xml3d.js /** @@ -20,11 +22,7 @@ var XML3DDataAdapter= function (factory, node) { }; createClass(XML3DDataAdapter, NodeAdapter); -XML3DDataAdapter.prototype.init = function() -{ - // todo(ksons): Check if system attribute is set, - // request data adapter and use this (with appenden sysdatanode) instead if it exists - +XML3DDataAdapter.prototype.init = function(){ /** @@ -36,23 +34,123 @@ XML3DDataAdapter.prototype.init = function() this.xflowDataNode.userData = this.node; this.setDefaultValues(); - // TODO: create accessor for internal sys data and overall sys data (including user defined stuff) - var systemDataNodeURI = this.node.getAttribute("sys"); if (systemDataNodeURI){ - var systemDataNodeHandler = this.getAdapterHandle(systemDataNodeURI); - // Remove previously added sys data node and add to xflowDataNode as second child. - addUserDefinedThingee() - var xflowDataNode = new DataNode(false); // User defined system data node - xflowDataNode.sourceNode = systemDataNodeHandler.adapter.getXflowNode(); - this.xflowDataNode.appendChild(xflowDataNode); + this.addUserDefinedSystemNode(systemDataNodeURI); } }; +/** + * @return {DataNode} + */ +XML3DDataAdapter.prototype.getUserDefinedSystemNode = function(){ + return this.xflowDataNode._children[1]; +}; + + +/** + * @return {DataNode} + */ +XML3DDataAdapter.prototype.getSystemDefinedSystemNode = function(){ + return this.xflowDataNode._children[0]; +}; + +/** +* +*/ +XML3DDataAdapter.prototype.notifyChanged = function (evt) { + if (evt.type == Events.VALUE_MODIFIED) { + var attr = evt.mutation.attributeName; + if (!attr) { + delete this.node._configured.scriptValue; + this.xflowInputNode.data.setValue(this.node.value); + } + } +}; + + XML3DDataAdapter.prototype.attributeChangedCallback = function (name, oldValue, newValue) { - // TODO: listen for attribute sys changed. Remove sys node from new referenced node. - // Add new reference as sourcenode for user sys node. Readd sys node to old user sys node. + if (name == "sys"){ + if (newValue){ + this.addUserDefinedSystemNode(newValue); + }else{ + if (this.xflowDataNode._children.length == 2){ + var oldUserDefinedSystemNode = this.getUserDefinedSystemNode(); + this.xflowDataNode.removeChild(oldUserDefinedSystemNode); + oldUserDefinedSystemNode.appendChild(this.xflowDataNode); + } + } + } + +}; + +XML3DDataAdapter.prototype.addUserDefinedSystemNode = function (URI) { //Here we create separately an adapter for + var node = URIResolver.resolveLocal(URI); //userDefined systemNode + + if (!node){ + XML3D.debug.logWarning("Requested system node \""+URI+"\" does not exist!"); + if (this.xflowDataNode._children.length == 2){ //if the requested node doesn't exists we remove + this.xflowDataNode.removeChild(this.getUserDefinedSystemNode()); //the old user defined system node + } + return; + } + + if(node && node._configured === undefined) + config.element(node); + if (!node || node._configured === undefined) + return null; + + var elemHandler = node._configured; + var key = this.factory.aspect + "_" + this.factory.canvasId; + var adapter = elemHandler.adapters[key]; + if (adapter == undefined){ + + adapter = this.factory.createAdapter(node); + + if (adapter) { + elemHandler.adapters[key] = adapter; + } + + var xflowDataNode = new DataNode(false); // Higher level dataNode to contain the actual Nodes + adapter.xflowDataNode = new DataNode(false); + adapter.xflowDataNode.addLoadListener(adapter.onXflowLoadEvent.bind(adapter)); + adapter.xflowDataNode.userData = adapter.node; + + // Setting platform and node type information for a data sequence + adapter.xflowDataNode.setPlatform(adapter.node.getAttribute("platform")); + + adapter.updateAdapterHandle("src", adapter.node.getAttribute("src")); + if(!adapter.assetData){ + adapter.xflowDataNode.setFilter(adapter.node.getAttribute("filter")); + updateCompute(adapter); + } + + for (var child = adapter.node.firstElementChild; child !== null; child = child.nextElementSibling) { + var subadapter = adapter.factory.getAdapter(child); + if (subadapter.getXflowNode){ + // adapter.xflowDataNode.appendChild(subadapter.getXflowNode()); + xflowDataNode.appendChild(subadapter.getXflowNode()); + } + } + adapter.xflowDataNode.appendChild(xflowDataNode); + + if (adapter.xflowDataNode._platform !== null) { + recursiveDataNodeAttrInit(adapter.getXflowNode()); + } + }else{ + adapter.xflowDataNode.removeChild(adapter.xflowDataNode._children[1]); + } + + if (this.xflowDataNode._children.length == 2){ + var oldUserSystemNode = this.getUserDefinedSystemNode(); + this.xflowDataNode.removeChild(oldUserSystemNode); + this.xflowDataNode.appendChild(adapter.xflowDataNode); //We add the new user defined system data node + oldUserSystemNode.appendChild(this.xflowDataNode); //We remove whole system data node from the old user defined node + }else{ + this.xflowDataNode.appendChild(adapter.xflowDataNode); + } + }; XML3DDataAdapter.prototype.setDefaultValues = function(){ @@ -61,19 +159,17 @@ XML3DDataAdapter.prototype.setDefaultValues = function(){ var inputNode = new InputNode(); inputNode.name="time"; inputNode.data = new BufferEntry(XC.DATA_TYPE.FLOAT, new Float32Array([0.0])); -// this.xflowDataNode.appendChild(inputNode); xflowDataNode.appendChild(inputNode); inputNode = new InputNode(); inputNode.name="test"; inputNode.data = new BufferEntry(XC.DATA_TYPE.FLOAT, new Float32Array([5.0])); -// this.xflowDataNode.appendChild(inputNode); xflowDataNode.appendChild(inputNode); this.xflowDataNode.appendChild(xflowDataNode); -} +}; XML3DDataAdapter.prototype.onXflowLoadEvent = function(node, newLevel, oldLevel){ if(newLevel == Infinity){ @@ -84,5 +180,40 @@ XML3DDataAdapter.prototype.onXflowLoadEvent = function(node, newLevel, oldLevel) } }; +function updateCompute(dataAdapter) { + var xflowNode = dataAdapter.xflowDataNode; + xflowNode.setCompute(dataAdapter.node.getAttribute("compute")); + if (xflowNode.computeDataflowUrl) { + dataAdapter.updateAdapterHandle("dataflow", xflowNode.computeDataflowUrl); + } + else { + dataAdapter.disconnectAdapterHandle("dataflow"); + updateLoadState(dataAdapter); + } +}; + +function updateLoadState(dataAdpater) { + var loading = false, handle; + + handle = dataAdpater.getConnectedAdapterHandle("src"); + if (handle && handle.status === AdapterHandle.STATUS.LOADING) { + loading = true; + } + + handle = dataAdpater.getConnectedAdapterHandle("dataflow"); + if (handle && handle.status === AdapterHandle.STATUS.LOADING) { + loading = true; + } + + for (var name in dataAdpater.externalScripts) { + handle = dataAdpater.getConnectedAdapterHandle(name); + if (handle && handle.status === AdapterHandle.STATUS.LOADING) { + loading = true; + } + } + + dataAdpater.xflowDataNode.setLoading(loading); +} + module.exports = XML3DDataAdapter; diff --git a/src/renderer/renderer/scene/scene.js b/src/renderer/renderer/scene/scene.js index e109e244..cb47a571 100644 --- a/src/renderer/renderer/scene/scene.js +++ b/src/renderer/renderer/scene/scene.js @@ -147,11 +147,8 @@ XML3D.createClass(Scene, EventEmitter, { }, setRendererIndependentData: function () { - //Set the time - var timeIndex = this.systemDataAdapter.xflowDataNode._children[0].getOutputNames().indexOf("time"); - var testIndex = this.systemDataAdapter.xflowDataNode._children[0].getOutputNames().indexOf("test"); - this.systemDataAdapter.xflowDataNode._children[0]._children[timeIndex].data.setValue(new Float32Array([performance.now()])); - this.systemDataAdapter.xflowDataNode._children[0]._children[testIndex].data.setValue(new Float32Array([10.0])); + this.systemDataAdapter.xflowDataNode._children[0]._children[0].data.setValue(new Float32Array([performance.now()])); + this.systemDataAdapter.xflowDataNode._children[0]._children[1].data.setValue(new Float32Array([10.0])); } }); diff --git a/src/renderer/webgl/scene/glscene.js b/src/renderer/webgl/scene/glscene.js index 270d5e58..d3024075 100644 --- a/src/renderer/webgl/scene/glscene.js +++ b/src/renderer/webgl/scene/glscene.js @@ -95,7 +95,6 @@ XML3D.extend(GLScene.prototype, { this.updateLightParameters(); this.lights.lightValueChanged(); } - this.updateUserDefinedSystemNode(); this.setRendererDependentData(); this.updateObjectsForRendering(); @@ -260,13 +259,7 @@ XML3D.extend(GLScene.prototype, { setRendererDependentData: function(){ }, - updateUserDefinedSystemNode:function (){ - var systemDataNodeURI = this.systemDataAdapter.node.getAttribute("sys"); - if (systemDataNodeURI){ - var systemDataNodeHandler = this.systemDataAdapter.getAdapterHandle(systemDataNodeURI); - this.systemDataAdapter.xflowDataNode._children[1]._sourceNode = systemDataNodeHandler.adapter.xflowDataNode; - } - } + }); module.exports = GLScene; diff --git a/tests/scenes/scripts/animateColor.js b/tests/scenes/scripts/animateColor.js index c8fb73f0..c1f6ab89 100644 --- a/tests/scenes/scripts/animateColor.js +++ b/tests/scenes/scripts/animateColor.js @@ -6,7 +6,7 @@ Xflow.registerOperator("xflow.animateColor", { var d = new Date(); diffuseColor[0] = Math.cos(time[0]/500); diffuseColor[1] = Math.sin(time[0]/500); - diffuseColor[2] = 0; + diffuseColor[2] = Math.tan(time[0]/500); return diffuseColor; } }); diff --git a/tests/scenes/system-parameters.html b/tests/scenes/system-parameters.html index b84aa7c1..3dde2784 100644 --- a/tests/scenes/system-parameters.html +++ b/tests/scenes/system-parameters.html @@ -13,9 +13,12 @@ - 0 + 785.398163397 + + 392.699081699 + 0 1 0 @@ -39,7 +42,7 @@ -1.0 0.0 -0.0 -1.0 0.0 -0.0 -1.0 0.0 -0.0 -1.0 0.0 -0.0 0.0 1.0 -0.0 0.0 1.0 -0.0 0.0 1.0 -0.0 0.0 1.0 -0.0 1.0 0.0 -0.0 1.0 0.0 -0.0 1.0 0.0 -0.0 1.0 0.0 -0.0 0.0 -1.0 -0.0 0.0 -1.0 -0.0 0.0 -1.0 -0.0 0.0 -1.0 -0.0 -0.0 0.0 -1.0 -0.0 0.0 -1.0 -0.0 0.0 -1.0 -0.0 0.0 -1.0 -0.0 0.0 1.0 -0.0 0.0 1.0 -0.0 0.0 1.0 -0.0 0.0 1.0 0 1 2 2 3 0 4 5 6 6 7 4 8 9 10 10 11 8 12 13 14 14 15 12 16 17 18 18 19 16 20 21 22 22 23 20 - 500 + 0