From 4c8b2961d128c39ea194f26a6982d9c8199d7403 Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Tue, 3 Mar 2015 15:43:45 -0500 Subject: [PATCH] moving properties into lattice from appstate --- js/fea/DmaCell.js | 6 +++-- js/main.js | 1 + js/menus/LatticeMenuView.js | 45 ++++++++++++++++++------------------- js/menus/MenuWrapperView.js | 4 ++-- js/models/AppState.js | 28 ++++------------------- js/models/Lattice.js | 35 ++++++++++++++++++++--------- 6 files changed, 57 insertions(+), 62 deletions(-) diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js index 9fc15150..16dfd171 100644 --- a/js/fea/DmaCell.js +++ b/js/fea/DmaCell.js @@ -5,9 +5,11 @@ //a Cell, a unit piece of the lattice -function DMACell(indices, scale, lattice) { +function DMACell(indices, scale, lattice, inverse) { this.indices = indices; + if (!inverse) inverse = false; + this.isInverse = inverse; this.lattice = lattice;//need ref back to lattice this.cellMesh = this._buildCellMesh(indices.z); this.parts = this._initParts(indices.z); @@ -160,7 +162,7 @@ DMACell.prototype.destroy = function(){ unitTetraCellGeo.applyMatrix(new THREE.Matrix4().makeTranslation(0,0,Math.sqrt(3/8)-1/Math.sqrt(6))); function DMATetraCell(indices, scale, lattice){ - DMACell.call(this, indices, scale, lattice); + DMACell.call(this, indices, scale, lattice, true); } DMATetraCell.prototype = Object.create(DMACell.prototype); diff --git a/js/main.js b/js/main.js index 3e6e6889..a6ae2c61 100644 --- a/js/main.js +++ b/js/main.js @@ -16,6 +16,7 @@ $(function(){ dmaGlobals.three = new ThreeModel(); dmaGlobals.lattice = new Lattice(); + dmaGlobals.lattice._updateLatticeType();//todo get rid of this dmaGlobals.appState = new AppState({lattice:dmaGlobals.lattice}); diff --git a/js/menus/LatticeMenuView.js b/js/menus/LatticeMenuView.js index 66cf2992..1a6cbbe4 100644 --- a/js/menus/LatticeMenuView.js +++ b/js/menus/LatticeMenuView.js @@ -24,15 +24,15 @@ LatticeMenuView = Backbone.View.extend({ this.lattice = options.lattice; _.bindAll(this, "render"); - var self = this; - this.listenTo(this.model, "change", function(){ - var change = false; - _.each(_.keys(self.model.changedAttributes()), function(attribute){ - if (attribute != "currentNav" && attribute != "currentTab") change = true; - }); - if (change) this.render(); - }); - this.listenTo(this.lattice, "change", this.render()); +// var self = this; +// this.listenTo(this.model, "change", function(){ +// var change = false; +// _.each(_.keys(self.model.changedAttributes()), function(attribute){ +// if (attribute != "currentNav" && attribute != "currentTab") change = true; +// }); +// if (change) this.render(); +// }); + this.listenTo(this.lattice, "change", this.render); }, _clearCells: function(e){ @@ -49,7 +49,6 @@ LatticeMenuView = Backbone.View.extend({ _sliderDidSlide: function(e){ var scale = $(e.target)[0].value; -// this.lattice.set("scale", $(e.target)[0].value); this.lattice.previewScaleChange(scale);//does not trigger lattice change event - no rerendering of ui $("#latticeScale").val(scale); dmaGlobals.three.render(); @@ -64,43 +63,43 @@ LatticeMenuView = Backbone.View.extend({ var cellType = $(e.target).data("type"); //reset everything to defaults silently - if (cellType != this.model.get("cellType")){ + if (cellType != this.lattice.get("cellType")){ this._setAppStateToDefaultsSilently(cellType); } - this.model.set("cellType", cellType); + this.lattice.set("cellType", cellType); }, _changeConnectionType: function(e){ e.preventDefault(); var connectionType = $(e.target).data("type"); - if (connectionType != this.model.get("connectionType")){ - this._setAppStateToDefaultsSilently(this.model.get("cellType"), connectionType); + if (connectionType != this.lattice.get("connectionType")){ + this._setAppStateToDefaultsSilently(this.lattice.get("cellType"), connectionType); } - this.model.set("connectionType", connectionType); + this.lattice.set("connectionType", connectionType); }, _setAppStateToDefaultsSilently: function(newCellType, newConnectionType){ if (newCellType == "cube") { if (!newConnectionType){ newConnectionType = "face"; - this.model.set("connectionType", newConnectionType, {silent:true}); + this.lattice.set("connectionType", newConnectionType, {silent:true}); } this.model.set("connectionType", newConnectionType, {silent:true}); if (newConnectionType == "face"){ - this.model.set("partType", null, {silent:true}); + this.lattice.set("partType", null, {silent:true}); } } else if (newCellType == "octa") { if (!newConnectionType){ newConnectionType = "face"; - this.model.set("connectionType", newConnectionType, {silent:true}); + this.lattice.set("connectionType", newConnectionType, {silent:true}); } if (newConnectionType == "face"){ - this.model.set("partType", "triangle", {silent:true}); + this.lattice.set("partType", "triangle", {silent:true}); } else if (newConnectionType == "edge"){ - this.model.set("partType", "triangle", {silent:true}); + this.lattice.set("partType", "triangle", {silent:true}); } else if (newConnectionType == "vertex"){ - this.model.set("partType", "square", {silent:true}); + this.lattice.set("partType", "square", {silent:true}); } } }, @@ -110,7 +109,7 @@ LatticeMenuView = Backbone.View.extend({ }, _showInverseCells: function(e){ - this.lattice.set("shouldShowInverseCells", $(e.target).prop("checked")); + this.lattice.set("inverseMode", $(e.target).prop("checked")); }, render: function(){ @@ -148,7 +147,7 @@ LatticeMenuView = Backbone.View.extend({ Preserve cells on lattice change\ </label><br/><br/>\ <label class="checkbox">\ - <input type="checkbox" <% if (shouldShowInverseCells) { %> checked="checked" <% } %> value="" id="showInverse" data-toggle="checkbox" class="custom-checkbox"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\ + <input type="checkbox" <% if (inverseMode) { %> checked="checked" <% } %> value="" id="showInverse" data-toggle="checkbox" class="custom-checkbox"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\ Show Inverse Geometry\ </label><br/><br/>\ Scale: <input id="scaleSlider" data-slider-id="ex1Slider" type="text" data-slider-min="1" data-slider-max="100" data-slider-step="0.1" data-slider-value="<%= scale %>"/>\ diff --git a/js/menus/MenuWrapperView.js b/js/menus/MenuWrapperView.js index 113f46e7..76a92940 100644 --- a/js/menus/MenuWrapperView.js +++ b/js/menus/MenuWrapperView.js @@ -34,7 +34,7 @@ MenuWrapper = Backbone.View.extend({ //bind events this.listenTo(this.model, "change:currentNav", this.render); - this.listenTo(this.model, "change:cellType change:connectionType", this._populateAndShow); + this.listenTo(options.lattice, "change:cellType change:connectionType", this._populateAndShow); this.listenTo(this.model, "change:currentTab", this._updateCurrentTab); this.listenTo(this.model, "change:menuIsVisible", this._setVisibility); @@ -97,7 +97,7 @@ MenuWrapper = Backbone.View.extend({ {navDesign:this.designMenuTabs, navSim:this.simMenuTabs, navAssemble:this.assemMenuTabs, - }))); + }, dmaGlobals.lattice.attributes))); this._updateCurrentTab(); this._show(); }, diff --git a/js/models/AppState.js b/js/models/AppState.js index 7a997f04..c9113726 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -19,12 +19,6 @@ AppState = Backbone.Model.extend({ lattice: null, menuWrapper: null, - cellMode: "cell",//show cells vs parts - inverseMode: false,//show negative space - cellType: "octa", - connectionType: "face", - partType: "triangle", - allCellTypes: {octa:"Octahedron", cube:"Cube"}, allConnectionTypes: { octa: {face:"Face", edge:"Edge", edgeRot:"Rotated Edge", vertex:"Vertex"}, @@ -60,13 +54,9 @@ AppState = Backbone.Model.extend({ this.listenTo(this, "change:currentTab", this._storeTab); this.listenTo(this, "change:currentNav", this._updateCurrentTabForNav); - this.listenTo(this, "change:currentTab", this._updateCellMode); - this.listenTo(this, "change:cellMode", this._cellModeDidChange); - this.listenTo(this, "change:cellType change:connectionType", this._updateLatticetype); this.set("lattice", options.lattice); - this.get("lattice").updateLatticeType(this.get("cellType"), this.get("connectionType")); this.set("menuWrapper", new MenuWrapper({model: this, lattice:this.get("lattice")})); }, @@ -87,10 +77,10 @@ AppState = Backbone.Model.extend({ _updateCellMode: function(){ var currentTab = this.get("currentTab"); - 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"); + 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"); }, //update to last tab open in that section @@ -105,16 +95,6 @@ AppState = Backbone.Model.extend({ this._updateCellMode();//a little bit hacky, this updates the cell mode, but holds off on updating the menus til the animation has happened }, - _cellModeDidChange: function(){ - this.get("lattice").cellModeDidChange(this.get("cellMode")); - }, - - _updateLatticetype: function(){ - var type = this.get("cellType"); - var connectionType = this.get("connectionType"); - this.get("lattice").updateLatticeType(type, connectionType); - }, - /////////////////////////////////////////////////////////////////////////////// /////////////////////KEY BINDINGS////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// diff --git a/js/models/Lattice.js b/js/models/Lattice.js index cd368e10..983e8813 100644 --- a/js/models/Lattice.js +++ b/js/models/Lattice.js @@ -6,7 +6,7 @@ Lattice = Backbone.Model.extend({ defaults: { - scale: window.defaultLatticeScale, + nodes: [], cells: [[[null]]],//3D matrix containing all cells and null, dynamic size cellsMin: {x:0, y:0, z:0},//min position of cells matrix @@ -16,20 +16,29 @@ Lattice = Backbone.Model.extend({ inverseCellsMax: {x:0, y:0, z:0},//max position of inverse cells matrix numCells: 0, numInvCells: 0, + basePlane: null,//plane to build from + scale: window.defaultLatticeScale, columnSeparation: 0.0, highlighter: null,//highlights build-able surfaces shouldPreserveCells: true,//preserve cells when changing lattice type - shouldShowInverseCells: false + + cellMode: "cell",//show cells vs parts + inverseMode: false,//show negative space + cellType: "octa", + connectionType: "face", + partType: "triangle" }, //pass in fillGeometry - initialize: function(options){ + initialize: function(){ //bind events this.listenTo(this, "change:scale", this._scaleDidChange); - this.listenTo(this, "change:shouldShowInverseCells", this._showInverseCells); + this.listenTo(this, "change:inverseMode", this._showInverseCells); + this.listenTo(this, "change:cellMode", this._cellModeDidChange); + this.listenTo(this, "change:cellType change:connectionType", this._updateLatticeType); }, @@ -290,8 +299,10 @@ Lattice = Backbone.Model.extend({ ////////////////////////////////////EVENTS////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// - cellModeDidChange: function(mode){ - if (mode == "part" || !this.get("shouldShowInverseCells")){ + _cellModeDidChange: function(){ + var mode = this.get("cellMode"); + var inverseMode = this.get("inverseMode"); + if (mode == "part" || !inverseMode){ this._iterCells(this.get("cells"), function(cell){ if (cell && cell.drawForMode) cell.drawForMode(mode); }); @@ -305,16 +316,16 @@ Lattice = Backbone.Model.extend({ }, _showInverseCells: function(){ - var shouldShow = this.get("shouldShowInverseCells"); + var inverseMode = this.get("inverseMode"); this._iterCells(this.get("cells"), function(cell){ if (cell) { - if (shouldShow) cell.hide(); + if (inverseMode) cell.hide(); else cell.show(); } }); this._iterCells(this.get("inverseCells"), function(cell){ if (cell) { - if (shouldShow) cell.show(); + if (inverseMode) cell.show(); else cell.hide(); } }); @@ -328,7 +339,7 @@ Lattice = Backbone.Model.extend({ this._iterCells(this.get("cells"), function(cell){ if (cell) cell.updateForScale(scale); }); - if (this.get("shouldShowInverseCells")){ + if (this.get("inverseMode")){ this._iterCells(this.get("inverseCells"), function(cell){ if (cell) cell.updateForScale(scale); }); @@ -344,7 +355,9 @@ Lattice = Backbone.Model.extend({ ///////////////////////////////CONNECTION TYPE////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// - updateLatticeType: function(cellType, connectionType){ + _updateLatticeType: function(){ + var cellType = this.get("cellType"); + var connectionType = this.get("connectionType"); if (!this.get("shouldPreserveCells")) this.clearCells(); if (this._undo) this._undo(); if (this.get("basePlane")) this.get("basePlane").destroy(); -- GitLab