Layer.js 30 KB

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