UIControl.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. value = {
  282. version:'2.0',
  283. type: type,
  284. rotate:compass.angle
  285. };
  286. break;
  287. case VectorType.CustomImage:
  288. const customImage = floorplanService.getCustomImage(item.vectorId);
  289. if (!customImage) {
  290. return;
  291. }
  292. value = {
  293. version:'2.0',
  294. type: type,
  295. url: customImage.url,
  296. rotate:customImage.angle,
  297. ratio:customImage.ratio,
  298. scale:customImage.scale
  299. };
  300. break;
  301. case VectorType.BgImage:
  302. const bgImage = floorplanService.getBgImage(item.vectorId);
  303. if (!bgImage) {
  304. return;
  305. }
  306. value = {
  307. version:'2.0',
  308. type: type,
  309. url: bgImage.url,
  310. scale:bgImage.scale
  311. };
  312. break;
  313. case VectorType.Circle:
  314. const circle = floorplanService.getCircle(item.vectorId);
  315. if (!circle) {
  316. return;
  317. }
  318. value = {
  319. version:'2.0',
  320. type: type,
  321. color: circle.color,
  322. };
  323. break;
  324. case VectorType.Rectangle:
  325. const rectangle = floorplanService.getRectangle(item.vectorId);
  326. if (!rectangle) {
  327. return;
  328. }
  329. value = {
  330. version:'2.0',
  331. type: type,
  332. color: rectangle.color,
  333. };
  334. break;
  335. case VectorType.Wall:
  336. const wall = floorplanService.getWall(item.vectorId);
  337. if (!wall) {
  338. return;
  339. }
  340. value = {
  341. version:'2.0',
  342. type: type,
  343. color: wall.color,
  344. };
  345. break;
  346. case VectorType.Arrow:
  347. const arrow = floorplanService.getArrow(item.vectorId);
  348. if (!arrow) {
  349. return;
  350. }
  351. value = {
  352. version:'2.0',
  353. type: type,
  354. color: arrow.color,
  355. };
  356. break;
  357. }
  358. }
  359. this.bus.emit("showAttribute", {
  360. type: type,
  361. value: value,
  362. });
  363. }
  364. clearUI() {
  365. this.selectUI = null;
  366. this.bus.emit("hideAttribute");
  367. this.bus.emit("hideUI");
  368. }
  369. deleteItem() {
  370. let item = stateService.getFocusItem();
  371. if (item) {
  372. if (item.type == VectorType.Wall) {
  373. floorplanService.deleteWall(item.vectorId);
  374. } else if (item.type == VectorType.Rectangle) {
  375. floorplanService.deleteRectangle(item.vectorId);
  376. } else if (item.type == VectorType.Circle) {
  377. floorplanService.deleteCircle(item.vectorId);
  378. } else if (item.type == VectorType.Arrow) {
  379. floorplanService.deleteArrow(item.vectorId);
  380. } else if (item.type == VectorType.Icon) {
  381. iconService.deleteIcon(item.vectorId);
  382. } else if (item.type == VectorType.Tag) {
  383. floorplanService.deleteTag(item.vectorId);
  384. } else if (item.type == VectorType.Table) {
  385. floorplanService.deleteTable(item.vectorId);
  386. } else if (signService.isSign(item.type)) {
  387. floorplanService.deleteSign(item.vectorId);
  388. } else if (item.type == VectorType.WallCorner) {
  389. wallService.deleteWallCorner(item.vectorId);
  390. } else if (item.type == VectorType.CustomImage) {
  391. floorplanService.deleteCustomImage(item.vectorId);
  392. } else if (item.type == VectorType.BgImage) {
  393. floorplanService.deleteBgImage();
  394. }
  395. history.save();
  396. this.layer.renderer.autoRedraw();
  397. }
  398. }
  399. getSignTypeForUI() {
  400. if (this.selectUI == UIEvents.Cigaret) {
  401. return VectorType.Cigaret;
  402. } else if (this.selectUI == UIEvents.FirePoint) {
  403. return VectorType.FirePoint;
  404. } else if (this.selectUI == UIEvents.LeftFootPrint) {
  405. return VectorType.LeftFootPrint;
  406. } else if (this.selectUI == UIEvents.RightFootPrint) {
  407. return VectorType.RightFootPrint;
  408. } else if (this.selectUI == UIEvents.LeftShoePrint) {
  409. return VectorType.LeftShoePrint;
  410. } else if (this.selectUI == UIEvents.RightShoePrint) {
  411. return VectorType.RightShoePrint;
  412. } else if (this.selectUI == UIEvents.FingerPrint) {
  413. return VectorType.FingerPrint;
  414. } else if (this.selectUI == UIEvents.DeadBody) {
  415. return VectorType.DeadBody;
  416. } else if (this.selectUI == UIEvents.BloodStain) {
  417. return VectorType.BloodStain;
  418. }
  419. }
  420. exportJSON() {
  421. const json = {
  422. version: floorplanData.version,
  423. floors: floorplanData.floors,
  424. currentId: floorplanService.getCurrentId(),
  425. };
  426. return json;
  427. }
  428. exportImg(canvas,filename, callback){
  429. coordinate.setRatio(3)
  430. canvas.width = canvas.width * coordinate.ratio
  431. canvas.height = canvas.height * coordinate.ratio
  432. stateService.clearItems();
  433. this.layer.renderer.autoRedrawForImg()
  434. setTimeout(() => {
  435. // let blobImg = this.downloadCadImg(canvas, filename)
  436. // // 完成callback传出blob
  437. // callback(blobImg)
  438. canvas.toBlob(callback, 'image/jpeg', 1)
  439. canvas.width = canvas.width / coordinate.ratio
  440. canvas.height = canvas.height / coordinate.ratio
  441. coordinate.setRatio(1)
  442. this.layer.renderer.autoRedraw()
  443. },100)
  444. }
  445. downloadCadImg(canvas, filename) {
  446. // 图片导出为 png 格式
  447. var type = "image/png";
  448. var imgData = canvas.toDataURL(type, 1);
  449. //let blobImg = this.base64ToBlob(imgData);
  450. // 加工image data,替换mime type
  451. // let blobImg = imgData.replace(this._fixType(type), 'image/octet-stream')
  452. // console.log(imgData)
  453. return imgData;
  454. // download
  455. //this.saveFile(imgData, filename)
  456. }
  457. saveFile(data, filename) {
  458. var save_link = document.createElementNS(
  459. "http://www.w3.org/1999/xhtml",
  460. "a"
  461. );
  462. save_link.href = data;
  463. save_link.download = filename;
  464. var event = document.createEvent("MouseEvents");
  465. event.initMouseEvent(
  466. "click",
  467. true,
  468. false,
  469. window,
  470. 0,
  471. 0,
  472. 0,
  473. 0,
  474. 0,
  475. false,
  476. false,
  477. false,
  478. false,
  479. 0,
  480. null
  481. );
  482. save_link.dispatchEvent(event);
  483. }
  484. _fixType(type) {
  485. type = type.toLowerCase().replace(/jpg/i, "jpeg");
  486. var r = type.match(/png|jpeg|bmp|gif/)[0];
  487. return "image/" + r;
  488. }
  489. base64ToBlob(base64) {
  490. let arr = base64.split(","),
  491. mime = arr[0].match(/:(.*?);/)[1],
  492. bstr = atob(arr[1]),
  493. n = bstr.length,
  494. u8arr = new Uint8Array(n);
  495. while (n--) {
  496. u8arr[n] = bstr.charCodeAt(n);
  497. }
  498. return new Blob([u8arr], { type: mime });
  499. }
  500. //截图
  501. menu_screenShot(fileName) {
  502. // this.menu_flex();
  503. // this.layer.stopAddVector()
  504. // setTimeout(function(){
  505. // this.downloadCadImg(this.layer.canvas,fileName)
  506. // }.bind(this),100)
  507. this.layer.stopAddVector();
  508. return this.downloadCadImg(this.layer.canvas, fileName);
  509. }
  510. menu_flex() {
  511. coordinate.reSet();
  512. this.layer.renderer.autoRedraw();
  513. }
  514. initTopTable(value) {
  515. let center = {
  516. x: 770,
  517. y: 250,
  518. };
  519. center = coordinate.getXYFromScreen(center);
  520. let table = tableService.createTable(center);
  521. table.setValue(value);
  522. this.layer.renderer.autoRedraw();
  523. }
  524. initDownTable(value) {
  525. let center = {
  526. x: 770,
  527. y: 520,
  528. };
  529. center = coordinate.getXYFromScreen(center);
  530. let table = tableService.createTable(center);
  531. table.setValue(value);
  532. this.layer.renderer.autoRedraw();
  533. }
  534. /******************************************************************************************************************************************************************/
  535. }