diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index 923e05a0aa36ff651809a3fb761753a183b98244..e8bef73ae1995a9cafc4106e919ac6e8478ea5cb 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -13,7 +13,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'], if (json.index) this.index = new THREE.Vector3(json.index.x, json.index.y, json.index.z); if (superCell) this.superCell = superCell; - this.material = json.material || lattice.get("materialType"); + this.material = json.material || appState.get("materialType"); //object 3d is parent to all 3d elements owned by cell: cell mesh and wireframe, parts, beams, nodes, etc this.object3D = this._buildObject3D(); @@ -223,7 +223,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'], DMACell.prototype.getMaterial = function(returnTHREEObject){ if (!this.material) return null; - var materialClass = lattice.get("materialClass"); + var materialClass = appState.get("materialClass"); if (!globals.materials[materialClass]) { console.warn("no material class found of type " + materialClass); return null; diff --git a/js/highlighter/Highlighter.js b/js/highlighter/Highlighter.js index 7d745f1f7ff379b60f5f9e80d82097437e515a96..20e2f807399407c412af953d8a332b01c61a3525 100644 --- a/js/highlighter/Highlighter.js +++ b/js/highlighter/Highlighter.js @@ -157,7 +157,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' addRemoveVoxel: function(shouldAdd){ if (shouldAdd){ if (!this.isVisible() || !this.highlightedObject) return; - lattice.getUItarget().addCellAtIndex(this._getNextCellPosition(), false, false, lattice.get("materialType")); + lattice.getUItarget().addCellAtIndex(this._getNextCellPosition(), false, false, appState.get("materialType")); } else { if (!this.highlightedObject) return; if (!(this.highlightedObject instanceof DMACell)) return; diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index c4cff5e7022ca13f43b2b8e2c8f09b81a305c8d0..56a67d180c30cfbc70a61293a94466ffe5317b4b 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -15,8 +15,6 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre cellType: "cube", connectionType: "face", partType: null, - materialType: null, - materialClass: "mechanical", nodes: [], @@ -33,9 +31,6 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre }); this.listenTo(this, "change:cellSeparation", this._updateCellSeparation); - this.listenTo(this, "change:materialType", this._materialTypeChanged); - this.listenTo(this, "change:materialClass", this._loadMaterialClass); - this.listenTo(appState, "change:currentNav", this._navChanged); }, @@ -60,7 +55,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre } var partType = _.keys(plist["allPartTypes"][newCellType][newConnectionType])[0]; this.set("partType", partType, {silent:true}); - this.set("materialClass", plist.allMaterialTypes[newCellType][newConnectionType], {silent:true}); + appState.set("materialClass", plist.allMaterialTypes[newCellType][newConnectionType], {silent:true}); }, _setDefaultCellMode: function(){//if no part associated with this lattice type @@ -111,36 +106,6 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre //events - _materialTypeChanged: function(){ - var materialType = this.get("materialType"); - //verify that correct class is in sync - if (materialType.substr(0,5) != "super") { - if (this.previous("materialType").substr(0,5) != "super") return; - //re init highlighter - require([this.getHighlighterFile()], function(HighlighterClass){ - globals.highlighter = new HighlighterClass(); - }); - return; - } - - //composite material - appState.set("superCellRange", globals.materials.compositeMaterials[materialType].dimensions.clone()); - appState.set("superCellIndex", new THREE.Vector3(0,0,0)); - require(['superCellHighlighter'], function(SuperCellHighlighter){ - globals.highlighter = new SuperCellHighlighter(); - }); - }, - - _loadMaterialClass: function(){ - var materialClass = this.get("materialClass"); - var materialType = this.get("materialType"); - if (!plist.allMaterials[materialClass].materialType) this.set("materialType", _.keys(plist.allMaterials[materialClass])[0], {silent:true});//set to default silently - if (globals.materials[materialClass]) return;//already loaded - require([materialClass + "Materials"], function(MaterialClass){ - globals.materials[materialClass] = MaterialClass; - }); - }, - showCellAtIndex: function(index){ var latticeIndex = (new THREE.Vector3()).subVectors(index, this.get("cellsMin"));//index is probably a json object from gcode comment var cell = this.cells[latticeIndex.x][latticeIndex.y][latticeIndex.z]; diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index 3310bd191046be4063bcdb499ef730a1810d03e6..0bb1395a4e34017d87b4e195e5f19853061ee358 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -50,7 +50,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre } if (this._setDefaultCellMode) this._setDefaultCellMode(); - if (this._loadMaterialClass) this._loadMaterialClass(); + appState._loadMaterialClass(); var cellsMin = this.get("cellsMin"); var cellsMax = this.get("cellsMax"); diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js index bdb4f2bc9c0b735f90a33e11256933f834d17bf1..b65c5baed350ab958e40dcdc1699e820500525cd 100644 --- a/js/materials/DMAMaterials.js +++ b/js/materials/DMAMaterials.js @@ -3,7 +3,46 @@ */ -define(['underscore', 'three', 'appState'], function(_, THREE, appState){ +define(['underscore', 'three', 'appState', 'globals'], function(_, THREE, appState, globals){ + + var materials = { + deleteMaterial: { + color: "#ff0000", + threeMaterial:new THREE.MeshLambertMaterial({color:"#ff0000", shading:THREE.FlatShading}) + } + }; + + _.extend(materials, Backbone.Events); + + materials.listenTo(appState, "change:realisticColorScheme", changeColorScheme); + materials.listenTo(appState, "change:materialClass", loadMaterialClass); + + globals.materials = materials; + + function loadMaterialClass(){ + var materialClass = appState.get("materialClass"); + var materialType = appState.get("materialType"); + if (!plist.allMaterials[materialClass].materialType) this.set("materialType", _.keys(plist.allMaterials[materialClass])[0], {silent:true});//set to default silently + if (globals.materials[materialClass]) return;//already loaded + require([materialClass + "Materials"], function(MaterialClass){ + globals.materials[materialClass] = MaterialClass; + }); + } + + function changeColorScheme(){ + var state = appState.get("realisticColorScheme"); + _.each(_.keys(materials), function(name){ + var materialInfo = materials[name]; + + var color = materialInfo.color; + if (!color) console.warn("no color for material type " + name); + if (!state && materialInfo.altColor) color = materialInfo.altColor; + + if (materialInfo.threeMaterial) materialInfo.threeMaterial.color = new THREE.Color(color); + else materialInfo.threeMaterial = new THREE.MeshLambertMaterial({color:color, shading:THREE.FlatShading}); + }); + } + function DMAMaterials(materialList){ this.materials = {}; @@ -34,5 +73,5 @@ define(['underscore', 'three', 'appState'], function(_, THREE, appState){ }); }; - return DMAMaterials; + return materials; }); \ No newline at end of file diff --git a/js/menus/CompositeMenuView.js b/js/menus/CompositeMenuView.js index 025eba66a2be64018474e348b4d20d13e3527a2f..c753fc3c2f364d6ce669607335069057d63eecaa 100644 --- a/js/menus/CompositeMenuView.js +++ b/js/menus/CompositeMenuView.js @@ -80,8 +80,6 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals'], fu _makeTemplateJSON: function(){ return _.extend(this.model.toJSON(), plist, globals, lattice.compositeEditor.toJSON(), { - materialClass:lattice.get("materialClass"), - materialType:lattice.get("materialType"), dimensions: dimensions }); }, diff --git a/js/models/AppState.js b/js/models/AppState.js index c54e1207491421f66e7ea89a34eaa0aba93517a0..a1f5d8d3bbd3176c093bb65089cefc838700aaa9 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -5,7 +5,7 @@ //a class to store global app state, model for navbar and menu wrapper //never deallocated -define(['underscore', 'backbone', 'threeModel', 'three', 'plist'], function(_, Backbone, three, THREE, plist){ +define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], function(_, Backbone, three, THREE, plist, globals){ var AppState = Backbone.Model.extend({ @@ -37,6 +37,8 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist'], function(_, B superCellRange: new THREE.Vector3(1,1,1), realisticColorScheme: false, + materialType: null, + materialClass: "mechanical", stockSimulationPlaying: false, manualSelectOrigin: false//mode that allows user ot select origin from existing cell @@ -56,6 +58,7 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist'], function(_, B this.listenTo(this, "change:currentTab", this._tabChanged); this.listenTo(this, "change:currentNav", this._navChanged); this.listenTo(this, "change:realisticColorScheme", this._updateColorScheme); + this.listenTo(this, "change:materialType", this._materialTypeChanged); this.downKeys = {};//track keypresses to prevent repeat keystrokeson hold @@ -114,6 +117,26 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist'], function(_, B }); }, + _materialTypeChanged: function(){ + var materialType = this.get("materialType"); + //verify that correct class is in sync + if (materialType.substr(0,5) != "super") { + if (this.previous("materialType").substr(0,5) != "super") return; + //re init highlighter + require([this.getHighlighterFile()], function(HighlighterClass){ + globals.highlighter = new HighlighterClass(); + }); + return; + } + + //composite material + this.set("superCellRange", globals.materials.compositeMaterials[materialType].dimensions.clone()); + this.set("superCellIndex", new THREE.Vector3(0,0,0)); + require(['superCellHighlighter'], function(SuperCellHighlighter){ + globals.highlighter = new SuperCellHighlighter(); + }); + }, + /////////////////////////////////////////////////////////////////////////////// /////////////////////KEY BINDINGS////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// diff --git a/js/models/Globals.js b/js/models/Globals.js index a60ec65aa6fcc901ddd4ace8e5bdbd4a873c702f..8d73d057cbcf2f65630709252906d8226e7edc5f 100644 --- a/js/models/Globals.js +++ b/js/models/Globals.js @@ -4,16 +4,11 @@ //globals namespace, not sure if there's a way to get around this -define(['underscore', 'three'], function(_, THREE){ - - var materials = { - deleteMaterial: new THREE.MeshLambertMaterial({color:"#ff0000", shading:THREE.FlatShading}), - compositeMaterials: {} - }; +define([], function(){ return { baseplane: null, highlighter: null, - materials: materials + materials: null }; }); \ No newline at end of file