Layer.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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 { locationModeControl } from "./Controls/LocationModeControl";
  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 { addCrossRoad } from "./Controls/AddCrossRoad";
  14. import { addLine } from "./Controls/AddLine";
  15. import { addPoint } from "./Controls/AddPoint";
  16. import { addCircle } from "./Controls/AddCircle";
  17. import { addText } from "./Controls/AddText";
  18. import { addMagnifier } from "./Controls/AddMagnifier";
  19. import { addSVG } from "./Controls/AddSVG";
  20. import { moveRoad } from "./Controls/MoveRoad";
  21. import { moveLine } from "./Controls/MoveLine";
  22. import { movePoint } from "./Controls/MovePoint";
  23. import { moveCircle } from "./Controls/MoveCircle";
  24. import { coordinate } from "./Coordinate";
  25. import Render from "./Renderer/Render";
  26. import { draw } from "./Renderer/Draw";
  27. import { listenLayer } from "./ListenLayer";
  28. import LayerEvents from "./enum/LayerEvents.js";
  29. import UIEvents from "./enum/UIEvents.js";
  30. import SelectState from "./enum/SelectState.js";
  31. import VectorType from "./enum/VectorType";
  32. import { mathUtil } from "./Util/MathUtil";
  33. import History from "./History/History";
  34. import mitt from "mitt";
  35. import { roadService } from "./Service/RoadService";
  36. import { edgeService } from "./Service/EdgeService";
  37. import { roadPointService } from "./Service/RoadPointService";
  38. import { curveRoadService } from "./Service/CurveRoadService";
  39. import { curveRoadPointService } from "./Service/CurveRoadPointService";
  40. import VectorCategory from "./enum/VectorCategory";
  41. import Settings from "./Settings";
  42. import Constant from "./Constant";
  43. import { uiService } from "./Service/UIService";
  44. import { imageService } from "./Service/ImageService";
  45. import VectorEvents from "./enum/VectorEvents";
  46. import { lineService } from "./Service/LineService";
  47. const minDragDis = 10;
  48. const minZoom = 20;
  49. const maxZoom = 800;
  50. export default class Layer {
  51. constructor(canvas, newsletter, graphicState) {
  52. this.canvas = canvas;
  53. this.load = new Load(this);
  54. this.uiControl = new UIControl(this, newsletter, graphicState);
  55. this.renderer = new Render(this);
  56. this.history = new History(this);
  57. this.coordinate = coordinate;
  58. this.mousePosition = null;
  59. this.dragging = false; // 当前是否正在拖拽
  60. this.start();
  61. Object.setPrototypeOf(Object.getPrototypeOf(this), mitt());
  62. }
  63. start() {
  64. if (this.canvas) {
  65. this.canvas.width = this.canvas.clientWidth;
  66. this.canvas.height = this.canvas.clientHeight;
  67. coordinate.init(this.canvas);
  68. draw.initContext(this.canvas);
  69. dataService.initVectorData();
  70. this.bindEvents();
  71. }
  72. window.vectorData = dataService.vectorData;
  73. }
  74. bindEvents() {
  75. this.canvas.addEventListener("contextmenu", function (e) {
  76. e.preventDefault();
  77. });
  78. this.canvas.addEventListener("mousedown", this.onMouseDown.bind(this));
  79. this.canvas.addEventListener("mousemove", this.onMouseMove.bind(this));
  80. this.canvas.addEventListener("mouseup", this.onMouseUp.bind(this));
  81. this.canvas.addEventListener("touchstart", this.onMouseDown.bind(this));
  82. this.canvas.addEventListener("touchmove", this.onMouseMove.bind(this));
  83. this.canvas.addEventListener("touchend", this.onMouseUp.bind(this));
  84. this.canvas.addEventListener("mousewheel", this.onWheel.bind(this));
  85. this.canvas.addEventListener("DOMMouseScroll", this.onWheel.bind(this));
  86. this.canvas.addEventListener("resize", this.reSize.bind(this));
  87. document.addEventListener("keydown", this.onKeydown.bind(this));
  88. }
  89. reSize = function () {
  90. console.log("resize");
  91. coordinate.updateForCanvas();
  92. this.renderer.autoRedraw();
  93. };
  94. onMouseDown(e) {
  95. if (e instanceof TouchEvent) {
  96. this.startX = e.touches[0].pageX;
  97. this.startY = e.touches[0].pageY;
  98. this.lastX = e.touches[0].pageX;
  99. this.lastY = e.touches[0].pageY;
  100. } else {
  101. this.startX = e.offsetX || e.layerX;
  102. this.startY = e.offsetY || e.layerY;
  103. this.lastX = e.offsetX || e.layerX;
  104. this.lastY = e.offsetY || e.layerY;
  105. }
  106. let position = coordinate.getXYFromScreen({
  107. x: this.startX,
  108. y: this.startY,
  109. });
  110. // 右键
  111. if (e.button == 2) {
  112. this.stopAddVector();
  113. return;
  114. }
  115. this.dragging = false;
  116. //用于支持平板电脑
  117. listenLayer.start(position);
  118. let selectItem = stateService.getSelectItem();
  119. let focusItem = stateService.getFocusItem();
  120. if (selectItem && focusItem && selectItem.vectorId != focusItem.vectorId) {
  121. stateService.clearFocusItem();
  122. }
  123. this.setEventName("mouseDown");
  124. const eventName = stateService.getEventName();
  125. switch (eventName) {
  126. case LayerEvents.AddRoad:
  127. stateService.setEventName(LayerEvents.AddingRoad);
  128. addRoad.setNewRoadPoint("start", position);
  129. break;
  130. case LayerEvents.AddCurveRoad:
  131. stateService.setEventName(LayerEvents.AddingCurveRoad);
  132. addRoad.setNewRoadPoint("start", position);
  133. break;
  134. case LayerEvents.AddLine:
  135. stateService.setEventName(LayerEvents.AddingLine);
  136. addLine.setNewLinePoint(position);
  137. break;
  138. // case LayerEvents.AddCurveLine:
  139. // stateService.setEventName(LayerEvents.AddingCurveLine);
  140. // addLine.setNewLinePoint(position);
  141. // break;
  142. case LayerEvents.AddPoint:
  143. stateService.setEventName(LayerEvents.MovePoint);
  144. const newPoint = addPoint.buildPoint(position);
  145. if (newPoint) {
  146. stateService.setSelectItem(newPoint.vectorId, VectorType.Point, SelectState.Select);
  147. this.renderer.autoRedraw();
  148. }
  149. break;
  150. case LayerEvents.AddCircle:
  151. stateService.setEventName(LayerEvents.AddingCircle);
  152. addCircle.setCenter(position);
  153. break;
  154. case LayerEvents.AddText:
  155. stateService.setEventName(LayerEvents.MoveText);
  156. addText.buildText(position);
  157. stateService.setSelectItem(addText.newText.vectorId, VectorType.Text, SelectState.Select);
  158. addText.clear();
  159. break;
  160. case LayerEvents.AddSVG:
  161. stateService.setEventName(LayerEvents.MoveSVG);
  162. addSVG.buildSVG(position, Settings.selectSVGType);
  163. // addSVG.newSVG.setType(Settings.selectSVGType);
  164. stateService.setSelectItem(addSVG.newSVG.vectorId, VectorType.SVG, -1);
  165. addSVG.clear();
  166. break;
  167. case LayerEvents.AddMagnifier:
  168. stateService.setEventName(LayerEvents.MoveMagnifier);
  169. addMagnifier.buildMagnifier(position);
  170. stateService.setSelectItem(addMagnifier.newMagnifier.vectorId, VectorType.Magnifier, SelectState.Select);
  171. addMagnifier.clear();
  172. break;
  173. case VectorEvents.AddLane:
  174. let selectAddLaneFlag = false;
  175. if (selectItem && selectItem.dir && selectItem.vectorId) {
  176. let road = dataService.getRoad(selectItem.vectorId);
  177. if (road) {
  178. selectAddLaneFlag = true;
  179. let roadLanCount = road.getLanesCount(selectItem.dir);
  180. if (selectItem.dir == "left") {
  181. roadService.updateForAddSubtractLanesCount(road.vectorId, roadLanCount + 1, selectItem.dir);
  182. } else {
  183. roadService.updateForAddSubtractLanesCount(road.vectorId, roadLanCount + 1, selectItem.dir);
  184. }
  185. roadService.initRoadWidthTipsPos(road);
  186. let leftEdge = dataService.getRoadEdge(road.leftEdgeId);
  187. let rightEdge = dataService.getRoadEdge(road.rightEdgeId);
  188. if (leftEdge.roadSide) {
  189. leftEdge.initRoadSide();
  190. }
  191. if (rightEdge.roadSide) {
  192. rightEdge.initRoadSide();
  193. }
  194. } else {
  195. road = dataService.getCurveRoad(selectItem.vectorId);
  196. if (road) {
  197. selectAddLaneFlag = true;
  198. let curveRoadLanCount = road.getLanesCount(selectItem.dir);
  199. if (selectItem.dir == "left") {
  200. curveRoadService.updateForAddSubtractLanesCount(road.vectorId, curveRoadLanCount + 1, selectItem.dir);
  201. } else {
  202. curveRoadService.updateForAddSubtractLanesCount(road.vectorId, curveRoadLanCount + 1, selectItem.dir);
  203. }
  204. let leftEdge = dataService.getRoadEdge(road.leftEdgeId);
  205. let rightEdge = dataService.getRoadEdge(road.rightEdgeId);
  206. // if (leftEdge.roadSide) {
  207. // leftEdge.initCurveRoadSide();
  208. // }
  209. // if (rightEdge.roadSide) {
  210. // rightEdge.initCurveRoadSide();
  211. // }
  212. }
  213. }
  214. this.history.save();
  215. this.renderer.autoRedraw();
  216. }
  217. if (!selectAddLaneFlag) {
  218. stateService.clearEventName();
  219. }
  220. break;
  221. case VectorEvents.DelLane:
  222. let selectDelLaneFlag = false;
  223. if (selectItem && selectItem.dir && selectItem.vectorId) {
  224. let road = dataService.getRoad(selectItem.vectorId);
  225. if (road) {
  226. selectDelLaneFlag = true;
  227. let roadLanCount = road.getLanesCount(selectItem.dir);
  228. if (selectItem.dir == "left") {
  229. roadService.updateForAddSubtractLanesCount(road.vectorId, roadLanCount - 1, selectItem.dir);
  230. } else {
  231. roadService.updateForAddSubtractLanesCount(road.vectorId, roadLanCount - 1, selectItem.dir);
  232. }
  233. let leftEdge = dataService.getRoadEdge(road.leftEdgeId);
  234. let rightEdge = dataService.getRoadEdge(road.rightEdgeId);
  235. if (leftEdge.roadSide) {
  236. leftEdge.initRoadSide();
  237. }
  238. if (rightEdge.roadSide) {
  239. rightEdge.initRoadSide();
  240. }
  241. roadService.initRoadWidthTipsPos(road);
  242. } else {
  243. road = dataService.getCurveRoad(selectItem.vectorId);
  244. if (road) {
  245. selectDelLaneFlag = true;
  246. let curveRoadLanCount = road.getLanesCount(selectItem.dir);
  247. if (selectItem.dir == "left") {
  248. curveRoadService.updateForAddSubtractLanesCount(road.vectorId, curveRoadLanCount - 1, selectItem.dir);
  249. } else {
  250. curveRoadService.updateForAddSubtractLanesCount(road.vectorId, curveRoadLanCount - 1, selectItem.dir);
  251. }
  252. let leftEdge = dataService.getRoadEdge(road.leftEdgeId);
  253. let rightEdge = dataService.getRoadEdge(road.rightEdgeId);
  254. // 暂时屏蔽 曲路暂时还没有 会报错
  255. // if (leftEdge.roadSide) {
  256. // leftEdge.initCurveRoadSide();
  257. // }
  258. // if (rightEdge.roadSide) {
  259. // rightEdge.initCurveRoadSide();
  260. // }
  261. }
  262. }
  263. // roadService.initRoadWidthTipsPos(road);
  264. this.history.save();
  265. this.renderer.autoRedraw();
  266. }
  267. if (!selectDelLaneFlag) {
  268. stateService.clearEventName();
  269. }
  270. break;
  271. case VectorEvents.AddCrossPoint:
  272. if (focusItem && focusItem.vectorId) {
  273. if (focusItem.type == VectorType.CurveRoad) {
  274. const curveRoad = dataService.getCurveRoad(focusItem.vectorId);
  275. let index = mathUtil.getIndexForCurvesPoints(position, curveRoad.points);
  276. if (index != -1) {
  277. curveRoadService.addCPoint(curveRoad, position, index);
  278. } else {
  279. const dis1 = mathUtil.getDistance(curveRoad.points[0], position);
  280. const dis2 = mathUtil.getDistance(curveRoad.points[curveRoad.points.length - 1], position);
  281. if (dis1 > dis2) {
  282. index = curveRoad.points.length - 2;
  283. } else {
  284. index = 1;
  285. }
  286. curveRoadService.addCPoint(curveRoad, position, index);
  287. }
  288. curveRoadService.setLanes(curveRoad.vectorId);
  289. // curveRoadService.updateForMovePoint(
  290. // curveRoad.points[index + 1].vectorId,
  291. // position
  292. // );
  293. } else if (focusItem.type == VectorType.Line) {
  294. let line = dataService.getLine(focusItem.vectorId);
  295. const weight = line.getWeight();
  296. let startPoint = dataService.getPoint(line.startId);
  297. let endPoint = dataService.getPoint(line.endId);
  298. dataService.deleteLine(focusItem.vectorId);
  299. let curveLine = lineService.createCurveLine(startPoint, endPoint);
  300. mathUtil.clonePoint(curveLine.points[1], position);
  301. curveLine.curves = mathUtil.getCurvesByPoints(curveLine.points);
  302. curveLine.setWeight(weight);
  303. const style = line.getStyle();
  304. curveLine.setStyle(style);
  305. } else if (focusItem.type == VectorType.CurveLine) {
  306. let curveLine = dataService.getCurveLine(focusItem.vectorId);
  307. let index = mathUtil.getIndexForCurvesPoints(position, curveLine.points);
  308. if (index != -1) {
  309. lineService.addCPoint(position, index, focusItem.vectorId);
  310. } else {
  311. const dis1 = mathUtil.getDistance(curveLine.points[0], position);
  312. const dis2 = mathUtil.getDistance(curveLine.points[curveLine.points.length - 1], position);
  313. if (dis1 > dis2) {
  314. index = curveLine.points.length - 2;
  315. } else {
  316. index = 1;
  317. }
  318. lineService.addCPoint(position, index, focusItem.vectorId);
  319. }
  320. }
  321. stateService.clearEventName();
  322. this.history.save();
  323. this.renderer.autoRedraw();
  324. }
  325. break;
  326. case VectorEvents.MinusCrossPoint:
  327. if (selectItem && selectItem.vectorId) {
  328. const curvePoint = dataService.getCurveRoadPoint(selectItem.vectorId);
  329. if (curvePoint) {
  330. const curveRoad = dataService.getCurveRoad(curvePoint.parent);
  331. if (curveRoad.points.length > 3) {
  332. curveRoadService.subCPoint(curveRoad, curvePoint.getIndex());
  333. curveRoadService.setLanes(curveRoad.vectorId);
  334. stateService.clearEventName();
  335. }
  336. this.history.save();
  337. this.renderer.autoRedraw();
  338. }
  339. }
  340. break;
  341. }
  342. selectItem = stateService.getSelectItem();
  343. stateService.setDraggingItem(selectItem);
  344. stateService.clearFocusItem();
  345. if (selectItem && this.uiControl.focusVector && selectItem.vectorId == this.uiControl.focusVector.vectorId) {
  346. } else {
  347. this.uiControl.clearFocusVector();
  348. }
  349. // 清除上一个状态
  350. // 设置当前事件名称
  351. e.preventDefault();
  352. e.stopPropagation();
  353. }
  354. onMouseMove(e) {
  355. let X = e.offsetX || e.layerX;
  356. let Y = e.offsetY || e.layerY;
  357. if (e instanceof TouchEvent) {
  358. X = e.touches[0].pageX;
  359. Y = e.touches[0].pageY;
  360. //切换到缩放
  361. if (e.touches.length > 1) {
  362. //记录开始的两个触摸点的坐标
  363. if (!this.StorePage1 || !this.StorePage2) {
  364. this.onMouseUp(e);
  365. this.StorePage1 = { x: e.touches[0].pageX, y: e.touches[0].pageY };
  366. this.StorePage2 = { x: e.touches[1].pageX, y: e.touches[1].pageY };
  367. this.StoreMidePage = {
  368. x: (this.StorePage1.x + this.StorePage2.x) / 2,
  369. y: (this.StorePage1.y + this.StorePage2.y) / 2,
  370. };
  371. return;
  372. }
  373. const point1 = {
  374. x: e.touches[0].pageX,
  375. y: e.touches[0].pageY,
  376. };
  377. const point2 = {
  378. x: e.touches[1].pageX,
  379. y: e.touches[1].pageY,
  380. };
  381. let dis1 = mathUtil.getDistance(point1, point2) / 100;
  382. let dis2 = mathUtil.getDistance(this.StorePage1, this.StorePage2) / 100;
  383. let zoom = (coordinate.zoom * dis1) / dis2;
  384. this.zoomVector(this.StoreMidePage, zoom);
  385. mathUtil.clonePoint(this.StorePage1, point1);
  386. mathUtil.clonePoint(this.StorePage2, point2);
  387. return;
  388. }
  389. }
  390. let dx = X - this.lastX;
  391. let dy = Y - this.lastY;
  392. dx = dx * coordinate.ratio;
  393. dy = dy * coordinate.ratio;
  394. let position = coordinate.getXYFromScreen({
  395. x: X,
  396. y: Y,
  397. });
  398. this.mousePosition = {
  399. x: position.x,
  400. y: position.y,
  401. };
  402. const eventName = stateService.getEventName();
  403. if (!this.dragging && Math.abs(X - this.startX) < minDragDis && Math.abs(Y - this.startY) < minDragDis) {
  404. return;
  405. }
  406. this.dragging = true;
  407. if (Math.abs(X - this.startX) > minDragDis || Math.abs(Y - this.startY) > minDragDis) {
  408. // 是否拖拽了
  409. if (eventName != null) {
  410. if (eventName == LayerEvents.MoveMagnifier && stateService.getSelectItem().state != 0) {
  411. } else {
  412. stateService.clearFocusItem();
  413. this.uiControl.clearFocusVector();
  414. }
  415. }
  416. }
  417. // 是否需要重绘
  418. let needAutoRedraw = false;
  419. let point = null;
  420. this.lastX = X;
  421. this.lastY = Y;
  422. const draggingItem = stateService.getDraggingItem();
  423. switch (eventName) {
  424. case null:
  425. //监控
  426. needAutoRedraw = listenLayer.start(position);
  427. let seleteItem = stateService.getSelectItem();
  428. if (seleteItem != null) {
  429. // console.log("选中:" + seleteItem.vectorId);
  430. } else {
  431. }
  432. break;
  433. case LayerEvents.PanBackGround:
  434. stateService.clearItems();
  435. let center = {};
  436. center.x = coordinate.center.x - (dx * coordinate.defaultZoom) / coordinate.zoom;
  437. center.y = coordinate.center.y + (dy * coordinate.defaultZoom) / coordinate.zoom;
  438. let tempCenter = {};
  439. mathUtil.clonePoint(tempCenter, coordinate.center);
  440. mathUtil.clonePoint(coordinate.center, center);
  441. let inScreen = imageService.backgroundImgVertexsInScreenXY();
  442. if (!inScreen) {
  443. dataService.setGridForPan();
  444. needAutoRedraw = true;
  445. } else {
  446. mathUtil.clonePoint(coordinate.center, tempCenter);
  447. }
  448. break;
  449. case LayerEvents.AddRoad:
  450. needAutoRedraw = true;
  451. listenLayer.start(position);
  452. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  453. position = {
  454. x: listenLayer.modifyPoint.x,
  455. y: listenLayer.modifyPoint.y,
  456. };
  457. }
  458. elementService.hideAll();
  459. //鼠标样式
  460. elementService.setPoint(position);
  461. elementService.showPoint();
  462. this.showElementLine(position, eventName);
  463. break;
  464. case LayerEvents.AddLine:
  465. needAutoRedraw = true;
  466. listenLayer.start(position);
  467. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  468. position = {
  469. x: listenLayer.modifyPoint.x,
  470. y: listenLayer.modifyPoint.y,
  471. };
  472. }
  473. elementService.hideAll();
  474. elementService.setPoint(position);
  475. elementService.showPoint();
  476. this.showElementLine(position, eventName);
  477. break;
  478. case LayerEvents.AddCurveLine:
  479. needAutoRedraw = true;
  480. listenLayer.start(position);
  481. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  482. position = {
  483. x: listenLayer.modifyPoint.x,
  484. y: listenLayer.modifyPoint.y,
  485. };
  486. }
  487. elementService.hideAll();
  488. elementService.setPoint(position);
  489. elementService.showPoint();
  490. this.showElementLine(position, eventName);
  491. break;
  492. case LayerEvents.AddCircle:
  493. needAutoRedraw = true;
  494. listenLayer.start(position);
  495. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  496. position = {
  497. x: listenLayer.modifyPoint.x,
  498. y: listenLayer.modifyPoint.y,
  499. };
  500. }
  501. elementService.hideAll();
  502. elementService.setPoint(position);
  503. elementService.showPoint();
  504. this.showElementLine(position);
  505. break;
  506. case LayerEvents.AddingRoad:
  507. needAutoRedraw = true;
  508. listenLayer.start(position);
  509. // listenLayer.start(position, {
  510. // exceptLineId: exceptLineId,
  511. // exceptPointId: exceptPointId,
  512. // addRoad.startInfo.linkedRoadId
  513. // addRoad.startInfo.linkedRoadPointId
  514. // });
  515. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  516. position = {
  517. x: listenLayer.modifyPoint.x,
  518. y: listenLayer.modifyPoint.y,
  519. };
  520. // console.log(
  521. // "LayerEvents.AddingRoad:" + JSON.stringify(listenLayer.modifyPoint)
  522. // );
  523. }
  524. elementService.execute(addRoad.startInfo.position, position);
  525. elementService.setPoint(position);
  526. elementService.setNewRoad(addRoad.startInfo.position, position);
  527. elementService.showNewRoad();
  528. addRoad.setNewRoadPoint("end", position);
  529. addRoad.canAdd = addRoad.canAddRoadForEnd(position);
  530. if (!addRoad.canAdd) {
  531. elementService.setNewRoadState("error");
  532. } else {
  533. elementService.setNewRoadState("normal");
  534. }
  535. elementService.showPoint();
  536. this.showElementLine(position, eventName);
  537. break;
  538. case LayerEvents.AddingLine:
  539. needAutoRedraw = true;
  540. let exceptLineId = null;
  541. let exceptPointId = null;
  542. if (addLine.newLine != null) {
  543. exceptLineId = addLine.newLine.vectorId;
  544. exceptPointId = addLine.newLine.endId;
  545. if (exceptPointId) {
  546. let exceptPoint = dataService.getPoint(exceptPointId);
  547. exceptLineId = exceptPoint.getParent();
  548. }
  549. }
  550. listenLayer.start(position, {
  551. //exceptLineId: exceptLineId,
  552. exceptLineIds: exceptLineId,
  553. exceptPointId: exceptPointId,
  554. });
  555. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  556. position = {
  557. x: listenLayer.modifyPoint.x,
  558. y: listenLayer.modifyPoint.y,
  559. };
  560. }
  561. // elementService.execute(addLine.startInfo.position, position);
  562. // elementService.setPoint(position);
  563. if (addLine.newLine == null) {
  564. addLine.buildLine(position);
  565. } else {
  566. addLine.updateLine(position);
  567. }
  568. elementService.hideAll();
  569. elementService.setPoint(position);
  570. elementService.showPoint();
  571. this.showElementLine(position, eventName);
  572. break;
  573. case LayerEvents.AddingCurveLine:
  574. needAutoRedraw = true;
  575. let exceptCurveLineId = null;
  576. let exceptCurvePointId = null;
  577. if (addLine.newLine != null) {
  578. exceptCurveLineId = addLine.newLine.vectorId;
  579. exceptCurvePointId = addLine.newLine.endId;
  580. }
  581. listenLayer.start(position, {
  582. exceptCurveLineId: exceptCurveLineId,
  583. exceptCurvePointId: exceptCurvePointId,
  584. });
  585. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  586. position = {
  587. x: listenLayer.modifyPoint.x,
  588. y: listenLayer.modifyPoint.y,
  589. };
  590. }
  591. // elementService.execute(addLine.startInfo.position, position);
  592. // elementService.setPoint(position);
  593. if (addLine.newLine == null) {
  594. addLine.buildCurveLine(position);
  595. } else {
  596. addLine.updateCurveLine(position);
  597. }
  598. elementService.hideAll();
  599. elementService.setPoint(position);
  600. elementService.showPoint();
  601. this.showElementLine(position, eventName);
  602. break;
  603. case LayerEvents.AddingCircle:
  604. needAutoRedraw = true;
  605. let exceptCircleId = null;
  606. if (addCircle.newCircle != null) {
  607. exceptCircleId = addCircle.newCircle.vectorId;
  608. }
  609. listenLayer.start(position, { exceptCircleId: exceptCircleId });
  610. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  611. position = {
  612. x: listenLayer.modifyPoint.x,
  613. y: listenLayer.modifyPoint.y,
  614. };
  615. }
  616. if (addCircle.newCircle == null) {
  617. addCircle.buildCircle(position);
  618. } else {
  619. addCircle.updateCircle(position);
  620. }
  621. elementService.hideAll();
  622. elementService.setPoint(position);
  623. elementService.showPoint();
  624. this.showElementLine(position);
  625. break;
  626. case LayerEvents.MoveRoad:
  627. needAutoRedraw = true;
  628. //只允许拖拽一条公路
  629. let road = dataService.getRoad(draggingItem.vectorId);
  630. let start = dataService.getRoadPoint(road.startId);
  631. let end = dataService.getRoadPoint(road.endId);
  632. if (Object.keys(start.getParent()).length == 1 && Object.keys(end.getParent()).length == 1) {
  633. //拖拽的路只有一条
  634. moveRoad.moveRoad(draggingItem.vectorId, (dx * coordinate.defaultZoom) / coordinate.zoom, (dy * coordinate.defaultZoom) / coordinate.zoom);
  635. }
  636. break;
  637. case LayerEvents.MoveRoadPoint:
  638. point = dataService.getRoadPoint(draggingItem.vectorId);
  639. listenLayer.start(position, {
  640. exceptRoadPointId: draggingItem.vectorId,
  641. exceptRoadIds: point.parent,
  642. });
  643. if (
  644. listenLayer.modifyPoint &&
  645. (listenLayer.modifyPoint.linkedRoadPointId ||
  646. listenLayer.modifyPoint.linkedRoadId ||
  647. listenLayer.modifyPoint.linkedCurveRoadPointId ||
  648. listenLayer.modifyPoint.linkedRoadPointIdX ||
  649. listenLayer.modifyPoint.linkedRoadPointIdY)
  650. ) {
  651. position = {
  652. x: listenLayer.modifyPoint.x,
  653. y: listenLayer.modifyPoint.y,
  654. };
  655. } else {
  656. listenLayer.modifyPoint = null;
  657. }
  658. let flag = moveRoad.moveingRoadPoint(draggingItem.vectorId, position, listenLayer.modifyPoint);
  659. if (!flag) {
  660. elementService.hideAll();
  661. } else {
  662. this.showElementLine(point, eventName);
  663. }
  664. needAutoRedraw = true;
  665. break;
  666. case LayerEvents.AddCurveRoad:
  667. needAutoRedraw = true;
  668. listenLayer.start(position);
  669. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  670. position = {
  671. x: listenLayer.modifyPoint.x,
  672. y: listenLayer.modifyPoint.y,
  673. };
  674. }
  675. elementService.hideAll();
  676. elementService.setPoint(position);
  677. elementService.showPoint();
  678. this.showElementLine(position, eventName);
  679. break;
  680. case LayerEvents.AddingCurveRoad:
  681. needAutoRedraw = true;
  682. listenLayer.start(position);
  683. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  684. position = {
  685. x: listenLayer.modifyPoint.x,
  686. y: listenLayer.modifyPoint.y,
  687. };
  688. }
  689. elementService.execute(addRoad.startInfo.position, position);
  690. elementService.setPoint(position);
  691. elementService.setNewRoad(addRoad.startInfo.position, position);
  692. elementService.showNewRoad();
  693. addRoad.setNewRoadPoint("end", position);
  694. addRoad.canAdd = addRoad.canAddRoadForEnd(position);
  695. if (!addRoad.canAdd) {
  696. elementService.setNewRoadState("error");
  697. } else {
  698. elementService.setNewRoadState("normal");
  699. }
  700. elementService.showPoint();
  701. this.showElementLine(position, eventName);
  702. break;
  703. case LayerEvents.MoveCurveRoad:
  704. needAutoRedraw = true;
  705. moveRoad.moveCurveRoad(draggingItem.vectorId, (dx * coordinate.defaultZoom) / coordinate.zoom, (dy * coordinate.defaultZoom) / coordinate.zoom);
  706. break;
  707. case LayerEvents.MoveCurveRoadPoint:
  708. if (!draggingItem || !draggingItem.vectorId) {
  709. return;
  710. }
  711. point = dataService.getCurveRoadPoint(draggingItem.vectorId);
  712. listenLayer.start(position, {
  713. exceptCurveRoadPointId: draggingItem.vectorId,
  714. exceptCurveRoadId: point.parent, //不会融合,所以parent只有一个
  715. });
  716. if (
  717. listenLayer.modifyPoint &&
  718. (listenLayer.modifyPoint.linkedCurveRoadPointId ||
  719. listenLayer.modifyPoint.linkedRoadPointId ||
  720. listenLayer.modifyPoint.linkedRoadId ||
  721. listenLayer.modifyPoint.linkedRoadPointIdX ||
  722. listenLayer.modifyPoint.linkedRoadPointIdY ||
  723. listenLayer.modifyPoint.linkedCurvePointIdX ||
  724. listenLayer.modifyPoint.linkedCurvePointIdY ||
  725. listenLayer.modifyPoint.linkedCurveRoadPointIdX)
  726. ) {
  727. position = {
  728. x: listenLayer.modifyPoint.x,
  729. y: listenLayer.modifyPoint.y,
  730. };
  731. }
  732. moveRoad.moveCurveRoadPoint(draggingItem.vectorId, position);
  733. this.showElementLine(point, eventName);
  734. needAutoRedraw = true;
  735. break;
  736. case LayerEvents.MoveCrossPoint:
  737. if (!draggingItem || !draggingItem.vectorId) {
  738. return;
  739. }
  740. moveRoad.moveCrossPoint(draggingItem.vectorId, position);
  741. needAutoRedraw = true;
  742. break;
  743. case LayerEvents.MoveEdge:
  744. moveRoad.moveEdge(draggingItem.vectorId, position);
  745. needAutoRedraw = true;
  746. break;
  747. case LayerEvents.MoveCurveEdge:
  748. if (listenLayer.modifyPoint) {
  749. moveRoad.moveCurveEdge(draggingItem.vectorId, listenLayer.modifyPoint.selectIndex, position);
  750. }
  751. needAutoRedraw = true;
  752. break;
  753. case LayerEvents.MoveLine:
  754. if (draggingItem != null) {
  755. let flag = moveLine.moveLine(draggingItem.vectorId, (dx * coordinate.defaultZoom) / coordinate.zoom, (dy * coordinate.defaultZoom) / coordinate.zoom);
  756. if (!flag) {
  757. this.lastX = this.lastX - dx / coordinate.ratio;
  758. this.lastY = this.lastY - dy / coordinate.ratio;
  759. }
  760. needAutoRedraw = true;
  761. }
  762. break;
  763. case LayerEvents.MovePoint:
  764. if (draggingItem != null) {
  765. point = dataService.getPoint(draggingItem.vectorId);
  766. listenLayer.start(position, {
  767. exceptPointId: draggingItem.vectorId,
  768. exceptLineIds: point.parent,
  769. });
  770. if (listenLayer.modifyPoint && listenLayer.modifyPoint.x && listenLayer.modifyPoint.y) {
  771. position = {
  772. x: listenLayer.modifyPoint.x,
  773. y: listenLayer.modifyPoint.y,
  774. };
  775. }
  776. let movePointFlag = movePoint.movePoint(position, draggingItem.vectorId);
  777. needAutoRedraw = true;
  778. if (!point) {
  779. stateService.clearEventName();
  780. } else if (movePointFlag) {
  781. this.showElementLine(point, eventName);
  782. }
  783. }
  784. break;
  785. case LayerEvents.MoveCurvePoint:
  786. if (draggingItem != null) {
  787. let curvePoint = dataService.getCurvePoint(draggingItem.vectorId);
  788. listenLayer.start(position, {
  789. exceptCurvePointId: draggingItem.vectorId,
  790. exceptCurveLineId: curvePoint.parent,
  791. });
  792. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty("x")) {
  793. position = {
  794. x: listenLayer.modifyPoint.x,
  795. y: listenLayer.modifyPoint.y,
  796. };
  797. }
  798. movePoint.moveCurvePoint(position, draggingItem.vectorId);
  799. let curveLine = dataService.getCurveLine(curvePoint.getParent());
  800. this.showElementLine(curvePoint, eventName);
  801. needAutoRedraw = true;
  802. }
  803. break;
  804. case LayerEvents.MoveCurveLine:
  805. if (draggingItem != null) {
  806. moveLine.moveCurveLine(draggingItem.vectorId, (dx * coordinate.defaultZoom) / coordinate.zoom, (dy * coordinate.defaultZoom) / coordinate.zoom);
  807. needAutoRedraw = true;
  808. }
  809. break;
  810. case LayerEvents.MoveCircle:
  811. if (draggingItem != null) {
  812. if (draggingItem.state == -1) {
  813. moveCircle.moveFull(draggingItem.vectorId, (dx * coordinate.defaultZoom) / coordinate.zoom, (dy * coordinate.defaultZoom) / coordinate.zoom);
  814. } else if (draggingItem.state == 0 || draggingItem.state == 1 || draggingItem.state == 2 || draggingItem.state == 3) {
  815. moveCircle.movePoint(position, draggingItem.vectorId, draggingItem.state);
  816. } else {
  817. debugger;
  818. }
  819. needAutoRedraw = true;
  820. }
  821. break;
  822. case LayerEvents.MoveText:
  823. needAutoRedraw = true;
  824. if (draggingItem != null) {
  825. let vectorText = dataService.getText(draggingItem.vectorId);
  826. if (!vectorText.linkedPointId) {
  827. moveText.moveFullText(position, draggingItem.vectorId);
  828. } else {
  829. movePoint.movePoint(position, vectorText.linkedPointId);
  830. }
  831. }
  832. break;
  833. case LayerEvents.MoveSVG:
  834. needAutoRedraw = true;
  835. if (draggingItem != null) {
  836. if (draggingItem.state == -1) {
  837. moveSVG.moveFullSVG(position, draggingItem.vectorId);
  838. } else {
  839. moveSVG.movePoint(position, draggingItem.vectorId, draggingItem.state);
  840. }
  841. }
  842. break;
  843. case LayerEvents.MoveMagnifier:
  844. needAutoRedraw = true;
  845. if (draggingItem != null) {
  846. moveMagnifier.moveFullMagnifier(position, draggingItem.vectorId, draggingItem.state);
  847. }
  848. break;
  849. }
  850. if (needAutoRedraw) {
  851. this.renderer.autoRedraw();
  852. }
  853. }
  854. onMouseUp(e) {
  855. // 右键
  856. if (e.button == 2) {
  857. this.dragging = false;
  858. return;
  859. }
  860. let X = e.offsetX || e.layerX;
  861. let Y = e.offsetY || e.layerY;
  862. if (e instanceof TouchEvent) {
  863. X = this.lastX;
  864. Y = this.lastY;
  865. this.StorePage1 = null;
  866. this.StorePage2 = null;
  867. this.StoreMidePage = null;
  868. if (e.touches.length > 1) {
  869. this.dragging = false;
  870. return;
  871. }
  872. }
  873. let eventName = stateService.getEventName();
  874. const draggingItem = stateService.getDraggingItem();
  875. const selectItem = stateService.getSelectItem();
  876. let focusItem = null;
  877. if (!this.dragging && selectItem) {
  878. focusItem = {
  879. vectorId: selectItem.vectorId,
  880. type: selectItem.type,
  881. cursor: { x: this.lastX, y: this.lastY },
  882. dir: selectItem.dir,
  883. };
  884. stateService.setFocusItem(focusItem);
  885. this.uiControl.focusVector = focusItem;
  886. stateService.clearDraggingItem();
  887. } else {
  888. this.uiControl.clearFocusVector();
  889. }
  890. this.dragging = false;
  891. let position = coordinate.getXYFromScreen({
  892. x: X,
  893. y: Y,
  894. });
  895. let needAutoRedraw = false;
  896. switch (eventName) {
  897. case null:
  898. if (e instanceof TouchEvent) {
  899. stateService.clearSelectItem();
  900. stateService.clearDraggingItem();
  901. this.uiControl.clearFocusVector();
  902. this.renderer.autoRedraw();
  903. }
  904. return;
  905. case LayerEvents.PanBackGround:
  906. needAutoRedraw = true;
  907. stateService.clearFocusItem();
  908. this.uiControl.clearFocusVector();
  909. this.uiControl.clearCurrentUI();
  910. break;
  911. case LayerEvents.MoveRoadPoint:
  912. if (!draggingItem || !draggingItem.vectorId) {
  913. break;
  914. }
  915. needAutoRedraw = true;
  916. elementService.hideAll();
  917. let point = dataService.getRoadPoint(draggingItem.vectorId);
  918. if (point) {
  919. listenLayer.start(point, {
  920. exceptRoadPointId: draggingItem.vectorId,
  921. exceptRoadIds: point.parent,
  922. });
  923. moveRoad.finishByMoveRoadPoint(point);
  924. this.history.save();
  925. }
  926. break;
  927. // case LayerEvents.AddRoad:
  928. // addRoad.setNewRoadPoint("start", position);
  929. // break;
  930. case LayerEvents.AddingRoad:
  931. needAutoRedraw = true;
  932. if (addRoad.canAdd) {
  933. addRoad.buildRoad();
  934. elementService.hideAll();
  935. }
  936. let roadEdges = dataService.getRoadEdges();
  937. for (let roadEdgeId in roadEdges) {
  938. let roadEdge = dataService.getRoadEdge(roadEdgeId);
  939. if (roadEdge.roadSide) {
  940. roadEdge.initRoadSide();
  941. }
  942. }
  943. this.uiControl.confirmEntry(); //不能连续创建道路
  944. break;
  945. case LayerEvents.AddingLine:
  946. needAutoRedraw = true;
  947. addLine.finish(position);
  948. this.updateForLocation();
  949. //绘制的是基准线
  950. if (addLine.newLine && Settings.baseLineId == addLine.newLine.vectorId) {
  951. stateService.clearEventName();
  952. this.history.save();
  953. }
  954. addLine.clearVectorData();
  955. elementService.hideAll();
  956. break;
  957. case LayerEvents.AddingCurveLine:
  958. needAutoRedraw = true;
  959. addLine.finishCurveLine(position);
  960. addLine.clearVectorData();
  961. this.history.save();
  962. elementService.hideAll();
  963. break;
  964. case LayerEvents.AddingCircle:
  965. needAutoRedraw = true;
  966. addCircle.finish(position);
  967. addCircle.clear();
  968. this.uiControl.clearSelectUI();
  969. this.history.save();
  970. elementService.hideAll();
  971. break;
  972. case LayerEvents.MoveText:
  973. needAutoRedraw = true;
  974. this.history.save();
  975. elementService.hideAll();
  976. this.uiControl.clearUI();
  977. break;
  978. case LayerEvents.MoveSVG:
  979. needAutoRedraw = true;
  980. this.history.save();
  981. elementService.hideAll();
  982. break;
  983. case LayerEvents.MoveMagnifier:
  984. needAutoRedraw = true;
  985. this.history.save();
  986. elementService.hideAll();
  987. this.uiControl.clearUI();
  988. break;
  989. case LayerEvents.MoveSVG:
  990. needAutoRedraw = true;
  991. this.history.save();
  992. elementService.hideAll();
  993. break;
  994. case LayerEvents.AddingCurveRoad:
  995. needAutoRedraw = true;
  996. if (addRoad.canAdd) {
  997. addRoad.buildCurveRoad();
  998. elementService.hideAll();
  999. }
  1000. this.uiControl.confirmEntry(); //不能连续创建道路
  1001. break;
  1002. case LayerEvents.MoveRoad:
  1003. needAutoRedraw = true;
  1004. this.history.save();
  1005. break;
  1006. case LayerEvents.MoveCurveRoad:
  1007. needAutoRedraw = true;
  1008. this.history.save();
  1009. break;
  1010. case LayerEvents.MoveCurveRoadPoint:
  1011. needAutoRedraw = true;
  1012. if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedCurveRoadPointId) {
  1013. let curveRoadPoint1 = dataService.getCurveRoadPoint(listenLayer.modifyPoint.linkedCurveRoadPointId);
  1014. let curveRoadPoint2 = dataService.getCurveRoadPoint(draggingItem.vectorId);
  1015. if (listenLayer.modifyPoint.linkedCurveRoadPointId != draggingItem.vectorId && curveRoadPoint1.getParent() == curveRoadPoint2.getParent()) {
  1016. curveRoadPointService.deleteCurveRoadPoint(listenLayer.modifyPoint.linkedCurveRoadPointId);
  1017. }
  1018. }
  1019. this.history.save();
  1020. break;
  1021. case LayerEvents.MoveCrossPoint:
  1022. needAutoRedraw = true;
  1023. this.history.save();
  1024. break;
  1025. case LayerEvents.MoveEdge:
  1026. needAutoRedraw = true;
  1027. this.history.save();
  1028. break;
  1029. case LayerEvents.MoveCurveEdge:
  1030. needAutoRedraw = true;
  1031. this.history.save();
  1032. break;
  1033. case LayerEvents.MoveLine:
  1034. needAutoRedraw = true;
  1035. this.history.save();
  1036. break;
  1037. case LayerEvents.MovePoint:
  1038. needAutoRedraw = true;
  1039. if (draggingItem && draggingItem.vectorId) {
  1040. movePoint.finish(draggingItem.vectorId);
  1041. uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
  1042. }
  1043. this.history.save();
  1044. elementService.hideAll();
  1045. break;
  1046. case LayerEvents.MoveCurvePoint:
  1047. needAutoRedraw = true;
  1048. if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedCurvePointId) {
  1049. let curvePoint1 = dataService.getCurvePoint(listenLayer.modifyPoint.linkedCurvePointId);
  1050. let curvePoint2 = dataService.getCurvePoint(draggingItem.vectorId);
  1051. if (listenLayer.modifyPoint.linkedCurvePointId != draggingItem.vectorId && curvePoint1.getParent() == curvePoint2.getParent()) {
  1052. lineService.deleteCrossPointForCurveLine(listenLayer.modifyPoint.linkedCurvePointId, curvePoint1.getParent());
  1053. }
  1054. }
  1055. elementService.hideAll();
  1056. this.history.save();
  1057. break;
  1058. case LayerEvents.MoveCurveLine:
  1059. needAutoRedraw = true;
  1060. this.history.save();
  1061. break;
  1062. case LayerEvents.MoveCircle:
  1063. needAutoRedraw = true;
  1064. this.history.save();
  1065. break;
  1066. case LayerEvents.AddPoint:
  1067. // 绘制的是基准点
  1068. if (Settings.selectPointCategory == VectorCategory.Point.BasePoint) {
  1069. uiService.setSelectPointCategory(null);
  1070. stateService.clearEventName();
  1071. this.history.save();
  1072. } else {
  1073. this.uiControl.showConfirm();
  1074. }
  1075. needAutoRedraw = true;
  1076. elementService.hideAll();
  1077. break;
  1078. case LayerEvents.AddRoadTemplate:
  1079. addCrossRoad.build(position);
  1080. this.history.save();
  1081. this.renderer.autoRedraw();
  1082. break;
  1083. }
  1084. this.setEventName("mouseUp");
  1085. stateService.clearDraggingItem();
  1086. stateService.clearSelectItem();
  1087. if (needAutoRedraw) {
  1088. this.renderer.autoRedraw();
  1089. }
  1090. }
  1091. onWheel(e) {
  1092. e.preventDefault();
  1093. const type = e.type;
  1094. if (type == "DOMMouseScroll" || type == "mousewheel") {
  1095. // 当在canvas用滚轮滚动时
  1096. const delta = e.wheelDelta ? (e.wheelDelta / 120) * 20 : (-(e.detail || 0) / 3) * 20;
  1097. const zoom = coordinate.zoom + delta;
  1098. let X = e.offsetX || e.layerX;
  1099. let Y = e.offsetY || e.layerY;
  1100. this.zoomVector(
  1101. {
  1102. x: X,
  1103. y: Y,
  1104. },
  1105. zoom
  1106. );
  1107. }
  1108. }
  1109. zoomVector(position, zoom) {
  1110. if (zoom < minZoom || zoom > maxZoom) {
  1111. return;
  1112. }
  1113. let tempCenter = {};
  1114. mathUtil.clonePoint(tempCenter, coordinate.center);
  1115. let tempZoom = coordinate.zoom;
  1116. coordinate.updateZoom(position, zoom);
  1117. let inScreen = imageService.backgroundImgVertexsInScreenXY();
  1118. if (!inScreen) {
  1119. dataService.setGridForZoom();
  1120. this.renderer.autoRedraw();
  1121. } else {
  1122. mathUtil.clonePoint(coordinate.center, tempCenter);
  1123. coordinate.zoom = tempZoom;
  1124. }
  1125. }
  1126. setEventName(eventType) {
  1127. let eventName = stateService.getEventName();
  1128. if (eventType == "mouseDown") {
  1129. if (eventName == null) {
  1130. const selectItem = stateService.getSelectItem();
  1131. if (selectItem == null) {
  1132. stateService.setEventName(LayerEvents.PanBackGround);
  1133. } else if (selectItem.type == VectorType.Road) {
  1134. stateService.setEventName(LayerEvents.MoveRoad);
  1135. } else if (selectItem.type == VectorType.RoadPoint) {
  1136. stateService.setEventName(LayerEvents.MoveRoadPoint);
  1137. } else if (selectItem.type == VectorType.CurveRoad) {
  1138. stateService.setEventName(LayerEvents.MoveCurveRoad);
  1139. } else if (selectItem.type == VectorType.CurveRoadPoint) {
  1140. stateService.setEventName(LayerEvents.MoveCurveRoadPoint);
  1141. } else if (selectItem.type == VectorType.CrossPoint) {
  1142. stateService.setEventName(LayerEvents.MoveCrossPoint);
  1143. } else if (selectItem.type == VectorType.RoadEdge) {
  1144. stateService.setEventName(LayerEvents.MoveEdge);
  1145. } else if (selectItem.type == VectorType.CurveRoadEdge) {
  1146. stateService.setEventName(LayerEvents.MoveCurveEdge);
  1147. } else if (selectItem.type == VectorType.Point) {
  1148. stateService.setEventName(LayerEvents.MovePoint);
  1149. } else if (selectItem.type == VectorType.Line) {
  1150. stateService.setEventName(LayerEvents.MoveLine);
  1151. } else if (selectItem.type == VectorType.CurvePoint) {
  1152. stateService.setEventName(LayerEvents.MoveCurvePoint);
  1153. } else if (selectItem.type == VectorType.CurveLine) {
  1154. stateService.setEventName(LayerEvents.MoveCurveLine);
  1155. } else if (selectItem.type == VectorType.Circle) {
  1156. stateService.setEventName(LayerEvents.MoveCircle);
  1157. } else if (selectItem.type == VectorType.Text) {
  1158. stateService.setEventName(LayerEvents.MoveText);
  1159. } else if (selectItem.type == VectorType.Magnifier) {
  1160. stateService.setEventName(LayerEvents.MoveMagnifier);
  1161. } else if (selectItem.type == VectorType.SVG) {
  1162. stateService.setEventName(LayerEvents.MoveSVG);
  1163. }
  1164. }
  1165. } else if (eventType == "mouseUp") {
  1166. if (eventName == LayerEvents.AddingRoad) {
  1167. // this.uiControl.showConfirm();
  1168. stateService.setEventName(LayerEvents.AddRoad);
  1169. } else if (eventName == LayerEvents.AddingLine) {
  1170. this.uiControl.showConfirm();
  1171. stateService.setEventName(LayerEvents.AddLine);
  1172. } else if (eventName == LayerEvents.AddingCurveRoad) {
  1173. // this.uiControl.showConfirm();
  1174. stateService.setEventName(LayerEvents.AddCurveRoad);
  1175. } else if (eventName == LayerEvents.AddLine) {
  1176. stateService.setEventName(LayerEvents.AddingLine);
  1177. } else if (eventName == LayerEvents.AddPoint) {
  1178. } else if (eventName == VectorEvents.AddLane) {
  1179. } else if (eventName == VectorEvents.DelLane) {
  1180. } else {
  1181. stateService.clearEventName();
  1182. }
  1183. }
  1184. }
  1185. exit() {
  1186. stateService.clear();
  1187. this.uiControl.clearUI();
  1188. this.uiControl.clearFocusVector();
  1189. this.uiControl.clearCurrentUI();
  1190. }
  1191. stopAddVector() {
  1192. let eventName = stateService.getEventName();
  1193. if (eventName != LayerEvents.AddingRoad && eventName != LayerEvents.AddingLine) {
  1194. stateService.clearEventName();
  1195. } else if (eventName == LayerEvents.AddingRoad) {
  1196. stateService.setEventName(LayerEvents.AddRoad);
  1197. } else if (eventName == LayerEvents.AddingLine) {
  1198. stateService.setEventName(LayerEvents.AddLine);
  1199. }
  1200. addLine.clear(); //之前会保留category
  1201. this.uiControl.clearUI();
  1202. elementService.hideAll();
  1203. this.renderer.autoRedraw();
  1204. }
  1205. initLocation() {
  1206. uiService.setBaseLineId(null);
  1207. this.uiControl.graphicStateUI.canAngleLocationMode = false;
  1208. this.uiControl.graphicStateUI.canAllLocationMode = false;
  1209. this.uiControl.graphicStateUI.canVerticalMeasure = false;
  1210. this.uiControl.graphicStateUI.existsBaseLine = false;
  1211. }
  1212. //更新定位信息
  1213. updateForLocation() {
  1214. if (Settings.baseLineId) {
  1215. this.uiControl.graphicStateUI.canAngleLocationMode = true;
  1216. this.uiControl.graphicStateUI.canAllLocationMode = true;
  1217. this.uiControl.graphicStateUI.existsBaseLine = true;
  1218. this.uiControl.graphicStateUI.canVerticalMeasure = true;
  1219. } else {
  1220. this.uiControl.graphicStateUI.canAngleLocationMode = false;
  1221. this.uiControl.graphicStateUI.canAllLocationMode = false;
  1222. this.uiControl.graphicStateUI.existsBaseLine = false;
  1223. this.uiControl.graphicStateUI.canVerticalMeasure = false;
  1224. }
  1225. }
  1226. // showElementLine(point, eventName) {
  1227. // let otherPoint1 = null;
  1228. // let otherPoint2 = null;
  1229. // if (
  1230. // listenLayer.modifyPoint &&
  1231. // listenLayer.modifyPoint.linkedRoadPointIdX &&
  1232. // (eventName == LayerEvents.AddingRoad ||
  1233. // eventName == LayerEvents.MoveRoadPoint ||
  1234. // eventName == LayerEvents.AddRoad)
  1235. // ) {
  1236. // otherPoint1 = dataService.getRoadPoint(
  1237. // listenLayer.modifyPoint.linkedRoadPointIdX
  1238. // );
  1239. // } else if (
  1240. // listenLayer.modifyPoint &&
  1241. // listenLayer.modifyPoint.linkedCurveRoadPointIdX &&
  1242. // (eventName == LayerEvents.AddingCurveRoad ||
  1243. // eventName == LayerEvents.MoveCurveRoadPoint ||
  1244. // eventName == LayerEvents.AddCurveRoad)
  1245. // ) {
  1246. // otherPoint1 = dataService.getCurveRoadPoint(
  1247. // listenLayer.modifyPoint.linkedCurvePointIdX
  1248. // );
  1249. // } else if (
  1250. // listenLayer.modifyPoint &&
  1251. // listenLayer.modifyPoint.linkedPointIdX &&
  1252. // (eventName == LayerEvents.AddLine || eventName == LayerEvents.MovePoint)
  1253. // ) {
  1254. // otherPoint1 = dataService.getPoint(
  1255. // listenLayer.modifyPoint.linkedPointIdX
  1256. // );
  1257. // } else if (
  1258. // listenLayer.modifyPoint &&
  1259. // listenLayer.modifyPoint.linkedCurvePointIdX &&
  1260. // (eventName == LayerEvents.AddCurveLine ||
  1261. // eventName == LayerEvents.MoveCurvePoint)
  1262. // ) {
  1263. // otherPoint1 = dataService.getCurvePoint(
  1264. // listenLayer.modifyPoint.linkedCurvePointIdX
  1265. // );
  1266. // }
  1267. // if (
  1268. // listenLayer.modifyPoint &&
  1269. // listenLayer.modifyPoint.linkedRoadPointIdY &&
  1270. // (eventName == LayerEvents.AddingRoad ||
  1271. // eventName == LayerEvents.MoveRoadPoint ||
  1272. // eventName == LayerEvents.AddRoad)
  1273. // ) {
  1274. // otherPoint2 = dataService.getRoadPoint(
  1275. // listenLayer.modifyPoint.linkedRoadPointIdY
  1276. // );
  1277. // } else if (
  1278. // listenLayer.modifyPoint &&
  1279. // listenLayer.modifyPoint.linkedCurvePointIdY &&
  1280. // (eventName == LayerEvents.AddingCurveRoad ||
  1281. // eventName == LayerEvents.MoveCurveRoadPoint ||
  1282. // eventName == LayerEvents.AddCurveRoad)
  1283. // ) {
  1284. // otherPoint2 = dataService.getCurveRoadPoint(
  1285. // listenLayer.modifyPoint.linkedCurvePointIdY
  1286. // );
  1287. // } else if (
  1288. // listenLayer.modifyPoint &&
  1289. // listenLayer.modifyPoint.linkedPointIdY &&
  1290. // (eventName == LayerEvents.AddLine || eventName == LayerEvents.MovePoint)
  1291. // ) {
  1292. // otherPoint2 = dataService.getPoint(
  1293. // listenLayer.modifyPoint.linkedPointIdY
  1294. // );
  1295. // } else if (
  1296. // listenLayer.modifyPoint &&
  1297. // listenLayer.modifyPoint.linkedCurvePointIdY &&
  1298. // (eventName == LayerEvents.AddCurveLine ||
  1299. // eventName == LayerEvents.MoveCurvePoint)
  1300. // ) {
  1301. // otherPoint2 = dataService.getCurvePoint(
  1302. // listenLayer.modifyPoint.linkedCurvePointIdY
  1303. // );
  1304. // }
  1305. // let otherPoint = {};
  1306. // if (otherPoint1) {
  1307. // otherPoint.x = otherPoint1.x;
  1308. // otherPoint.y = otherPoint1.y;
  1309. // }
  1310. // if (otherPoint2) {
  1311. // otherPoint.y = otherPoint2.y;
  1312. // if (!otherPoint.hasOwnProperty("x")) {
  1313. // otherPoint.x = otherPoint2.x;
  1314. // }
  1315. // }
  1316. // if (otherPoint.hasOwnProperty("x") && otherPoint.hasOwnProperty("y")) {
  1317. // elementService.execute(otherPoint, point);
  1318. // } else {
  1319. // elementService.hideAll();
  1320. // }
  1321. // }
  1322. showElementLine(point) {
  1323. let otherPoint1 = null;
  1324. let otherPoint2 = null;
  1325. if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedRoadPointIdX) {
  1326. otherPoint1 = dataService.getRoadPoint(listenLayer.modifyPoint.linkedRoadPointIdX);
  1327. } else if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedCurveRoadPointIdX) {
  1328. otherPoint1 = dataService.getCurveRoadPoint(listenLayer.modifyPoint.linkedCurvePointIdX);
  1329. } else if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedPointIdX) {
  1330. otherPoint1 = dataService.getPoint(listenLayer.modifyPoint.linkedPointIdX);
  1331. } else if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedCurvePointIdX) {
  1332. otherPoint1 = dataService.getCurvePoint(listenLayer.modifyPoint.linkedCurvePointIdX);
  1333. }
  1334. if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedRoadPointIdY) {
  1335. otherPoint2 = dataService.getRoadPoint(listenLayer.modifyPoint.linkedRoadPointIdY);
  1336. } else if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedCurvePointIdY) {
  1337. otherPoint2 = dataService.getCurveRoadPoint(listenLayer.modifyPoint.linkedCurvePointIdY);
  1338. } else if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedPointIdY) {
  1339. otherPoint2 = dataService.getPoint(listenLayer.modifyPoint.linkedPointIdY);
  1340. } else if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedCurvePointIdY) {
  1341. otherPoint2 = dataService.getCurvePoint(listenLayer.modifyPoint.linkedCurvePointIdY);
  1342. }
  1343. let otherPoint = {};
  1344. if (otherPoint1) {
  1345. otherPoint.x = otherPoint1.x;
  1346. otherPoint.y = otherPoint1.y;
  1347. }
  1348. if (otherPoint2) {
  1349. otherPoint.y = otherPoint2.y;
  1350. if (!otherPoint.hasOwnProperty("x")) {
  1351. otherPoint.x = otherPoint2.x;
  1352. }
  1353. }
  1354. elementService.execute(otherPoint, point);
  1355. }
  1356. //测试用
  1357. onKeydown(e) {
  1358. let focusItem = stateService.getFocusItem();
  1359. if (focusItem) {
  1360. console.log("键盘(foucus有效):" + e.code);
  1361. //添加固定点
  1362. if (e.code == "KeyQ") {
  1363. uiService.setSelectPointCategory(VectorCategory.Point.FixPoint);
  1364. stateService.setEventName(LayerEvents.AddPoint);
  1365. }
  1366. //一键测量
  1367. if (e.code == "KeyA") {
  1368. locationModeControl.setAngle();
  1369. this.renderer.autoRedraw();
  1370. this.history.save();
  1371. }
  1372. }
  1373. }
  1374. }