Layer.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //CLASS:图层类
  2. function Layer(width, height) {
  3. var size = new CanvasSketch.Size(parseInt(width), parseInt(height));
  4. this.size = size;
  5. this.div = div;
  6. this.parameter = new Parameter();
  7. this.scale = new Scale(this);
  8. this.currentState = new CurrentState();
  9. this.previousState = new PreviousState();
  10. this.calculateLine = new CalculateLine(this);
  11. this.calculateElement = new CalculateElement(this);
  12. this.build = new Build(this);
  13. this.select = new Select(this);
  14. this.data2d = new data2d();
  15. this.move = new Move(this);
  16. this.pan = new Pan(this);
  17. this.managerSymbol2D = new ManagerSymbol2D();
  18. this.selectFloor = 1;
  19. this.layer3D = new Layer3D(this);
  20. //this.addSymbol2D=new AddSymbol2D(this);
  21. //this.tempSymbol=new TempSymbol(this);
  22. this.variable = new Variable();
  23. this.menu = new Menu(this);
  24. this.maxBounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2);
  25. this.bounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2);
  26. this.center = this.bounds.getCenter();
  27. this.control = new Control();
  28. //墙的厚度
  29. this.thickness = null;
  30. this.zoom = 100;
  31. this.getRes();
  32. this.vectors = {};
  33. //加入矢量图形的总个数。
  34. this.vectorsCount = 0;
  35. //创建一个渲染器。
  36. this.renderer = new Canvas(this);
  37. };
  38. //这个res代表当前zoom下每像素代表的单位长度。
  39. //比如当前缩放比率为 200% 则通过计算得到 res为0.5,说明当前zoom下每个像素只表示0.5个单位长度。
  40. Layer.prototype.getRes = function () {
  41. this.res = 1 / (this.zoom / 100);
  42. return this.res;
  43. };
  44. Layer.prototype.getResFromZoom = function (zoom) {
  45. return res = 1 / (zoom / 100);
  46. };
  47. Layer.prototype.addBackGround = function () {
  48. this.renderer.backgroundcontext.clearRect(0, 0, this.layer.size.w, this.layer.size.h);
  49. this.renderer.addGrid();
  50. this.renderer.addMeter();
  51. };
  52. Layer.prototype.addVectors = function (vectors) {
  53. this.renderer.lock = true;
  54. for (var i = 0, len = vectors.length; i < len; i++) {
  55. if (i == len - 1) { this.renderer.lock = false; }
  56. this.vectors[vectors[i].id] = vectors[i];
  57. this.drawVector(vectors[i]);
  58. }
  59. this.vectorsCount += vectors.length;
  60. };
  61. Layer.prototype.deleteVector = function (vectorId) {
  62. if (vectorId == null || this.vectors[vectorId] == null) {
  63. return;
  64. }
  65. var geometryid = this.vectors[vectorId].geometry.id;
  66. delete this.vectors[vectorId];
  67. delete this.renderer.geometrys[geometryid];
  68. --this.vectorsCount;
  69. };
  70. Layer.prototype.deleteSymbol = function (vectorId, wallid) {
  71. if (vectorId == null || this.vectors[vectorId] == null) {
  72. return;
  73. }
  74. var geometryid = this.vectors[vectorId].geometry.id;
  75. delete this.vectors[vectorId];
  76. delete this.renderer.geometrys[geometryid];
  77. --this.vectorsCount;
  78. if (wallid != null && wallid != "undefined") {
  79. delete layer.vectors[wallid].symbol2Ds[geometryid];
  80. --layer.vectors[wallid].symbol2dsCount;
  81. }
  82. };
  83. Layer.prototype.deleteOnlySymbol = function (wallid, geometryid) {
  84. if (wallid != null && wallid != "undefined") {
  85. delete layer.vectors[wallid].symbol2Ds[geometryid];
  86. --layer.vectors[wallid].symbol2dsCount;
  87. }
  88. };
  89. Layer.prototype.drawSingleVector = function (vector) {
  90. var style;
  91. if (!vector.style) {
  92. style = new CanvasSketch.defaultStyle();
  93. } else {
  94. style = vector.style;
  95. }
  96. this.vectors[vector.id] = vector;
  97. this.renderer.drawSingleGeometry(vector.geometry, style);
  98. ++this.vectorsCount;
  99. };
  100. Layer.prototype.drawVector = function (vector) {
  101. var style;
  102. if (!vector.style) {
  103. style = new CanvasSketch.defaultStyle();
  104. } else {
  105. style = vector.style;
  106. }
  107. this.renderer.drawGeometry(vector.geometry, style, vector.geometry.contextIndex);
  108. };
  109. Layer.prototype.zoomscale = function () {
  110. var zoom = 1 / this.res;
  111. var t = this.renderer.backgroundcanvas;
  112. t.scale(zoom, zoom);
  113. };
  114. //保证背景Grid只画当前屏幕显示的部分
  115. Layer.prototype.moveTo = function (zoom, center) {
  116. this.zoom = zoom;
  117. this.center = center;
  118. var res = this.getRes();
  119. var width = this.size.w * res;
  120. var height = this.size.h * res;
  121. var left = center.x - width / 2;
  122. var bottom = center.y - height / 2;
  123. var right = center.x + width / 2;
  124. var top = center.y + height / 2
  125. if (width / 2 > -startx) {
  126. left = -width / 2;
  127. right = width / 2;
  128. }
  129. else {
  130. if (right > -startx) {
  131. right = -startx;
  132. left = -startx - width;
  133. }
  134. if (left < startx) {
  135. left = startx;
  136. right = startx + width;
  137. }
  138. }
  139. if (bottom < starty) {
  140. bottom = starty;
  141. top = starty + height;
  142. }
  143. if (top > -starty) {
  144. top = -starty;
  145. bottom = -starty - height;
  146. }
  147. var bounds = new CanvasSketch.Bounds(left, bottom, right, top);
  148. this.bounds = bounds;
  149. this.center = this.bounds.getCenter();
  150. /*
  151. //记录已经绘制vector的个数
  152. var index = 0;
  153. this.renderer.lock = true;
  154. for(var id in this.vectors){
  155. index++;
  156. if(index == this.vectorsCount) {
  157. this.renderer.lock = false;
  158. }
  159. this.drawVector(this.vectors[id]);
  160. }
  161. this.renderer.drawBackGround();
  162. */
  163. //this.renderer.redraw(0);
  164. //this.renderer.redraw(1);
  165. //this.renderer.redraw(2);
  166. this.control.refreshCanvas = true;
  167. this.control.refreshBackgroundCanvas = true;
  168. this.control.refreshSelectCanvas = true;
  169. };
  170. //通过屏幕坐标设定center。
  171. Layer.prototype.getPositionFromPx = function (px) {
  172. return new CanvasSketch.Position((px.x + this.bounds.left / this.res) * this.res,
  173. (this.bounds.top / this.res - px.y) * this.res);
  174. };
  175. /*
  176. Layer.prototype.getWallThickness=function(id)
  177. {
  178. if(id==null||typeof(id)=="undefined")
  179. {
  180. id=this.currentState.currentWallId;
  181. }
  182. if(id==null)
  183. {
  184. return null;
  185. }
  186. if(this.vectors[id].geometry.wallType==1)
  187. {
  188. return this.parameter.wallThickness;
  189. }
  190. else if(this.vectors[id].geometry.wallType==2)
  191. {
  192. return this.parameter.partitionThickness;
  193. }
  194. else
  195. {
  196. return null;
  197. }
  198. };
  199. */
  200. Layer.prototype.getThickness = function (id, index) {
  201. return this.vectors[id].geometry.wallInfo[index].thick;
  202. };
  203. //获取symbol的厚度
  204. Layer.prototype.getThickness2 = function (geometry) {
  205. if (geometry != null && typeof (geometry.wallType) != "undefined") {
  206. if (geometry.wallType == 1) {
  207. this.thickness = wallThickness;
  208. }
  209. else {
  210. this.thickness = partitionThickness;
  211. }
  212. return this.thickness;
  213. }
  214. else {
  215. return null;
  216. }
  217. };
  218. Layer.prototype.clear = function () {
  219. var size = new CanvasSketch.Size(parseInt(window.innerWidth), parseInt(window.innerHeight));
  220. this.size = size;
  221. this.maxBounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2);
  222. this.bounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2);
  223. this.center = this.bounds.getCenter();
  224. this.build.firstLines = [];
  225. this.build.endLines = [];
  226. //墙的厚度
  227. this.thickness = null;
  228. this.zoom = 100;
  229. this.getRes();
  230. this.vectors = {};
  231. //加入矢量图形的总个数。
  232. this.vectorsCount = 0;
  233. };