Layer.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. import Load from "./Load";
  2. import { stateService } from "./Service/StateService";
  3. import { elementService } from "./Service/ElementService";
  4. import { dataService } from "./Service/DataService";
  5. import { textService } from "./Service/TextService";
  6. import { historyService } from "./Service/HistoryService";
  7. import UIControl from "./Controls/UIControl";
  8. // import { moveRectangle } from "./Controls/MoveRectangle";
  9. import { moveText } from "./Controls/MoveText";
  10. import { moveSVG } from "./Controls/MoveSVG";
  11. import { moveMagnifier } from "./Controls/MoveMagnifier";
  12. import { addRoad } from "./Controls/AddRoad";
  13. import { addLine } from "./Controls/AddLine";
  14. import { addCircle } from "./Controls/AddCircle";
  15. import { addText } from "./Controls/AddText";
  16. import { addMagnifier } from "./Controls/AddMagnifier";
  17. import { addSVG } from "./Controls/AddSVG";
  18. import { moveRoad } from "./Controls/MoveRoad";
  19. import { moveLine } from "./Controls/MoveLine";
  20. import { moveCircle } from "./Controls/MoveCircle";
  21. import { coordinate } from "./Coordinate";
  22. import Render from "./Renderer/Render";
  23. import { draw } from "./Renderer/Draw";
  24. import { listenLayer } from "./ListenLayer";
  25. import LayerEvents from "./enum/LayerEvents.js";
  26. import UIEvents from "./enum/UIEvents.js";
  27. import SelectState from "./enum/SelectState.js";
  28. import VectorType from "./enum/VectorType";
  29. import { mathUtil } from "./Util/MathUtil";
  30. import History from "./History/History";
  31. import mitt from "mitt";
  32. import { roadService } from "./Service/RoadService";
  33. import { edgeService } from "./Service/EdgeService";
  34. import { roadPointService } from "./Service/RoadPointService";
  35. import { curveRoadService } from "./Service/CurveRoadService";
  36. import VectorCategory from "./enum/VectorCategory";
  37. const minDragDis = 10;
  38. export default class Layer {
  39. constructor(canvas, newsletter, graphicState) {
  40. this.canvas = canvas;
  41. this.load = new Load(this);
  42. this.uiControl = new UIControl(this, newsletter, graphicState);
  43. this.renderer = new Render(this);
  44. this.history = new History(this);
  45. this.mousePosition = null;
  46. this.dragging = false; // 当前是否正在拖拽
  47. this.start();
  48. Object.setPrototypeOf(Object.getPrototypeOf(this), mitt());
  49. }
  50. start() {
  51. if (this.canvas) {
  52. this.canvas.width = this.canvas.clientWidth;
  53. this.canvas.height = this.canvas.clientHeight;
  54. coordinate.init(this.canvas);
  55. draw.initContext(this.canvas);
  56. dataService.initVectorData();
  57. this.bindEvents();
  58. }
  59. window.vectorData = dataService.vectorData;
  60. }
  61. bindEvents() {
  62. this.canvas.addEventListener("contextmenu", function (e) {
  63. e.preventDefault();
  64. });
  65. this.canvas.addEventListener("mousedown", this.onMouseDown.bind(this));
  66. this.canvas.addEventListener("mousemove", this.onMouseMove.bind(this));
  67. this.canvas.addEventListener("mouseup", this.onMouseUp.bind(this));
  68. this.canvas.addEventListener("mousewheel", this.onWheel.bind(this));
  69. this.canvas.addEventListener("DOMMouseScroll", this.onWheel.bind(this));
  70. this.canvas.addEventListener("resize", this.reSize.bind(this));
  71. document.addEventListener("keydown", this.onKeydown.bind(this));
  72. }
  73. reSize = function () {
  74. console.log("resize");
  75. coordinate.updateForCanvas();
  76. this.renderer.autoRedraw();
  77. };
  78. onMouseDown(e) {
  79. this.startX = e.offsetX || e.layerX;
  80. this.startY = e.offsetY || e.layerY;
  81. this.lastX = e.offsetX || e.layerX;
  82. this.lastY = e.offsetY || e.layerY;
  83. let position = coordinate.getXYFromScreen({
  84. x: this.startX,
  85. y: this.startY,
  86. });
  87. // 右键
  88. if (e.button == 2) {
  89. this.stopAddVector();
  90. this.uiControl.currentUI = null;
  91. this.renderer.autoRedraw();
  92. return;
  93. }
  94. this.dragging = false;
  95. //用于支持平板电脑
  96. listenLayer.start(position);
  97. this.setEventName("mouseDown");
  98. const eventName = stateService.getEventName();
  99. switch (eventName) {
  100. case LayerEvents.AddRoad:
  101. stateService.setEventName(LayerEvents.AddingRoad);
  102. addRoad.setNewRoadPoint("start", position);
  103. break;
  104. case LayerEvents.AddCurveRoad:
  105. stateService.setEventName(LayerEvents.AddingCurveRoad);
  106. addRoad.setNewRoadPoint("start", position);
  107. break;
  108. case LayerEvents.AddLine:
  109. stateService.setEventName(LayerEvents.AddingLine);
  110. addLine.setNewLinePoint(position);
  111. break;
  112. case LayerEvents.AddCircle:
  113. stateService.setEventName(LayerEvents.AddingCircle);
  114. addCircle.setCenter(position);
  115. break;
  116. case LayerEvents.AddText:
  117. stateService.setEventName(LayerEvents.MoveText);
  118. addText.buildText(position);
  119. stateService.setSelectItem(
  120. addText.newText.vectorId,
  121. VectorType.Text,
  122. SelectState.Select
  123. );
  124. addText.clear();
  125. break;
  126. case LayerEvents.AddMagnifier:
  127. stateService.setEventName(LayerEvents.MoveMagnifier);
  128. addMagnifier.buildMagnifier(position);
  129. stateService.setSelectItem(
  130. addMagnifier.newMagnifier.vectorId,
  131. VectorType.Magnifier,
  132. SelectState.Select
  133. );
  134. addMagnifier.clear();
  135. break;
  136. case LayerEvents.AddSVG:
  137. stateService.setEventName(LayerEvents.MoveSVG);
  138. addSVG.buildSVG(position);
  139. stateService.setSelectItem(
  140. addSVG.newSVG.vectorId,
  141. VectorType.SVG,
  142. SelectState.Select
  143. );
  144. addSVG.clear();
  145. break;
  146. }
  147. const selectItem = stateService.getSelectItem();
  148. stateService.setDraggingItem(selectItem);
  149. // 清除上一个状态
  150. // 设置当前事件名称
  151. e.preventDefault();
  152. e.stopPropagation();
  153. }
  154. onMouseMove(e) {
  155. const X = e.offsetX || e.layerX;
  156. const Y = e.offsetY || e.layerY;
  157. let dx = X - this.lastX;
  158. let dy = Y - this.lastY;
  159. let position = coordinate.getXYFromScreen({
  160. x: X,
  161. y: Y,
  162. });
  163. this.mousePosition = {
  164. x: position.x,
  165. y: position.y,
  166. };
  167. if (
  168. Math.abs(X - this.startX) > minDragDis ||
  169. Math.abs(Y - this.startY) > minDragDis
  170. ) {
  171. // 是否拖拽了
  172. this.dragging = true;
  173. }
  174. const eventName = stateService.getEventName();
  175. // 是否需要重绘
  176. let needAutoRedraw = false;
  177. let point = null;
  178. const draggingItem = stateService.getDraggingItem();
  179. switch (eventName) {
  180. case null:
  181. //监控
  182. needAutoRedraw = listenLayer.start(position);
  183. let seleteItem = stateService.getSelectItem();
  184. if (seleteItem != null) {
  185. console.log("选中:" + seleteItem.vectorId);
  186. } else {
  187. console.log("什么也没选中");
  188. }
  189. break;
  190. case LayerEvents.PanBackGround:
  191. stateService.clearItems();
  192. coordinate.center.x =
  193. coordinate.center.x - (dx * coordinate.defaultZoom) / coordinate.zoom;
  194. coordinate.center.y =
  195. coordinate.center.y + (dy * coordinate.defaultZoom) / coordinate.zoom;
  196. dataService.setGridForPan(
  197. (dx * coordinate.defaultZoom) / coordinate.zoom,
  198. (dy * coordinate.defaultZoom) / coordinate.zoom
  199. );
  200. this.lastX = X;
  201. this.lastY = Y;
  202. needAutoRedraw = true;
  203. break;
  204. case LayerEvents.AddRoad:
  205. needAutoRedraw = true;
  206. listenLayer.start(position);
  207. if (listenLayer.modifyPoint) {
  208. position = {
  209. x: listenLayer.modifyPoint.x,
  210. y: listenLayer.modifyPoint.y,
  211. };
  212. }
  213. elementService.hideAll();
  214. elementService.setPoint(position);
  215. elementService.showPoint();
  216. if (listenLayer.modifyPoint) {
  217. elementService.execute(listenLayer.modifyPoint, position);
  218. }
  219. break;
  220. case LayerEvents.AddLine:
  221. needAutoRedraw = true;
  222. listenLayer.start(position);
  223. if (listenLayer.modifyPoint) {
  224. position = {
  225. x: listenLayer.modifyPoint.x,
  226. y: listenLayer.modifyPoint.y,
  227. };
  228. }
  229. elementService.hideAll();
  230. elementService.setPoint(position);
  231. elementService.showPoint();
  232. if (listenLayer.modifyPoint) {
  233. elementService.execute(listenLayer.modifyPoint, position);
  234. }
  235. break;
  236. case LayerEvents.AddCircle:
  237. needAutoRedraw = true;
  238. listenLayer.start(position);
  239. if (listenLayer.modifyPoint) {
  240. position = {
  241. x: listenLayer.modifyPoint.x,
  242. y: listenLayer.modifyPoint.y,
  243. };
  244. }
  245. elementService.hideAll();
  246. elementService.setPoint(position);
  247. elementService.showPoint();
  248. if (listenLayer.modifyPoint) {
  249. elementService.execute(listenLayer.modifyPoint, position);
  250. }
  251. break;
  252. case LayerEvents.AddingRoad:
  253. needAutoRedraw = true;
  254. listenLayer.start(position);
  255. if (listenLayer.modifyPoint) {
  256. position = {
  257. x: listenLayer.modifyPoint.x,
  258. y: listenLayer.modifyPoint.y,
  259. };
  260. }
  261. elementService.execute(addRoad.startInfo.position, position);
  262. elementService.setPoint(position);
  263. elementService.setNewRoad(addRoad.startInfo.position, position);
  264. elementService.showNewRoad();
  265. addRoad.setNewRoadPoint("end", position);
  266. addRoad.canAdd = addRoad.canAddRoadForEnd(position);
  267. if (!addRoad.canAdd) {
  268. elementService.setNewRoadState("error");
  269. } else {
  270. elementService.setNewRoadState("normal");
  271. }
  272. break;
  273. case LayerEvents.AddingLine:
  274. needAutoRedraw = true;
  275. let exceptLineId = null;
  276. if (addLine.newLine != null) {
  277. exceptLineId = addLine.newLine.vectorId;
  278. }
  279. listenLayer.start(position, {
  280. exceptLineId: exceptLineId,
  281. });
  282. if (listenLayer.modifyPoint) {
  283. position = {
  284. x: listenLayer.modifyPoint.x,
  285. y: listenLayer.modifyPoint.y,
  286. };
  287. }
  288. elementService.execute(addLine.startInfo.position, position);
  289. elementService.setPoint(position);
  290. if (addLine.newLine == null) {
  291. addLine.buildLine(position);
  292. } else {
  293. addLine.updateLine(position);
  294. }
  295. break;
  296. case LayerEvents.AddingCircle:
  297. needAutoRedraw = true;
  298. let exceptCircleId = null;
  299. if (addCircle.newCircle != null) {
  300. exceptCircleId = addCircle.newCircle.vectorId;
  301. }
  302. listenLayer.start(position, { exceptCircleId: exceptCircleId });
  303. if (listenLayer.modifyPoint) {
  304. position = {
  305. x: listenLayer.modifyPoint.x,
  306. y: listenLayer.modifyPoint.y,
  307. };
  308. }
  309. elementService.execute(addCircle.center, position);
  310. elementService.setPoint(position);
  311. if (addCircle.newCircle == null) {
  312. addCircle.buildCircle(position);
  313. } else {
  314. addCircle.updateCircle(position);
  315. }
  316. break;
  317. case LayerEvents.MoveRoad:
  318. needAutoRedraw = true;
  319. //只允许拖拽一条公路
  320. let road = dataService.getRoad(draggingItem.vectorId);
  321. let start = dataService.getRoadPoint(road.startId);
  322. let end = dataService.getRoadPoint(road.endId);
  323. if (
  324. Object.keys(start.getParent()).length == 1 &&
  325. Object.keys(end.getParent()).length == 1
  326. ) {
  327. //拖拽的路只有一条
  328. moveRoad.moveRoad(
  329. draggingItem.vectorId,
  330. (dx * coordinate.defaultZoom) / coordinate.zoom,
  331. (dy * coordinate.defaultZoom) / coordinate.zoom
  332. );
  333. }
  334. this.lastX = X;
  335. this.lastY = Y;
  336. break;
  337. case LayerEvents.MoveRoadPoint:
  338. point = dataService.getRoadPoint(draggingItem.vectorId);
  339. listenLayer.start(position, {
  340. exceptRoadPointId: draggingItem.vectorId,
  341. exceptRoadIds: point.parent,
  342. });
  343. if (listenLayer.modifyPoint) {
  344. position = {
  345. x: listenLayer.modifyPoint.x,
  346. y: listenLayer.modifyPoint.y,
  347. };
  348. }
  349. let flag = moveRoad.moveingRoadPoint(
  350. draggingItem.vectorId,
  351. position,
  352. listenLayer.modifyPoint
  353. );
  354. if (!flag) {
  355. elementService.hideAll();
  356. } else {
  357. // point = dataService.getRoadPoint(draggingItem.vectorId);
  358. // listenLayer.start(point, {
  359. // exceptRoadPointId: draggingItem.vectorId,
  360. // exceptRoadIds: point.parent,
  361. // });
  362. let otherPoint = null;
  363. if (
  364. listenLayer.modifyPoint &&
  365. listenLayer.modifyPoint.linkedRoadPointId
  366. ) {
  367. otherPoint = dataService.getRoadPoint(
  368. listenLayer.modifyPoint.linkedRoadPointId
  369. );
  370. } else if (
  371. listenLayer.modifyPoint &&
  372. listenLayer.modifyPoint.linkedRoadPointIdX
  373. ) {
  374. otherPoint = dataService.getRoadPoint(
  375. listenLayer.modifyPoint.linkedRoadPointIdX
  376. );
  377. } else if (
  378. listenLayer.modifyPoint &&
  379. listenLayer.modifyPoint.linkedRoadPointIdY
  380. ) {
  381. otherPoint = dataService.getRoadPoint(
  382. listenLayer.modifyPoint.linkedRoadPointIdY
  383. );
  384. }
  385. elementService.execute(otherPoint, point);
  386. }
  387. needAutoRedraw = true;
  388. break;
  389. case LayerEvents.AddCurveRoad:
  390. needAutoRedraw = true;
  391. listenLayer.start(position);
  392. if (listenLayer.modifyPoint) {
  393. position = {
  394. x: listenLayer.modifyPoint.x,
  395. y: listenLayer.modifyPoint.y,
  396. };
  397. }
  398. elementService.hideAll();
  399. elementService.setPoint(position);
  400. elementService.showPoint();
  401. if (listenLayer.modifyPoint) {
  402. elementService.execute(listenLayer.modifyPoint, position);
  403. }
  404. break;
  405. case LayerEvents.AddingCurveRoad:
  406. needAutoRedraw = true;
  407. listenLayer.start(position);
  408. if (listenLayer.modifyPoint) {
  409. position = {
  410. x: listenLayer.modifyPoint.x,
  411. y: listenLayer.modifyPoint.y,
  412. };
  413. }
  414. elementService.execute(addRoad.startInfo.position, position);
  415. elementService.setPoint(position);
  416. elementService.setNewRoad(addRoad.startInfo.position, position);
  417. elementService.showNewRoad();
  418. addRoad.setNewRoadPoint("end", position);
  419. addRoad.canAdd = addRoad.canAddRoadForEnd(position);
  420. if (!addRoad.canAdd) {
  421. elementService.setNewRoadState("error");
  422. } else {
  423. elementService.setNewRoadState("normal");
  424. }
  425. break;
  426. case LayerEvents.MoveCurveRoad:
  427. needAutoRedraw = true;
  428. moveRoad.moveCurveRoad(
  429. draggingItem.vectorId,
  430. (dx * coordinate.defaultZoom) / coordinate.zoom,
  431. (dy * coordinate.defaultZoom) / coordinate.zoom
  432. );
  433. this.lastX = X;
  434. this.lastY = Y;
  435. break;
  436. case LayerEvents.MoveCurveRoadPoint:
  437. if (!draggingItem || !draggingItem.vectorId) {
  438. return;
  439. }
  440. point = dataService.getCurveRoadPoint(draggingItem.vectorId);
  441. listenLayer.start(position, {
  442. exceptRoadPointId: draggingItem.vectorId,
  443. exceptCurveRoadId: point.parent, //不会融合,所以parent只有一个
  444. });
  445. if (listenLayer.modifyPoint) {
  446. position = {
  447. x: listenLayer.modifyPoint.x,
  448. y: listenLayer.modifyPoint.y,
  449. };
  450. }
  451. moveRoad.moveCurveRoadPoint(draggingItem.vectorId, position);
  452. needAutoRedraw = true;
  453. break;
  454. case LayerEvents.MoveControlPoint:
  455. if (!draggingItem || !draggingItem.vectorId) {
  456. return;
  457. }
  458. moveRoad.moveControlPoint(draggingItem.vectorId, position);
  459. needAutoRedraw = true;
  460. break;
  461. case LayerEvents.MoveEdge:
  462. moveRoad.moveEdge(draggingItem.vectorId, position);
  463. needAutoRedraw = true;
  464. break;
  465. case LayerEvents.MoveCurveEdge:
  466. if (listenLayer.modifyPoint) {
  467. moveRoad.moveCurveEdge(
  468. draggingItem.vectorId,
  469. listenLayer.modifyPoint.selectIndex,
  470. position
  471. );
  472. }
  473. needAutoRedraw = true;
  474. break;
  475. case LayerEvents.MoveLine:
  476. if (draggingItem != null) {
  477. moveLine.moveLine(
  478. draggingItem.vectorId,
  479. (dx * coordinate.defaultZoom) / coordinate.zoom,
  480. (dy * coordinate.defaultZoom) / coordinate.zoom
  481. );
  482. needAutoRedraw = true;
  483. }
  484. this.lastX = X;
  485. this.lastY = Y;
  486. break;
  487. case LayerEvents.MovePoint:
  488. if (draggingItem != null) {
  489. point = dataService.getPoint(draggingItem.vectorId);
  490. listenLayer.start(position, {
  491. exceptPointId: draggingItem.vectorId,
  492. exceptLineId: point.parent,
  493. });
  494. if (listenLayer.modifyPoint) {
  495. position = {
  496. x: listenLayer.modifyPoint.x,
  497. y: listenLayer.modifyPoint.y,
  498. };
  499. }
  500. moveLine.movePoint(position, draggingItem.vectorId);
  501. needAutoRedraw = true;
  502. }
  503. break;
  504. case LayerEvents.MoveCircle:
  505. if (draggingItem != null) {
  506. if (draggingItem.state == SelectState.Select) {
  507. moveCircle.moveFull(
  508. draggingItem.vectorId,
  509. (dx * coordinate.defaultZoom) / coordinate.zoom,
  510. (dy * coordinate.defaultZoom) / coordinate.zoom
  511. );
  512. this.lastX = X;
  513. this.lastY = Y;
  514. } else {
  515. moveCircle.moveRing(position, draggingItem.vectorId);
  516. }
  517. needAutoRedraw = true;
  518. }
  519. break;
  520. case LayerEvents.MoveText:
  521. needAutoRedraw = true;
  522. if (draggingItem != null) {
  523. moveText.moveFullText(position, draggingItem.vectorId);
  524. }
  525. break;
  526. case LayerEvents.MoveMagnifier:
  527. needAutoRedraw = true;
  528. if (draggingItem != null) {
  529. moveMagnifier.moveFullMagnifier(position, draggingItem.vectorId);
  530. }
  531. break;
  532. case LayerEvents.MoveSVG:
  533. needAutoRedraw = true;
  534. if (draggingItem != null) {
  535. moveSVG.moveFullSVG(position, draggingItem.vectorId);
  536. }
  537. break;
  538. }
  539. if (needAutoRedraw) {
  540. this.renderer.autoRedraw();
  541. }
  542. }
  543. onMouseUp(e) {
  544. // 右键
  545. if (e.button == 2) {
  546. return;
  547. }
  548. const X = e.offsetX || e.layerX;
  549. const Y = e.offsetY || e.layerY;
  550. let eventName = stateService.getEventName();
  551. const draggingItem = stateService.getDraggingItem();
  552. const selectItem = stateService.getSelectItem();
  553. let focusItem = null;
  554. if (!this.dragging && selectItem) {
  555. focusItem = {
  556. vectorId: selectItem.vectorId,
  557. type: selectItem.type,
  558. cursor: { x: this.lastX, y: this.lastY },
  559. };
  560. stateService.setFocusItem(focusItem);
  561. this.uiControl.focusVector = focusItem;
  562. stateService.clearDraggingItem();
  563. }
  564. let position = coordinate.getXYFromScreen({
  565. x: X,
  566. y: Y,
  567. });
  568. let needAutoRedraw = false;
  569. switch (eventName) {
  570. case null:
  571. return;
  572. case LayerEvents.PanBackGround:
  573. needAutoRedraw = true;
  574. stateService.clearFocusItem();
  575. this.uiControl.focusVector = null;
  576. this.uiControl.currentUI = null;
  577. break;
  578. case LayerEvents.MoveRoadPoint:
  579. if (!draggingItem || !draggingItem.vectorId) {
  580. break;
  581. }
  582. needAutoRedraw = true;
  583. elementService.hideAll();
  584. let point = dataService.getRoadPoint(draggingItem.vectorId);
  585. if (point) {
  586. listenLayer.start(point, {
  587. exceptPointId: draggingItem.vectorId,
  588. exceptRoadIds: point.parent,
  589. });
  590. if (
  591. listenLayer.modifyPoint &&
  592. listenLayer.modifyPoint.hasOwnProperty("linkedRoadPointId")
  593. ) {
  594. moveRoad.moveTo(
  595. draggingItem.vectorId,
  596. listenLayer.modifyPoint.linkedRoadPointId
  597. );
  598. } else if (
  599. listenLayer.modifyPoint &&
  600. (listenLayer.modifyPoint.linkedRoadPointIdX ||
  601. listenLayer.modifyPoint.linkedRoadPointIdY)
  602. ) {
  603. mathUtil.clonePoint(point, listenLayer.modifyPoint);
  604. } else if (
  605. listenLayer.modifyPoint &&
  606. listenLayer.modifyPoint.hasOwnProperty("linkedRoadId")
  607. ) {
  608. point = roadPointService.create({
  609. x: listenLayer.modifyPoint.x,
  610. y: listenLayer.modifyPoint.y,
  611. });
  612. roadService.splitRoad(
  613. listenLayer.modifyPoint.linkedRoadId,
  614. point.vectorId,
  615. "start"
  616. );
  617. moveRoad.moveTo(draggingItem.vectorId, point.vectorId);
  618. } else if (moveRoad.splitRoadId != null) {
  619. roadService.splitRoad(
  620. moveRoad.splitRoadId,
  621. draggingItem.vectorId,
  622. "start"
  623. );
  624. }
  625. //draggingItem.vectorId所在的墙面与其他墙角相交
  626. moveRoad.updateForAbsorbRoadPoints();
  627. let parent = point.getParent();
  628. for (let key in parent) {
  629. roadService.setLanes(key);
  630. }
  631. this.history.save();
  632. }
  633. break;
  634. // case LayerEvents.AddRoad:
  635. // addRoad.setNewRoadPoint("start", position);
  636. // break;
  637. case LayerEvents.AddingRoad:
  638. needAutoRedraw = true;
  639. if (addRoad.canAdd) {
  640. addRoad.buildRoad();
  641. this.history.save();
  642. elementService.hideAll();
  643. }
  644. break;
  645. case LayerEvents.AddingLine:
  646. needAutoRedraw = true;
  647. addLine.finish(position);
  648. addLine.clearVectorData();
  649. this.history.save();
  650. elementService.hideAll();
  651. break;
  652. case LayerEvents.AddingCircle:
  653. needAutoRedraw = true;
  654. addCircle.finish(position);
  655. addCircle.clear();
  656. this.history.save();
  657. elementService.hideAll();
  658. break;
  659. case LayerEvents.MoveText:
  660. needAutoRedraw = true;
  661. this.history.save();
  662. elementService.hideAll();
  663. break;
  664. case LayerEvents.MoveMagnifier:
  665. needAutoRedraw = true;
  666. this.history.save();
  667. elementService.hideAll();
  668. break;
  669. case LayerEvents.MoveSVG:
  670. needAutoRedraw = true;
  671. this.history.save();
  672. elementService.hideAll();
  673. break;
  674. case LayerEvents.AddingCurveRoad:
  675. needAutoRedraw = true;
  676. if (addRoad.canAdd) {
  677. addRoad.buildCurveRoad();
  678. this.history.save();
  679. elementService.hideAll();
  680. }
  681. break;
  682. case LayerEvents.MoveRoad:
  683. needAutoRedraw = true;
  684. this.history.save();
  685. break;
  686. case LayerEvents.MoveCurveRoad:
  687. needAutoRedraw = true;
  688. this.history.save();
  689. break;
  690. case LayerEvents.MoveCurveRoadPoint:
  691. needAutoRedraw = true;
  692. this.history.save();
  693. break;
  694. case LayerEvents.MoveControlPoint:
  695. needAutoRedraw = true;
  696. this.history.save();
  697. break;
  698. case LayerEvents.MoveEdge:
  699. needAutoRedraw = true;
  700. this.history.save();
  701. break;
  702. case LayerEvents.MoveCurveEdge:
  703. needAutoRedraw = true;
  704. this.history.save();
  705. break;
  706. case LayerEvents.MoveLine:
  707. needAutoRedraw = true;
  708. this.history.save();
  709. break;
  710. case LayerEvents.MovePoint:
  711. needAutoRedraw = true;
  712. this.history.save();
  713. break;
  714. }
  715. this.setEventName("mouseUp");
  716. stateService.clearDraggingItem();
  717. if (needAutoRedraw) {
  718. this.renderer.autoRedraw();
  719. }
  720. }
  721. onWheel(e) {
  722. e.preventDefault();
  723. const type = e.type;
  724. if (type == "DOMMouseScroll" || type == "mousewheel") {
  725. // 当在canvas用滚轮滚动时
  726. const delta = e.wheelDelta
  727. ? (e.wheelDelta / 120) * 20
  728. : (-(e.detail || 0) / 3) * 20;
  729. const zoom = coordinate.zoom + delta;
  730. if (zoom < 14) {
  731. return;
  732. }
  733. coordinate.updateZoom(zoom);
  734. dataService.setGridForZoom(
  735. coordinate.width,
  736. coordinate.height,
  737. coordinate.zoom / coordinate.defaultZoom
  738. );
  739. this.renderer.autoRedraw();
  740. }
  741. }
  742. //测试用
  743. onKeydown(e) {
  744. let focusItem = stateService.getFocusItem();
  745. let dir = "left";
  746. if (focusItem) {
  747. console.log("键盘(foucus有效):" + e.code);
  748. if (e.code == "Delete") {
  749. //删除
  750. const road = dataService.getRoad(focusItem.vectorId);
  751. roadService.subtraRoadFromIntersect(road.startId, focusItem.vectorId);
  752. roadService.subtraRoadFromIntersect(road.endId, focusItem.vectorId);
  753. //dataService.deleteControlPoint()
  754. dataService.deleteRoad(focusItem.vectorId);
  755. this.renderer.autoRedraw();
  756. this.history.save();
  757. }
  758. //加宽
  759. else if (e.code == "KeyA") {
  760. let road = dataService.getRoad(focusItem.vectorId);
  761. if (road) {
  762. roadService.updateForWidth(road.vectorId, road.leftWidth + 50, dir);
  763. } else {
  764. road = dataService.getCurveRoad(focusItem.vectorId);
  765. curveRoadService.updateForWidth(
  766. road.vectorId,
  767. road.leftWidth + 50,
  768. dir
  769. );
  770. }
  771. this.renderer.autoRedraw();
  772. this.history.save();
  773. }
  774. //变窄
  775. else if (e.code == "KeyB") {
  776. let road = dataService.getRoad(focusItem.vectorId);
  777. if (road) {
  778. roadService.updateForWidth(road.vectorId, road.leftWidth - 25, dir);
  779. } else {
  780. road = dataService.getCurveRoad(focusItem.vectorId);
  781. curveRoadService.updateForWidth(
  782. road.vectorId,
  783. road.leftWidth - 25,
  784. dir
  785. );
  786. }
  787. this.renderer.autoRedraw();
  788. this.history.save();
  789. }
  790. //添加左车道
  791. else if (e.code == "KeyQ") {
  792. let road = dataService.getRoad(focusItem.vectorId);
  793. if (road) {
  794. roadService.updateForAddSubtractLanesCount(
  795. focusItem.vectorId,
  796. road.leftDrivewayCount + 1,
  797. "left"
  798. );
  799. } else {
  800. road = dataService.getCurveRoad(focusItem.vectorId);
  801. curveRoadService.updateForAddSubtractLanesCount(
  802. road,
  803. road.leftDrivewayCount + 1, //rightDrivewayCount
  804. "left"
  805. );
  806. }
  807. this.renderer.autoRedraw();
  808. this.history.save();
  809. }
  810. //减少左车道
  811. else if (e.code == "KeyW") {
  812. let road = dataService.getRoad(focusItem.vectorId);
  813. if (road) {
  814. roadService.updateForAddSubtractLanesCount(
  815. focusItem.vectorId,
  816. road.leftDrivewayCount - 1,
  817. "left"
  818. );
  819. } else {
  820. road = dataService.getCurveRoad(focusItem.vectorId);
  821. curveRoadService.updateForAddSubtractLanesCount(
  822. road,
  823. road.leftDrivewayCount - 1, //rightDrivewayCount
  824. "left"
  825. );
  826. }
  827. this.renderer.autoRedraw();
  828. this.history.save();
  829. }
  830. //添加右车道
  831. else if (e.code == "KeyE") {
  832. let road = dataService.getRoad(focusItem.vectorId);
  833. if (road) {
  834. roadService.updateForAddSubtractLanesCount(
  835. focusItem.vectorId,
  836. road.rightDrivewayCount + 1,
  837. "right"
  838. );
  839. } else {
  840. road = dataService.getCurveRoad(focusItem.vectorId);
  841. curveRoadService.updateForAddSubtractLanesCount(
  842. road,
  843. road.rightDrivewayCount + 1, //rightDrivewayCount
  844. "right"
  845. );
  846. }
  847. this.renderer.autoRedraw();
  848. this.history.save();
  849. }
  850. //减少右车道
  851. else if (e.code == "KeyR") {
  852. let road = dataService.getRoad(focusItem.vectorId);
  853. if (road) {
  854. roadService.updateForAddSubtractLanesCount(
  855. focusItem.vectorId,
  856. road.rightDrivewayCount - 1,
  857. "right"
  858. );
  859. } else {
  860. road = dataService.getCurveRoad(focusItem.vectorId);
  861. curveRoadService.updateForAddSubtractLanesCount(
  862. road,
  863. road.rightDrivewayCount - 1, //rightDrivewayCount
  864. "right"
  865. );
  866. }
  867. this.renderer.autoRedraw();
  868. this.history.save();
  869. }
  870. //弯路添加控制点
  871. else if (e.code == "KeyT") {
  872. const curveRoad = dataService.getCurveRoad(focusItem.vectorId);
  873. let index = mathUtil.getIndexForCurvesPoints(
  874. this.mousePosition,
  875. curveRoad.points
  876. );
  877. if (index != -1) {
  878. curveRoadService.addCPoint(curveRoad, this.mousePosition, index);
  879. } else {
  880. const dis1 = mathUtil.getDistance(
  881. curveRoad.points[0],
  882. this.mousePosition
  883. );
  884. const dis2 = mathUtil.getDistance(
  885. curveRoad.points[curveRoad.points.length - 1],
  886. this.mousePosition
  887. );
  888. if (dis1 > dis2) {
  889. curveRoadService.addCPoint(
  890. curveRoad,
  891. this.mousePosition,
  892. curveRoad.points.length - 2
  893. );
  894. } else {
  895. curveRoadService.addCPoint(curveRoad, this.mousePosition, 1);
  896. }
  897. }
  898. this.renderer.autoRedraw();
  899. this.history.save();
  900. }
  901. //弯路删除控制点
  902. else if (e.code == "KeyY") {
  903. const curvePoint = dataService.getCurveRoadPoint(focusItem.vectorId);
  904. const curveRoad = dataService.getCurveRoad(curvePoint.parent);
  905. curveRoadService.subCPoint(curveRoad, curvePoint.getIndex());
  906. this.renderer.autoRedraw();
  907. this.history.save();
  908. }
  909. } else {
  910. console.log("键盘(foucus无效):" + e.code);
  911. }
  912. }
  913. setEventName(eventType) {
  914. let eventName = stateService.getEventName();
  915. if (eventType == "mouseDown") {
  916. if (eventName == null) {
  917. const selectItem = stateService.getSelectItem();
  918. if (selectItem == null) {
  919. stateService.setEventName(LayerEvents.PanBackGround);
  920. } else if (selectItem.type == VectorType.Road) {
  921. stateService.setEventName(LayerEvents.MoveRoad);
  922. } else if (selectItem.type == VectorType.RoadPoint) {
  923. stateService.setEventName(LayerEvents.MoveRoadPoint);
  924. } else if (selectItem.type == VectorType.CurveRoad) {
  925. stateService.setEventName(LayerEvents.MoveCurveRoad);
  926. } else if (selectItem.type == VectorType.CurveRoadPoint) {
  927. stateService.setEventName(LayerEvents.MoveCurveRoadPoint);
  928. } else if (selectItem.type == VectorType.ControlPoint) {
  929. stateService.setEventName(LayerEvents.MoveControlPoint);
  930. } else if (selectItem.type == VectorType.RoadEdge) {
  931. stateService.setEventName(LayerEvents.MoveEdge);
  932. } else if (selectItem.type == VectorType.CurveRoadEdge) {
  933. stateService.setEventName(LayerEvents.MoveCurveEdge);
  934. } else if (selectItem.type == VectorType.Point) {
  935. stateService.setEventName(LayerEvents.MovePoint);
  936. } else if (selectItem.type == VectorType.Line) {
  937. stateService.setEventName(LayerEvents.MoveLine);
  938. } else if (selectItem.type == VectorType.CurveLine) {
  939. stateService.setEventName(LayerEvents.MoveCurveLine);
  940. } else if (selectItem.type == VectorType.Circle) {
  941. stateService.setEventName(LayerEvents.MoveCircle);
  942. } else if (selectItem.type == VectorType.Text) {
  943. stateService.setEventName(LayerEvents.MoveText);
  944. } else if (selectItem.type == VectorType.Magnifier) {
  945. stateService.setEventName(LayerEvents.MoveMagnifier);
  946. } else if (selectItem.type == VectorType.SVG) {
  947. stateService.setEventName(LayerEvents.MoveSVG);
  948. }
  949. }
  950. } else if (eventType == "mouseUp") {
  951. if (eventName == LayerEvents.AddingRoad) {
  952. stateService.setEventName(LayerEvents.AddRoad);
  953. } else if (eventName == LayerEvents.AddingLine) {
  954. stateService.setEventName(LayerEvents.AddLine);
  955. } else if (eventName == LayerEvents.AddingCurveRoad) {
  956. stateService.setEventName(LayerEvents.AddCurveRoad);
  957. } else if (eventName == LayerEvents.AddLine) {
  958. stateService.setEventName(LayerEvents.AddingLine);
  959. } else if (eventName == LayerEvents.AddingLine) {
  960. stateService.setEventName(LayerEvents.AddLine);
  961. } else {
  962. stateService.clearEventName();
  963. }
  964. }
  965. }
  966. exit() {
  967. stateService.clear();
  968. this.uiControl.clearUI();
  969. this.uiControl.focusVector = null;
  970. }
  971. stopAddVector() {
  972. let eventName = stateService.getEventName();
  973. if (
  974. eventName != LayerEvents.AddingRoad &&
  975. eventName != LayerEvents.AddingLine
  976. ) {
  977. stateService.clearEventName();
  978. } else if (eventName == LayerEvents.AddingRoad) {
  979. stateService.setEventName(LayerEvents.AddRoad);
  980. } else if (eventName == LayerEvents.AddingLine) {
  981. stateService.setEventName(LayerEvents.AddLine);
  982. }
  983. addLine.clear(); //之前会保留category
  984. this.uiControl.clearUI();
  985. elementService.hideAll();
  986. }
  987. }