diff --git a/js/main.js b/js/main.js index c88cfa2ac24c3a2bfbfb100df456ce051476f364..1e5b3f559597938512b8430cfe12ed1f4ddd69da 100644 --- a/js/main.js +++ b/js/main.js @@ -10,15 +10,12 @@ $(function(){ //init threeJS and geometry models window.three = new ThreeModel(); - window.lattice = new OctaFaceLattice(); //setup ui - var appState = new AppState({lattice:window.lattice}); - new MenuWrapper({lattice:window.lattice, model:appState}); - new NavBar({model:appState}); + window.appState = new AppState(); + var highlighter = new Highlighter({model:window.appState.get("lattice")}); + new NavBar({model:window.appState}); //threeJS View - new ThreeView({model:window.three, lattice:window.lattice, appState:appState}); - - window.lattice.addCellAtIndex({x:0,y:0,z:0}); + new ThreeView({model:window.three, appState:window.appState, highlighter:highlighter}); }); diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js index f319c579ac01b2d18bf74ddeff21a64c2ce5edb5..38081fbef7e40a8eaa01a200cef6a33a237b74d2 100644 --- a/js/menus/LatticeMenuView.js +++ b/js/menus/LatticeMenuView.js @@ -23,7 +23,7 @@ LatticeMenuView = Backbone.View.extend({ _.bindAll(this, "render"); this.listenTo(this.model, "change", function(){ - if(!this.model.hasChanged('cellMode') && !(this.model.hasChanged('scale'))){//ignore cell mode changes + if(!(this.model.hasChanged('scale'))){//ignore scale mode changes this.render(); }; }); diff --git a/js/models/AppState.js b/js/models/AppState.js index e42ff88f5f70fec0da1f76857492e2aafc43896e..fa93c457344e70bd0c5358062158054652404c44 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -3,7 +3,7 @@ */ //a class to store global app state, model for navbar and menu wrapper -//maybe other things eventually +//never deallocated AppState = Backbone.Model.extend({ @@ -16,6 +16,10 @@ AppState = Backbone.Model.extend({ lastSimulationTab: "physics", lastAssembleTab: "assembler", + lattice: null, + menuWrapper: null, + cellMode: "cell", + menuIsVisible: true, //key bindings @@ -24,7 +28,7 @@ AppState = Backbone.Model.extend({ extrudeMode: false }, - initialize: function(options){ + initialize: function(){ _.bindAll(this, "_handleKeyStroke"); @@ -33,11 +37,20 @@ AppState = Backbone.Model.extend({ $(document).bind('keyup', {state:false}, this._handleKeyStroke); this.listenTo(this, "change:currentTab", this._storeTab); this.listenTo(this, "change:currentTab", this._updateLatticeMode); + this.listenTo(this, "change:cellMode", this._cellModeDidChange); this.listenTo(this, "change:currentNav", this._updateCurrentTabForNav); + this.listenTo(this, "change:lattice", this._renderNewLattice); - this.lattice = options.lattice; + this.set("lattice", new OctaFaceLattice({appState:this})); + this.get("lattice").addCellAtIndex({x:0,y:0,z:0}); }, + + /////////////////////////////////////////////////////////////////////////////// + /////////////////////EVENTS//////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////////// + + _storeTab: function(){ var currentNav = this.get("currentNav"); var currentTab = this.get("currentTab"); @@ -48,10 +61,10 @@ AppState = Backbone.Model.extend({ _updateLatticeMode: function(){ var currentTab = this.get("currentTab"); - if (currentTab == "lattice") this.lattice.set("cellMode", "cell"); - else if (currentTab == "import") this.lattice.set("cellMode", "cell"); - else if (currentTab == "sketch") this.lattice.set("cellMode", "cell"); - else if (currentTab == "part") this.lattice.set("cellMode", "part"); + if (currentTab == "lattice") this.set("cellMode", "cell"); + else if (currentTab == "import") this.set("cellMode", "cell"); + else if (currentTab == "sketch") this.set("cellMode", "cell"); + else if (currentTab == "part") this.set("cellMode", "part"); }, //update to last tab open in that section @@ -66,6 +79,14 @@ AppState = Backbone.Model.extend({ this._updateLatticeMode();//a little bit hacky, this updates the lattice, but holds off on updating the menus til the animation has happened }, + _cellModeDidChange: function(){ + this.get("lattice").cellModeDidChange(this.get("cellMode")); + }, + + _renderNewLattice: function(){ + this.set("menuWrapper", new MenuWrapper({lattice:this.get("lattice"), model:this})); + }, + /////////////////////////////////////////////////////////////////////////////// /////////////////////KEY BINDINGS////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// @@ -82,7 +103,7 @@ AppState = Backbone.Model.extend({ break; case 32://space bar e.preventDefault(); - if (this.lattice.get("cellMode") == "cell") this.set("deleteMode", state);//only for cell mode + if (this.get("cellMode") == "cell") this.set("deleteMode", state);//only for cell mode else this.set("deleteMode", false); break; case 69://e @@ -90,10 +111,10 @@ AppState = Backbone.Model.extend({ this.set("extrudeMode", state); break; case 76://l lattice mode - this.lattice.set("cellMode", "cell"); + this.set("cellMode", "cell"); break; case 80://p part mode - this.lattice.set("cellMode", "part"); + this.set("cellMode", "part"); break; default: break; diff --git a/js/models/Lattice.js b/js/models/Lattice.js index f238b009ba0eac8e0145330aa2c4db66d8860cd0..49ba5c9a3c18f42a3d3a9e111ea392812ca2c5b6 100644 --- a/js/models/Lattice.js +++ b/js/models/Lattice.js @@ -11,13 +11,14 @@ Lattice = Backbone.Model.extend({ connectionType: "face", allCellTypes: {octa:"Octahedron", cube:"Cube"}, allConnectionTypes: { - octa: {face:"Face", edge:"Edge", vertex:"Vertex"}, + octa: {face:"Face", edge:"Edge", edgeRot:"Rotated Edge", vertex:"Vertex"}, cube: {face:"Face"} }, allPartTypes:{ octa:{ face: {triangle:"Triangle"}, edge: {triangle:"Triangle"}, + edgeRot: {triangle:"Triangle"}, vertex:{square:"Square", xShape:"X"} }, cube:{ @@ -31,19 +32,18 @@ Lattice = Backbone.Model.extend({ cellsMax: {x:0, y:0, z:0},//max position of cells matrix numCells: 0, partType: "triangle", - cellMode: "cell", basePlane: null//plane to build from }, //pass in fillGeometry - initialize: function(){ + initialize: function(options){ //bind events - this.listenTo(this, "change:cellMode", this._cellModeDidChange); this.listenTo(this, "change:scale", this._scaleDidChange); this.listenTo(this, "change:cellType change:connectionType", this._changeLatticeStructure); + this.appState = options.appState; this._initialize();//call subclass init }, @@ -273,8 +273,7 @@ Lattice = Backbone.Model.extend({ ////////////////////////////////////EVENTS////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// - _cellModeDidChange: function(){ - var mode = this.get("cellMode"); + cellModeDidChange: function(mode){ this._iterCells(this.get("cells"), function(cell){ if (cell && cell.drawForMode) cell.drawForMode(mode); }); @@ -312,6 +311,10 @@ Lattice = Backbone.Model.extend({ }); }); + }, + + getScale: function(){ + return this.get("scale"); } }); @@ -335,10 +338,6 @@ OctaFaceLattice = Lattice.extend({ this.set("columnSeparation", 0.0); }, - getScale: function(){ - return this.get("scale")*(1.0+2*this.get("columnSeparation")); - }, - _changeColSeparation: function(){ var colSep = this.get("columnSeparation"); var scale = this.get("scale"); @@ -366,8 +365,12 @@ OctaFaceLattice = Lattice.extend({ this.addCellAtIndex(position); }, + getScale: function(){ + return this.get("scale")*(1.0+2*this.get("columnSeparation")); + }, + _makeCellForLatticeType: function(indices, scale){ - return new DMASideOctaCell(this.get("cellMode"), indices, scale, this); + return new DMASideOctaCell(this.appState.get("cellMode"), indices, scale, this); } }); diff --git a/js/threeViews/ThreeView.js b/js/threeViews/ThreeView.js index 737c6f77069d3f66022dbc8f22ed985fa44a81fe..8480e98ac46095cb56fa50889a8c52c1ab8e7e83 100644 --- a/js/threeViews/ThreeView.js +++ b/js/threeViews/ThreeView.js @@ -25,7 +25,6 @@ ThreeView = Backbone.View.extend({ initialize: function(options){ - this.lattice = options.lattice; this.appState = options.appState; _.bindAll(this, "_animate", "_mouseMoved"); @@ -39,7 +38,7 @@ ThreeView = Backbone.View.extend({ this.$el.append(this.model.domElement);//render only once //init highlighter - this.highlighter = new Highlighter({model:options.lattice}); + this.highlighter = options.highlighter; this.model.render(); this._animate(); @@ -100,7 +99,7 @@ ThreeView = Backbone.View.extend({ } this._handleCellIntersections(cellIntersections[0]); - if (this.lattice.get("cellMode") == "part"){//additionally check for part intersections in part mode + if (this.appState.get("cellMode") == "part"){//additionally check for part intersections in part mode var partIntersections = this.mouseProjection.intersectObjects(this.model.parts, false); if (partIntersections.length == 0) { this._setNoPartIntersections(); diff --git a/main.html b/main.html index 6a01592599c82c970ae40e8ddb193352fadf8789..c441abe2b2b92c2d78aa0f6582ae17d59eb80bf3 100644 --- a/main.html +++ b/main.html @@ -51,12 +51,12 @@ <script src="js/fea/DmaNode.js"></script> <!--models--> - <script src="js/models/AppState.js"></script> - <script src="js/models/Lattice.js"></script> <script src="js/models/ThreeModel.js"></script> + <script src="js/models/Lattice.js"></script> <script src="js/models/FillGeometry.js"></script> <script src="js/models/BasePlane.js"></script> <script src="js/models/extrudeVisualizer.js"></script> + <script src="js/models/AppState.js"></script> <!--views--> <script src="js/menus/MenuWrapperView.js"></script>