Layer.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. import Load from "./Load";
  2. import { stateService } from "./Service/StateService";
  3. import { elementService } from "./Service/ElementService";
  4. import { floorplanService } from "./Service/FloorplanService";
  5. import { tagService } from "./Service/TagService";
  6. import { tableService } from "./Service/TableService";
  7. import { historyService } from "./Service/HistoryService";
  8. import UIControl from "./Controls/UIControl";
  9. import { moveTag } from "./Controls/MoveTag";
  10. import { moveTable } from "./Controls/MoveTable";
  11. import { addWall } from "./Controls/AddWall";
  12. import { moveWall } from "./Controls/MoveWall";
  13. import { addRectangle } from "./Controls/AddRectangle";
  14. import { moveRectangle } from "./Controls/MoveRectangle";
  15. import { addCircle } from "./Controls/AddCircle";
  16. import { moveCircle } from "./Controls/MoveCircle";
  17. import { addIcon } from "./Controls/AddIcon";
  18. import { moveIcon } from "./Controls/MoveIcon";
  19. import { addArrow } from "./Controls/AddArrow";
  20. import { moveArrow } from "./Controls/MoveArrow";
  21. import { coordinate } from "./Coordinate";
  22. import Render from "./Renderer/Render";
  23. import { draw } from "./Renderer/Draw";
  24. import { listenLayer } from "./ListenLayer";
  25. import { floorplanData } from "./FloorplanData";
  26. import LayerEvents from "./enum/LayerEvents.js";
  27. import UIEvents from "./enum/UIEvents.js";
  28. import SelectState from "./enum/SelectState.js";
  29. import Constant from "./Constant";
  30. import VectorType from "./enum/VectorType";
  31. import MathUtil, { mathUtil } from "./MathUtil";
  32. import { wallService } from "./Service/WallService";
  33. import { history } from "./History/History.js";
  34. import { signService } from "./Service/SignService";
  35. import { iconService } from "./Service/IconService";
  36. import { customImageService } from "./Service/CustomImageService.js";
  37. import { moveCustomImage } from "./Controls/MoveCustomImage.js";
  38. import { moveBgImage } from "./Controls/MoveBgImage.js";
  39. export default class Layer {
  40. constructor() {
  41. //super()
  42. this.load = new Load(this);
  43. this.uiControl = new UIControl(this);
  44. this.renderer = new Render(this);
  45. //this.history = new History(this)
  46. this.canvas = null;
  47. this.startX = null;
  48. this.startY = null;
  49. history.layer = this;
  50. }
  51. //开始
  52. start(canvas, vectorData) {
  53. coordinate.init(canvas);
  54. this.canvas = canvas;
  55. draw.initContext(this.canvas);
  56. this.bindEvents();
  57. return this.load.load(vectorData).then(() => {
  58. //。。。
  59. this.renderer.autoRedraw();
  60. });
  61. }
  62. bindEvents() {
  63. this.canvas.addEventListener("contextmenu", function (e) {
  64. e.preventDefault();
  65. });
  66. this.canvas.addEventListener("mousedown", this.onMouseDown.bind(this));
  67. this.canvas.addEventListener("mousemove", this.onMouseMove.bind(this));
  68. this.canvas.addEventListener("mouseup", this.onMouseUp.bind(this));
  69. this.canvas.addEventListener("mousewheel", this.onWheel.bind(this));
  70. this.canvas.addEventListener("DOMMouseScroll", this.onWheel.bind(this));
  71. }
  72. onMouseDown(e) {
  73. this.startX = e.offsetX || e.layerX;
  74. this.startY = e.offsetY || e.layerY;
  75. this.lastX = e.offsetX || e.layerX;
  76. this.lastY = e.offsetY || e.layerY;
  77. // 右键
  78. if (e.button == 2) {
  79. this.stopAddVector();
  80. this.renderer.autoRedraw();
  81. return;
  82. }
  83. const eventName = stateService.getEventName();
  84. //点击第一次
  85. if (eventName == LayerEvents.AddWall) {
  86. let flag = addWall.setNewWallPoint("start", {
  87. x: this.startX,
  88. y: this.startY,
  89. });
  90. if (!flag) {
  91. return;
  92. }
  93. }
  94. //点击第二次
  95. else if (eventName == LayerEvents.AddingWall) {
  96. let flag = addWall.setNewWallPoint("end", {
  97. x: this.startX,
  98. y: this.startY,
  99. });
  100. if (!flag) {
  101. return;
  102. }
  103. if (addWall.canAdd) {
  104. addWall.buildWall();
  105. history.save();
  106. } else {
  107. return;
  108. }
  109. } else if (eventName == LayerEvents.AddRectangle) {
  110. addRectangle.execute({
  111. x: this.startX,
  112. y: this.startY,
  113. });
  114. } else if (eventName == LayerEvents.AddCircle) {
  115. addCircle.execute({
  116. x: this.startX,
  117. y: this.startY,
  118. });
  119. } else if (eventName == LayerEvents.AddIcon) {
  120. addIcon.execute({
  121. x: this.startX,
  122. y: this.startY,
  123. });
  124. } else if (eventName == LayerEvents.AddArrow) {
  125. addArrow.execute({
  126. x: this.startX,
  127. y: this.startY,
  128. });
  129. } else {
  130. const selectItem = stateService.getSelectItem();
  131. if (eventName == null && selectItem) {
  132. stateService.setDraggingItem(selectItem);
  133. this.uiControl.selectUI = selectItem.type;
  134. }
  135. // else if (eventName == null) {
  136. // this.uiControl.clearUI();
  137. // }
  138. }
  139. this.setEventName("mouseDown");
  140. this.uiControl.clearUI();
  141. // 清除上一个状态
  142. // 设置当前事件名称
  143. e.preventDefault();
  144. e.stopPropagation();
  145. }
  146. onMouseMove(e) {
  147. const X = e.offsetX || e.layerX;
  148. const Y = e.offsetY || e.layerY;
  149. if (this.lastX == null) {
  150. this.lastX = X;
  151. }
  152. if (this.lastY == null) {
  153. this.lastY = Y;
  154. }
  155. let dx = X - this.lastX;
  156. let dy = Y - this.lastY;
  157. let position = coordinate.getXYFromScreen({
  158. x: X,
  159. y: Y,
  160. });
  161. const eventName = stateService.getEventName();
  162. // 是否需要重绘
  163. let needAutoRedraw = false;
  164. const draggingItem = stateService.getDraggingItem();
  165. switch (eventName) {
  166. case null:
  167. //监控当前选择的构件
  168. needAutoRedraw = listenLayer.start(position);
  169. break;
  170. case LayerEvents.PanBackGround:
  171. break;
  172. case LayerEvents.AddWall:
  173. stateService.clearDraggingItem();
  174. stateService.clearFocusItem();
  175. needAutoRedraw = true;
  176. listenLayer.start(position);
  177. if (listenLayer.modifyPoint) {
  178. position = {
  179. x: listenLayer.modifyPoint.x,
  180. y: listenLayer.modifyPoint.y,
  181. };
  182. }
  183. elementService.execute(null, position);
  184. elementService.setStartAddWall(position);
  185. elementService.showStartAddWall();
  186. break;
  187. case LayerEvents.AddingWall:
  188. stateService.clearDraggingItem();
  189. stateService.clearFocusItem();
  190. needAutoRedraw = true;
  191. let startPosition = {
  192. x: addWall.startInfo.position.x,
  193. y: addWall.startInfo.position.y,
  194. };
  195. listenLayer.start(position, null, null, startPosition);
  196. if (listenLayer.modifyPoint) {
  197. position = {
  198. x: listenLayer.modifyPoint.x,
  199. y: listenLayer.modifyPoint.y,
  200. };
  201. }
  202. elementService.execute(startPosition, position);
  203. elementService.setStartAddWall(position);
  204. if (!elementService.newWall.display) {
  205. elementService.setNewWall(startPosition, position);
  206. elementService.showNewWall(); //画墙
  207. } else {
  208. if (!listenLayer.modifyPoint && addWall.startInfo.linkedPointId) {
  209. //角度很正
  210. let newEndPosition = elementService.checkAngle(
  211. position,
  212. addWall.startInfo.linkedPointId,
  213. null
  214. );
  215. if (newEndPosition) {
  216. mathUtil.clonePoint(position, newEndPosition);
  217. listenLayer.modifyPoint = {
  218. x: newEndPosition.x,
  219. y: newEndPosition.y,
  220. };
  221. }
  222. }
  223. elementService.setNewWallEndPosition(position); //改变end位置
  224. }
  225. addWall.canAdd = addWall.canAddWallForEnd(position);
  226. if (!addWall.canAdd) {
  227. elementService.setNewWallState("error");
  228. } else {
  229. elementService.setNewWallState("normal");
  230. }
  231. break;
  232. case LayerEvents.MoveWall:
  233. dx = (dx * Constant.defaultZoom) / coordinate.zoom;
  234. dy = (dy * Constant.defaultZoom) / coordinate.zoom;
  235. // 1表示可以继续移动,2表示不能移动(启动距离还不够),3表示wallId被删除了,4表示重新开始移动(需要达到一定距离才能启动),5表示不能移动(不合适)
  236. let moveFlag = moveWall.moveWallPlane(draggingItem.vectorId, dx, dy);
  237. // 启动的时候需要点距离,所以真正移动了才更新lastX和lastY
  238. if (moveFlag == 1) {
  239. this.lastX = X;
  240. this.lastY = Y;
  241. needAutoRedraw = true;
  242. }
  243. // 需要继续保持移动,一般是距离不够启动
  244. else if (moveFlag == 2) {
  245. }
  246. // wallId被删除了
  247. else if (moveFlag == 3) {
  248. history.save();
  249. stateService.clearSelectItem();
  250. stateService.clearDraggingItem();
  251. stateService.clearEventName();
  252. listenLayer.clear();
  253. needAutoRedraw = true;
  254. }
  255. // wallId有一端被吸附了,这时候需要重新启动
  256. else if (moveFlag == 4) {
  257. this.lastX = X;
  258. this.lastY = Y;
  259. this.startX = X;
  260. this.startY = Y;
  261. needAutoRedraw = true;
  262. } else if (moveFlag == 5) {
  263. this.lastX = X;
  264. this.lastY = Y;
  265. }
  266. break;
  267. case LayerEvents.MoveWallPoint:
  268. let point = floorplanService.getPoint(draggingItem.vectorId);
  269. listenLayer.start(position, draggingItem.vectorId, point.parent);
  270. if (listenLayer.modifyPoint) {
  271. position = {
  272. x: listenLayer.modifyPoint.x,
  273. y: listenLayer.modifyPoint.y,
  274. };
  275. }
  276. elementService.execute(null, position);
  277. let flag = moveWall.movePoint(
  278. draggingItem.vectorId,
  279. position,
  280. listenLayer.modifyPoint
  281. );
  282. if (!flag) {
  283. elementService.hideAll();
  284. }
  285. needAutoRedraw = true;
  286. break;
  287. case LayerEvents.AddRectangle:
  288. stateService.clearDraggingItem();
  289. stateService.clearFocusItem();
  290. needAutoRedraw = true;
  291. elementService.setStartAddWall(position);
  292. elementService.showStartAddWall();
  293. break;
  294. case LayerEvents.AddingRectangle:
  295. stateService.clearDraggingItem();
  296. stateService.clearFocusItem();
  297. needAutoRedraw = true;
  298. elementService.setStartAddWall(position);
  299. elementService.showStartAddWall();
  300. addRectangle.execute(position);
  301. break;
  302. case LayerEvents.MoveRectangle:
  303. needAutoRedraw = true;
  304. if (draggingItem != null) {
  305. moveRectangle.moveFullRectangle(dx, dy, draggingItem.vectorId);
  306. }
  307. this.lastX = X;
  308. this.lastY = Y;
  309. break;
  310. case LayerEvents.MoveRectangleVertex:
  311. needAutoRedraw = true;
  312. if (draggingItem != null) {
  313. elementService.setStartAddWall(position);
  314. elementService.showStartAddWall();
  315. moveRectangle.moveRectangleVertex(
  316. position,
  317. draggingItem.vectorId,
  318. parseInt(draggingItem.selectIndex.substring(7))
  319. );
  320. }
  321. break;
  322. case LayerEvents.MoveRectangleSide:
  323. needAutoRedraw = true;
  324. if (draggingItem != null) {
  325. moveRectangle.moveRectangleSide(
  326. position,
  327. draggingItem.vectorId,
  328. parseInt(draggingItem.selectIndex.substring(5))
  329. );
  330. }
  331. break;
  332. case LayerEvents.AddCircle:
  333. stateService.clearDraggingItem();
  334. stateService.clearFocusItem();
  335. needAutoRedraw = true;
  336. elementService.setStartAddWall(position);
  337. elementService.showStartAddWall();
  338. break;
  339. case LayerEvents.AddingCircle:
  340. stateService.clearDraggingItem();
  341. stateService.clearFocusItem();
  342. needAutoRedraw = true;
  343. elementService.setStartAddWall(position);
  344. elementService.showStartAddWall();
  345. addCircle.execute(position);
  346. break;
  347. case LayerEvents.MoveCircle:
  348. needAutoRedraw = true;
  349. if (draggingItem != null) {
  350. moveCircle.moveFullCircle(dx, dy, draggingItem.vectorId);
  351. }
  352. this.lastX = X;
  353. this.lastY = Y;
  354. break;
  355. case LayerEvents.MoveCircleVertex:
  356. needAutoRedraw = true;
  357. if (draggingItem != null) {
  358. moveCircle.moveCircleVertex(
  359. position,
  360. draggingItem.vectorId,
  361. parseInt(draggingItem.selectIndex.substring(7))
  362. );
  363. }
  364. break;
  365. case LayerEvents.AddIcon:
  366. stateService.clearDraggingItem();
  367. stateService.clearFocusItem();
  368. needAutoRedraw = true;
  369. elementService.setStartAddWall(position);
  370. elementService.showStartAddWall();
  371. break;
  372. case LayerEvents.AddingIcon:
  373. stateService.clearDraggingItem();
  374. stateService.clearFocusItem();
  375. needAutoRedraw = true;
  376. elementService.setStartAddWall(position);
  377. elementService.showStartAddWall();
  378. addIcon.execute(position);
  379. break;
  380. case LayerEvents.MoveIcon:
  381. needAutoRedraw = true;
  382. if (draggingItem != null) {
  383. moveIcon.moveFullIcon(dx, dy, draggingItem.vectorId);
  384. }
  385. this.lastX = X;
  386. this.lastY = Y;
  387. break;
  388. case LayerEvents.MoveIconVertex:
  389. needAutoRedraw = true;
  390. if (draggingItem != null) {
  391. moveIcon.moveIconVertex(
  392. position,
  393. draggingItem.vectorId,
  394. parseInt(draggingItem.selectIndex.substring(7))
  395. );
  396. }
  397. break;
  398. case LayerEvents.AddArrow:
  399. stateService.clearDraggingItem();
  400. stateService.clearFocusItem();
  401. needAutoRedraw = true;
  402. elementService.setStartAddWall(position);
  403. elementService.showStartAddWall();
  404. break;
  405. case LayerEvents.AddingArrow:
  406. stateService.clearDraggingItem();
  407. stateService.clearFocusItem();
  408. needAutoRedraw = true;
  409. elementService.setStartAddWall(position);
  410. elementService.showStartAddWall();
  411. addArrow.execute(position);
  412. break;
  413. case LayerEvents.MoveArrow:
  414. needAutoRedraw = true;
  415. if (draggingItem != null) {
  416. moveArrow.moveFullArrow(dx, dy, draggingItem.vectorId);
  417. }
  418. this.lastX = X;
  419. this.lastY = Y;
  420. break;
  421. case LayerEvents.moveArrowVertex:
  422. needAutoRedraw = true;
  423. if (draggingItem != null) {
  424. moveArrow.moveArrowVertex(
  425. position,
  426. draggingItem.vectorId,
  427. draggingItem.selectIndex
  428. );
  429. }
  430. break;
  431. case LayerEvents.AddTag:
  432. needAutoRedraw = true;
  433. if (draggingItem == null) {
  434. const tag = tagService.createTag(position);
  435. if (tag.vectorId) {
  436. stateService.setSelectItem(
  437. tag.vectorId,
  438. VectorType.Tag,
  439. SelectState.All
  440. );
  441. stateService.setDraggingItem(stateService.selectItem);
  442. }
  443. } else {
  444. moveTag.moveFullTag(dx, dy, draggingItem.vectorId);
  445. }
  446. this.lastX = X;
  447. this.lastY = Y;
  448. break;
  449. case LayerEvents.MoveTag:
  450. needAutoRedraw = true;
  451. if (draggingItem != null) {
  452. moveTag.moveFullTag(dx, dy, draggingItem.vectorId);
  453. this.lastX = X;
  454. this.lastY = Y;
  455. }
  456. break;
  457. case LayerEvents.AddTable:
  458. needAutoRedraw = true;
  459. if (draggingItem == null) {
  460. const table = tableService.createTable(position);
  461. if (table.vectorId) {
  462. stateService.setSelectItem(
  463. table.vectorId,
  464. VectorType.Table,
  465. SelectState.All
  466. );
  467. stateService.setDraggingItem(stateService.selectItem);
  468. }
  469. } else {
  470. moveTable.moveFullTable(dx, dy, draggingItem.vectorId);
  471. }
  472. this.lastX = X;
  473. this.lastY = Y;
  474. break;
  475. case LayerEvents.MoveTable:
  476. needAutoRedraw = true;
  477. if (draggingItem != null) {
  478. moveTable.moveFullTable(dx, dy, draggingItem.vectorId);
  479. this.lastX = X;
  480. this.lastY = Y;
  481. }
  482. break;
  483. case LayerEvents.AddSign:
  484. needAutoRedraw = true;
  485. if (draggingItem == null) {
  486. const signType = this.uiControl.getSignTypeForUI();
  487. const sign = signService.createSign(position, signType);
  488. if (sign.vectorId) {
  489. stateService.setSelectItem(
  490. sign.vectorId,
  491. signType,
  492. SelectState.All
  493. );
  494. stateService.setDraggingItem(stateService.selectItem);
  495. }
  496. } else {
  497. const sign = floorplanService.getSign(draggingItem.vectorId);
  498. mathUtil.clonePoint(sign.center, position);
  499. }
  500. break;
  501. case LayerEvents.MoveCustomImage:
  502. needAutoRedraw = true;
  503. if (draggingItem != null) {
  504. moveCustomImage.moveFullCustomImage(dx, dy, draggingItem.vectorId);
  505. this.lastX = X;
  506. this.lastY = Y;
  507. }
  508. break;
  509. case LayerEvents.MoveSign:
  510. needAutoRedraw = true;
  511. const sign = floorplanService.getSign(draggingItem.vectorId);
  512. mathUtil.clonePoint(sign.center, position);
  513. break;
  514. case LayerEvents.MoveBgImage:
  515. needAutoRedraw = true;
  516. if (draggingItem != null) {
  517. moveBgImage.moveFullBgImage(dx, dy, draggingItem.vectorId);
  518. this.lastX = X;
  519. this.lastY = Y;
  520. }
  521. break;
  522. }
  523. if (needAutoRedraw) {
  524. this.renderer.autoRedraw();
  525. }
  526. }
  527. onMouseUp(e) {
  528. const X = e.offsetX || e.layerX;
  529. const Y = e.offsetY || e.layerY;
  530. let eventName = stateService.getEventName();
  531. const draggingItem = stateService.getDraggingItem();
  532. let focusItem = null;
  533. if (draggingItem && draggingItem.vectorId) {
  534. if (
  535. mathUtil.getDistance(
  536. {
  537. x: this.startX,
  538. y: this.startY,
  539. },
  540. {
  541. x: X,
  542. y: Y,
  543. }
  544. ) < Constant.minMovePix
  545. ) {
  546. focusItem = {
  547. vectorId: draggingItem.vectorId,
  548. type: draggingItem.type,
  549. selectIndex: draggingItem.selectIndex,
  550. cursor: { x: this.lastX, y: this.lastY },
  551. };
  552. this.uiControl.showAttributes(focusItem);
  553. } else {
  554. focusItem = null;
  555. }
  556. stateService.setFocusItem(focusItem);
  557. //this.uiControl.clearUI();
  558. }
  559. let position = coordinate.getXYFromScreen({
  560. x: X,
  561. y: Y,
  562. });
  563. let needAutoRedraw = false;
  564. switch (eventName) {
  565. case null:
  566. return;
  567. case LayerEvents.PanBackGround:
  568. needAutoRedraw = true;
  569. stateService.clearFocusItem();
  570. this.uiControl.clearUI();
  571. break;
  572. case LayerEvents.MoveWallPoint:
  573. if (focusItem == null) {
  574. needAutoRedraw = true;
  575. let point = floorplanService.getPoint(draggingItem.vectorId);
  576. if (point) {
  577. listenLayer.start(point, draggingItem.vectorId, point.parent);
  578. if (
  579. listenLayer.modifyPoint &&
  580. listenLayer.modifyPoint.hasOwnProperty("linkedPointId")
  581. ) {
  582. wallService.moveTo(
  583. draggingItem.vectorId,
  584. listenLayer.modifyPoint.linkedPointId
  585. );
  586. } else if (
  587. listenLayer.modifyPoint &&
  588. (listenLayer.modifyPoint.linkedPointIdX ||
  589. listenLayer.modifyPoint.linkedPointIdY)
  590. ) {
  591. mathUtil.clonePoint(point, listenLayer.modifyPoint);
  592. } else if (
  593. listenLayer.modifyPoint &&
  594. listenLayer.modifyPoint.hasOwnProperty("linkedWallId")
  595. ) {
  596. point = wallService.createPoint(
  597. listenLayer.modifyPoint.x,
  598. listenLayer.modifyPoint.y
  599. );
  600. wallService.splitWall(
  601. listenLayer.modifyPoint.linkedWallId,
  602. point.vectorId,
  603. "start"
  604. );
  605. wallService.moveTo(draggingItem.vectorId, point.vectorId);
  606. } else if (moveWall.splitWallId != null) {
  607. wallService.splitWall(
  608. moveWall.splitWallId,
  609. draggingItem.vectorId,
  610. "start"
  611. );
  612. }
  613. //draggingItem.vectorId所在的墙面与其他墙角相交
  614. moveWall.updateForAbsorbWallPoints();
  615. history.save();
  616. }
  617. } else {
  618. this.uiControl.showAttributes(focusItem);
  619. }
  620. elementService.hideAll();
  621. break;
  622. case LayerEvents.AddingWall:
  623. needAutoRedraw = true;
  624. if (addWall.startInfo && addWall.startInfo.linkedPointId) {
  625. let addWallStartPoint = floorplanService.getPoint(
  626. addWall.startInfo.linkedPointId
  627. );
  628. if (
  629. addWall.endInfo.position &&
  630. Object.keys(addWallStartPoint.parent).length > 1
  631. ) {
  632. stateService.clearEventName();
  633. addWall.clear();
  634. elementService.hideAll();
  635. this.uiControl.clearUI();
  636. history.save();
  637. }
  638. }
  639. break;
  640. case LayerEvents.MoveWall:
  641. if (focusItem == null) {
  642. needAutoRedraw = true;
  643. history.save();
  644. } else {
  645. this.uiControl.showAttributes(focusItem);
  646. }
  647. break;
  648. case LayerEvents.AddingRectangle:
  649. needAutoRedraw = true;
  650. if (
  651. mathUtil.getDistance(addRectangle.start, addRectangle.end) <
  652. Constant.minAdsorb
  653. ) {
  654. floorplanService.deleteRectangle(addRectangle.currentVectorId);
  655. }
  656. stateService.clearEventName();
  657. elementService.hideAll();
  658. addRectangle.clear();
  659. history.save();
  660. this.uiControl.clearUI();
  661. break;
  662. case LayerEvents.MoveRectangle:
  663. if (focusItem == null) {
  664. needAutoRedraw = true;
  665. history.save();
  666. } else {
  667. this.uiControl.showAttributes(focusItem);
  668. }
  669. break;
  670. case LayerEvents.MoveRectangleVertex:
  671. needAutoRedraw = true;
  672. elementService.hideAll();
  673. history.save();
  674. break;
  675. case LayerEvents.MoveRectangleSide:
  676. needAutoRedraw = true;
  677. history.save();
  678. break;
  679. case LayerEvents.AddingCircle:
  680. if (addCircle.end != null && addCircle.currentVectorId != null) {
  681. if (
  682. mathUtil.getDistance(addCircle.start, addCircle.end) <
  683. Constant.minAdsorb
  684. ) {
  685. floorplanService.deleteCircle(addCircle.currentVectorId);
  686. }
  687. needAutoRedraw = true;
  688. }
  689. stateService.clearEventName();
  690. elementService.hideAll();
  691. addCircle.clear();
  692. history.save();
  693. this.uiControl.clearUI();
  694. break;
  695. case LayerEvents.MoveCircle:
  696. if (focusItem == null) {
  697. needAutoRedraw = true;
  698. history.save();
  699. } else {
  700. this.uiControl.showAttributes(focusItem);
  701. }
  702. break;
  703. case LayerEvents.MoveCircleVertex:
  704. needAutoRedraw = true;
  705. history.save();
  706. break;
  707. case LayerEvents.AddingIcon:
  708. if (addIcon.end != null && addIcon.currentVectorId != null) {
  709. if (
  710. mathUtil.getDistance(addIcon.start, addIcon.end) <
  711. Constant.minAdsorb
  712. ) {
  713. iconService.deleteIcon(addIcon.currentVectorId);
  714. }
  715. needAutoRedraw = true;
  716. }
  717. stateService.clearEventName();
  718. elementService.hideAll();
  719. addIcon.clear();
  720. history.save();
  721. this.uiControl.clearUI();
  722. break;
  723. case LayerEvents.MoveIcon:
  724. if (focusItem == null) {
  725. needAutoRedraw = true;
  726. history.save();
  727. } else {
  728. this.uiControl.showAttributes(focusItem);
  729. }
  730. break;
  731. case LayerEvents.MoveIconVertex:
  732. needAutoRedraw = true;
  733. history.save();
  734. break;
  735. case LayerEvents.AddingArrow:
  736. if (addArrow.start != null && addArrow.currentVectorId != null) {
  737. if (
  738. mathUtil.getDistance(addArrow.start, addArrow.end) <
  739. Constant.minAdsorb
  740. ) {
  741. floorplanService.deleteArrow(addArrow.currentVectorId);
  742. }
  743. needAutoRedraw = true;
  744. }
  745. stateService.clearEventName();
  746. elementService.hideAll();
  747. addArrow.clear();
  748. history.save();
  749. this.uiControl.clearUI();
  750. break;
  751. case LayerEvents.MoveArrow:
  752. needAutoRedraw = true;
  753. history.save();
  754. break;
  755. case LayerEvents.MoveTag:
  756. if (focusItem == null) {
  757. needAutoRedraw = true;
  758. history.save();
  759. } else {
  760. this.uiControl.showAttributes(focusItem);
  761. }
  762. break;
  763. case LayerEvents.AddTag:
  764. needAutoRedraw = true;
  765. this.uiControl.showAttributes(focusItem);
  766. history.save();
  767. break;
  768. case LayerEvents.MoveTable:
  769. if (focusItem == null) {
  770. needAutoRedraw = true;
  771. history.save();
  772. } else {
  773. this.uiControl.showAttributes(focusItem);
  774. }
  775. break;
  776. case LayerEvents.AddTable:
  777. needAutoRedraw = true;
  778. this.uiControl.showAttributes(focusItem);
  779. history.save();
  780. break;
  781. case LayerEvents.AddSign:
  782. needAutoRedraw = true;
  783. this.uiControl.clearUI();
  784. this.uiControl.showAttributes(focusItem);
  785. history.save();
  786. break;
  787. case LayerEvents.MoveSign:
  788. if (focusItem != null && signService.isSign(focusItem.type)) {
  789. //history.save();
  790. this.uiControl.showAttributes(focusItem);
  791. } else {
  792. needAutoRedraw = true;
  793. history.save();
  794. }
  795. break;
  796. case LayerEvents.MoveCustomImage:
  797. if (focusItem == null) {
  798. history.save();
  799. } else {
  800. this.uiControl.showAttributes(focusItem);
  801. }
  802. break;
  803. case LayerEvents.MoveBgImage:
  804. if (focusItem == null) {
  805. history.save();
  806. } else {
  807. this.uiControl.showAttributes(focusItem);
  808. }
  809. break;
  810. }
  811. this.lastX = null;
  812. this.lastY = null;
  813. this.setEventName("mouseUp");
  814. stateService.clearDraggingItem();
  815. this.renderer.autoRedraw();
  816. }
  817. onWheel(e) {
  818. // e.preventDefault()
  819. // const type = e.type
  820. // if (type == 'DOMMouseScroll' || type == 'mousewheel') {
  821. // // 当在canvas用滚轮滚动时
  822. // const delta = e.wheelDelta ? (e.wheelDelta / 120) * 2 : (-(e.detail || 0) / 3) * 2
  823. // const zoom = coordinate.zoom + delta
  824. // if (zoom < 14) {
  825. // return
  826. // }
  827. // coordinate.updateZoom(zoom)
  828. // this.renderer.autoRedraw()
  829. // }
  830. }
  831. onKeydown(e) {
  832. if (!this.display) {
  833. return;
  834. }
  835. if (e.ctrlKey && e.code == "KeyZ") {
  836. this.revokeHistory();
  837. console.log("ctrl+z");
  838. } else if (e.ctrlKey && e.code == "KeyY") {
  839. this.recoveryHistory();
  840. console.log("ctrl+y");
  841. } else if (e.code == "Delete") {
  842. this.uiControl.deleteItem();
  843. this.uiControl.clearUI();
  844. history.save();
  845. this.renderer.autoRedraw();
  846. console.log("Delete");
  847. } else if (e.code == "Escape") {
  848. this.stopAddVector();
  849. this.renderer.autoRedraw();
  850. console.log("Esc");
  851. }
  852. }
  853. setEventName(eventType) {
  854. let eventName = stateService.getEventName();
  855. if (eventType == "mouseDown") {
  856. if (eventName == null) {
  857. const selectItem = stateService.getSelectItem();
  858. if (selectItem == null) {
  859. stateService.setEventName(LayerEvents.PanBackGround);
  860. } else if (selectItem.type == VectorType.Wall) {
  861. stateService.setEventName(LayerEvents.MoveWall);
  862. } else if (selectItem.type == VectorType.WallCorner) {
  863. stateService.setEventName(LayerEvents.MoveWallPoint);
  864. } else if (selectItem.type == VectorType.Tag) {
  865. stateService.setEventName(LayerEvents.MoveTag);
  866. } else if (signService.isSign(selectItem.type)) {
  867. stateService.setEventName(LayerEvents.MoveSign);
  868. } else if (selectItem.type == VectorType.Rectangle) {
  869. if (selectItem.selectIndex == SelectState.All) {
  870. stateService.setEventName(LayerEvents.MoveRectangle);
  871. } else if (selectItem.selectIndex.indexOf(SelectState.Vertex) > -1) {
  872. stateService.setEventName(LayerEvents.MoveRectangleVertex);
  873. } else if (selectItem.selectIndex.indexOf(SelectState.Side) > -1) {
  874. stateService.setEventName(LayerEvents.MoveRectangleSide);
  875. }
  876. } else if (selectItem.type == VectorType.Circle) {
  877. if (selectItem.selectIndex == SelectState.All) {
  878. stateService.setEventName(LayerEvents.MoveCircle);
  879. } else if (selectItem.selectIndex.indexOf(SelectState.Vertex) > -1) {
  880. stateService.setEventName(LayerEvents.MoveCircleVertex);
  881. }
  882. } else if (selectItem.type == VectorType.Icon) {
  883. if (selectItem.selectIndex == SelectState.All) {
  884. stateService.setEventName(LayerEvents.MoveIcon);
  885. } else if (selectItem.selectIndex.indexOf(SelectState.Vertex) > -1) {
  886. stateService.setEventName(LayerEvents.MoveIconVertex);
  887. }
  888. } else if (selectItem.type == VectorType.Arrow) {
  889. if (selectItem.selectIndex == SelectState.All) {
  890. stateService.setEventName(LayerEvents.MoveArrow);
  891. } else {
  892. stateService.setEventName(LayerEvents.moveArrowVertex);
  893. }
  894. } else if (selectItem.type == VectorType.Table) {
  895. stateService.setEventName(LayerEvents.MoveTable);
  896. } else if (selectItem.type == VectorType.Title) {
  897. stateService.setEventName(LayerEvents.MoveTitle);
  898. } else if (selectItem.type == VectorType.Compass) {
  899. stateService.setEventName(LayerEvents.MoveCompass);
  900. } else if (selectItem.type == VectorType.CustomImage) {
  901. stateService.setEventName(LayerEvents.MoveCustomImage);
  902. } else if (selectItem.type == VectorType.BgImage) {
  903. stateService.setEventName(LayerEvents.MoveBgImage);
  904. }
  905. } else if (eventName == LayerEvents.AddWall) {
  906. stateService.setEventName(LayerEvents.AddingWall);
  907. }
  908. } else if (eventType == "mouseUp") {
  909. if (eventName == LayerEvents.AddTag) {
  910. stateService.clearEventName();
  911. } else if (eventName == LayerEvents.AddRectangle) {
  912. stateService.setEventName(LayerEvents.AddingRectangle);
  913. } else if (eventName == LayerEvents.AddCircle) {
  914. stateService.setEventName(LayerEvents.AddingCircle);
  915. } else if (eventName == LayerEvents.AddIcon) {
  916. stateService.setEventName(LayerEvents.AddingIcon);
  917. } else if (eventName == LayerEvents.AddArrow) {
  918. stateService.setEventName(LayerEvents.AddingArrow);
  919. } else if (eventName == LayerEvents.AddTable) {
  920. stateService.clearEventName();
  921. }
  922. else if (
  923. eventName != LayerEvents.AddWall &&
  924. eventName != LayerEvents.AddingWall
  925. ) {
  926. //&& eventName != LayerEvents.AddRectangle && eventName != LayerEvents.AddingRectangle && eventName != LayerEvents.AddCircle && eventName != LayerEvents.AddingCircle && eventName != LayerEvents.AddArrow && eventName != LayerEvents.AddingArrow && eventName != LayerEvents.AddIcon && eventName != LayerEvents.AddingIcon
  927. stateService.clearEventName();
  928. }
  929. }
  930. }
  931. exit() {
  932. stateService.clearItems();
  933. stateService.clearEventName();
  934. this.uiControl.clearUI();
  935. }
  936. stopAddVector() {
  937. let eventName = stateService.getEventName();
  938. if (eventName != LayerEvents.AddingWall) {
  939. stateService.clearEventName();
  940. const draggingItem = stateService.getDraggingItem();
  941. if (eventName == LayerEvents.AddTag) {
  942. if (draggingItem && draggingItem.vectorId) {
  943. tagService.deleteTag(draggingItem.vectorId);
  944. this.uiControl.clearUI();
  945. stateService.clearItems();
  946. }
  947. } else if (eventName == LayerEvents.AddTable) {
  948. if (draggingItem && draggingItem.vectorId) {
  949. tableService.deleteTable(draggingItem.vectorId);
  950. this.uiControl.clearUI();
  951. stateService.clearItems();
  952. }
  953. } else if (eventName == LayerEvents.AddSign) {
  954. if (draggingItem && draggingItem.vectorId) {
  955. floorplanService.deleteSign(draggingItem.vectorId);
  956. stateService.clearItems();
  957. }
  958. }
  959. } else {
  960. stateService.setEventName(LayerEvents.AddWall);
  961. }
  962. this.uiControl.clearUI();
  963. elementService.hideAll();
  964. }
  965. }