UIControl.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. import { coordinate } from "../Coordinate.js";
  2. import LayerEvents from "../enum/LayerEvents.js";
  3. import UIEvents from "../enum/UIEvents.js";
  4. import RoadTemplate from "../enum/RoadTemplate.js";
  5. import RoadStructure from "../enum/RoadStructure.js";
  6. import VectorType from "../enum/VectorType.js";
  7. import VectorStyle from "../enum/VectorStyle.js";
  8. import GeoActions from "../enum/GeoActions.js";
  9. import VectorEvents from "../enum/VectorEvents.js";
  10. import SVGType from "../enum/SVGType.js";
  11. import { stateService } from "../Service/StateService.js";
  12. import { uiService } from "../Service/UIService.js";
  13. import { dataService } from "../Service/DataService.js";
  14. import { historyService } from "../Service/HistoryService.js";
  15. import { elementService } from "../Service/ElementService";
  16. import { lineService } from "../Service/LineService.js";
  17. import { circleService } from "../Service/CircleService.js";
  18. import { textService } from "../Service/TextService.js";
  19. import { magnifierService } from "../Service/MagnifierService.js";
  20. import { mathUtil } from "../Util/MathUtil";
  21. import Constant from "../Constant";
  22. // import { roomsUtil } from "../Room/RoomsUtil.js";
  23. import { addRoad } from "../Controls/AddRoad";
  24. import { addLine } from "./AddLine.js";
  25. import VectorCategory from "../enum/VectorCategory.js";
  26. // import { floorplanData } from "../VectorData.js";
  27. import Message from "@/components/base/components/message/message.vue";
  28. import { pointService } from "../Service/PointService.js";
  29. import Settings from "../Settings.js";
  30. import { addPoint } from "./AddPoint.js";
  31. import { ellipticService } from "../Service/EllipticService.js";
  32. import { curveRoadPointService } from "../Service/CurveRoadPointService.js";
  33. import { roadService } from "../Service/RoadService.js";
  34. import { curveRoadService } from "../Service/CurveRoadService.js";
  35. export default class UIControl {
  36. constructor(layer, newsletter, graphicStateUI) {
  37. this._prompts = [];
  38. this.layer = layer;
  39. this.newsletter = newsletter;
  40. this.graphicStateUI = graphicStateUI;
  41. }
  42. get selectUI() {
  43. return this.newsletter.selectUI;
  44. }
  45. set selectUI(selectUI) {
  46. this.updateEventNameForSelectUI(selectUI);
  47. this.newsletter.selectUI = selectUI;
  48. }
  49. get focusVector() {
  50. return this.newsletter.focusVector;
  51. }
  52. set focusVector(focusVector) {
  53. this.newsletter.focusVector = focusVector;
  54. }
  55. /**
  56. * 获取选中要操作的UI
  57. */
  58. get currentUI() {}
  59. /**
  60. * 设置选中要操作的UI
  61. */
  62. set currentUI(value) {
  63. this.selectUI = value;
  64. }
  65. clearUI() {
  66. this.currentUI = null;
  67. this.selectUI = null;
  68. }
  69. //点击左侧栏后,更新事件
  70. updateEventNameForSelectUI(selectUI) {
  71. console.log(this.selectUI, selectUI);
  72. if (selectUI != null) {
  73. if (this.selectUI == selectUI) {
  74. return;
  75. } else if (this.selectUI != selectUI) {
  76. if (this.selectUI != null) {
  77. //先取消当前事件和进程
  78. this.layer.exit();
  79. }
  80. //执行新的事件
  81. if (uiService.isBelongRoad(selectUI) || selectUI == "road") {
  82. stateService.setEventName(LayerEvents.AddRoad);
  83. } else if (selectUI == UIEvents.CurveRoad) {
  84. stateService.setEventName(LayerEvents.AddCurveRoad);
  85. } else if (uiService.isBelongLine(selectUI)) {
  86. stateService.setEventName(LayerEvents.AddLine);
  87. } else if (selectUI == UIEvents.CurveLine) {
  88. stateService.setEventName(LayerEvents.AddCurveLine);
  89. } else if (uiService.isBelongPoint(selectUI)) {
  90. stateService.setEventName(LayerEvents.AddPoint);
  91. } else if (selectUI == UIEvents.Circle) {
  92. stateService.setEventName(LayerEvents.AddCircle);
  93. } else if (selectUI == UIEvents.Elliptic) {
  94. stateService.setEventName(LayerEvents.AddElliptic);
  95. } else if (selectUI == UIEvents.Text) {
  96. stateService.setEventName(LayerEvents.AddText);
  97. } else if (selectUI == UIEvents.Magnifier) {
  98. stateService.setEventName(LayerEvents.AddMagnifier);
  99. } else if (SVGType[selectUI]) {
  100. uiService.setSelectSVGType(selectUI);
  101. stateService.setEventName(LayerEvents.AddSVG);
  102. } else if (selectUI == UIEvents.Img) {
  103. stateService.setEventName(LayerEvents.Img);
  104. } else if (uiService.isBelongRoadTemplate(selectUI)) {
  105. stateService.setEventName(LayerEvents.AddRoadTemplate);
  106. } else if (uiService.isBelongRoadStructure(selectUI)) {
  107. stateService.setEventName(LayerEvents.AddRoadStructure);
  108. }
  109. }
  110. }
  111. }
  112. updateVectorForSelectUI(selectUI) {
  113. const focusItem = stateService.getFocusItem();
  114. if (uiService.isBelongRoadEdgeStyle(selectUI)) {
  115. let roadEdge = dataService.getRoadEdge(focusItem.vectorId);
  116. if (roadEdge) {
  117. roadEdge.setStyle(selectUI);
  118. } else {
  119. roadEdge = dataService.getCurveRoadEdge(focusItem.vectorId);
  120. roadEdge.setStyle(selectUI);
  121. }
  122. } else if (selectUI == VectorEvents.AddLane) {
  123. if (focusItem && focusItem.vectorId) {
  124. stateService.setEventName(VectorEvents.AddLane);
  125. }
  126. // if (focusItem && focusItem.dir && focusItem.vectorId) {
  127. // let road = dataService.getRoad(focusItem.vectorId);
  128. // if (road) {
  129. // if (focusItem.dir == "left") {
  130. // roadService.updateForAddSubtractLanesCount(
  131. // road.vectorId,
  132. // road.leftDrivewayCount + 1,
  133. // focusItem.dir
  134. // );
  135. // } else {
  136. // roadService.updateForAddSubtractLanesCount(
  137. // road.vectorId,
  138. // road.rightDrivewayCount + 1,
  139. // focusItem.dir
  140. // );
  141. // }
  142. // } else {
  143. // road = dataService.getCurveRoad(focusItem.vectorId);
  144. // if (focusItem.dir == "left") {
  145. // curveRoadService.updateForAddSubtractLanesCount(
  146. // road.vectorId,
  147. // road.leftDrivewayCount + 1,
  148. // focusItem.dir
  149. // );
  150. // } else {
  151. // curveRoadService.updateForAddSubtractLanesCount(
  152. // road.vectorId,
  153. // road.rightDrivewayCount + 1,
  154. // focusItem.dir
  155. // );
  156. // }
  157. // }
  158. // }
  159. } else if (selectUI == VectorEvents.DelLane) {
  160. if (focusItem && focusItem.vectorId) {
  161. stateService.setEventName(VectorEvents.DelLane);
  162. }
  163. // if (focusItem && focusItem.dir && focusItem.vectorId) {
  164. // let road = dataService.getRoad(focusItem.vectorId);
  165. // if (road) {
  166. // if (focusItem.dir == "left") {
  167. // roadService.updateForAddSubtractLanesCount(
  168. // road.vectorId,
  169. // road.leftDrivewayCount - 1,
  170. // focusItem.dir
  171. // );
  172. // } else {
  173. // roadService.updateForAddSubtractLanesCount(
  174. // road.vectorId,
  175. // road.rightDrivewayCount - 1,
  176. // focusItem.dir
  177. // );
  178. // }
  179. // } else {
  180. // road = dataService.getCurveRoad(focusItem.vectorId);
  181. // if (focusItem.dir == "left") {
  182. // curveRoadService.updateForAddSubtractLanesCount(
  183. // road.vectorId,
  184. // road.leftDrivewayCount - 1,
  185. // focusItem.dir
  186. // );
  187. // } else {
  188. // curveRoadService.updateForAddSubtractLanesCount(
  189. // road.vectorId,
  190. // road.rightDrivewayCount - 1,
  191. // focusItem.dir
  192. // );
  193. // }
  194. // }
  195. // }
  196. } else if (selectUI == VectorEvents.AddCrossPoint) {
  197. if (focusItem && focusItem.vectorId) {
  198. stateService.setEventName(VectorEvents.AddCrossPoint);
  199. }
  200. // if (focusItem && focusItem.dir && focusItem.vectorId) {
  201. // const curveRoad = dataService.getCurveRoad(focusItem.vectorId);
  202. // let index = mathUtil.getIndexForCurvesPoints(
  203. // this.mousePosition,
  204. // curveRoad.points
  205. // );
  206. // if (index != -1) {
  207. // curveRoadService.addCPoint(curveRoad, this.mousePosition, index);
  208. // } else {
  209. // const dis1 = mathUtil.getDistance(
  210. // curveRoad.points[0],
  211. // this.mousePosition
  212. // );
  213. // const dis2 = mathUtil.getDistance(
  214. // curveRoad.points[curveRoad.points.length - 1],
  215. // this.mousePosition
  216. // );
  217. // if (dis1 > dis2) {
  218. // curveRoadService.addCPoint(
  219. // curveRoad,
  220. // this.mousePosition,
  221. // curveRoad.points.length - 2
  222. // );
  223. // } else {
  224. // curveRoadService.addCPoint(curveRoad, this.mousePosition, 1);
  225. // }
  226. // }
  227. // }
  228. } else if (selectUI == VectorEvents.MinusCrossPoint) {
  229. if (focusItem && focusItem.vectorId) {
  230. stateService.setEventName(VectorEvents.MinusCrossPoint);
  231. }
  232. // if (focusItem && focusItem.dir && focusItem.vectorId) {
  233. // const curvePoint = dataService.getCurveRoadPoint(focusItem.vectorId);
  234. // const curveRoad = dataService.getCurveRoad(curvePoint.parent);
  235. // curveRoadService.subCPoint(curveRoad, curvePoint.getIndex());
  236. // }
  237. } else if (selectUI == VectorEvents.AddBranchRoad) {
  238. } else if (selectUI == VectorEvents.AddNarrowRoad) {
  239. } else if (selectUI == VectorEvents.UnLock) {
  240. }
  241. }
  242. async handleGeo(action) {
  243. let needAutoRedraw = false;
  244. const item = stateService.getFocusItem();
  245. if (item && item.vectorId) {
  246. switch (action) {
  247. case GeoActions.CopyAction:
  248. await this.copyVector(item.vectorId, item.type);
  249. needAutoRedraw = true;
  250. break;
  251. case GeoActions.DeleteAction:
  252. this.deleteVector(item.vectorId, item.type);
  253. needAutoRedraw = true;
  254. break;
  255. }
  256. }
  257. if (needAutoRedraw) {
  258. this.layer.history.save();
  259. this.layer.renderer.autoRedraw();
  260. }
  261. }
  262. //删除按钮
  263. deleteVector(vectorId, geoType) {
  264. switch (geoType) {
  265. case VectorType.Point:
  266. pointService.deletePoint(vectorId);
  267. break;
  268. case VectorType.Line:
  269. dataService.deleteLine(vectorId);
  270. if (vectorId == Settings.baseLineId) {
  271. this.layer.initLocation();
  272. Settings.baseLineId = null;
  273. }
  274. break;
  275. case VectorType.CurveLine:
  276. break;
  277. case VectorType.Circle:
  278. dataService.deleteCircle(vectorId);
  279. break;
  280. case VectorType.Elliptic:
  281. dataService.deleteElliptic(vectorId);
  282. break;
  283. case VectorType.Text:
  284. dataService.deleteText(vectorId);
  285. break;
  286. case VectorType.Magnifier:
  287. dataService.deleteMagnifier(vectorId);
  288. break;
  289. case VectorType.Road:
  290. roadService.deleteRoadForLinked(vectorId);
  291. break;
  292. case VectorType.CurveRoad:
  293. dataService.deleteCurveRoad(vectorId);
  294. break;
  295. }
  296. this.layer.exit();
  297. uiService.setSelectLineCategory(VectorCategory.Line.NormalLine);
  298. uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
  299. this.focusVector = null;
  300. }
  301. //复制按钮
  302. async copyVector(vectorId, geoType) {
  303. let item;
  304. switch (geoType) {
  305. case VectorType.Line:
  306. lineService.copy(vectorId);
  307. break;
  308. case VectorType.CurveLine:
  309. break;
  310. case VectorType.Circle:
  311. circleService.copy(vectorId);
  312. break;
  313. case VectorType.Elliptic:
  314. ellipticService.copy(vectorId);
  315. break;
  316. case VectorType.Text:
  317. textService.copy(vectorId);
  318. break;
  319. case VectorType.Road:
  320. const roadId = roadService.copyRoad(vectorId);
  321. item = stateService.getFocusItem();
  322. item.vectorId = roadId;
  323. break;
  324. case VectorType.CurveRoad:
  325. const curveRoadId = curveRoadService.copyCurveRoad(vectorId);
  326. item = stateService.getFocusItem();
  327. item.vectorId = curveRoadId;
  328. break;
  329. case VectorType.Magnifier:
  330. await magnifierService.copy(vectorId);
  331. break;
  332. }
  333. }
  334. //截图
  335. async screenShot() {
  336. let canvas = this.layer.canvas;
  337. this.menu_view_reset();
  338. //隐藏grid
  339. dataService.setGridDisplay(false);
  340. this.layer.renderer.autoRedraw();
  341. // this.downloadCadImg(canvas, "test.jpg");
  342. const blob = await this.getCadBlob(canvas);
  343. //显示grid
  344. dataService.setGridDisplay(true);
  345. this.layer.renderer.autoRedraw();
  346. return blob;
  347. }
  348. getCadBlob(canvas) {
  349. var type = "jpg";
  350. return new Promise((resolve) => canvas.toBlob(resolve, `${type}/image`));
  351. }
  352. downloadCadImg(canvas, filename) {
  353. // 图片导出为 jpg 格式
  354. var type = "jpg";
  355. var imgData = canvas.toDataURL(type, 3);
  356. canvas.toBlob(`${type}/image`);
  357. // 加工image data,替换mime type
  358. imgData = imgData.replace(this._fixType(type), "image/octet-stream");
  359. // 下载后的图片名
  360. //var filename = 'cad_' + new Date().getTime() + '.' + type
  361. // download
  362. this.saveFile(imgData, filename);
  363. }
  364. saveFile(data, filename) {
  365. var save_link = document.createElementNS(
  366. "http://www.w3.org/1999/xhtml",
  367. "a"
  368. );
  369. save_link.href = data;
  370. save_link.download = filename;
  371. var event = document.createEvent("MouseEvents");
  372. event.initMouseEvent(
  373. "click",
  374. true,
  375. false,
  376. window,
  377. 0,
  378. 0,
  379. 0,
  380. 0,
  381. 0,
  382. false,
  383. false,
  384. false,
  385. false,
  386. 0,
  387. null
  388. );
  389. save_link.dispatchEvent(event);
  390. }
  391. _fixType(type) {
  392. type = type.toLowerCase().replace(/jpg/i, "jpeg");
  393. var r = type.match(/png|jpeg|bmp|gif/)[0];
  394. return "image/" + r;
  395. }
  396. /****************************************************************************针对菜单*******************************************************************************/
  397. //撤销
  398. menu_revoke() {
  399. this.layer.history.goPreState();
  400. const historyState = historyService.getHistoryState();
  401. this.graphicStateUI.canRevoke = historyState.pre;
  402. this.graphicStateUI.canRecovery = true;
  403. this.layer.stopAddVector();
  404. this.layer.renderer.autoRedraw();
  405. }
  406. //恢复
  407. menu_recovery() {
  408. this.layer.history.goNextState();
  409. const historyState = historyService.getHistoryState();
  410. this.graphicStateUI.canRecovery = historyState.next;
  411. this.graphicStateUI.canRevoke = true;
  412. this.layer.stopAddVector();
  413. this.layer.renderer.autoRedraw();
  414. }
  415. menu_view_reset() {
  416. coordinate.reSet(this.layer.canvas);
  417. this.layer.renderer.autoRedraw();
  418. }
  419. // value 为true则开 false则关
  420. menu_backgroundImg(value) {
  421. console.log(value);
  422. //
  423. const backgroundImg = dataService.getBackgroundImg();
  424. backgroundImg.setDisplay(value);
  425. this.graphicStateUI.showBackImage = value;
  426. this.layer.renderer.autoRedraw();
  427. }
  428. menu_clear() {
  429. dataService.clear();
  430. Settings.selectLocationMode = null;
  431. Settings.baseLineId = null;
  432. Settings.selectBasePointId = null;
  433. elementService.hideAll();
  434. this.layer.exit();
  435. this.layer.initLocation();
  436. this.layer.history.save();
  437. this.layer.renderer.autoRedraw();
  438. }
  439. /******************************************************************************************************************************************************************/
  440. prompt(msg) {
  441. this._prompts.push(
  442. Message.success(typeof msg === "string" ? { msg } : msg)
  443. );
  444. }
  445. // 进入持续添加出确认与取消框
  446. showConfirm() {
  447. this.graphicStateUI.continuedMode = true;
  448. }
  449. confirmEntry() {
  450. console.log("确认");
  451. this.graphicStateUI.continuedMode = false;
  452. this.layer.exit();
  453. uiService.setSelectLineCategory(VectorCategory.Line.NormalLine);
  454. uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
  455. this.layer.history.save();
  456. this.layer.renderer.autoRedraw();
  457. }
  458. confirmCancel() {
  459. console.log("取消");
  460. this.graphicStateUI.continuedMode = false;
  461. addPoint.deleteTestPoints();
  462. this.layer.exit();
  463. uiService.setSelectLineCategory(VectorCategory.Line.NormalLine);
  464. uiService.setSelectPointCategory(VectorCategory.Point.NormalPoint);
  465. this.layer.history.save();
  466. this.layer.renderer.autoRedraw();
  467. }
  468. hidePrompt() {
  469. for (let prompt of this._prompts) {
  470. prompt();
  471. }
  472. }
  473. }