Layer.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. this.setEventName("mouseDown");
  121. const eventName = stateService.getEventName();
  122. switch (eventName) {
  123. case LayerEvents.AddRoad:
  124. stateService.setEventName(LayerEvents.AddingRoad);
  125. addRoad.setNewRoadPoint("start", position);
  126. break;
  127. case LayerEvents.AddCurveRoad:
  128. stateService.setEventName(LayerEvents.AddingCurveRoad);
  129. addRoad.setNewRoadPoint("start", position);
  130. break;
  131. case LayerEvents.AddLine:
  132. stateService.setEventName(LayerEvents.AddingLine);
  133. addLine.setNewLinePoint(position);
  134. break;
  135. // case LayerEvents.AddCurveLine:
  136. // stateService.setEventName(LayerEvents.AddingCurveLine);
  137. // addLine.setNewLinePoint(position);
  138. // break;
  139. case LayerEvents.AddPoint:
  140. stateService.setEventName(LayerEvents.MovePoint);
  141. const newPoint = addPoint.buildPoint(position);
  142. if (newPoint) {
  143. stateService.setSelectItem(
  144. newPoint.vectorId,
  145. VectorType.Point,
  146. SelectState.Select
  147. );
  148. this.history.save();
  149. this.renderer.autoRedraw();
  150. } else {
  151. if (Settings.basePointIds.length > 1) {
  152. this.uiControl.prompt({ msg: "请先选择基准点", time: 1000 });
  153. } else {
  154. this.uiControl.prompt({ msg: "请先添加基准点", time: 1000 });
  155. }
  156. // this.uiControl.prompt({ msg: '请先选择基准点', time: 1000 });
  157. }
  158. break;
  159. case LayerEvents.AddCircle:
  160. stateService.setEventName(LayerEvents.AddingCircle);
  161. addCircle.setCenter(position);
  162. break;
  163. case LayerEvents.AddText:
  164. stateService.setEventName(LayerEvents.MoveText);
  165. addText.buildText(position);
  166. stateService.setSelectItem(
  167. addText.newText.vectorId,
  168. VectorType.Text,
  169. SelectState.Select
  170. );
  171. addText.clear();
  172. break;
  173. case LayerEvents.AddSVG:
  174. stateService.setEventName(LayerEvents.MoveSVG);
  175. addSVG.buildSVG(position);
  176. addSVG.newSVG.setType(Settings.selectSVGType);
  177. stateService.setSelectItem(addSVG.newSVG.vectorId, VectorType.SVG, -1);
  178. addSVG.clear();
  179. break;
  180. case LayerEvents.AddMagnifier:
  181. stateService.setEventName(LayerEvents.MoveMagnifier);
  182. addMagnifier.buildMagnifier(position);
  183. stateService.setSelectItem(
  184. addMagnifier.newMagnifier.vectorId,
  185. VectorType.Magnifier,
  186. SelectState.Select
  187. );
  188. addMagnifier.clear();
  189. break;
  190. case VectorEvents.AddLane:
  191. if (selectItem && selectItem.dir && selectItem.vectorId) {
  192. let road = dataService.getRoad(selectItem.vectorId);
  193. if (road) {
  194. let roadLanCount = road.getLanesCount(selectItem.dir);
  195. if (selectItem.dir == "left") {
  196. roadService.updateForAddSubtractLanesCount(
  197. road.vectorId,
  198. roadLanCount + 1,
  199. selectItem.dir
  200. );
  201. } else {
  202. roadService.updateForAddSubtractLanesCount(
  203. road.vectorId,
  204. roadLanCount + 1,
  205. selectItem.dir
  206. );
  207. }
  208. } else {
  209. road = dataService.getCurveRoad(selectItem.vectorId);
  210. let curveRoadLanCount = road.getLanesCount(selectItem.dir);
  211. if (selectItem.dir == "left") {
  212. curveRoadService.updateForAddSubtractLanesCount(
  213. road.vectorId,
  214. curveRoadLanCount + 1,
  215. selectItem.dir
  216. );
  217. } else {
  218. curveRoadService.updateForAddSubtractLanesCount(
  219. road.vectorId,
  220. curveRoadLanCount + 1,
  221. selectItem.dir
  222. );
  223. }
  224. }
  225. stateService.clearEventName();
  226. this.history.save();
  227. this.renderer.autoRedraw();
  228. }
  229. break;
  230. case VectorEvents.DelLane:
  231. if (selectItem && selectItem.dir && selectItem.vectorId) {
  232. let road = dataService.getRoad(selectItem.vectorId);
  233. if (road) {
  234. let roadLanCount = road.getLanesCount(selectItem.dir);
  235. if (selectItem.dir == "left") {
  236. roadService.updateForAddSubtractLanesCount(
  237. road.vectorId,
  238. roadLanCount - 1,
  239. selectItem.dir
  240. );
  241. } else {
  242. roadService.updateForAddSubtractLanesCount(
  243. road.vectorId,
  244. roadLanCount - 1,
  245. selectItem.dir
  246. );
  247. }
  248. } else {
  249. road = dataService.getCurveRoad(selectItem.vectorId);
  250. let curveRoadLanCount = road.getLanesCount(selectItem.dir);
  251. if (selectItem.dir == "left") {
  252. curveRoadService.updateForAddSubtractLanesCount(
  253. road.vectorId,
  254. curveRoadLanCount - 1,
  255. selectItem.dir
  256. );
  257. } else {
  258. curveRoadService.updateForAddSubtractLanesCount(
  259. road.vectorId,
  260. curveRoadLanCount - 1,
  261. selectItem.dir
  262. );
  263. }
  264. }
  265. stateService.clearEventName();
  266. this.history.save();
  267. this.renderer.autoRedraw();
  268. }
  269. break;
  270. case VectorEvents.AddCrossPoint:
  271. if (focusItem && focusItem.vectorId) {
  272. if (focusItem.type == VectorType.CurveRoad) {
  273. const curveRoad = dataService.getCurveRoad(focusItem.vectorId);
  274. let index = mathUtil.getIndexForCurvesPoints(
  275. position,
  276. curveRoad.points
  277. );
  278. if (index != -1) {
  279. curveRoadService.addCPoint(curveRoad, position, index);
  280. } else {
  281. const dis1 = mathUtil.getDistance(curveRoad.points[0], position);
  282. const dis2 = mathUtil.getDistance(
  283. curveRoad.points[curveRoad.points.length - 1],
  284. position
  285. );
  286. if (dis1 > dis2) {
  287. index = curveRoad.points.length - 2;
  288. } else {
  289. index = 1;
  290. }
  291. curveRoadService.addCPoint(curveRoad, position, index);
  292. }
  293. curveRoadService.setLanes(curveRoad.vectorId);
  294. // curveRoadService.updateForMovePoint(
  295. // curveRoad.points[index + 1].vectorId,
  296. // position
  297. // );
  298. } else if (focusItem.type == VectorType.Line) {
  299. let line = dataService.getLine(focusItem.vectorId);
  300. const weight = line.getWeight();
  301. let startPoint = dataService.getPoint(line.startId);
  302. let endPoint = dataService.getPoint(line.endId);
  303. dataService.deleteLine(focusItem.vectorId);
  304. let curveLine = lineService.createCurveLine(startPoint, endPoint);
  305. mathUtil.clonePoint(curveLine.points[1], position);
  306. curveLine.curves = mathUtil.getCurvesByPoints(curveLine.points);
  307. curveLine.setWeight(weight);
  308. const style = line.getStyle();
  309. curveLine.setStyle(style);
  310. } else if (focusItem.type == VectorType.CurveLine) {
  311. let curveLine = dataService.getCurveLine(focusItem.vectorId);
  312. let index = mathUtil.getIndexForCurvesPoints(
  313. position,
  314. curveLine.points
  315. );
  316. if (index != -1) {
  317. lineService.addCPoint(position, index, focusItem.vectorId);
  318. } else {
  319. const dis1 = mathUtil.getDistance(curveLine.points[0], position);
  320. const dis2 = mathUtil.getDistance(
  321. curveLine.points[curveLine.points.length - 1],
  322. position
  323. );
  324. if (dis1 > dis2) {
  325. index = curveLine.points.length - 2;
  326. } else {
  327. index = 1;
  328. }
  329. lineService.addCPoint(position, index, focusItem.vectorId);
  330. }
  331. }
  332. stateService.clearEventName();
  333. this.history.save();
  334. this.renderer.autoRedraw();
  335. }
  336. break;
  337. case VectorEvents.MinusCrossPoint:
  338. if (selectItem && selectItem.vectorId) {
  339. const curvePoint = dataService.getCurveRoadPoint(selectItem.vectorId);
  340. if (curvePoint) {
  341. const curveRoad = dataService.getCurveRoad(curvePoint.parent);
  342. if (curveRoad.points.length > 3) {
  343. curveRoadService.subCPoint(curveRoad, curvePoint.getIndex());
  344. curveRoadService.setLanes(curveRoad.vectorId);
  345. stateService.clearEventName();
  346. }
  347. this.history.save();
  348. this.renderer.autoRedraw();
  349. }
  350. }
  351. break;
  352. }
  353. selectItem = stateService.getSelectItem();
  354. stateService.setDraggingItem(selectItem);
  355. stateService.clearFocusItem();
  356. this.uiControl.focusVector = null;
  357. // 清除上一个状态
  358. // 设置当前事件名称
  359. e.preventDefault();
  360. e.stopPropagation();
  361. }
  362. onMouseMove(e) {
  363. let X = e.offsetX || e.layerX;
  364. let Y = e.offsetY || e.layerY;
  365. if (e instanceof TouchEvent) {
  366. X = e.touches[0].pageX;
  367. Y = e.touches[0].pageY;
  368. //切换到缩放
  369. if (e.touches.length > 1) {
  370. //记录开始的两个触摸点的坐标
  371. if (!this.StorePage1 || !this.StorePage2) {
  372. this.onMouseUp(e);
  373. this.StorePage1 = { x: e.touches[0].pageX, y: e.touches[0].pageY };
  374. this.StorePage2 = { x: e.touches[1].pageX, y: e.touches[1].pageY };
  375. this.StoreMidePage = {
  376. x: (this.StorePage1.x + this.StorePage2.x) / 2,
  377. y: (this.StorePage1.y + this.StorePage2.y) / 2,
  378. };
  379. return;
  380. }
  381. const point1 = {
  382. x: e.touches[0].pageX,
  383. y: e.touches[0].pageY,
  384. };
  385. const point2 = {
  386. x: e.touches[1].pageX,
  387. y: e.touches[1].pageY,
  388. };
  389. let dis1 = mathUtil.getDistance(point1, point2) / 100;
  390. let dis2 = mathUtil.getDistance(this.StorePage1, this.StorePage2) / 100;
  391. let zoom = (coordinate.zoom * dis1) / dis2;
  392. this.zoomVector(this.StoreMidePage, zoom);
  393. mathUtil.clonePoint(this.StorePage1, point1);
  394. mathUtil.clonePoint(this.StorePage2, point2);
  395. return;
  396. }
  397. }
  398. let dx = X - this.lastX;
  399. let dy = Y - this.lastY;
  400. dx = dx * coordinate.ratio;
  401. dy = dy * coordinate.ratio;
  402. let position = coordinate.getXYFromScreen({
  403. x: X,
  404. y: Y,
  405. });
  406. this.mousePosition = {
  407. x: position.x,
  408. y: position.y,
  409. };
  410. const eventName = stateService.getEventName();
  411. if (
  412. !this.dragging &&
  413. Math.abs(X - this.startX) < minDragDis &&
  414. Math.abs(Y - this.startY) < minDragDis
  415. ) {
  416. return;
  417. }
  418. this.dragging = true;
  419. if (
  420. Math.abs(X - this.startX) > minDragDis ||
  421. Math.abs(Y - this.startY) > minDragDis
  422. ) {
  423. // 是否拖拽了
  424. if (eventName != null) {
  425. if (
  426. eventName == LayerEvents.MoveMagnifier &&
  427. stateService.getSelectItem().state != 0
  428. ) {
  429. } else {
  430. stateService.clearFocusItem();
  431. this.uiControl.focusVector = null;
  432. }
  433. }
  434. }
  435. // 是否需要重绘
  436. let needAutoRedraw = false;
  437. let point = null;
  438. this.lastX = X;
  439. this.lastY = Y;
  440. const draggingItem = stateService.getDraggingItem();
  441. switch (eventName) {
  442. case null:
  443. //监控
  444. needAutoRedraw = listenLayer.start(position);
  445. let seleteItem = stateService.getSelectItem();
  446. if (seleteItem != null) {
  447. console.log("选中:" + seleteItem.vectorId);
  448. } else {
  449. }
  450. break;
  451. case LayerEvents.PanBackGround:
  452. stateService.clearItems();
  453. let center = {};
  454. center.x =
  455. coordinate.center.x - (dx * coordinate.defaultZoom) / coordinate.zoom;
  456. center.y =
  457. coordinate.center.y + (dy * coordinate.defaultZoom) / coordinate.zoom;
  458. let tempCenter = {};
  459. mathUtil.clonePoint(tempCenter, coordinate.center);
  460. mathUtil.clonePoint(coordinate.center, center);
  461. let inScreen = imageService.backgroundImgVertexsInScreenXY();
  462. if (!inScreen) {
  463. dataService.setGridForPan();
  464. needAutoRedraw = true;
  465. } else {
  466. mathUtil.clonePoint(coordinate.center, tempCenter);
  467. }
  468. break;
  469. case LayerEvents.AddRoad:
  470. needAutoRedraw = true;
  471. listenLayer.start(position);
  472. if (listenLayer.modifyPoint) {
  473. position = {
  474. x: listenLayer.modifyPoint.x,
  475. y: listenLayer.modifyPoint.y,
  476. };
  477. }
  478. elementService.hideAll();
  479. //鼠标样式
  480. elementService.setPoint(position);
  481. elementService.showPoint();
  482. this.showElementLine(position, eventName);
  483. break;
  484. case LayerEvents.AddLine:
  485. needAutoRedraw = true;
  486. listenLayer.start(position);
  487. if (listenLayer.modifyPoint) {
  488. position = {
  489. x: listenLayer.modifyPoint.x,
  490. y: listenLayer.modifyPoint.y,
  491. };
  492. }
  493. elementService.hideAll();
  494. elementService.setPoint(position);
  495. elementService.showPoint();
  496. this.showElementLine(position, eventName);
  497. break;
  498. case LayerEvents.AddCurveLine:
  499. needAutoRedraw = true;
  500. listenLayer.start(position);
  501. if (listenLayer.modifyPoint) {
  502. position = {
  503. x: listenLayer.modifyPoint.x,
  504. y: listenLayer.modifyPoint.y,
  505. };
  506. }
  507. elementService.hideAll();
  508. elementService.setPoint(position);
  509. elementService.showPoint();
  510. this.showElementLine(position, eventName);
  511. break;
  512. case LayerEvents.AddCircle:
  513. needAutoRedraw = true;
  514. listenLayer.start(position);
  515. if (listenLayer.modifyPoint) {
  516. position = {
  517. x: listenLayer.modifyPoint.x,
  518. y: listenLayer.modifyPoint.y,
  519. };
  520. }
  521. elementService.hideAll();
  522. elementService.setPoint(position);
  523. elementService.showPoint();
  524. this.showElementLine(position);
  525. break;
  526. case LayerEvents.AddingRoad:
  527. needAutoRedraw = true;
  528. listenLayer.start(position);
  529. // listenLayer.start(position, {
  530. // exceptLineId: exceptLineId,
  531. // exceptPointId: exceptPointId,
  532. // addRoad.startInfo.linkedRoadId
  533. // addRoad.startInfo.linkedRoadPointId
  534. // });
  535. if (listenLayer.modifyPoint) {
  536. position = {
  537. x: listenLayer.modifyPoint.x,
  538. y: listenLayer.modifyPoint.y,
  539. };
  540. // console.log(
  541. // "LayerEvents.AddingRoad:" + JSON.stringify(listenLayer.modifyPoint)
  542. // );
  543. }
  544. elementService.execute(addRoad.startInfo.position, position);
  545. elementService.setPoint(position);
  546. elementService.setNewRoad(addRoad.startInfo.position, position);
  547. elementService.showNewRoad();
  548. addRoad.setNewRoadPoint("end", position);
  549. addRoad.canAdd = addRoad.canAddRoadForEnd(position);
  550. if (!addRoad.canAdd) {
  551. elementService.setNewRoadState("error");
  552. } else {
  553. elementService.setNewRoadState("normal");
  554. }
  555. elementService.showPoint();
  556. this.showElementLine(position, eventName);
  557. break;
  558. case LayerEvents.AddingLine:
  559. needAutoRedraw = true;
  560. let exceptLineId = null;
  561. let exceptPointId = null;
  562. if (addLine.newLine != null) {
  563. exceptLineId = addLine.newLine.vectorId;
  564. exceptPointId = addLine.newLine.endId;
  565. if (exceptPointId) {
  566. let exceptPoint = dataService.getPoint(exceptPointId);
  567. exceptLineId = exceptPoint.getParent();
  568. }
  569. }
  570. listenLayer.start(position, {
  571. //exceptLineId: exceptLineId,
  572. exceptLineIds: exceptLineId,
  573. exceptPointId: exceptPointId,
  574. });
  575. if (listenLayer.modifyPoint) {
  576. position = {
  577. x: listenLayer.modifyPoint.x,
  578. y: listenLayer.modifyPoint.y,
  579. };
  580. }
  581. // elementService.execute(addLine.startInfo.position, position);
  582. // elementService.setPoint(position);
  583. if (addLine.newLine == null) {
  584. addLine.buildLine(position);
  585. } else {
  586. addLine.updateLine(position);
  587. }
  588. elementService.hideAll();
  589. elementService.setPoint(position);
  590. elementService.showPoint();
  591. this.showElementLine(position, eventName);
  592. break;
  593. case LayerEvents.AddingCurveLine:
  594. needAutoRedraw = true;
  595. let exceptCurveLineId = null;
  596. let exceptCurvePointId = null;
  597. if (addLine.newLine != null) {
  598. exceptCurveLineId = addLine.newLine.vectorId;
  599. exceptCurvePointId = addLine.newLine.endId;
  600. }
  601. listenLayer.start(position, {
  602. exceptCurveLineId: exceptCurveLineId,
  603. exceptCurvePointId: exceptCurvePointId,
  604. });
  605. if (listenLayer.modifyPoint) {
  606. position = {
  607. x: listenLayer.modifyPoint.x,
  608. y: listenLayer.modifyPoint.y,
  609. };
  610. }
  611. // elementService.execute(addLine.startInfo.position, position);
  612. // elementService.setPoint(position);
  613. if (addLine.newLine == null) {
  614. addLine.buildCurveLine(position);
  615. } else {
  616. addLine.updateCurveLine(position);
  617. }
  618. elementService.hideAll();
  619. elementService.setPoint(position);
  620. elementService.showPoint();
  621. this.showElementLine(position, eventName);
  622. break;
  623. case LayerEvents.AddingCircle:
  624. needAutoRedraw = true;
  625. let exceptCircleId = null;
  626. if (addCircle.newCircle != null) {
  627. exceptCircleId = addCircle.newCircle.vectorId;
  628. }
  629. listenLayer.start(position, { exceptCircleId: exceptCircleId });
  630. if (listenLayer.modifyPoint) {
  631. position = {
  632. x: listenLayer.modifyPoint.x,
  633. y: listenLayer.modifyPoint.y,
  634. };
  635. }
  636. if (addCircle.newCircle == null) {
  637. addCircle.buildCircle(position);
  638. } else {
  639. addCircle.updateCircle(position);
  640. }
  641. elementService.hideAll();
  642. elementService.setPoint(position);
  643. elementService.showPoint();
  644. this.showElementLine(position);
  645. break;
  646. case LayerEvents.MoveRoad:
  647. needAutoRedraw = true;
  648. //只允许拖拽一条公路
  649. let road = dataService.getRoad(draggingItem.vectorId);
  650. let start = dataService.getRoadPoint(road.startId);
  651. let end = dataService.getRoadPoint(road.endId);
  652. if (
  653. Object.keys(start.getParent()).length == 1 &&
  654. Object.keys(end.getParent()).length == 1
  655. ) {
  656. //拖拽的路只有一条
  657. moveRoad.moveRoad(
  658. draggingItem.vectorId,
  659. (dx * coordinate.defaultZoom) / coordinate.zoom,
  660. (dy * coordinate.defaultZoom) / coordinate.zoom
  661. );
  662. }
  663. break;
  664. case LayerEvents.MoveRoadPoint:
  665. point = dataService.getRoadPoint(draggingItem.vectorId);
  666. listenLayer.start(position, {
  667. exceptRoadPointId: draggingItem.vectorId,
  668. exceptRoadIds: point.parent,
  669. });
  670. if (
  671. listenLayer.modifyPoint &&
  672. (listenLayer.modifyPoint.linkedRoadPointId ||
  673. listenLayer.modifyPoint.linkedRoadId ||
  674. listenLayer.modifyPoint.linkedRoadPointIdX ||
  675. listenLayer.modifyPoint.linkedRoadPointIdY)
  676. ) {
  677. position = {
  678. x: listenLayer.modifyPoint.x,
  679. y: listenLayer.modifyPoint.y,
  680. };
  681. } else {
  682. listenLayer.modifyPoint = null;
  683. }
  684. let flag = moveRoad.moveingRoadPoint(
  685. draggingItem.vectorId,
  686. position,
  687. listenLayer.modifyPoint
  688. );
  689. if (!flag) {
  690. elementService.hideAll();
  691. } else {
  692. this.showElementLine(point, eventName);
  693. }
  694. needAutoRedraw = true;
  695. break;
  696. case LayerEvents.AddCurveRoad:
  697. needAutoRedraw = true;
  698. listenLayer.start(position);
  699. if (listenLayer.modifyPoint) {
  700. position = {
  701. x: listenLayer.modifyPoint.x,
  702. y: listenLayer.modifyPoint.y,
  703. };
  704. }
  705. elementService.hideAll();
  706. elementService.setPoint(position);
  707. elementService.showPoint();
  708. this.showElementLine(position, eventName);
  709. break;
  710. case LayerEvents.AddingCurveRoad:
  711. needAutoRedraw = true;
  712. listenLayer.start(position);
  713. if (listenLayer.modifyPoint) {
  714. position = {
  715. x: listenLayer.modifyPoint.x,
  716. y: listenLayer.modifyPoint.y,
  717. };
  718. }
  719. elementService.execute(addRoad.startInfo.position, position);
  720. elementService.setPoint(position);
  721. elementService.setNewRoad(addRoad.startInfo.position, position);
  722. elementService.showNewRoad();
  723. addRoad.setNewRoadPoint("end", position);
  724. addRoad.canAdd = addRoad.canAddRoadForEnd(position);
  725. if (!addRoad.canAdd) {
  726. elementService.setNewRoadState("error");
  727. } else {
  728. elementService.setNewRoadState("normal");
  729. }
  730. elementService.showPoint();
  731. this.showElementLine(position, eventName);
  732. break;
  733. case LayerEvents.MoveCurveRoad:
  734. needAutoRedraw = true;
  735. moveRoad.moveCurveRoad(
  736. draggingItem.vectorId,
  737. (dx * coordinate.defaultZoom) / coordinate.zoom,
  738. (dy * coordinate.defaultZoom) / coordinate.zoom
  739. );
  740. break;
  741. case LayerEvents.MoveCurveRoadPoint:
  742. if (!draggingItem || !draggingItem.vectorId) {
  743. return;
  744. }
  745. point = dataService.getCurveRoadPoint(draggingItem.vectorId);
  746. listenLayer.start(position, {
  747. exceptCurveRoadPointId: draggingItem.vectorId,
  748. exceptCurveRoadId: point.parent, //不会融合,所以parent只有一个
  749. });
  750. if (
  751. listenLayer.modifyPoint &&
  752. (listenLayer.modifyPoint.linkedCurveRoadPointId ||
  753. listenLayer.modifyPoint.linkedRoadPointId ||
  754. listenLayer.modifyPoint.linkedRoadId ||
  755. listenLayer.modifyPoint.linkedRoadPointIdX ||
  756. listenLayer.modifyPoint.linkedRoadPointIdY ||
  757. listenLayer.modifyPoint.linkedCurvePointIdX ||
  758. listenLayer.modifyPoint.linkedCurvePointIdY ||
  759. listenLayer.modifyPoint.linkedCurveRoadPointIdX)
  760. ) {
  761. position = {
  762. x: listenLayer.modifyPoint.x,
  763. y: listenLayer.modifyPoint.y,
  764. };
  765. }
  766. moveRoad.moveCurveRoadPoint(draggingItem.vectorId, position);
  767. this.showElementLine(point, eventName);
  768. needAutoRedraw = true;
  769. break;
  770. case LayerEvents.MoveCrossPoint:
  771. if (!draggingItem || !draggingItem.vectorId) {
  772. return;
  773. }
  774. moveRoad.moveCrossPoint(draggingItem.vectorId, position);
  775. needAutoRedraw = true;
  776. break;
  777. case LayerEvents.MoveEdge:
  778. moveRoad.moveEdge(draggingItem.vectorId, position);
  779. needAutoRedraw = true;
  780. break;
  781. case LayerEvents.MoveCurveEdge:
  782. if (listenLayer.modifyPoint) {
  783. moveRoad.moveCurveEdge(
  784. draggingItem.vectorId,
  785. listenLayer.modifyPoint.selectIndex,
  786. position
  787. );
  788. }
  789. needAutoRedraw = true;
  790. break;
  791. case LayerEvents.MoveLine:
  792. if (draggingItem != null) {
  793. let flag = moveLine.moveLine(
  794. draggingItem.vectorId,
  795. (dx * coordinate.defaultZoom) / coordinate.zoom,
  796. (dy * coordinate.defaultZoom) / coordinate.zoom
  797. );
  798. if (!flag) {
  799. this.lastX = this.lastX - dx / coordinate.ratio;
  800. this.lastY = this.lastY - dy / coordinate.ratio;
  801. }
  802. needAutoRedraw = true;
  803. }
  804. break;
  805. case LayerEvents.MovePoint:
  806. if (draggingItem != null) {
  807. point = dataService.getPoint(draggingItem.vectorId);
  808. listenLayer.start(position, {
  809. exceptPointId: draggingItem.vectorId,
  810. exceptLineIds: point.parent,
  811. });
  812. // if (listenLayer.modifyPoint) { //原本是这样的,不知用途,下面修改为了修复拖动点经过放大镜导致NaN或者错位
  813. if (
  814. listenLayer.modifyPoint &&
  815. listenLayer.modifyPoint.x &&
  816. listenLayer.modifyPoint.y
  817. ) {
  818. position = {
  819. x: listenLayer.modifyPoint.x,
  820. y: listenLayer.modifyPoint.y,
  821. };
  822. }
  823. movePoint.movePoint(position, draggingItem.vectorId);
  824. needAutoRedraw = true;
  825. if (!point) {
  826. stateService.clearEventName();
  827. } else {
  828. this.showElementLine(point, eventName);
  829. }
  830. }
  831. break;
  832. case LayerEvents.MoveCurvePoint:
  833. if (draggingItem != null) {
  834. let curvePoint = dataService.getCurvePoint(draggingItem.vectorId);
  835. listenLayer.start(position, {
  836. exceptCurvePointId: draggingItem.vectorId,
  837. exceptCurveLineId: curvePoint.parent,
  838. });
  839. if (listenLayer.modifyPoint) {
  840. position = {
  841. x: listenLayer.modifyPoint.x,
  842. y: listenLayer.modifyPoint.y,
  843. };
  844. }
  845. movePoint.moveCurvePoint(position, draggingItem.vectorId);
  846. let curveLine = dataService.getCurveLine(curvePoint.getParent());
  847. this.showElementLine(curvePoint, eventName);
  848. needAutoRedraw = true;
  849. }
  850. break;
  851. case LayerEvents.MoveCurveLine:
  852. if (draggingItem != null) {
  853. moveLine.moveCurveLine(
  854. draggingItem.vectorId,
  855. (dx * coordinate.defaultZoom) / coordinate.zoom,
  856. (dy * coordinate.defaultZoom) / coordinate.zoom
  857. );
  858. needAutoRedraw = true;
  859. }
  860. break;
  861. case LayerEvents.MoveCircle:
  862. if (draggingItem != null) {
  863. if (draggingItem.state == -1) {
  864. moveCircle.moveFull(
  865. draggingItem.vectorId,
  866. (dx * coordinate.defaultZoom) / coordinate.zoom,
  867. (dy * coordinate.defaultZoom) / coordinate.zoom
  868. );
  869. } else if (
  870. draggingItem.state == 0 ||
  871. draggingItem.state == 1 ||
  872. draggingItem.state == 2 ||
  873. draggingItem.state == 3
  874. ) {
  875. moveCircle.movePoint(
  876. position,
  877. draggingItem.vectorId,
  878. draggingItem.state
  879. );
  880. } else {
  881. debugger;
  882. }
  883. needAutoRedraw = true;
  884. }
  885. break;
  886. case LayerEvents.MoveText:
  887. needAutoRedraw = true;
  888. if (draggingItem != null) {
  889. let vectorText = dataService.getText(draggingItem.vectorId);
  890. if (!vectorText.linkedPointId) {
  891. moveText.moveFullText(position, draggingItem.vectorId);
  892. }
  893. }
  894. break;
  895. case LayerEvents.MoveSVG:
  896. needAutoRedraw = true;
  897. if (draggingItem != null) {
  898. if (draggingItem.state == -1) {
  899. moveSVG.moveFullSVG(position, draggingItem.vectorId);
  900. } else {
  901. moveSVG.movePoint(
  902. position,
  903. draggingItem.vectorId,
  904. draggingItem.state
  905. );
  906. }
  907. }
  908. break;
  909. case LayerEvents.MoveMagnifier:
  910. needAutoRedraw = true;
  911. if (draggingItem != null) {
  912. moveMagnifier.moveFullMagnifier(
  913. position,
  914. draggingItem.vectorId,
  915. draggingItem.state
  916. );
  917. }
  918. break;
  919. }
  920. if (needAutoRedraw) {
  921. this.renderer.autoRedraw();
  922. }
  923. }
  924. onMouseUp(e) {
  925. // 右键
  926. if (e.button == 2) {
  927. this.dragging = false;
  928. return;
  929. }
  930. let X = e.offsetX || e.layerX;
  931. let Y = e.offsetY || e.layerY;
  932. if (e instanceof TouchEvent) {
  933. X = this.lastX;
  934. Y = this.lastY;
  935. this.StorePage1 = null;
  936. this.StorePage2 = null;
  937. this.StoreMidePage = null;
  938. if (e.touches.length > 1) {
  939. this.dragging = false;
  940. return;
  941. }
  942. }
  943. let eventName = stateService.getEventName();
  944. const draggingItem = stateService.getDraggingItem();
  945. const selectItem = stateService.getSelectItem();
  946. let focusItem = null;
  947. if (!this.dragging && selectItem) {
  948. focusItem = {
  949. vectorId: selectItem.vectorId,
  950. type: selectItem.type,
  951. cursor: { x: this.lastX, y: this.lastY },
  952. dir: selectItem.dir,
  953. };
  954. stateService.setFocusItem(focusItem);
  955. this.uiControl.focusVector = focusItem;
  956. stateService.clearDraggingItem();
  957. } else {
  958. this.uiControl.focusVector = null;
  959. }
  960. this.dragging = false;
  961. let position = coordinate.getXYFromScreen({
  962. x: X,
  963. y: Y,
  964. });
  965. let needAutoRedraw = false;
  966. switch (eventName) {
  967. case null:
  968. if (e instanceof TouchEvent) {
  969. stateService.clearSelectItem();
  970. stateService.clearDraggingItem();
  971. this.uiControl.focusVector = null;
  972. this.renderer.autoRedraw();
  973. }
  974. return;
  975. case LayerEvents.PanBackGround:
  976. needAutoRedraw = true;
  977. stateService.clearFocusItem();
  978. this.uiControl.focusVector = null;
  979. this.uiControl.currentUI = null;
  980. Settings.selectBasePointId = null;
  981. break;
  982. case LayerEvents.MoveRoadPoint:
  983. if (!draggingItem || !draggingItem.vectorId) {
  984. break;
  985. }
  986. needAutoRedraw = true;
  987. elementService.hideAll();
  988. let point = dataService.getRoadPoint(draggingItem.vectorId);
  989. if (point) {
  990. listenLayer.start(point, {
  991. exceptRoadPointId: draggingItem.vectorId,
  992. exceptRoadIds: point.parent,
  993. });
  994. if (
  995. listenLayer.modifyPoint &&
  996. listenLayer.modifyPoint.hasOwnProperty("linkedRoadPointId")
  997. ) {
  998. moveRoad.moveTo(
  999. draggingItem.vectorId,
  1000. listenLayer.modifyPoint.linkedRoadPointId
  1001. );
  1002. } else if (
  1003. listenLayer.modifyPoint &&
  1004. (listenLayer.modifyPoint.linkedRoadPointIdX ||
  1005. listenLayer.modifyPoint.linkedRoadPointIdY)
  1006. ) {
  1007. mathUtil.clonePoint(point, listenLayer.modifyPoint);
  1008. } else if (
  1009. listenLayer.modifyPoint &&
  1010. listenLayer.modifyPoint.hasOwnProperty("linkedRoadId")
  1011. ) {
  1012. point = roadPointService.create({
  1013. x: listenLayer.modifyPoint.x,
  1014. y: listenLayer.modifyPoint.y,
  1015. });
  1016. roadService.splitRoad(
  1017. listenLayer.modifyPoint.linkedRoadId,
  1018. point.vectorId,
  1019. "start"
  1020. );
  1021. moveRoad.moveTo(draggingItem.vectorId, point.vectorId);
  1022. } else if (moveRoad.splitRoadId != null) {
  1023. roadService.splitRoad(
  1024. moveRoad.splitRoadId,
  1025. draggingItem.vectorId,
  1026. "start"
  1027. );
  1028. }
  1029. //draggingItem.vectorId所在的墙面与其他墙角相交
  1030. moveRoad.updateForAbsorbRoadPoints();
  1031. let parent = point.getParent();
  1032. for (let key in parent) {
  1033. roadService.setLanes(key);
  1034. }
  1035. this.history.save();
  1036. }
  1037. break;
  1038. // case LayerEvents.AddRoad:
  1039. // addRoad.setNewRoadPoint("start", position);
  1040. // break;
  1041. case LayerEvents.AddingRoad:
  1042. needAutoRedraw = true;
  1043. if (addRoad.canAdd) {
  1044. addRoad.buildRoad();
  1045. this.history.save();
  1046. elementService.hideAll();
  1047. }
  1048. break;
  1049. case LayerEvents.AddingLine:
  1050. if (Settings.selectLocationMode == Constant.freeLocationMode) {
  1051. this.uiControl.showConfirm();
  1052. }
  1053. needAutoRedraw = true;
  1054. addLine.finish(position);
  1055. this.updateForLocation();
  1056. addLine.clearVectorData();
  1057. this.history.save();
  1058. elementService.hideAll();
  1059. break;
  1060. case LayerEvents.AddingCurveLine:
  1061. needAutoRedraw = true;
  1062. addLine.finishCurveLine(position);
  1063. addLine.clearVectorData();
  1064. this.history.save();
  1065. elementService.hideAll();
  1066. break;
  1067. case LayerEvents.AddingCircle:
  1068. needAutoRedraw = true;
  1069. addCircle.finish(position);
  1070. addCircle.clear();
  1071. this.history.save();
  1072. elementService.hideAll();
  1073. break;
  1074. case LayerEvents.MoveText:
  1075. needAutoRedraw = true;
  1076. this.history.save();
  1077. elementService.hideAll();
  1078. break;
  1079. case LayerEvents.MoveSVG:
  1080. needAutoRedraw = true;
  1081. this.history.save();
  1082. elementService.hideAll();
  1083. break;
  1084. case LayerEvents.MoveMagnifier:
  1085. needAutoRedraw = true;
  1086. this.history.save();
  1087. elementService.hideAll();
  1088. //拖拽完放大镜后需要重新打开
  1089. // focusItem = {
  1090. // vectorId: selectItem.vectorId,
  1091. // type: selectItem.type,
  1092. // cursor: { x: this.lastX, y: this.lastY },
  1093. // dir: selectItem.dir,
  1094. // };
  1095. // this.uiControl.focusVector = focusItem;
  1096. // stateService.setFocusItem(focusItem);
  1097. break;
  1098. case LayerEvents.MoveSVG:
  1099. needAutoRedraw = true;
  1100. this.history.save();
  1101. elementService.hideAll();
  1102. break;
  1103. case LayerEvents.AddingCurveRoad:
  1104. needAutoRedraw = true;
  1105. if (addRoad.canAdd) {
  1106. addRoad.buildCurveRoad();
  1107. this.history.save();
  1108. elementService.hideAll();
  1109. }
  1110. break;
  1111. case LayerEvents.MoveRoad:
  1112. needAutoRedraw = true;
  1113. this.history.save();
  1114. break;
  1115. case LayerEvents.MoveCurveRoad:
  1116. needAutoRedraw = true;
  1117. this.history.save();
  1118. break;
  1119. case LayerEvents.MoveCurveRoadPoint:
  1120. needAutoRedraw = true;
  1121. if (
  1122. listenLayer.modifyPoint &&
  1123. listenLayer.modifyPoint.linkedCurveRoadPointId
  1124. ) {
  1125. let curveRoadPoint1 = dataService.getCurveRoadPoint(
  1126. listenLayer.modifyPoint.linkedCurveRoadPointId
  1127. );
  1128. let curveRoadPoint2 = dataService.getCurveRoadPoint(
  1129. draggingItem.vectorId
  1130. );
  1131. if (
  1132. listenLayer.modifyPoint.linkedCurveRoadPointId !=
  1133. draggingItem.vectorId &&
  1134. curveRoadPoint1.getParent() == curveRoadPoint2.getParent()
  1135. ) {
  1136. curveRoadPointService.deleteCurveRoadPoint(
  1137. listenLayer.modifyPoint.linkedCurveRoadPointId
  1138. );
  1139. }
  1140. }
  1141. this.history.save();
  1142. break;
  1143. case LayerEvents.MoveCrossPoint:
  1144. needAutoRedraw = true;
  1145. this.history.save();
  1146. break;
  1147. case LayerEvents.MoveEdge:
  1148. needAutoRedraw = true;
  1149. this.history.save();
  1150. break;
  1151. case LayerEvents.MoveCurveEdge:
  1152. needAutoRedraw = true;
  1153. this.history.save();
  1154. break;
  1155. case LayerEvents.MoveLine:
  1156. needAutoRedraw = true;
  1157. this.history.save();
  1158. break;
  1159. case LayerEvents.MovePoint:
  1160. needAutoRedraw = true;
  1161. if (draggingItem && draggingItem.vectorId) {
  1162. movePoint.finish(draggingItem.vectorId);
  1163. uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
  1164. }
  1165. this.history.save();
  1166. elementService.hideAll();
  1167. break;
  1168. case LayerEvents.MoveCurvePoint:
  1169. needAutoRedraw = true;
  1170. if (
  1171. listenLayer.modifyPoint &&
  1172. listenLayer.modifyPoint.linkedCurvePointId
  1173. ) {
  1174. let curvePoint1 = dataService.getCurvePoint(
  1175. listenLayer.modifyPoint.linkedCurvePointId
  1176. );
  1177. let curvePoint2 = dataService.getCurvePoint(draggingItem.vectorId);
  1178. if (
  1179. listenLayer.modifyPoint.linkedCurvePointId !=
  1180. draggingItem.vectorId &&
  1181. curvePoint1.getParent() == curvePoint2.getParent()
  1182. ) {
  1183. lineService.deleteCrossPointForCurveLine(
  1184. listenLayer.modifyPoint.linkedCurvePointId,
  1185. curvePoint1.getParent()
  1186. );
  1187. }
  1188. }
  1189. this.history.save();
  1190. break;
  1191. case LayerEvents.MoveCurveLine:
  1192. needAutoRedraw = true;
  1193. this.history.save();
  1194. break;
  1195. case LayerEvents.MoveCircle:
  1196. needAutoRedraw = true;
  1197. this.history.save();
  1198. break;
  1199. case LayerEvents.AddPoint:
  1200. if (
  1201. (Settings.selectBasePointId != null &&
  1202. (Settings.selectLocationMode == Constant.angleLocationMode ||
  1203. Settings.selectLocationMode == Constant.allLocationMode)) ||
  1204. (Settings.baseLineId != null &&
  1205. Settings.selectLocationMode == Constant.normalLocationMode)
  1206. ) {
  1207. this.uiControl.showConfirm();
  1208. needAutoRedraw = true;
  1209. elementService.hideAll();
  1210. }
  1211. break;
  1212. case LayerEvents.AddRoadTemplate:
  1213. addCrossRoad.build(position);
  1214. this.history.save();
  1215. this.renderer.autoRedraw();
  1216. break;
  1217. }
  1218. this.setEventName("mouseUp");
  1219. stateService.clearDraggingItem();
  1220. if (needAutoRedraw) {
  1221. this.renderer.autoRedraw();
  1222. }
  1223. }
  1224. onWheel(e) {
  1225. e.preventDefault();
  1226. const type = e.type;
  1227. if (type == "DOMMouseScroll" || type == "mousewheel") {
  1228. // 当在canvas用滚轮滚动时
  1229. const delta = e.wheelDelta
  1230. ? (e.wheelDelta / 120) * 20
  1231. : (-(e.detail || 0) / 3) * 20;
  1232. const zoom = coordinate.zoom + delta;
  1233. let X = e.offsetX || e.layerX;
  1234. let Y = e.offsetY || e.layerY;
  1235. this.zoomVector(
  1236. {
  1237. x: X,
  1238. y: Y,
  1239. },
  1240. zoom
  1241. );
  1242. }
  1243. }
  1244. zoomVector(position, zoom) {
  1245. if (zoom < minZoom || zoom > maxZoom) {
  1246. return;
  1247. }
  1248. let tempCenter = {};
  1249. mathUtil.clonePoint(tempCenter, coordinate.center);
  1250. let tempZoom = coordinate.zoom;
  1251. coordinate.updateZoom(position, zoom);
  1252. let inScreen = imageService.backgroundImgVertexsInScreenXY();
  1253. if (!inScreen) {
  1254. dataService.setGridForZoom();
  1255. this.renderer.autoRedraw();
  1256. } else {
  1257. mathUtil.clonePoint(coordinate.center, tempCenter);
  1258. coordinate.zoom = tempZoom;
  1259. }
  1260. }
  1261. setEventName(eventType) {
  1262. let eventName = stateService.getEventName();
  1263. if (eventType == "mouseDown") {
  1264. if (eventName == null) {
  1265. const selectItem = stateService.getSelectItem();
  1266. if (selectItem == null) {
  1267. stateService.setEventName(LayerEvents.PanBackGround);
  1268. } else if (selectItem.type == VectorType.Road) {
  1269. stateService.setEventName(LayerEvents.MoveRoad);
  1270. } else if (selectItem.type == VectorType.RoadPoint) {
  1271. stateService.setEventName(LayerEvents.MoveRoadPoint);
  1272. } else if (selectItem.type == VectorType.CurveRoad) {
  1273. stateService.setEventName(LayerEvents.MoveCurveRoad);
  1274. } else if (selectItem.type == VectorType.CurveRoadPoint) {
  1275. stateService.setEventName(LayerEvents.MoveCurveRoadPoint);
  1276. } else if (selectItem.type == VectorType.CrossPoint) {
  1277. stateService.setEventName(LayerEvents.MoveCrossPoint);
  1278. } else if (selectItem.type == VectorType.RoadEdge) {
  1279. stateService.setEventName(LayerEvents.MoveEdge);
  1280. } else if (selectItem.type == VectorType.CurveRoadEdge) {
  1281. stateService.setEventName(LayerEvents.MoveCurveEdge);
  1282. } else if (selectItem.type == VectorType.Point) {
  1283. stateService.setEventName(LayerEvents.MovePoint);
  1284. } else if (selectItem.type == VectorType.Line) {
  1285. stateService.setEventName(LayerEvents.MoveLine);
  1286. } else if (selectItem.type == VectorType.CurvePoint) {
  1287. stateService.setEventName(LayerEvents.MoveCurvePoint);
  1288. } else if (selectItem.type == VectorType.CurveLine) {
  1289. stateService.setEventName(LayerEvents.MoveCurveLine);
  1290. } else if (selectItem.type == VectorType.Circle) {
  1291. stateService.setEventName(LayerEvents.MoveCircle);
  1292. } else if (selectItem.type == VectorType.Text) {
  1293. stateService.setEventName(LayerEvents.MoveText);
  1294. } else if (selectItem.type == VectorType.Magnifier) {
  1295. stateService.setEventName(LayerEvents.MoveMagnifier);
  1296. } else if (selectItem.type == VectorType.SVG) {
  1297. stateService.setEventName(LayerEvents.MoveSVG);
  1298. }
  1299. }
  1300. } else if (eventType == "mouseUp") {
  1301. if (eventName == LayerEvents.AddingRoad) {
  1302. if (Settings.isMobile) {
  1303. stateService.clearEventName();
  1304. this.exit();
  1305. } else {
  1306. stateService.setEventName(LayerEvents.AddRoad);
  1307. }
  1308. } else if (eventName == LayerEvents.AddingLine) {
  1309. if (Settings.isMobile) {
  1310. if (Settings.selectLocationMode == Constant.freeLocationMode) {
  1311. stateService.setEventName(LayerEvents.AddLine);
  1312. } else {
  1313. stateService.clearEventName();
  1314. this.exit();
  1315. uiService.setSelectLineCategory(VectorCategory.Line.NormalLine);
  1316. }
  1317. } else {
  1318. stateService.setEventName(LayerEvents.AddLine);
  1319. }
  1320. } else if (eventName == LayerEvents.AddingCurveRoad) {
  1321. if (Settings.isMobile) {
  1322. stateService.clearEventName();
  1323. this.exit();
  1324. } else {
  1325. stateService.setEventName(LayerEvents.AddCurveRoad);
  1326. }
  1327. } else if (eventName == LayerEvents.AddLine) {
  1328. stateService.setEventName(LayerEvents.AddingLine);
  1329. } else if (
  1330. (eventName == LayerEvents.AddPoint &&
  1331. Settings.selectBasePointId != null &&
  1332. (Settings.selectLocationMode == Constant.angleLocationMode ||
  1333. Settings.selectLocationMode == Constant.allLocationMode)) ||
  1334. (eventName == LayerEvents.AddPoint &&
  1335. Settings.baseLineId != null &&
  1336. Settings.selectLocationMode == Constant.normalLocationMode)
  1337. ) {
  1338. } else {
  1339. stateService.clearEventName();
  1340. }
  1341. }
  1342. }
  1343. exit() {
  1344. stateService.clear();
  1345. this.uiControl.clearUI();
  1346. this.uiControl.focusVector = null;
  1347. this.uiControl.currentUI = null;
  1348. }
  1349. stopAddVector() {
  1350. let eventName = stateService.getEventName();
  1351. if (
  1352. eventName != LayerEvents.AddingRoad &&
  1353. eventName != LayerEvents.AddingLine
  1354. ) {
  1355. stateService.clearEventName();
  1356. } else if (eventName == LayerEvents.AddingRoad) {
  1357. stateService.setEventName(LayerEvents.AddRoad);
  1358. } else if (eventName == LayerEvents.AddingLine) {
  1359. stateService.setEventName(LayerEvents.AddLine);
  1360. }
  1361. addLine.clear(); //之前会保留category
  1362. this.uiControl.clearUI();
  1363. elementService.hideAll();
  1364. this.renderer.autoRedraw();
  1365. }
  1366. initLocation() {
  1367. Settings.baseLineId = null;
  1368. this.uiControl.graphicStateUI.canAngleLocationMode = false;
  1369. this.uiControl.graphicStateUI.canAllLocationMode = false;
  1370. this.uiControl.graphicStateUI.canVerticalMeasure = false;
  1371. this.uiControl.graphicStateUI.existsBaseLine = false;
  1372. }
  1373. //更新定位信息
  1374. updateForLocation() {
  1375. if (Settings.baseLineId) {
  1376. this.uiControl.graphicStateUI.canAngleLocationMode = true;
  1377. this.uiControl.graphicStateUI.canAllLocationMode = true;
  1378. this.uiControl.graphicStateUI.existsBaseLine = true;
  1379. this.uiControl.graphicStateUI.canVerticalMeasure = true;
  1380. } else {
  1381. this.uiControl.graphicStateUI.canAngleLocationMode = false;
  1382. this.uiControl.graphicStateUI.canAllLocationMode = false;
  1383. this.uiControl.graphicStateUI.existsBaseLine = false;
  1384. this.uiControl.graphicStateUI.canVerticalMeasure = false;
  1385. }
  1386. }
  1387. // showElementLine(point, eventName) {
  1388. // let otherPoint1 = null;
  1389. // let otherPoint2 = null;
  1390. // if (
  1391. // listenLayer.modifyPoint &&
  1392. // listenLayer.modifyPoint.linkedRoadPointIdX &&
  1393. // (eventName == LayerEvents.AddingRoad ||
  1394. // eventName == LayerEvents.MoveRoadPoint ||
  1395. // eventName == LayerEvents.AddRoad)
  1396. // ) {
  1397. // otherPoint1 = dataService.getRoadPoint(
  1398. // listenLayer.modifyPoint.linkedRoadPointIdX
  1399. // );
  1400. // } else if (
  1401. // listenLayer.modifyPoint &&
  1402. // listenLayer.modifyPoint.linkedCurveRoadPointIdX &&
  1403. // (eventName == LayerEvents.AddingCurveRoad ||
  1404. // eventName == LayerEvents.MoveCurveRoadPoint ||
  1405. // eventName == LayerEvents.AddCurveRoad)
  1406. // ) {
  1407. // otherPoint1 = dataService.getCurveRoadPoint(
  1408. // listenLayer.modifyPoint.linkedCurvePointIdX
  1409. // );
  1410. // } else if (
  1411. // listenLayer.modifyPoint &&
  1412. // listenLayer.modifyPoint.linkedPointIdX &&
  1413. // (eventName == LayerEvents.AddLine || eventName == LayerEvents.MovePoint)
  1414. // ) {
  1415. // otherPoint1 = dataService.getPoint(
  1416. // listenLayer.modifyPoint.linkedPointIdX
  1417. // );
  1418. // } else if (
  1419. // listenLayer.modifyPoint &&
  1420. // listenLayer.modifyPoint.linkedCurvePointIdX &&
  1421. // (eventName == LayerEvents.AddCurveLine ||
  1422. // eventName == LayerEvents.MoveCurvePoint)
  1423. // ) {
  1424. // otherPoint1 = dataService.getCurvePoint(
  1425. // listenLayer.modifyPoint.linkedCurvePointIdX
  1426. // );
  1427. // }
  1428. // if (
  1429. // listenLayer.modifyPoint &&
  1430. // listenLayer.modifyPoint.linkedRoadPointIdY &&
  1431. // (eventName == LayerEvents.AddingRoad ||
  1432. // eventName == LayerEvents.MoveRoadPoint ||
  1433. // eventName == LayerEvents.AddRoad)
  1434. // ) {
  1435. // otherPoint2 = dataService.getRoadPoint(
  1436. // listenLayer.modifyPoint.linkedRoadPointIdY
  1437. // );
  1438. // } else if (
  1439. // listenLayer.modifyPoint &&
  1440. // listenLayer.modifyPoint.linkedCurvePointIdY &&
  1441. // (eventName == LayerEvents.AddingCurveRoad ||
  1442. // eventName == LayerEvents.MoveCurveRoadPoint ||
  1443. // eventName == LayerEvents.AddCurveRoad)
  1444. // ) {
  1445. // otherPoint2 = dataService.getCurveRoadPoint(
  1446. // listenLayer.modifyPoint.linkedCurvePointIdY
  1447. // );
  1448. // } else if (
  1449. // listenLayer.modifyPoint &&
  1450. // listenLayer.modifyPoint.linkedPointIdY &&
  1451. // (eventName == LayerEvents.AddLine || eventName == LayerEvents.MovePoint)
  1452. // ) {
  1453. // otherPoint2 = dataService.getPoint(
  1454. // listenLayer.modifyPoint.linkedPointIdY
  1455. // );
  1456. // } else if (
  1457. // listenLayer.modifyPoint &&
  1458. // listenLayer.modifyPoint.linkedCurvePointIdY &&
  1459. // (eventName == LayerEvents.AddCurveLine ||
  1460. // eventName == LayerEvents.MoveCurvePoint)
  1461. // ) {
  1462. // otherPoint2 = dataService.getCurvePoint(
  1463. // listenLayer.modifyPoint.linkedCurvePointIdY
  1464. // );
  1465. // }
  1466. // let otherPoint = {};
  1467. // if (otherPoint1) {
  1468. // otherPoint.x = otherPoint1.x;
  1469. // otherPoint.y = otherPoint1.y;
  1470. // }
  1471. // if (otherPoint2) {
  1472. // otherPoint.y = otherPoint2.y;
  1473. // if (!otherPoint.hasOwnProperty("x")) {
  1474. // otherPoint.x = otherPoint2.x;
  1475. // }
  1476. // }
  1477. // if (otherPoint.hasOwnProperty("x") && otherPoint.hasOwnProperty("y")) {
  1478. // elementService.execute(otherPoint, point);
  1479. // } else {
  1480. // elementService.hideAll();
  1481. // }
  1482. // }
  1483. showElementLine(point) {
  1484. let otherPoint1 = null;
  1485. let otherPoint2 = null;
  1486. if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedRoadPointIdX) {
  1487. otherPoint1 = dataService.getRoadPoint(
  1488. listenLayer.modifyPoint.linkedRoadPointIdX
  1489. );
  1490. } else if (
  1491. listenLayer.modifyPoint &&
  1492. listenLayer.modifyPoint.linkedCurveRoadPointIdX
  1493. ) {
  1494. otherPoint1 = dataService.getCurveRoadPoint(
  1495. listenLayer.modifyPoint.linkedCurvePointIdX
  1496. );
  1497. } else if (
  1498. listenLayer.modifyPoint &&
  1499. listenLayer.modifyPoint.linkedPointIdX
  1500. ) {
  1501. otherPoint1 = dataService.getPoint(
  1502. listenLayer.modifyPoint.linkedPointIdX
  1503. );
  1504. } else if (
  1505. listenLayer.modifyPoint &&
  1506. listenLayer.modifyPoint.linkedCurvePointIdX
  1507. ) {
  1508. otherPoint1 = dataService.getCurvePoint(
  1509. listenLayer.modifyPoint.linkedCurvePointIdX
  1510. );
  1511. }
  1512. if (listenLayer.modifyPoint && listenLayer.modifyPoint.linkedRoadPointIdY) {
  1513. otherPoint2 = dataService.getRoadPoint(
  1514. listenLayer.modifyPoint.linkedRoadPointIdY
  1515. );
  1516. } else if (
  1517. listenLayer.modifyPoint &&
  1518. listenLayer.modifyPoint.linkedCurvePointIdY
  1519. ) {
  1520. otherPoint2 = dataService.getCurveRoadPoint(
  1521. listenLayer.modifyPoint.linkedCurvePointIdY
  1522. );
  1523. } else if (
  1524. listenLayer.modifyPoint &&
  1525. listenLayer.modifyPoint.linkedPointIdY
  1526. ) {
  1527. otherPoint2 = dataService.getPoint(
  1528. listenLayer.modifyPoint.linkedPointIdY
  1529. );
  1530. } else if (
  1531. listenLayer.modifyPoint &&
  1532. listenLayer.modifyPoint.linkedCurvePointIdY
  1533. ) {
  1534. otherPoint2 = dataService.getCurvePoint(
  1535. listenLayer.modifyPoint.linkedCurvePointIdY
  1536. );
  1537. }
  1538. let otherPoint = {};
  1539. if (otherPoint1) {
  1540. otherPoint.x = otherPoint1.x;
  1541. otherPoint.y = otherPoint1.y;
  1542. }
  1543. if (otherPoint2) {
  1544. otherPoint.y = otherPoint2.y;
  1545. if (!otherPoint.hasOwnProperty("x")) {
  1546. otherPoint.x = otherPoint2.x;
  1547. }
  1548. }
  1549. elementService.execute(otherPoint, point);
  1550. }
  1551. //测试用
  1552. onKeydown(e) {
  1553. let focusItem = stateService.getFocusItem();
  1554. if (focusItem) {
  1555. console.log("键盘(foucus有效):" + e.code);
  1556. //添加固定点
  1557. if (e.code == "KeyQ") {
  1558. uiService.setSelectPointCategory(VectorCategory.Point.FixPoint);
  1559. stateService.setEventName(LayerEvents.AddPoint);
  1560. }
  1561. //一键测量
  1562. if (e.code == "KeyA") {
  1563. locationModeControl.setAngle();
  1564. this.renderer.autoRedraw();
  1565. this.history.save();
  1566. }
  1567. }
  1568. }
  1569. }