Layer.js 53 KB

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