From 2d2a78d6e5edbff3ef668d7291a860e7a3768881 Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Thu, 11 Jun 2015 14:56:23 -0700 Subject: [PATCH] composite editor subclass --- js/lattice/CompositeEditorLattice.js | 68 +++++++++++---------- js/lattice/Lattice.js | 91 +++++++++++++++++++++++----- js/lattice/LatticeBase.js | 66 +------------------- js/menus/CompositeMenu.js | 2 +- 4 files changed, 116 insertions(+), 111 deletions(-) diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js index 1f711275..2c4920fb 100644 --- a/js/lattice/CompositeEditorLattice.js +++ b/js/lattice/CompositeEditorLattice.js @@ -3,43 +3,45 @@ */ -define(['underscore', 'backbone', 'appState', 'lattice', 'globals', 'plist', 'three', 'threeModel'], - function(_, Backbone, appState, lattice, globals, plist, THREE, three){ +define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'threeModel', 'latticeBase'], + function(_, Backbone, appState, globals, plist, THREE, three, LatticeBase){ function makeRandomColor(){ return '#' + Math.floor(Math.random()*16777215).toString(16); } - var defaults = {}; - var compositeId = 0; + var compositeNum = 1; - var CompositeEditorLattice = { + var CompositeEditorLattice = LatticeBase.extend({ - _initCompositeEditor: function(id, data){ + defaults: _.extend(LatticeBase.prototype.defaults, { + compositeName: "", + compositeColor: makeRandomColor() + }), - if (!id) id = "composite" + ++compositeId;//todo real unique id here - this.set("compositeId", id, {silent:true}); - - _.extend(defaults, { - compositeName: "", - compositeColor: makeRandomColor(), - compositeNumCells:2, - compositeCellsMin: new THREE.Vector3(0,0,0),//null, - compositeCellsMax: new THREE.Vector3(2,4,5)//null - }); + __initialize: function(options){ + console.log(options); + this.set("id", this.cid); + }, + initLatticeSubclass: function(subclass){ var self = this; - _.each(_.keys(defaults), function(key){ - self.set(key, defaults[key], {silent:true}); - }); - this.compositeCells = [[[null]]]; + require([subclass], function(subclassObject){ - if (data){ - _.each(_.keys(data), function(key){ - self.set("composite" + key.charAt(0).toUpperCase() + key.slice(1), data[key]); + _.extend(self, subclassObject); + + //copy over cells to new lattice type + var cells = self.cells; + self._loopCells(cells, function(cell, x, y, z){ + if (!cell) return; + var index = _.clone(cell.index); + if (cell.destroy) cell.destroy(); + self.makeCellForLatticeType(index, function(newCell){ + cells[x][y][z] = newCell; + }); }); - this.compositeCells = data.cells;//todo parse cells - } + three.render(); + }); }, _changeRandomColor: function(){ @@ -47,12 +49,12 @@ define(['underscore', 'backbone', 'appState', 'lattice', 'globals', 'plist', 'th }, makeNewCompositeMaterial: function(name){ - if (this.get("numCompositeCells") == 0) { + if (this.get("numCells") == 0) { console.warn("no cells in this composite"); return; } - if (name == "") name = "Composite Material " + compositeId; - var id = this.get("compositeId"); + if (name == "") name = "Composite Material " + compositeNum++; + var id = this.get("id"); var data = { name: name, color: this.get("compositeColor"), @@ -67,11 +69,11 @@ define(['underscore', 'backbone', 'appState', 'lattice', 'globals', 'plist', 'th }, deleteComposite: function(){ - var id = this.get("compositeId"); + var id = this.get("id"); delete globals.materials.compositeMaterials[id];//todo trigger change on all instances }, - _undoCompositeEditor: function(){ + destroy: function(){ var self = this; _.each(_.keys(CompositeEditorLattice), function(key){ self[key] = null; @@ -80,9 +82,9 @@ define(['underscore', 'backbone', 'appState', 'lattice', 'globals', 'plist', 'th self.unset(key, {silent:true}); }); this.compositeCells = null; - this.showCells(); - }, - }; + lattice.showCells(); + } + }); return CompositeEditorLattice; }); diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index 701b7ceb..9ede3c2d 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -12,10 +12,15 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre units: "mm", + cellType: "cube", + connectionType: "face", + partType: null, + materialType: null, + materialClass: "mechanical", + nodes: [], - cellSeparation: {xy:0, z:0},//spacing for connectors/joints - partType: null + cellSeparation: {xy:0, z:0}//spacing for connectors/joints }), @@ -59,7 +64,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre if (globals.basePlane) globals.basePlane.destroy(); if (globals.highlighter) globals.highlighter.destroy(); - this._initLatticeSubclass(); + this._initLatticeSubclass(this._getSubclassForLatticeType()); }, _setToDefaultsSilently: function(){ @@ -80,16 +85,57 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre } }, - _updateCellSeparation: function(){ - var cellSep = this.get("cellSeparation"); - globals.basePlane.updateXYSeparation(cellSep.xy); + _initLatticeSubclass: function(subclass){ + var self = this; + require([subclass], function(subclassObject){ + + _.extend(self, subclassObject); + self._initLatticeType(); + + //copy over cells to new lattice type + var cells = self.cells; + self._loopCells(cells, function(cell, x, y, z){ + if (!cell) return; + var index = _.clone(cell.index); + if (cell.destroy) cell.destroy(); + self.makeCellForLatticeType(index, function(newCell){ + cells[x][y][z] = newCell; + }); + }); + three.render(); + }); + }, - var cellMode = appState.get("cellMode"); - var partType = this.get("partType"); -// this._iterCells(this.cells, function(cell){ -// if (cell) cell.updateForScale(cellMode, partType); -// }); - three.render(); + _getSubclassForLatticeType: function(){ + var cellType = this.get("cellType"); + var connectionType = this.get("connectionType"); + if (cellType == "octa"){ + if (connectionType == "face"){ + return "octaFaceLattice"; + } else if (connectionType == "edge"){ + return "octaEdgeLattice"; + } else if (connectionType == "edgeRot"){ + return "octaRotEdgeLattice"; + } else if (connectionType == "vertex"){ + return "octaVertexLattice"; + } + } else if (cellType == "tetra"){ + if (connectionType == "stacked") return "tetraStackedLattice"; + else if (connectionType == "vertex") return "tetraVertexLattice"; + } else if (cellType == "cube"){ + if (connectionType == "face"){ + return "cubeLattice"; + } else if (connectionType == "gik"){ + return "gikLattice"; + } + } else if (cellType == "truncatedCube"){ + return "truncatedCubeLattice"; + } else if (cellType == "kelvin"){ + return "kelvinLattice"; + } else { + console.warn("unrecognized cell type " + cellType); + } + return null; }, @@ -97,6 +143,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre + //events _loadMaterialClass: function(){ @@ -120,6 +167,18 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre if (!this.inCompositeMode() && this._undoCompositeEditor) this._undoCompositeEditor(); }, + _updateCellSeparation: function(){ + var cellSep = this.get("cellSeparation"); + globals.basePlane.updateXYSeparation(cellSep.xy); + + var cellMode = appState.get("cellMode"); + var partType = this.get("partType"); +// this._iterCells(this.cells, function(cell){ +// if (cell) cell.updateForScale(cellMode, partType); +// }); + three.render(); + }, + @@ -185,8 +244,12 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre var self = this; require(['compositeEditorLattice'], function(CompositeEditorLattice){ self.hideCells(); - _.extend(self, CompositeEditorLattice); - self._initCompositeEditor(id, data); + if (self.compositeEditor) { + console.warn("composite editor already allocated"); + self.compositeEditor.destroy(); + } + self.compositeEditor = new CompositeEditorLattice(); + self.compositeEditor.initLatticeSubclass(self._getSubclassForLatticeType()); appState.set("currentNav", "navComposite"); }); }, diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index 5c8d7986..ada87bb2 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -17,16 +17,11 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre cellsMin: null,//min position of cells matrix cellsMax: null,//max position of cells matrix - numCells: 0, - - cellType: "cube", - connectionType: "face", - materialType: null, - materialClass: "mechanical" + numCells: 0 }, - initialize: function(){ + initialize: function(options){ this.cells = [[[null]]];//3D matrix containing all cells and null, dynamic size this.sparseCells = [[[null]]];//3D matrix containing highest hierarchical level of cells and null @@ -37,62 +32,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre this.listenTo(appState, "change:cellsVisible", this._setCellVisibility); if (this.__bindEvents) this.__bindEvents(); - if (this.__initialize) this.__initialize(); - }, - - - _initLatticeSubclass: function(){ - var subclass = this._getSubclassForLatticeType(); - var self = this; - require([subclass], function(subclassObject){ - - _.extend(self, subclassObject); - self._initLatticeType(); - - //copy over cells to new lattice type - var cells = self.cells; - self._loopCells(cells, function(cell, x, y, z){ - if (!cell) return; - var index = _.clone(cell.index); - if (cell.destroy) cell.destroy(); - self.makeCellForLatticeType(index, function(newCell){ - cells[x][y][z] = newCell; - }); - }); - three.render(); - }); - }, - - _getSubclassForLatticeType: function(){ - var cellType = this.get("cellType"); - var connectionType = this.get("connectionType"); - if (cellType == "octa"){ - if (connectionType == "face"){ - return "octaFaceLattice"; - } else if (connectionType == "edge"){ - return "octaEdgeLattice"; - } else if (connectionType == "edgeRot"){ - return "octaRotEdgeLattice"; - } else if (connectionType == "vertex"){ - return "octaVertexLattice"; - } - } else if (cellType == "tetra"){ - if (connectionType == "stacked") return "tetraStackedLattice"; - else if (connectionType == "vertex") return "tetraVertexLattice"; - } else if (cellType == "cube"){ - if (connectionType == "face"){ - return "cubeLattice"; - } else if (connectionType == "gik"){ - return "gikLattice"; - } - } else if (cellType == "truncatedCube"){ - return "truncatedCubeLattice"; - } else if (cellType == "kelvin"){ - return "kelvinLattice"; - } else { - console.warn("unrecognized cell type " + cellType); - } - return null; + if (this.__initialize) this.__initialize(options); }, diff --git a/js/menus/CompositeMenu.js b/js/menus/CompositeMenu.js index 4cc18fdb..00a953fb 100644 --- a/js/menus/CompositeMenu.js +++ b/js/menus/CompositeMenu.js @@ -72,7 +72,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice'], function($, _ }, _makeTemplateJSON: function(){ - return _.extend(lattice.toJSON()); + return _.extend(lattice.compositeEditor.toJSON()); }, template: _.template('\ -- GitLab