Layer.js 56 KB

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