diff --git a/js/API/LatticeAPI.js b/js/API/LatticeAPI.js index ff9b219e2f210e6e732711f5e0ef3a1339340d60..eb4c9b454b03caba10d1230dbbbfd1721e502e13 100644 --- a/js/API/LatticeAPI.js +++ b/js/API/LatticeAPI.js @@ -3,12 +3,24 @@ */ -define(['lattice', 'plist'], function(lattice, plist){ +define(['lattice', 'plist'], function(lattice){ return { //getters + getUnits: function(){ + return lattice.getUnits(); + }, + + getScale: function(){ + return lattice.getScale(); + }, + + getNumCells: function(){ + return lattice.getNumCells(); + }, + getSize: function(){ return lattice.getSize(); }, @@ -41,27 +53,27 @@ define(['lattice', 'plist'], function(lattice, plist){ //setters setAspectRatio: function(x, y, z){ - lattice.setAspectRatio(x, y, z); + lattice.setAspectRatio({x:x, y:y, z:z}, false); }, setCellType: function(cellType){ - lattice.setCellType(cellType); + lattice.setCellType(cellType, false); }, setConnectionType: function(connectionType){ - lattice.setConnectionType(connectionType); + lattice.setConnectionType(connectionType, false); }, setApplicationType: function(applicationType){ - lattice.setApplicationType(applicationType); + lattice.setApplicationType(applicationType, false); }, setPartType: function(partType){ - lattice.setPartType(partType); + lattice.setPartType(partType, false); }, - setLatticeType: function(cellType, connectionType, applicationType, partType){ - lattice.setLatticeType(cellType, connectionType, applicationType, partType); + set: function(data){ + lattice.setLatticeMetaData(data); }, diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index ccca998de4d16aa83a798005f45bf2e13bd1db69..7c2d186ac9cd3566dab8267ac27967c8f8983b46 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -60,14 +60,133 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'materialsPlis + //getters + + getUnits: function(){ + return this.get("units"); + }, + + getScale: function(){ + return this.get("scale"); + }, + + getAspectRatio: function(){ + return this.get("aspectRatio").clone(); + }, + + getCellType: function(){ + return this.get("cellType"); + }, + + getConnectionType: function(){ + return this.get("connectionType"); + }, + + getApplicationType: function(){ + return this.get("applicationType"); + }, + + getPartType: function(){ + return this.get("partType"); + }, + + + + //setters + + + setProperty: function(property, value, silent){ + var changed = this.get(property) != value; + if (silent !== true) silent = false; + this.set(property, value, {silent:silent}); + return changed; + }, + + _getSetterName: function(property){ + return "set" + property.charAt(0).toUpperCase() + property.slice(1); + }, + + setAspectRatio: function(aspectRatio, silent){ + if (!aspectRatio.x || !aspectRatio.y || !aspectRatio.z || aspectRatio.x<0 || aspectRatio.y<0 || aspectRatio.z<0) {//no 0, undefined, null, or neg #'s + console.warn("invalid aspect ratio params"); + return; + } + return this.setProperty("aspectRatio", new THREE.Vector3(aspectRatio.x, aspectRatio.y, aspectRatio.z), silent); + }, + + setCellType: function(cellType, silent){ + if (plist.allLattices[cellType] === undefined){ + console.warn("no cell type " + cellType); + return; + } + return this.setProperty("cellType", cellType, silent); + }, + + setConnectionType: function(connectionType, silent){ + var cellType = this.get("cellType"); + var plistCellData = plist.allLattices[cellType]; + if (plistCellData[connectionType] === undefined){ + console.warn("no connection type " + connectionType + " for cell type " + plistCellData.name); + return; + } + return this.setProperty("connectionType", connectionType, silent); + }, + + setApplicationType: function(applicationType, silent){ + var cellType = this.get("cellType"); + var plistCellData = plist.allLattices[cellType]; + var connectionType = this.get("connectionType"); + var plistConnectionData = plistCellData[connectionType]; + if (plistConnectionData[applicationType] === undefined){ + console.warn("no application type " + applicationType + " for cell type " + plistCellData.name + " and connection type " + plistConnectionData.name); + return; + } + return this.setProperty("applicationType", applicationType, silent); + }, + + setPartType: function(partType, silent){ + var cellType = this.get("cellType"); + var plistCellData = plist.allLattices[cellType]; + var connectionType = this.get("connectionType"); + var plistConnectionData = plistCellData[connectionType]; + var applicationType = this.get("applicationType"); + var plistAppData = plistConnectionData[applicationType]; + if (plistConnectionData[applicationType] === undefined){ + console.warn("no part type " + partType + " for cell type " + plistCellData.name + " and connection type " + plistConnectionData.name + " and application type " + plistAppData.name); + return; + } + return this.setProperty("partType", partType, silent); + }, + + setLatticeMetaData: function(data){ + if (!data) { + console.warn("no data received"); + return; + } + var changed = false; + var self = this; + _.each(data, function(val, key){ + if (self[self._getSetterName(key)]) { + changed |= self[self._getSetterName(key)](val, true); + } + else console.warn("no setter found for param " + key); + }); + if (changed){ + //todo trigger event + } + }, + + + + //latticeType _cellTypeChanged: function(){ - var cellType = this.get("cellType"); - if (plist.allLattices[cellType].connection[this.get("connectionType")] === undefined){ + var cellType = this.getCellType(); + if (plist.allLattices[cellType].connection[this.getConnectionType()] === undefined){ var connectionType = _.keys(plist.allLattices[cellType].connection)[0]; this.set("connectionType", connectionType, {silent:true}); } diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index bf91f278ed540c9213b95a665e2c8703d66dc413..0107a95f2d8482e6b3f4cafd633df6c6f8ac80c4 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -45,90 +45,15 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre return this.calculateBoundingBox(); }, - getAspectRatio: function(){ - return this.get("aspectRatio").clone(); - }, - - getCellType: function(){ - return this.get("cellType"); - }, - - getConnectionType: function(){ - return this.get("connectionType"); - }, - - getApplicationType: function(){ - return this.get("applicationType"); - }, - getPartType: function(){ - return this.get("partType"); + getNumCells: function(){ + return this.get("numCells"); }, - //setters - setAspectRatio: function(aspectRatio){ - if (!aspectRatio.x || !aspectRatio.y || !aspectRatio.z || aspectRatio.x<0 || aspectRatio.y<0 || aspectRatio.z<0) {//no 0, undefined, null, or neg #'s - console.warn("invalid aspect ratio params"); - return; - } - this.set("aspectRatio", new THREE.Vector3(aspectRatio.x, aspectRatio.y, aspectRatio.z)); - }, - - setCellType: function(cellType){ - if (plist.allLattices[cellType] === undefined){ - console.warn("no cell type " + cellType); - return; - } - this.set("cellType", cellType); - }, - - setConnectionType: function(connectionType){ - var cellType = this.get("cellType"); - var plistCellData = plist.allLattices[cellType]; - if (plistCellData[connectionType] === undefined){ - console.warn("no connection type " + connectionType + " for cell type " + plistCellData.name); - return; - } - this.set("connectionType", connectionType); - }, - - setApplicationType: function(applicationType){ - var cellType = this.get("cellType"); - var plistCellData = plist.allLattices[cellType]; - var connectionType = this.get("connectionType"); - var plistConnectionData = plistCellData[connectionType]; - if (plistConnectionData[applicationType] === undefined){ - console.warn("no application type " + applicationType + " for cell type " + plistCellData.name + " and connection type " + plistConnectionData.name); - return; - } - this.set("applicationType", applicationType); - }, - - setPartType: function(partType){ - var cellType = this.get("cellType"); - var plistCellData = plist.allLattices[cellType]; - var connectionType = this.get("connectionType"); - var plistConnectionData = plistCellData[connectionType]; - var applicationType = this.get("applicationType"); - var plistAppData = plistConnectionData[applicationType]; - if (plistConnectionData[applicationType] === undefined){ - console.warn("no part type " + partType + " for cell type " + plistCellData.name + " and connection type " + plistConnectionData.name + " and application type " + plistAppData.name); - return; - } - this.set("partType", partType); - }, - - setLatticeType: function(cellType, connectionType, applicationType, partType){ - //todo check if this causes too many renders - if (cellType) this.setCellType(cellType); - if (connectionType) this.setConnectionType(connectionType); - if (applicationType) this.setApplicationType(applicationType); - if (partType) this.setPartType(partType); - },