Skip to content

Commit

Permalink
Add userDefindeSystemNode to XML3D data adapter. Not Working
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ariyapour committed Nov 9, 2015
1 parent 0ce2130 commit ee24812
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 63 deletions.
41 changes: 15 additions & 26 deletions src/data/adapter/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,7 +114,7 @@ function recursiveDataAdapterConstruction(adapter, systemDataAdapter) {
</data>
</data>
*/
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) {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/data/adapter/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
Expand Down
167 changes: 149 additions & 18 deletions src/data/adapter/xml3d.js
Original file line number Diff line number Diff line change
@@ -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
/**
Expand All @@ -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(){
/**
<data>
<data src="#sys">
Expand All @@ -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(){
Expand All @@ -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){
Expand All @@ -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;
7 changes: 2 additions & 5 deletions src/renderer/renderer/scene/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]));

}
});
Expand Down
9 changes: 1 addition & 8 deletions src/renderer/webgl/scene/glscene.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ XML3D.extend(GLScene.prototype, {
this.updateLightParameters();
this.lights.lightValueChanged();
}
this.updateUserDefinedSystemNode();
this.setRendererDependentData();
this.updateObjectsForRendering();

Expand Down Expand Up @@ -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;

2 changes: 1 addition & 1 deletion tests/scenes/scripts/animateColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Expand Down
Loading

0 comments on commit ee24812

Please sign in to comment.