Layer.js 7.3 KB

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