EditScence.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. import { SMouseEvent, SUndoStack } from "@saga-web/base";
  2. import { SGraphScene, SGraphLayoutType } from '@saga-web/graph/lib';
  3. import { SFloorParser, SLineItem, SPolylineItem, SItemStatus, ItemOrder } from "@saga-web/big";
  4. import { SGraphItem, SLineStyle, SImageItem, STextItem, SGraphPointListInsert, SGraphPointListDelete, SGraphPointListUpdate, SGraphAddCommand } from "@saga-web/graph/lib";
  5. import { SZoneLegendItem } from "@/lib/items/SZoneLegendItem";
  6. import { SImageLegendItem } from "@/lib/items/SImageLegendItem";
  7. import { TipelineItem } from "@/lib/items/TipelineItem";
  8. import { SImgTextItem } from "@/lib/items/SImgTextItem";
  9. import { SImageMarkerItem } from "@/lib/items/SImageMarkerItem"
  10. import { SPoint, SFont } from '@saga-web/draw/lib';
  11. import { Legend } from '@/lib/types/Legend';
  12. import { Relation } from '@/lib/types/Legend';
  13. import { uuid } from "@/components/mapClass/until";
  14. import { STextMarkerItem } from '@/lib/items/STextMarkerItem';
  15. import { SLineMarkerItem } from '@/lib/items/SLineMarkerItem';
  16. /**
  17. * 在线绘图
  18. *
  19. * @author 韩耀龙
  20. */
  21. export class EditScence extends SGraphScene {
  22. undoStack = new SUndoStack();
  23. /** 命令 1 绘制直线 */
  24. private cmd = 'choice';
  25. /** 获取当前状态 */
  26. get getCmd(): string {
  27. return this.cmd;
  28. }
  29. /** 编辑当前状态 */
  30. set setCmd(cmd: string) {
  31. if (cmd == 'choice') {
  32. this.grabItem = null;
  33. }
  34. this.cmd = cmd;
  35. if (this.focusItem) {
  36. // 取消操作
  37. this.focusItem.cancelOperate();
  38. this.focusItem = null;
  39. // this.selectContainer.
  40. }
  41. if (this.view) {
  42. this.view.update();
  43. }
  44. };
  45. /** 当前选中焦点Item */
  46. focusItem: SGraphItem | null = null;
  47. /**图例节点 */
  48. Nodes: any = []; // 图例节点,所有与工程信息化相关的图例(图标类型与区域)
  49. /**图例节点 */ // 与工程信息无关的标识对象(增加文本注释,图上的图片说明)
  50. Markers: any = [];
  51. /** 管线对象 */
  52. Relations: any = [];
  53. constructor() {
  54. super();
  55. // // 选择绑定选额item事件
  56. this.selectContainer.connect("listChange", this, this.listChange);
  57. }
  58. /** 绘制图例样式 */
  59. _legend: any | null = null;
  60. get getlegend(): any {
  61. return this._legend;
  62. };
  63. set setlegend(obj: any) {
  64. this._legend = obj;
  65. console.log('aaaaaa', obj)
  66. }
  67. /**
  68. * 监听变化
  69. * @param obj 变化后的对象
  70. */
  71. listChange(obj: any) {
  72. let itemType: string = 'equipment';
  73. if (obj.itemList[0] instanceof STextMarkerItem) {
  74. itemType = 'baseText'
  75. console.log('obj.itemList[0]', itemType, obj.itemList[0])
  76. } else if (obj.itemList[0] instanceof SImageMarkerItem) {
  77. itemType = 'baseImage'
  78. } else if (obj.itemList[0] instanceof SLineMarkerItem) {
  79. itemType = 'baseLine'
  80. } else if (obj.itemList[0] instanceof SZoneLegendItem) {
  81. itemType = 'Zone'
  82. } else if (obj.itemList[0] instanceof SImageLegendItem) {
  83. itemType = 'Image'
  84. } else if (obj.itemList[0] instanceof TipelineItem) {
  85. itemType = 'Line'
  86. } else {
  87. itemType = ''
  88. };
  89. if (obj.itemList.length == 1) {
  90. // 获取聚焦item
  91. this.focusItem = obj.itemList[0]
  92. }
  93. let msg = {
  94. itemList: obj.itemList,
  95. itemType,
  96. }
  97. this.emitChange(msg)
  98. }
  99. emitChange(msg: any) {
  100. }
  101. /**
  102. * 增加线段item
  103. */
  104. addLine(event: SMouseEvent): boolean {
  105. const data = {
  106. /** ID */
  107. ID: uuid(),
  108. /** 名称 */
  109. Name: '直线',
  110. /** 图标(Image),线类型(Line) */
  111. Type: "Line",
  112. /** 位置 */
  113. Pos: { X: 0, Y: 0 },
  114. /** 由应用自己定义 */
  115. Properties: {
  116. Line: [{ X: event.x, Y: event.y }]
  117. }
  118. }
  119. const item = new SLineMarkerItem(null, data);
  120. item.status = SItemStatus.Create;
  121. item.zOrder = ItemOrder.lineOrder;
  122. item.selectable = true;
  123. this.addItem(item);
  124. item.connect("finishCreated", this, this.finishCreated);
  125. this.grabItem = item;
  126. this.Markers.push(item)
  127. // this.undoStack.push(new SGraphAddCommand(this, item));
  128. // item.connect("onMove", this, this.onItemMove.bind(this));
  129. return true
  130. }
  131. /**
  132. * 增加折线item
  133. */
  134. addPolylineItem(event: SMouseEvent): boolean {
  135. const point = new SPoint(event.x, event.y)
  136. const item = new TipelineItem(null, [point]);
  137. //设置状态
  138. item.selectable = true;
  139. item.status = SItemStatus.Create;
  140. item.zOrder = ItemOrder.polylineOrder
  141. this.addItem(item);
  142. item.connect("finishCreated", this, this.finishCreated);
  143. // this.undoStack.push(new SGraphAddCommand(this, item));
  144. this.grabItem = item;
  145. this.focusItem = item;
  146. return true
  147. }
  148. /**
  149. * 增加管道 lenged
  150. */
  151. addTipelineItem(event: SMouseEvent): boolean {
  152. const LegendData: Relation = {
  153. ID: uuid(),
  154. Name: this._legend.Name,
  155. GraphElementType: this._legend.Type,
  156. Num: 0,
  157. GraphElementId: this._legend.Id,
  158. AttachObjectIds: [],
  159. Type: "Line",
  160. PointList: [{ X: event.x, Y: event.y }],
  161. LineType: 0,
  162. Properties: {
  163. LineDash: this._legend.LineDash,
  164. LineWidth: this._legend.LineWidth,
  165. Color: this._legend.Color,
  166. },
  167. }
  168. const item = new TipelineItem(null, LegendData);
  169. //设置状态
  170. item.selectable = true;
  171. item.status = SItemStatus.Create;
  172. item.zOrder = ItemOrder.polylineOrder
  173. this.addItem(item);
  174. this.Relations.push(item);
  175. item.connect("finishCreated", this, this.finishCreated);
  176. // this.undoStack.push(new SGraphAddCommand(this, item));
  177. this.grabItem = item;
  178. this.focusItem = item;
  179. return true
  180. }
  181. /**
  182. * 增加多边形item lenged
  183. */
  184. addPolygonItem(event: SMouseEvent): void {
  185. const LegendData: Legend = {
  186. ID: uuid(),
  187. Name: this._legend.Name,
  188. GraphElementType: this._legend.Type,
  189. Num: 0,
  190. GraphElementId: this._legend.Id,
  191. AttachObjectIds: [],
  192. Type: "Zone",
  193. Pos: { X: event.x, Y: event.y },
  194. OutLine:[{X:event.x, Y:event.y}],
  195. Properties: {
  196. StrokeColor: this._legend.Color,
  197. FillColor: this._legend.FillColor,
  198. LineDash: this._legend.LineDash,
  199. LineWidth: this._legend.LineWidth,
  200. },
  201. }
  202. const Polylines = new SZoneLegendItem(null, LegendData);
  203. Polylines.selectable = true;
  204. //设置状态
  205. Polylines.status = SItemStatus.Create;
  206. Polylines.zOrder = ItemOrder.polygonOrder;
  207. // Polylines.moveable = true;
  208. this.addItem(Polylines);
  209. Polylines.connect("finishCreated", this, this.finishCreated);
  210. this.grabItem = Polylines;
  211. this.focusItem = Polylines;
  212. this.Nodes.push(Polylines);
  213. }
  214. /**
  215. * 增加图片Item mark
  216. */
  217. addImgItem(event: SMouseEvent) {
  218. const data = {
  219. /** ID */
  220. ID: uuid(),
  221. /** 名称 */
  222. Name: '图片',
  223. Num: 3,
  224. /** 图标(Image),线类型(Line) */
  225. Type: "Image",
  226. /** 位置 */
  227. Pos: { X: event.x, Y: event.y },
  228. /** 由应用自己定义 */
  229. Properties: {
  230. Url: '',
  231. }
  232. }
  233. const item = new SImageMarkerItem(null, data);
  234. item.zOrder = ItemOrder.imageOrder;
  235. item.selectable = true;
  236. item.moveable = true;
  237. this.addItem(item);
  238. this.grabItem == null;
  239. this.focusItem = item;
  240. this.cmd = 'choice';
  241. this.Markers.push(item)
  242. }
  243. /**
  244. * 增加文字item
  245. */
  246. addTextItem(event: SMouseEvent): void {
  247. const data = {
  248. /** ID */
  249. ID: uuid(),
  250. /** 名称 */
  251. Name: '文本',
  252. /** 图标 */
  253. Type: "Text",
  254. /** 位置 */
  255. Pos: { X: event.x, Y: event.y },
  256. /** 由应用自己定义 */
  257. Properties: {
  258. Text: '请在右侧属性栏输入文字!',
  259. Color: '',
  260. Font: 0,
  261. BackgroundColor: ''
  262. }
  263. }
  264. const item = new STextMarkerItem(null, data);
  265. item.moveTo(event.x, event.y);
  266. item.selectable = true;
  267. item.moveable = true;
  268. item.zOrder = ItemOrder.textOrder;
  269. this.addItem(item);
  270. this.grabItem == null
  271. this.cmd = 'choice';
  272. this.Markers.push(item)
  273. }
  274. /**
  275. * 增加图标lenged图标
  276. */
  277. addIconItem(event: SMouseEvent): void {
  278. console.log('this._legend.url', this._legend)
  279. const LegendData: Legend = {
  280. ID: uuid(),
  281. Name: this._legend.Name,
  282. GraphElementType: this._legend.Type,
  283. Num: 0,
  284. GraphElementId: this._legend.Id,
  285. AttachObjectIds: [],
  286. Pos: { X: event.x, Y: event.y },
  287. Scale: { X: 1, Y: 1, Z: 1 }, // 缩放
  288. Rolate: { X: 0, Y: 0, Z: 0 },
  289. Size: { Width: 0, Height: 0 }, // 大小
  290. Properties: {
  291. Url: '/serve/topology-wanda/Picture/query/' + this._legend.Url
  292. },
  293. }
  294. const item = new SImageLegendItem(null, LegendData);
  295. this.grabItem == null
  296. this.cmd = 'choice';
  297. item.selectable = true;
  298. item.moveable = true;
  299. item.zOrder = ItemOrder.markOrder;
  300. // item.moveTo(event.x, event.y);
  301. this.addItem(item);
  302. this.Nodes.push(item);
  303. }
  304. /**
  305. * 更改item对应属性
  306. */
  307. editItemStatus(): void {
  308. }
  309. /**
  310. * 更改文本对应属性
  311. * @param str string 文字内容
  312. */
  313. updatedText(str: string): void {
  314. if (this.focusItem) {
  315. const old = this.focusItem.text;
  316. this.focusItem.text = str;
  317. // this.undoStack.push(new SGraphPropertyCommand(this, this.focusItem, "text", old, str));
  318. }
  319. }
  320. /**
  321. * 更改文本fontSize属性
  322. * @param size number 文字大小
  323. */
  324. updatedFontSize(size: number): void {
  325. if (this.focusItem) {
  326. let old = new SFont(this.focusItem.font);
  327. let font = new SFont(this.focusItem.font);
  328. font.size = size;
  329. this.focusItem.font = font;
  330. // this.undoStack.push(new SGraphPropertyCommand(this, this.focusItem, "font", old, font));
  331. }
  332. }
  333. /**
  334. * 更改线宽属性
  335. * @param lineWidth number 线宽大小
  336. */
  337. updatedLineWidth(lineWidth: number): void {
  338. if (this.focusItem) {
  339. // let old = new SFont(this.focusItem.font);
  340. // let font = new SFont(this.focusItem.font);
  341. // font.size = size;
  342. this.focusItem.lineWidth = lineWidth;
  343. // this.undoStack.push(new SGraphPropertyCommand(this, this.focusItem, "font", old, font));
  344. }
  345. }
  346. /**
  347. * 更改文本颜色属性
  348. * @param str string 颜色
  349. */
  350. updatedFontColor(color: string): void {
  351. if (this.focusItem) {
  352. let old = this.focusItem.color;
  353. this.focusItem.color = color;
  354. // this.undoStack.push(new SGraphPropertyCommand(this, this.focusItem, "color", old, color));
  355. }
  356. }
  357. /**
  358. * 更改border颜色
  359. * @param color string 颜色
  360. */
  361. updatedBorderColor(color: string): void {
  362. if (this.focusItem) {
  363. let old = this.focusItem.strokeColor;
  364. this.focusItem.strokeColor = color;
  365. // this.undoStack.push(new SGraphPropertyCommand(this, this.focusItem, "color", old, color));
  366. }
  367. }
  368. /**
  369. * 更改item宽
  370. * @param width number 颜色
  371. */
  372. updatedWidth(width: number): void {
  373. if (this.focusItem) {
  374. // let old = this.focusItem.width;
  375. this.focusItem.width = width;
  376. // this.undoStack.push(new SGraphPropertyCommand(this, this.focusItem, "color", old, color));
  377. }
  378. }
  379. /**
  380. * 更改item高
  381. * @param height number 颜色
  382. */
  383. updatedHeight(height: number): void {
  384. if (this.focusItem) {
  385. // let old = this.focusItem.width;
  386. this.focusItem.height = height;
  387. // this.undoStack.push(new SGraphPropertyCommand(this, this.focusItem, "color", old, color));
  388. }
  389. }
  390. /**
  391. * 更改item坐标
  392. * @param x number x x坐标
  393. * @param y number y y坐标
  394. */
  395. updatedPosition(x: number, y: number): void {
  396. if (this.focusItem) {
  397. let p = this.focusItem.mapFromScene(x, y)
  398. // newx - oldx = newleft - oldleft
  399. // 要求的值(新的x坐标) - 旧的x坐标 = 新的左边界(用户输入的值) - 旧的左边界
  400. this.focusItem.x = p.x - this.focusItem.boundingRect().left + this.focusItem.x;
  401. this.focusItem.y = p.y - this.focusItem.boundingRect().top + this.focusItem.y;
  402. }
  403. }
  404. /**
  405. * 更改item 背景色坐标
  406. * @param color string 颜色color
  407. */
  408. updatedbackColor(color: string): void {
  409. if (this.focusItem) {
  410. this.focusItem.backgroundColor = color;
  411. }
  412. }
  413. /**
  414. * 更改item Url
  415. * @param url string 图片key
  416. */
  417. upadataImageUrl(url: string): void {
  418. if (this.focusItem) {
  419. this.focusItem.url = '/serve/topology-wanda/Picture/query/' + url;
  420. }
  421. }
  422. /**
  423. * 更改item border
  424. * @param val string border类型
  425. */
  426. upadataBorder(val: string): void {
  427. if (this.focusItem) {
  428. let borderStyle = null
  429. if (val == 'dashed') {
  430. borderStyle = SLineStyle.Dashed
  431. } else if (val == 'dotted') {
  432. borderStyle = SLineStyle.Dotted
  433. } else if (val == 'solid') {
  434. borderStyle = SLineStyle.Soild
  435. }
  436. this.focusItem.lineStyle = borderStyle;
  437. }
  438. }
  439. /**
  440. * 更改item 名称
  441. * @param val string border类型
  442. */
  443. upadataLengedName(val: string): void {
  444. console.log('xxxxxxx',val,this.focusItem)
  445. if (this.focusItem && this.focusItem.data) {
  446. this.focusItem.text= val
  447. }
  448. }
  449. /**
  450. * 删除指定item
  451. */
  452. deleiteItem(): void {
  453. if (this.focusItem) {
  454. this.removeItem(this.focusItem);
  455. let a = -1
  456. this.Nodes.forEach((item: any, index: number) => {
  457. if (item.id == this.focusItem.id) {
  458. a = index
  459. }
  460. });
  461. this.Nodes.splice(a, 1);
  462. let b = -1;
  463. this.Markers.forEach((item: any, index: number) => {
  464. if (item.id == this.focusItem.id) {
  465. b = index
  466. }
  467. });
  468. this.Markers.splice(b, 1);
  469. let c = -1;
  470. this.Relations.forEach((item: any, index: number) => {
  471. if (item.id == this.focusItem.id) {
  472. c = index
  473. }
  474. });
  475. this.Relations.splice(c, 1);
  476. this.focusItem = null;
  477. }
  478. if (this.view) {
  479. this.view.update();
  480. }
  481. }
  482. /**
  483. * 对齐指定item
  484. * @param v
  485. */
  486. changeAlignItem(v: any): void {
  487. this.selectContainer.layout(v);
  488. }
  489. /**
  490. * 提取item
  491. */
  492. extractItem(): void {
  493. console.log(this)
  494. }
  495. /**
  496. * 保存数据
  497. */
  498. saveMsgItem(): any {
  499. const Nodes: any = [];
  500. const Markers: any = [];
  501. const Relations: any = [];
  502. this.Nodes.forEach(e => {
  503. Nodes.push(e.toData())
  504. });
  505. this.Markers.forEach(e => {
  506. Markers.push(e.toData())
  507. });
  508. this.Relations.forEach(e => {
  509. Relations.push(e.toData())
  510. });
  511. let element = {
  512. Nodes, Markers, Relations
  513. }
  514. return element
  515. }
  516. /**
  517. * 锁住item
  518. */
  519. lockItem(): void {
  520. }
  521. /**
  522. * 执行取消操作
  523. */
  524. redo(): void {
  525. this.undoStack.undo();
  526. }
  527. /**
  528. * 执行重做操作执行
  529. */
  530. undo(): void {
  531. this.undoStack.redo();
  532. }
  533. /**
  534. * 完成事件创建的回调函数
  535. */
  536. finishCreated(item: any) {
  537. this.setCmd = 'choice';
  538. this.focusItem = item;
  539. this.selectContainer.toggleItem(item)
  540. }
  541. ////////////////////////
  542. // 以下为鼠标键盘操作事件
  543. onMouseDown(event: SMouseEvent): any {
  544. if (this.grabItem) {
  545. return this.grabItem.onMouseDown(event);
  546. }
  547. switch (this.cmd) {
  548. case 'baseLine':
  549. this.addLine(event);
  550. break;
  551. case 'baseText':
  552. this.addTextItem(event);
  553. break;
  554. case 'baseImage':
  555. this.addImgItem(event)
  556. break;
  557. case 'Zone':
  558. this.addPolygonItem(event);
  559. break;
  560. case 'Image':
  561. this.addIconItem(event);
  562. break;
  563. case 'Line':
  564. this.addTipelineItem(event);
  565. break;
  566. default:
  567. return super.onMouseDown(event);
  568. }
  569. }
  570. /**
  571. * 键盘事件
  572. *
  573. * @param event 事件参数
  574. * @return boolean
  575. */
  576. onKeyDown(event: KeyboardEvent): any {
  577. if (this.grabItem) {
  578. this.grabItem.onKeyDown(event);
  579. }
  580. // if (event.key == "Enter") {
  581. // this.cmd = 0
  582. // }
  583. return false
  584. }
  585. }