UIControl.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 { stateService } from "../Service/StateService.js";
  6. import { floorplanService } from "../Service/FloorplanService.js";
  7. import { historyService } from "../Service/HistoryService.js";
  8. import { elementService } from "../Service/ElementService";
  9. import { mathUtil } from "../MathUtil.js";
  10. import { wallService } from "../Service/WallService.js";
  11. import { tagService } from "../Service/TagService.js";
  12. import { tableService } from "../Service/TableService.js";
  13. import Constant from "../Constant";
  14. import { addWall } from "../Controls/AddWall";
  15. import { floorplanData } from "../FloorplanData.js";
  16. import { signService } from "../Service/SignService.js";
  17. import { customImageService } from "../Service/CustomImageService.js";
  18. import mitt from "mitt";
  19. import { history } from "../History/History.js";
  20. import { iconService } from "../Service/IconService.js";
  21. import { bgImageService } from "../Service/BgImageService.js";
  22. export default class UIControl {
  23. constructor(layer) {
  24. this.layer = layer;
  25. this.bus = mitt();
  26. this.selectUI = null;
  27. this.appendData = null;
  28. // this.bus.emit('')
  29. }
  30. //点击左侧栏后,更新事件
  31. updateEventNameForSelectUI() {
  32. elementService.hideAll();
  33. //正在添加tag的时候,需要先删除
  34. const eventName = stateService.getEventName();
  35. // if (eventName == LayerEvents.AddTag) {
  36. // let item = stateService.getDraggingItem()
  37. // if (item && item.type == VectorType.Tag) {
  38. // floorplanService.deleteTag(item.vectorId)
  39. // }
  40. // }
  41. // stateService.clearItems()
  42. if (this.selectUI == UIEvents.Wall) {
  43. stateService.setEventName(LayerEvents.AddWall);
  44. } else if (this.selectUI == UIEvents.Table) {
  45. stateService.setEventName(LayerEvents.AddTable);
  46. } else if (this.selectUI == UIEvents.Rectangle) {
  47. stateService.setEventName(LayerEvents.AddRectangle);
  48. } else if (this.selectUI == UIEvents.Circle) {
  49. stateService.setEventName(LayerEvents.AddCircle);
  50. } else if (this.selectUI == UIEvents.Arrow) {
  51. stateService.setEventName(LayerEvents.AddArrow);
  52. } else if (this.selectUI == UIEvents.Icon) {
  53. stateService.setEventName(LayerEvents.AddIcon);
  54. } else if (this.selectUI == UIEvents.Tag) {
  55. stateService.setEventName(LayerEvents.AddTag);
  56. } else if (
  57. this.selectUI == UIEvents.Cigaret ||
  58. this.selectUI == UIEvents.FirePoint ||
  59. this.selectUI == UIEvents.LeftFootPrint ||
  60. this.selectUI == UIEvents.RightFootPrint ||
  61. this.selectUI == UIEvents.LeftShoePrint ||
  62. this.selectUI == UIEvents.RightShoePrint ||
  63. this.selectUI == UIEvents.FingerPrint ||
  64. this.selectUI == UIEvents.DeadBody ||
  65. this.selectUI == UIEvents.BloodStain
  66. ) {
  67. stateService.setEventName(LayerEvents.AddSign);
  68. }
  69. }
  70. /**
  71. * @param {*} type 部件类型
  72. * @param {*} name 属性名称
  73. * @param {*} value 属性值
  74. */
  75. async setAttributes(type, name, value) {
  76. let item = stateService.getFocusItem();
  77. let flag = true;
  78. switch (name) {
  79. case "delete":
  80. this.deleteItem();
  81. break;
  82. case 'update':
  83. if(type == VectorType.Tag){
  84. const tag = floorplanService.getTag(item.vectorId)
  85. if(value.hasOwnProperty('version')){
  86. value.color&&tag.setColor(value.color)
  87. value.fontSize&&tag.setFontSize(value.fontSize)
  88. value.text&&tag.setValue(value.text)
  89. }
  90. else{
  91. tag.setValue(value)
  92. }
  93. }
  94. else if(type == VectorType.Arrow){
  95. const arrow = floorplanService.getArrow(item.vectorId)
  96. if(value.hasOwnProperty('version')){
  97. arrow.setColor(value.color)
  98. }
  99. }
  100. else if(type == VectorType.Wall){
  101. const wall = floorplanService.getWall(item.vectorId)
  102. if(value.hasOwnProperty('version')){
  103. wall.setColor(value.color)
  104. }
  105. }
  106. else if(type == VectorType.Rectangle){
  107. const rectangle = floorplanService.getRectangle(item.vectorId)
  108. if(value.hasOwnProperty('version')){
  109. rectangle.setColor(value.color)
  110. }
  111. }
  112. else if(type == VectorType.Circle){
  113. const circle = floorplanService.getCircle(item.vectorId)
  114. if(value.hasOwnProperty('version')){
  115. circle.setColor(value.color)
  116. }
  117. }
  118. else if(type == VectorType.Table){
  119. const table = floorplanService.getTable(item.vectorId)
  120. if(value.hasOwnProperty('version')){
  121. table.setValue(value.content)
  122. }
  123. else{
  124. table.setValue(value)
  125. }
  126. }
  127. else if(type == VectorType.Title){
  128. if(value.hasOwnProperty('version')){
  129. floorplanService.updateTitle(value.text);
  130. }
  131. else{
  132. floorplanService.updateTitle(value);
  133. }
  134. }
  135. else if(type == VectorType.Compass){
  136. if(value.hasOwnProperty('version')){
  137. floorplanService.updateCompass(value.rotate)
  138. flag = value.save
  139. }
  140. else{
  141. floorplanService.updateCompass(value)
  142. }
  143. }
  144. else if(type == VectorType.CustomImage){
  145. const customImage = floorplanService.getCustomImage(item.vectorId)
  146. if(value.hasOwnProperty('rotate')){
  147. customImage.setAngle(value.rotate)
  148. flag = value.save
  149. }
  150. else if(value.hasOwnProperty('scale')){
  151. customImage.setScale(value.scale)
  152. flag = value.save
  153. }
  154. else if(value.hasOwnProperty('ratio')){
  155. customImage.setRatio(floor.customImages[key].ratio)
  156. }
  157. }
  158. else if(type == VectorType.BgImage){
  159. const bgImage = floorplanService.getBgImage()
  160. if(value.hasOwnProperty('scale')){
  161. bgImage.setScale(value.scale)
  162. flag = value.save
  163. }
  164. }
  165. else if(signService.isSign(type)){
  166. const sign = floorplanService.getSign(item.vectorId)
  167. if(value.hasOwnProperty('rotate')){
  168. sign.setAngle(value.rotate)
  169. flag = value.save
  170. }
  171. else if(value.hasOwnProperty('scale')){
  172. sign.setScale(value.scale)
  173. flag = value.save
  174. }
  175. }
  176. break;
  177. case "upload":
  178. if(type == VectorType.CustomImage){
  179. const customImage = await customImageService.createCustomImage(value.url,{
  180. x:0,
  181. y:0
  182. })
  183. //stateService.setEventName(LayerEvents.MoveCustomImage);
  184. let focusItem = {
  185. vectorId: customImage.vectorId,
  186. type: VectorType.CustomImage,
  187. };
  188. stateService.setFocusItem(focusItem);
  189. this.showAttributes(focusItem);
  190. }
  191. else if(type == VectorType.BgImage){
  192. const bgImage = await bgImageService.createBgImage(value.url)
  193. //stateService.setEventName(LayerEvents.MoveBgImage);
  194. let focusItem = {
  195. vectorId: bgImage.vectorId,
  196. type: VectorType.BgImage,
  197. };
  198. stateService.setFocusItem(focusItem);
  199. this.showAttributes(focusItem);
  200. }
  201. break;
  202. }
  203. if(flag){
  204. history.save();
  205. }
  206. //stateService.clearFocusItem();
  207. this.layer.renderer.autoRedraw();
  208. }
  209. showAttributes(item) {
  210. let type = item.type;
  211. let value = null;
  212. if(signService.isSign(type)){
  213. const sign = floorplanService.getSign(item.vectorId);
  214. if (!sign) {
  215. return;
  216. }
  217. value = {
  218. version:'2.0',
  219. type: type,
  220. rotate:sign.angle,
  221. scale:sign.scale
  222. };
  223. }
  224. else{
  225. switch (item.type) {
  226. case VectorType.Tag:
  227. const tag = floorplanService.getTag(item.vectorId);
  228. if (!tag) {
  229. return;
  230. }
  231. value = {
  232. version:'2.0',
  233. type: type,
  234. text:tag.value,
  235. color: tag.color,
  236. fontSize: tag.fontSize,
  237. };
  238. break;
  239. case VectorType.Table:
  240. const table = floorplanService.getTable(item.vectorId);
  241. if (!table) {
  242. return;
  243. }
  244. const cellIds = table.cells;
  245. let content = [];
  246. for (let i = 0; i < cellIds.length; ++i) {
  247. for (let j = 0; j < cellIds[i].length; ++j) {
  248. const cell = floorplanService.getCell(cellIds[i][j]);
  249. content.push({
  250. width: cell.width,
  251. height: cell.height,
  252. value: cell.value,
  253. colIndex: cell.colIndex,
  254. rowIndex: cell.rowIndex,
  255. });
  256. }
  257. }
  258. value = {
  259. version:'2.0',
  260. type: type,
  261. content: content,
  262. };
  263. break;
  264. case VectorType.Title:
  265. const title = floorplanService.getTitle();
  266. if (!title) {
  267. return;
  268. }
  269. value = {
  270. version:'2.0',
  271. type: type,
  272. text: title.value,
  273. };
  274. break;
  275. case VectorType.Compass:
  276. const compass = floorplanService.getCompass();
  277. if (!compass) {
  278. return;
  279. }
  280. value = compass.angle;
  281. break;
  282. case VectorType.CustomImage:
  283. const customImage = floorplanService.getCustomImage(item.vectorId);
  284. if (!customImage) {
  285. return;
  286. }
  287. value = {
  288. version:'2.0',
  289. type: type,
  290. url: customImage.url,
  291. rotate:customImage.angle,
  292. ratio:customImage.ratio,
  293. scale:customImage.scale
  294. };
  295. break;
  296. case VectorType.BgImage:
  297. const bgImage = floorplanService.getBgImage(item.vectorId);
  298. if (!bgImage) {
  299. return;
  300. }
  301. value = {
  302. version:'2.0',
  303. type: type,
  304. url: bgImage.url,
  305. scale:bgImage.scale
  306. };
  307. break;
  308. case VectorType.Circle:
  309. const circle = floorplanService.getCircle(item.vectorId);
  310. if (!circle) {
  311. return;
  312. }
  313. value = {
  314. version:'2.0',
  315. type: type,
  316. color: circle.color,
  317. };
  318. break;
  319. case VectorType.Rectangle:
  320. const rectangle = floorplanService.getRectangle(item.vectorId);
  321. if (!rectangle) {
  322. return;
  323. }
  324. value = {
  325. version:'2.0',
  326. type: type,
  327. color: rectangle.color,
  328. };
  329. break;
  330. case VectorType.Wall:
  331. const wall = floorplanService.getWall(item.vectorId);
  332. if (!wall) {
  333. return;
  334. }
  335. value = {
  336. version:'2.0',
  337. type: type,
  338. color: wall.color,
  339. };
  340. break;
  341. case VectorType.Arrow:
  342. const arrow = floorplanService.getArrow(item.vectorId);
  343. if (!arrow) {
  344. return;
  345. }
  346. value = {
  347. version:'2.0',
  348. type: type,
  349. color: arrow.color,
  350. };
  351. break;
  352. }
  353. }
  354. this.bus.emit("showAttribute", {
  355. type: type,
  356. value: value,
  357. });
  358. }
  359. clearUI() {
  360. this.selectUI = null;
  361. this.bus.emit("hideAttribute");
  362. this.bus.emit("hideUI");
  363. }
  364. deleteItem() {
  365. let item = stateService.getFocusItem();
  366. if (item) {
  367. if (item.type == VectorType.Wall) {
  368. floorplanService.deleteWall(item.vectorId);
  369. } else if (item.type == VectorType.Rectangle) {
  370. floorplanService.deleteRectangle(item.vectorId);
  371. } else if (item.type == VectorType.Circle) {
  372. floorplanService.deleteCircle(item.vectorId);
  373. } else if (item.type == VectorType.Arrow) {
  374. floorplanService.deleteArrow(item.vectorId);
  375. } else if (item.type == VectorType.Icon) {
  376. iconService.deleteIcon(item.vectorId);
  377. } else if (item.type == VectorType.Tag) {
  378. floorplanService.deleteTag(item.vectorId);
  379. } else if (item.type == VectorType.Table) {
  380. floorplanService.deleteTable(item.vectorId);
  381. } else if (signService.isSign(item.type)) {
  382. floorplanService.deleteSign(item.vectorId);
  383. } else if (item.type == VectorType.WallCorner) {
  384. wallService.deleteWallCorner(item.vectorId);
  385. } else if (item.type == VectorType.CustomImage) {
  386. floorplanService.deleteCustomImage(item.vectorId);
  387. } else if (item.type == VectorType.BgImage) {
  388. floorplanService.deleteBgImage();
  389. }
  390. history.save();
  391. this.layer.renderer.autoRedraw();
  392. }
  393. }
  394. getSignTypeForUI() {
  395. if (this.selectUI == UIEvents.Cigaret) {
  396. return VectorType.Cigaret;
  397. } else if (this.selectUI == UIEvents.FirePoint) {
  398. return VectorType.FirePoint;
  399. } else if (this.selectUI == UIEvents.LeftFootPrint) {
  400. return VectorType.LeftFootPrint;
  401. } else if (this.selectUI == UIEvents.RightFootPrint) {
  402. return VectorType.RightFootPrint;
  403. } else if (this.selectUI == UIEvents.LeftShoePrint) {
  404. return VectorType.LeftShoePrint;
  405. } else if (this.selectUI == UIEvents.RightShoePrint) {
  406. return VectorType.RightShoePrint;
  407. } else if (this.selectUI == UIEvents.FingerPrint) {
  408. return VectorType.FingerPrint;
  409. } else if (this.selectUI == UIEvents.DeadBody) {
  410. return VectorType.DeadBody;
  411. } else if (this.selectUI == UIEvents.BloodStain) {
  412. return VectorType.BloodStain;
  413. }
  414. }
  415. exportJSON() {
  416. const json = {
  417. version: floorplanData.version,
  418. floors: floorplanData.floors,
  419. currentId: floorplanService.getCurrentId(),
  420. };
  421. return json;
  422. }
  423. exportImg(canvas,filename, callback){
  424. coordinate.setRatio(3)
  425. canvas.width = canvas.width * coordinate.ratio
  426. canvas.height = canvas.height * coordinate.ratio
  427. this.layer.renderer.autoRedraw()
  428. setTimeout(() => {
  429. let blobImg = this.downloadCadImg(canvas, filename)
  430. // 完成callback传出blob
  431. callback(blobImg)
  432. canvas.width = canvas.width / coordinate.ratio
  433. canvas.height = canvas.height / coordinate.ratio
  434. coordinate.setRatio(1)
  435. this.layer.renderer.autoRedraw()
  436. },100)
  437. }
  438. downloadCadImg(canvas, filename) {
  439. // 图片导出为 png 格式
  440. var type = "png";
  441. var imgData = canvas.toDataURL(type, 1);
  442. //let blobImg = this.base64ToBlob(imgData);
  443. // 加工image data,替换mime type
  444. let blobImg = imgData.replace(this._fixType(type), 'image/octet-stream')
  445. return blobImg;
  446. // download
  447. //this.saveFile(imgData, filename)
  448. }
  449. saveFile(data, filename) {
  450. var save_link = document.createElementNS(
  451. "http://www.w3.org/1999/xhtml",
  452. "a"
  453. );
  454. save_link.href = data;
  455. save_link.download = filename;
  456. var event = document.createEvent("MouseEvents");
  457. event.initMouseEvent(
  458. "click",
  459. true,
  460. false,
  461. window,
  462. 0,
  463. 0,
  464. 0,
  465. 0,
  466. 0,
  467. false,
  468. false,
  469. false,
  470. false,
  471. 0,
  472. null
  473. );
  474. save_link.dispatchEvent(event);
  475. }
  476. _fixType(type) {
  477. type = type.toLowerCase().replace(/jpg/i, "jpeg");
  478. var r = type.match(/png|jpeg|bmp|gif/)[0];
  479. return "image/" + r;
  480. }
  481. base64ToBlob(base64) {
  482. let arr = base64.split(","),
  483. mime = arr[0].match(/:(.*?);/)[1],
  484. bstr = atob(arr[1]),
  485. n = bstr.length,
  486. u8arr = new Uint8Array(n);
  487. while (n--) {
  488. u8arr[n] = bstr.charCodeAt(n);
  489. }
  490. return new Blob([u8arr], { type: mime });
  491. }
  492. //截图
  493. menu_screenShot(fileName) {
  494. // this.menu_flex();
  495. // this.layer.stopAddVector()
  496. // setTimeout(function(){
  497. // this.downloadCadImg(this.layer.canvas,fileName)
  498. // }.bind(this),100)
  499. this.layer.stopAddVector();
  500. return this.downloadCadImg(this.layer.canvas, fileName);
  501. }
  502. menu_flex() {
  503. coordinate.reSet();
  504. this.layer.renderer.autoRedraw();
  505. }
  506. initTopTable(value) {
  507. let center = {
  508. x: 770,
  509. y: 200,
  510. };
  511. center = coordinate.getXYFromScreen(center);
  512. let table = tableService.createTable(center);
  513. table.setValue(value);
  514. this.layer.renderer.autoRedraw();
  515. }
  516. initDownTable(value) {
  517. let center = {
  518. x: 770,
  519. y: 520,
  520. };
  521. center = coordinate.getXYFromScreen(center);
  522. let table = tableService.createTable(center);
  523. table.setValue(value);
  524. this.layer.renderer.autoRedraw();
  525. }
  526. /******************************************************************************************************************************************************************/
  527. }