FloorScene.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { SGraphScene, SAnchorItem } from '@saga-web/graph/lib';
  2. import { SMouseEvent } from '@saga-web/base/lib';
  3. import { SPoint, SPainter, SRect, SColor } from '@saga-web/draw';
  4. import { SFloorParser, SLineItem, SPolylineItem, SItemStatus } from "@saga-web/big";
  5. import { SMathUtil } from '@saga-web/big/lib/utils/SMathUtil';
  6. import { SImgTextItem } from './items/SImgTextItem';
  7. import { TipelineItem } from './items/TipelineItem';
  8. export class FloorScene extends SGraphScene {
  9. CMD: number = 0;
  10. // 图标itemlist
  11. imgtextList: SImgTextItem[] = [];
  12. // 构造函数
  13. constructor() {
  14. super();
  15. this.selectContainer.connect('listChange', this, this.listChange)
  16. }
  17. //
  18. listChange() {
  19. console.log(arguments)
  20. }
  21. // 创建图标item
  22. createSImgText(event: SMouseEvent): SImgTextItem {
  23. const item = new SImgTextItem(null);
  24. item.moveTo(event.x, event.y);
  25. this.addItem(item);
  26. this.imgtextList.push(item);
  27. this.CMD = 0;
  28. // this.grabItem = item;
  29. return item;
  30. }
  31. // 创建图标item
  32. createSPolyline(event: SMouseEvent, anchor: SAnchorItem | null = null): TipelineItem {
  33. const item = new TipelineItem(null, [new SPoint(event.x, event.y)]);
  34. this.addItem(item);
  35. item.startAnchor = anchor;
  36. if (anchor) {
  37. anchor.parent?.connect('changePos', item, item.changePos)
  38. }
  39. this.grabItem = item;
  40. return item;
  41. }
  42. onMouseDown(event: SMouseEvent): boolean {
  43. if (this.CMD == 1) {
  44. this.createSImgText(event);
  45. return true;
  46. } else if (this.CMD == 2) {
  47. const anc = this.clickIsAnchor(event);
  48. if (anc) {
  49. const p = anc.mapToScene(0, 0)
  50. anc.isConnected = true;
  51. event.x = p.x;
  52. event.y = p.y;
  53. }
  54. if (this.grabItem instanceof TipelineItem) {
  55. // 是否点击锚点
  56. if (anc){
  57. if (this.grabItem.pointList.length) {
  58. this.grabItem.endAnchor = anc;
  59. anc.parent?.connect('changePos', this.grabItem, this.grabItem.changePos)
  60. }
  61. this.grabItem.onMouseDown(event);
  62. this.grabItem.status = SItemStatus.Edit;
  63. this.CMD = 0;
  64. return true
  65. }
  66. return super.onMouseDown(event);
  67. }
  68. this.createSPolyline(event, anc);
  69. return true;
  70. }
  71. this.CMD = 0
  72. return super.onMouseDown(event);
  73. }
  74. /**
  75. * 划线时点击位置是否是锚点
  76. *
  77. * @param event 事件
  78. * @param len 限制距离
  79. * @return 点击的锚点
  80. * */
  81. clickIsAnchor(event: SMouseEvent): SAnchorItem | null {
  82. let minAnchor = null;
  83. let len: number = -1;
  84. this.imgtextList.forEach(imgtext => {
  85. console.log(imgtext.anchorList)
  86. imgtext.anchorList.forEach(anchor => {
  87. let anchorPoint = anchor.mapToScene(0, 0);
  88. let dis = SMathUtil.pointDistance(
  89. event.x,
  90. event.y,
  91. anchorPoint.x,
  92. anchorPoint.y
  93. );
  94. if (len < 0) {
  95. len = anchor.sceneDis;
  96. }
  97. if (dis < len) {
  98. minAnchor = anchor;
  99. len = dis;
  100. }
  101. })
  102. })
  103. return minAnchor;
  104. } // Function clickIsAnchor()
  105. drawScene(painter: SPainter, rect: SRect) {
  106. super.drawScene(painter, rect);
  107. }
  108. }