Layer.js 55 KB

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