diff --git a/js/cells/GIKCell.js b/js/cells/GIKCell.js index f3a0b06f91ebebad02e47047888d5656b0ac69c1..7a35dc59e5c5640c1bdf16b0b43b61bf90fd0db1 100644 --- a/js/cells/GIKCell.js +++ b/js/cells/GIKCell.js @@ -11,14 +11,6 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cubeCell'], } GIKCell.prototype = Object.create(CubeCell.prototype); - GIKCell.prototype._translateCell = function(object3D){ - if (this.index) { - var offset = this.index.x-this.superCell.getLength(); - object3D.position.set(offset*this.xScale(),0, 0); - } - return object3D; - }; - GIKCell.prototype._getMeshName = function(){ return null; }; @@ -41,15 +33,5 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cubeCell'], } }; - GIKCell.prototype.calcHighlighterPosition = function(face){ - var direction = face.normal.clone().applyEuler(this.object3D.rotation).applyEuler(this.object3D.parent.rotation); - var position = this.getPosition(); - var self = this; - _.each(_.keys(position), function(key){ - position[key] += direction[key]*self.axisScale(key)/2; - }); - return {index: _.clone(this.index), direction:direction, position:position}; - }; - return GIKCell; }); \ No newline at end of file diff --git a/js/highlighter/Highlighter.js b/js/highlighter/Highlighter.js index 2788d308c46f0f7b8e286a150c68e92d8e1831e9..c9380d1025f047149f43df67c4b9da101ca9de4e 100644 --- a/js/highlighter/Highlighter.js +++ b/js/highlighter/Highlighter.js @@ -26,10 +26,9 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' this.hide(); //bind events - this.listenTo(appState, "change:superCellRange", this._superCellParamDidChange); - this.listenTo(appState, "change:superCellIndex", this._superCellParamDidChange); - this.listenTo(appState, "change:deleteMode", this._updateDeleteMode); + + if (this._initialize) this._initialize(); }, @@ -105,6 +104,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' } this.position = params.position; this.direction = params.direction; + this._setScale(); this._setPosition(params.position, params.direction);//position of center point this._setRotation(params.direction); @@ -123,7 +123,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' parseFloat(position.y.toFixed(4)), parseFloat(position.z.toFixed(4))); } - console.warn("highlighted object is not a DMACell") + console.warn("highlighted object is not a DMACell"); return null; }, @@ -135,8 +135,7 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' this.mesh.rotation.set(direction.y*Math.PI/2, direction.x*Math.PI/2, 0); }, - _superCellParamDidChange: function(){ - if (this.updateGikLength) this.updateGikLength(); + _setScale: function(){ }, diff --git a/js/highlighter/SuperCellHighlighter.js b/js/highlighter/SuperCellHighlighter.js index 10326a54824d041d33dce24c3174df689c7c4e3c..8a0a1f1204b432237d29fc3cf2b2a4a4ff39a061 100644 --- a/js/highlighter/SuperCellHighlighter.js +++ b/js/highlighter/SuperCellHighlighter.js @@ -7,29 +7,44 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' return Highlighter.extend({ + _initialize: function(){ + + //bind events + this.listenTo(appState, "change:superCellRange", this._superCellParamDidChange); + this.listenTo(appState, "change:superCellIndex", this._superCellParamDidChange); + }, + _makeGeometry: function(){ - return new THREE.BoxGeometry(lattice.xScale(0),lattice.yScale(0),lattice.zScale(0)); + return new THREE.BoxGeometry(lattice.xScale(),lattice.yScale(),lattice.zScale()); + }, + + _setScale: function(){ + this.mesh.scale.set(appState.get("superCellRange").x, appState.get("superCellRange").y, + appState.get("superCellRange").z); }, _setPosition: function(position, direction){ - this.mesh.position.set(position.x+direction.x/2, position.y+direction.y/2, position.z+lattice.zScale()*direction.z/2); + this.mesh.position.set(position.x+lattice.xScale()*direction.x/2, position.y+lattice.yScale()*direction.y/2, + position.z+lattice.zScale()*direction.z/2); }, _setRotation: function(direction){ if (!this.highlightedObject) return; var index = this.highlightedObject.getAbsoluteIndex(); - var superCellIndex = appState.get("superCellIndex"); + if ((index.z%2 == 0 && Math.abs(direction.z) > 0.9) || (index.z%2 != 0 && Math.abs(direction.z) < 0.1)) this.mesh.rotation.set(0, 0, Math.PI/2); else this.mesh.rotation.set(0,0,0); - this.mesh.translateX(superCellIndex - this.mesh.scale.x/2 + 0.5); + + var superCellIndex = appState.get("superCellIndex"); + this.mesh.translateX((superCellIndex.x + 0.5)*lattice.xScale() - this.mesh.scale.x/2); + this.mesh.translateY((superCellIndex.y + 0.5)*lattice.yScale() - this.mesh.scale.y/2); + this.mesh.translateZ((superCellIndex.z + 0.5)*lattice.zScale() - this.mesh.scale.z/2); }, - updateGikLength: function(){ - if (!this.mesh) return; - this.mesh.scale.set(appState.get("superCellRange").x, appState.get("superCellRange").y, appState.get("superCellRange").z); - three.render(); - if (!this.direction) return; + _superCellParamDidChange: function(){ + if (!this.mesh || !this.direction) return; + this._setScale(); this._setPosition(this.position, this.direction);//position of center point this._setRotation(this.direction); three.render(); @@ -37,14 +52,18 @@ define(['underscore', 'backbone', 'threeModel', 'appState', 'lattice', 'cell', ' _getNextCellPosition: function(){//add direction vector to current index var newIndex = this.highlightedObject.getAbsoluteIndex(); + console.log(this.highlightedObject.index); var direction = this.direction; _.each(_.keys(newIndex), function(key){ newIndex[key] = Math.round(newIndex[key] + direction[key]); }); + console.log(newIndex); + var offset = appState.get("superCellIndex"); - if (newIndex.z%2 == 0) newIndex.x -= offset; - else newIndex.y -= offset; +// this.highlightedObject. + if (newIndex.z%2 == 0) newIndex.x -= offset.x; + else newIndex.y -= offset.x; return newIndex; } }); diff --git a/js/lattice/GIKLattice.js b/js/lattice/GIKLattice.js index 79f9c5926743776967b1dd9315ade0e966d471d7..c92f0443f79e214706f015e6e56ac4033d831e7e 100644 --- a/js/lattice/GIKLattice.js +++ b/js/lattice/GIKLattice.js @@ -13,7 +13,6 @@ define(['underscore', 'backbone', 'appState', 'lattice', 'globals', 'plist', 'th }); require(['superCellHighlighter'], function(superCellHighlighter){ globals.highlighter = new superCellHighlighter(); - globals.highlighter.updateGikLength(); }); }, diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index 729880868bf6c788fc5f06502c279e0e1767311f..f7c254dc9cc686e4e1ada1c5e5f77d87c7db605c 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -47,6 +47,8 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre this.listenTo(this, "change:materialClass", this._loadMaterialClass); this._updateLatticeType(false); + + appState.setLattice(this); }, diff --git a/js/models/AppState.js b/js/models/AppState.js index a02882b0ae753284e3e7c4a9723273a5f49aef71..b6ef4f7e55aea82060ac2020ba5cfe3a7164e393 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -36,7 +36,7 @@ define(['underscore', 'backbone', 'threeModel', 'three'], function(_, Backbone, cellMode: "cell",//supercell, cell, part, node, beam cellsVisible: true, - superCellIndex: 0,//offset of superCell adds + superCellIndex: new THREE.Vector3(0,0,0),//offset of superCell adds superCellRange: new THREE.Vector3(4,1,1), realisticColorScheme: false, @@ -47,6 +47,8 @@ define(['underscore', 'backbone', 'threeModel', 'three'], function(_, Backbone, initialize: function(){ + this.lattice = null; + _.bindAll(this, "_handleKeyStroke", "_handleScroll"); //bind events @@ -67,6 +69,11 @@ define(['underscore', 'backbone', 'threeModel', 'three'], function(_, Backbone, return (window.innerWidth <= 700); }, + setLattice: function(lattice){ + //a little bit hacky, wait for lattice to init then pass reference + this.lattice = lattice; + }, + /////////////////////////////////////////////////////////////////////////////// /////////////////////EVENTS//////////////////////////////////////////////////// @@ -190,7 +197,7 @@ define(['underscore', 'backbone', 'threeModel', 'three'], function(_, Backbone, case 55: case 56: case 57: - if (globals.lattice.get("connectionType") != "gik") break; + if (this.lattice.get("connectionType") != "gik") break; if (state) { var range = this.get("superCellRange").clone(); range.x = e.keyCode-48; @@ -198,16 +205,20 @@ define(['underscore', 'backbone', 'threeModel', 'three'], function(_, Backbone, } break; case 81://q - increase supercell index + if (this.lattice.get("connectionType") != "gik") break; if (state) { - var index = this.get("superCellIndex")+1; - if (index > this.get("superCellRange").x-1) index = 0; + var index = this.get("superCellIndex").clone(); + index.x += 1; + if (index.x > this.get("superCellRange").x-1) index.x = 0; this.set("superCellIndex", index); } break; case 65://a - decrease supercell index + if (this.lattice.get("connectionType") != "gik") break; if (state) { - var index = this.get("superCellIndex")-1; - if (index < 0) index = this.get("superCellRange").x-1; + var index = this.get("superCellIndex").clone(); + index.x -= 1; + if (index.x < 0) index.x = this.get("superCellRange").x-1; this.set("superCellIndex", index); } break;