UIControl.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import { coordinate } from "../Coordinate.js";
  2. import LayerEvents from "../enum/LayerEvents.js";
  3. import UIEvents from "../enum/UIEvents.js";
  4. import VectorType from "../enum/VectorType.js";
  5. import GeoActions from "../enum/GeoActions.js";
  6. import { stateService } from "../Service/StateService.js";
  7. import { uiService } from "../Service/UIService.js";
  8. import { dataService } from "../Service/DataService.js";
  9. import { historyService } from "../Service/HistoryService.js";
  10. import { elementService } from "../Service/ElementService";
  11. import { lineService } from "../Service/LineService.js";
  12. import { circleService } from "../Service/CircleService.js";
  13. import { textService } from "../Service/TextService.js";
  14. import { magnifierService } from "../Service/MagnifierService.js";
  15. import { mathUtil } from "../Util/MathUtil";
  16. import Constant from "../Constant";
  17. // import { roomsUtil } from "../Room/RoomsUtil.js";
  18. import { addRoad } from "../Controls/AddRoad";
  19. import { addLine } from "./AddLine.js";
  20. import VectorCategory from "../enum/VectorCategory.js";
  21. // import { floorplanData } from "../VectorData.js";
  22. import Message from "@/components/base/components/message/message.vue";
  23. import { pointService } from "../Service/PointService.js";
  24. import Settings from "../Settings.js";
  25. export default class UIControl {
  26. constructor(layer, newsletter, graphicStateUI) {
  27. this._prompts = [];
  28. this.layer = layer;
  29. this.newsletter = newsletter;
  30. this.graphicStateUI = graphicStateUI;
  31. }
  32. get selectUI() {
  33. return this.newsletter.selectUI;
  34. }
  35. set selectUI(selectUI) {
  36. this.updateEventNameForSelectUI(selectUI);
  37. this.newsletter.selectUI = selectUI;
  38. }
  39. get focusVector() {
  40. return this.newsletter.focusVector;
  41. }
  42. set focusVector(focusVector) {
  43. this.newsletter.focusVector = focusVector;
  44. }
  45. /**
  46. * 获取选中要操作的UI
  47. */
  48. get currentUI() {}
  49. /**
  50. * 设置选中要操作的UI
  51. */
  52. set currentUI(value) {
  53. this.selectUI = value;
  54. }
  55. clearUI() {
  56. this.currentUI = null;
  57. this.selectUI = null;
  58. }
  59. //点击左侧栏后,更新事件
  60. updateEventNameForSelectUI(selectUI) {
  61. console.log(this.selectUI, selectUI);
  62. if (selectUI != null) {
  63. if (this.selectUI == selectUI) {
  64. return;
  65. } else if (this.selectUI != selectUI) {
  66. if (this.selectUI != null) {
  67. //先取消当前事件和进程
  68. this.layer.exit();
  69. }
  70. //执行新的事件
  71. if (uiService.isBelongRoad(selectUI) || selectUI == "road") {
  72. stateService.setEventName(LayerEvents.AddRoad);
  73. } else if (selectUI == UIEvents.CurveRoad) {
  74. stateService.setEventName(LayerEvents.AddCurveRoad);
  75. } else if (uiService.isBelongLine(selectUI)) {
  76. stateService.setEventName(LayerEvents.AddLine);
  77. } else if (uiService.isBelongPoint(selectUI)) {
  78. stateService.setEventName(LayerEvents.AddPoint);
  79. } else if (selectUI == UIEvents.Circle) {
  80. stateService.setEventName(LayerEvents.AddCircle);
  81. } else if (selectUI == UIEvents.Text) {
  82. stateService.setEventName(LayerEvents.AddText);
  83. } else if (selectUI == UIEvents.Magnifier) {
  84. stateService.setEventName(LayerEvents.AddMagnifier);
  85. } else if (selectUI == UIEvents.SVG) {
  86. stateService.setEventName(LayerEvents.AddSVG);
  87. } else if (selectUI == UIEvents.Img) {
  88. stateService.setEventName(LayerEvents.Img);
  89. }
  90. }
  91. }
  92. }
  93. async handleGeo(action) {
  94. let needAutoRedraw = false;
  95. const item = stateService.getFocusItem();
  96. if (item && item.vectorId) {
  97. switch (action) {
  98. case GeoActions.CopyAction:
  99. await this.copyVector(item.vectorId, item.type);
  100. needAutoRedraw = true;
  101. break;
  102. case GeoActions.DeleteAction:
  103. this.deleteVector(item.vectorId, item.type);
  104. needAutoRedraw = true;
  105. break;
  106. }
  107. }
  108. if (needAutoRedraw) {
  109. this.layer.history.save();
  110. this.layer.renderer.autoRedraw();
  111. }
  112. }
  113. //删除按钮
  114. deleteVector(vectorId, geoType) {
  115. switch (geoType) {
  116. case VectorType.Point:
  117. pointService.deletePoint(vectorId);
  118. break;
  119. case VectorType.Line:
  120. dataService.deleteLine(vectorId);
  121. if (vectorId == Settings.baseLineId) {
  122. this.layer.initLocation();
  123. Settings.baseLineId = null;
  124. }
  125. break;
  126. case VectorType.Circle:
  127. dataService.deleteCircle(vectorId);
  128. break;
  129. case VectorType.Text:
  130. dataService.deleteText(vectorId);
  131. break;
  132. case VectorType.Magnifier:
  133. dataService.deleteMagnifier(vectorId);
  134. break;
  135. }
  136. }
  137. //复制按钮
  138. async copyVector(vectorId, geoType) {
  139. switch (geoType) {
  140. case VectorType.Line:
  141. lineService.copy(vectorId);
  142. break;
  143. case VectorType.Circle:
  144. circleService.copy(vectorId);
  145. break;
  146. case VectorType.Text:
  147. textService.copy(vectorId);
  148. break;
  149. case VectorType.Magnifier:
  150. await magnifierService.copy(vectorId);
  151. break;
  152. }
  153. }
  154. //截图
  155. async screenShot() {
  156. let canvas = this.layer.canvas;
  157. this.menu_view_reset();
  158. //隐藏grid
  159. dataService.setGridDisplay(false);
  160. this.layer.renderer.autoRedraw();
  161. // this.downloadCadImg(canvas, "test.jpg");
  162. const blob = await this.getCadBlob(canvas);
  163. //显示grid
  164. dataService.setGridDisplay(true);
  165. this.layer.renderer.autoRedraw();
  166. return blob;
  167. }
  168. getCadBlob(canvas) {
  169. var type = "jpg";
  170. return new Promise((resolve) => canvas.toBlob(resolve, `${type}/image`));
  171. }
  172. downloadCadImg(canvas, filename) {
  173. // 图片导出为 jpg 格式
  174. var type = "jpg";
  175. var imgData = canvas.toDataURL(type, 3);
  176. canvas.toBlob(`${type}/image`);
  177. // 加工image data,替换mime type
  178. imgData = imgData.replace(this._fixType(type), "image/octet-stream");
  179. // 下载后的图片名
  180. //var filename = 'cad_' + new Date().getTime() + '.' + type
  181. // download
  182. debugger;
  183. this.saveFile(imgData, filename);
  184. }
  185. saveFile(data, filename) {
  186. var save_link = document.createElementNS(
  187. "http://www.w3.org/1999/xhtml",
  188. "a"
  189. );
  190. save_link.href = data;
  191. save_link.download = filename;
  192. var event = document.createEvent("MouseEvents");
  193. event.initMouseEvent(
  194. "click",
  195. true,
  196. false,
  197. window,
  198. 0,
  199. 0,
  200. 0,
  201. 0,
  202. 0,
  203. false,
  204. false,
  205. false,
  206. false,
  207. 0,
  208. null
  209. );
  210. save_link.dispatchEvent(event);
  211. }
  212. _fixType(type) {
  213. type = type.toLowerCase().replace(/jpg/i, "jpeg");
  214. var r = type.match(/png|jpeg|bmp|gif/)[0];
  215. return "image/" + r;
  216. }
  217. /****************************************************************************针对菜单*******************************************************************************/
  218. //撤销
  219. menu_revoke() {
  220. this.layer.history.goPreState();
  221. const historyState = historyService.getHistoryState();
  222. this.graphicStateUI.canRevoke = historyState.pre;
  223. this.graphicStateUI.canRecovery = true;
  224. this.layer.stopAddVector();
  225. this.layer.renderer.autoRedraw();
  226. }
  227. //恢复
  228. menu_recovery() {
  229. this.layer.history.goNextState();
  230. const historyState = historyService.getHistoryState();
  231. this.graphicStateUI.canRecovery = historyState.next;
  232. this.graphicStateUI.canRevoke = true;
  233. this.layer.stopAddVector();
  234. this.layer.renderer.autoRedraw();
  235. }
  236. menu_view_reset() {
  237. coordinate.reSet(this.layer.canvas);
  238. this.layer.renderer.autoRedraw();
  239. }
  240. // value 为true则开 false则关
  241. menu_backgroundImg(value) {
  242. console.log(value);
  243. //
  244. const backgroundImg = dataService.getBackgroundImg();
  245. backgroundImg.setDisplay(value);
  246. this.graphicStateUI.showBackImage = value;
  247. this.layer.renderer.autoRedraw();
  248. }
  249. menu_clear() {
  250. dataService.clear();
  251. Settings.locationMode = null;
  252. Settings.baseLineId = null;
  253. Settings.selectBasePointId = null;
  254. elementService.hideAll();
  255. this.layer.exit();
  256. this.layer.initLocation();
  257. this.layer.history.save();
  258. this.layer.renderer.autoRedraw();
  259. }
  260. /******************************************************************************************************************************************************************/
  261. prompt(msg) {
  262. this._prompts.push(Message.success(typeof msg === 'string' ? { msg } : msg));
  263. }
  264. // 进入持续添加出确认与取消框
  265. showConfirm() {
  266. this.graphicStateUI.continuedMode = true;
  267. }
  268. confirmEntry() {
  269. console.log("确认");
  270. this.graphicStateUI.continuedMode = false;
  271. }
  272. confirmCancel() {
  273. console.log("取消");
  274. this.graphicStateUI.continuedMode = false;
  275. }
  276. hidePrompt() {
  277. for (let prompt of this._prompts) {
  278. prompt();
  279. }
  280. }
  281. }