//CLASS:图层类 function Layer(width, height) { var size = new CanvasSketch.Size(parseInt(width), parseInt(height)); this.size = size; this.div = div; this.parameter = new Parameter(); this.scale = new Scale(this); this.currentState = new CurrentState(); this.previousState = new PreviousState(); this.calculateLine = new CalculateLine(this); this.calculateElement = new CalculateElement(this); this.build = new Build(this); this.select = new Select(this); this.data2d = new data2d(); this.move = new Move(this); this.pan = new Pan(this); this.managerSymbol2D = new ManagerSymbol2D(); this.selectFloor = 1; this.layer3D = new Layer3D(this); //this.addSymbol2D=new AddSymbol2D(this); //this.tempSymbol=new TempSymbol(this); this.variable = new Variable(); this.menu = new Menu(this); this.maxBounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2); this.bounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2); this.center = this.bounds.getCenter(); this.control = new Control(); //墙的厚度 this.thickness = null; this.zoom = 100; this.getRes(); this.vectors = {}; //加入矢量图形的总个数。 this.vectorsCount = 0; //创建一个渲染器。 this.renderer = new Canvas(this); }; //这个res代表当前zoom下每像素代表的单位长度。 //比如当前缩放比率为 200% 则通过计算得到 res为0.5,说明当前zoom下每个像素只表示0.5个单位长度。 Layer.prototype.getRes = function () { this.res = 1 / (this.zoom / 100); return this.res; }; Layer.prototype.getResFromZoom = function (zoom) { return res = 1 / (zoom / 100); }; Layer.prototype.addBackGround = function () { this.renderer.backgroundcontext.clearRect(0, 0, this.layer.size.w, this.layer.size.h); this.renderer.addGrid(); this.renderer.addMeter(); }; Layer.prototype.addVectors = function (vectors) { this.renderer.lock = true; for (var i = 0, len = vectors.length; i < len; i++) { if (i == len - 1) { this.renderer.lock = false; } this.vectors[vectors[i].id] = vectors[i]; this.drawVector(vectors[i]); } this.vectorsCount += vectors.length; }; Layer.prototype.deleteVector = function (vectorId) { if (vectorId == null || this.vectors[vectorId] == null) { return; } var geometryid = this.vectors[vectorId].geometry.id; delete this.vectors[vectorId]; delete this.renderer.geometrys[geometryid]; --this.vectorsCount; }; Layer.prototype.deleteSymbol = function (vectorId, wallid) { if (vectorId == null || this.vectors[vectorId] == null) { return; } var geometryid = this.vectors[vectorId].geometry.id; delete this.vectors[vectorId]; delete this.renderer.geometrys[geometryid]; --this.vectorsCount; if (wallid != null && wallid != "undefined") { delete layer.vectors[wallid].symbol2Ds[geometryid]; --layer.vectors[wallid].symbol2dsCount; } }; Layer.prototype.deleteOnlySymbol = function (wallid, geometryid) { if (wallid != null && wallid != "undefined") { delete layer.vectors[wallid].symbol2Ds[geometryid]; --layer.vectors[wallid].symbol2dsCount; } }; Layer.prototype.drawSingleVector = function (vector) { var style; if (!vector.style) { style = new CanvasSketch.defaultStyle(); } else { style = vector.style; } this.vectors[vector.id] = vector; this.renderer.drawSingleGeometry(vector.geometry, style); ++this.vectorsCount; }; Layer.prototype.drawVector = function (vector) { var style; if (!vector.style) { style = new CanvasSketch.defaultStyle(); } else { style = vector.style; } this.renderer.drawGeometry(vector.geometry, style, vector.geometry.contextIndex); }; Layer.prototype.zoomscale = function () { var zoom = 1 / this.res; var t = this.renderer.backgroundcanvas; t.scale(zoom, zoom); }; //保证背景Grid只画当前屏幕显示的部分 Layer.prototype.moveTo = function (zoom, center) { this.zoom = zoom; this.center = center; var res = this.getRes(); var width = this.size.w * res; var height = this.size.h * res; var left = center.x - width / 2; var bottom = center.y - height / 2; var right = center.x + width / 2; var top = center.y + height / 2 if (width / 2 > -startx) { left = -width / 2; right = width / 2; } else { if (right > -startx) { right = -startx; left = -startx - width; } if (left < startx) { left = startx; right = startx + width; } } if (bottom < starty) { bottom = starty; top = starty + height; } if (top > -starty) { top = -starty; bottom = -starty - height; } var bounds = new CanvasSketch.Bounds(left, bottom, right, top); this.bounds = bounds; this.center = this.bounds.getCenter(); /* //记录已经绘制vector的个数 var index = 0; this.renderer.lock = true; for(var id in this.vectors){ index++; if(index == this.vectorsCount) { this.renderer.lock = false; } this.drawVector(this.vectors[id]); } this.renderer.drawBackGround(); */ //this.renderer.redraw(0); //this.renderer.redraw(1); //this.renderer.redraw(2); this.control.refreshCanvas = true; this.control.refreshBackgroundCanvas = true; this.control.refreshSelectCanvas = true; }; //通过屏幕坐标设定center。 Layer.prototype.getPositionFromPx = function (px) { return new CanvasSketch.Position((px.x + this.bounds.left / this.res) * this.res, (this.bounds.top / this.res - px.y) * this.res); }; /* Layer.prototype.getWallThickness=function(id) { if(id==null||typeof(id)=="undefined") { id=this.currentState.currentWallId; } if(id==null) { return null; } if(this.vectors[id].geometry.wallType==1) { return this.parameter.wallThickness; } else if(this.vectors[id].geometry.wallType==2) { return this.parameter.partitionThickness; } else { return null; } }; */ Layer.prototype.getThickness = function (id, index) { return this.vectors[id].geometry.wallInfo[index].thick; }; //获取symbol的厚度 Layer.prototype.getThickness2 = function (geometry) { if (geometry != null && typeof (geometry.wallType) != "undefined") { if (geometry.wallType == 1) { this.thickness = wallThickness; } else { this.thickness = partitionThickness; } return this.thickness; } else { return null; } }; Layer.prototype.clear = function () { var size = new CanvasSketch.Size(parseInt(window.innerWidth), parseInt(window.innerHeight)); this.size = size; this.maxBounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2); this.bounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2); this.center = this.bounds.getCenter(); this.build.firstLines = []; this.build.endLines = []; //墙的厚度 this.thickness = null; this.zoom = 100; this.getRes(); this.vectors = {}; //加入矢量图形的总个数。 this.vectorsCount = 0; };