Layer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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) {
  56. this.renderer.lock = false;
  57. }
  58. this.vectors[vectors[i].id] = vectors[i];
  59. this.drawVector(vectors[i]);
  60. }
  61. this.vectorsCount += vectors.length;
  62. };
  63. Layer.prototype.deleteVector = function (vectorId) {
  64. if (vectorId == null || this.vectors[vectorId] == null) {
  65. return;
  66. }
  67. var geometryid = this.vectors[vectorId].geometry.id;
  68. delete this.vectors[vectorId];
  69. delete this.renderer.geometrys[geometryid];
  70. --this.vectorsCount;
  71. };
  72. Layer.prototype.deleteSymbol = function (vectorId, wallid) {
  73. if (vectorId == null || this.vectors[vectorId] == null) {
  74. return;
  75. }
  76. var geometryid = this.vectors[vectorId].geometry.id;
  77. delete this.vectors[vectorId];
  78. delete this.renderer.geometrys[geometryid];
  79. --this.vectorsCount;
  80. console.log('delete ', vectorId, geometryid, wallid)
  81. if (wallid != null && wallid != "undefined") {
  82. // delete layer.vectors[wallid].symbol2Ds[geometryid];
  83. delete layer.vectors[wallid].symbol2Ds[vectorId];
  84. --layer.vectors[wallid].symbol2dsCount;
  85. }
  86. };
  87. Layer.prototype.deleteOnlySymbol = function (wallid, geometryid) {
  88. if (wallid != null && wallid != "undefined") {
  89. delete layer.vectors[wallid].symbol2Ds[geometryid];
  90. --layer.vectors[wallid].symbol2dsCount;
  91. }
  92. };
  93. Layer.prototype.drawSingleVector = function (vector) {
  94. var style;
  95. if (!vector.style) {
  96. style = new CanvasSketch.defaultStyle();
  97. } else {
  98. style = vector.style;
  99. }
  100. this.vectors[vector.id] = vector;
  101. this.renderer.drawSingleGeometry(vector.geometry, style);
  102. ++this.vectorsCount;
  103. };
  104. Layer.prototype.drawVector = function (vector) {
  105. var style;
  106. if (!vector.style) {
  107. style = new CanvasSketch.defaultStyle();
  108. } else {
  109. style = vector.style;
  110. }
  111. this.renderer.drawGeometry(vector.geometry, style, vector.geometry.contextIndex);
  112. };
  113. Layer.prototype.zoomscale = function () {
  114. var zoom = 1 / this.res;
  115. var t = this.renderer.backgroundcanvas;
  116. t.scale(zoom, zoom);
  117. };
  118. //保证背景Grid只画当前屏幕显示的部分
  119. Layer.prototype.moveTo = function (zoom, center) {
  120. this.zoom = zoom;
  121. this.center = center;
  122. var res = this.getRes();
  123. var width = this.size.w * res;
  124. var height = this.size.h * res;
  125. var left = center.x - width / 2;
  126. var bottom = center.y - height / 2;
  127. var right = center.x + width / 2;
  128. var top = center.y + height / 2
  129. if (width / 2 > -startx) {
  130. left = -width / 2;
  131. right = width / 2;
  132. } else {
  133. if (right > -startx) {
  134. right = -startx;
  135. left = -startx - width;
  136. }
  137. if (left < startx) {
  138. left = startx;
  139. right = startx + width;
  140. }
  141. }
  142. if (bottom < starty) {
  143. bottom = starty;
  144. top = starty + height;
  145. }
  146. if (top > -starty) {
  147. top = -starty;
  148. bottom = -starty - height;
  149. }
  150. var bounds = new CanvasSketch.Bounds(left, bottom, right, top);
  151. this.bounds = bounds;
  152. this.center = this.bounds.getCenter();
  153. /*
  154. //记录已经绘制vector的个数
  155. var index = 0;
  156. this.renderer.lock = true;
  157. for(var id in this.vectors){
  158. index++;
  159. if(index == this.vectorsCount) {
  160. this.renderer.lock = false;
  161. }
  162. this.drawVector(this.vectors[id]);
  163. }
  164. this.renderer.drawBackGround();
  165. */
  166. //this.renderer.redraw(0);
  167. //this.renderer.redraw(1);
  168. //this.renderer.redraw(2);
  169. this.control.refreshCanvas = true;
  170. this.control.refreshBackgroundCanvas = true;
  171. this.control.refreshSelectCanvas = true;
  172. };
  173. //通过屏幕坐标设定center。
  174. Layer.prototype.getPositionFromPx = function (px) {
  175. return new CanvasSketch.Position(
  176. (px.x + this.bounds.left / this.res) * this.res,
  177. (this.bounds.top / this.res - px.y) * this.res
  178. );
  179. };
  180. /*
  181. Layer.prototype.getWallThickness=function(id)
  182. {
  183. if(id==null||typeof(id)=="undefined")
  184. {
  185. id=this.currentState.currentWallId;
  186. }
  187. if(id==null)
  188. {
  189. return null;
  190. }
  191. if(this.vectors[id].geometry.wallType==1)
  192. {
  193. return this.parameter.wallThickness;
  194. }
  195. else if(this.vectors[id].geometry.wallType==2)
  196. {
  197. return this.parameter.partitionThickness;
  198. }
  199. else
  200. {
  201. return null;
  202. }
  203. };
  204. */
  205. Layer.prototype.getThickness = function (id, index) {
  206. return this.vectors[id].geometry.wallInfo[index].thick;
  207. };
  208. //获取symbol的厚度
  209. Layer.prototype.getThickness2 = function (geometry) {
  210. if (geometry != null && typeof (geometry.wallType) != "undefined") {
  211. if (geometry.wallType == 1) {
  212. this.thickness = wallThickness;
  213. } else {
  214. this.thickness = partitionThickness;
  215. }
  216. return this.thickness;
  217. } else {
  218. return null;
  219. }
  220. };
  221. Layer.prototype.clear = function () {
  222. var size = new CanvasSketch.Size(parseInt(window.innerWidth), parseInt(window.innerHeight));
  223. this.size = size;
  224. this.maxBounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2);
  225. this.bounds = new CanvasSketch.Bounds(-size.w / 2, -size.h / 2, size.w / 2, size.h / 2);
  226. this.center = this.bounds.getCenter();
  227. this.build.firstLines = [];
  228. this.build.endLines = [];
  229. //墙的厚度
  230. this.thickness = null;
  231. this.zoom = 100;
  232. this.getRes();
  233. this.vectors = {};
  234. //加入矢量图形的总个数。
  235. this.vectorsCount = 0;
  236. };