SPolylineItem.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SColor, SLine, SPainter, SPoint, SRect } from "@persagy-web/draw";
  27. import { SKeyCode, SMouseEvent, SUndoStack } from "@persagy-web/base";
  28. import { SItemStatus } from "..";
  29. import { SMathUtil } from "../utils/SMathUtil";
  30. import {
  31. SGraphItem,
  32. SGraphPointListDelete,
  33. SGraphPointListInsert,
  34. SGraphPointListUpdate,
  35. SLineStyle
  36. } from "@persagy-web/graph";
  37. /**
  38. * 折线item
  39. *
  40. * @author 郝建龙
  41. * */
  42. export class SPolylineItem extends SGraphItem {
  43. /** X坐标最小值 */
  44. private minX = Number.MAX_SAFE_INTEGER;
  45. /** X坐标最大值 */
  46. private maxX = Number.MIN_SAFE_INTEGER;
  47. /** Y坐标最小值 */
  48. private minY = Number.MAX_SAFE_INTEGER;
  49. /** Y坐标最大值 */
  50. private maxY = Number.MIN_SAFE_INTEGER;
  51. /** 折点信息 */
  52. pointList: SPoint[] = [];
  53. /** 是否绘制完成 */
  54. _status: SItemStatus = SItemStatus.Normal;
  55. get status(): SItemStatus {
  56. return this._status;
  57. } // Get status
  58. set status(v: SItemStatus) {
  59. this._status = v;
  60. this.undoStack.clear();
  61. this.update();
  62. } // Set status
  63. /** 鼠标移动时的点 */
  64. private lastPoint: SPoint | null = null;
  65. /** 线条颜色 */
  66. _strokeColor: SColor = SColor.Black;
  67. get strokeColor(): SColor {
  68. return this._strokeColor;
  69. } // Get strokeColor
  70. set strokeColor(v: SColor) {
  71. this._strokeColor = v;
  72. this.update();
  73. } // Set strokeColor
  74. /** 填充色 */
  75. _fillColor: SColor = new SColor("#2196f3");
  76. get fillColor(): SColor {
  77. return this._fillColor;
  78. } // Get fillColor
  79. set fillColor(v: SColor) {
  80. this._fillColor = v;
  81. this.update();
  82. } // Set fillColor
  83. /** 线条宽度 */
  84. _lineWidth: number = 1;
  85. get lineWidth(): number {
  86. return this._lineWidth;
  87. } // Get lineWidth
  88. set lineWidth(v: number) {
  89. this._lineWidth = v;
  90. this.update();
  91. } // Set lineWidth
  92. /** 边框样式 */
  93. _lineStyle: SLineStyle = SLineStyle.Solid;
  94. get lineStyle(): SLineStyle {
  95. return this._lineStyle;
  96. } // Get lineStyle
  97. set lineStyle(v: SLineStyle) {
  98. this._lineStyle = v;
  99. this.update();
  100. } // Set lineStyle
  101. /** 全局灵敏度 */
  102. dis: number = 10;
  103. /** 灵敏度转换为场景长度 */
  104. private sceneDis: number = 10;
  105. /** 当前点索引 */
  106. private curIndex: number = -1;
  107. /** 当前点 */
  108. private curPoint: SPoint | null = null;
  109. /** undo/redo堆栈 */
  110. private undoStack: SUndoStack = new SUndoStack();
  111. /**
  112. * 构造函数
  113. *
  114. * @param parent 父级
  115. * @param list 坐标集合
  116. * */
  117. constructor(parent: null | SGraphItem, list: SPoint[]);
  118. /**
  119. * 构造函数
  120. *
  121. * @param parent 父级
  122. * @param list 第一个坐标
  123. * */
  124. constructor(parent: null | SGraphItem, list: SPoint);
  125. /**
  126. * 构造函数
  127. *
  128. * @param parent 父级
  129. * @param list 第一个坐标|坐标集合
  130. * */
  131. constructor(parent: null | SGraphItem, list: SPoint | SPoint[]) {
  132. super(parent);
  133. if (list instanceof SPoint) {
  134. this.pointList.push(list);
  135. } else {
  136. this.pointList = list;
  137. }
  138. } // Constructor
  139. /**
  140. * 添加点至数组中
  141. *
  142. * @param p 添加的点
  143. * @param index 添加到的索引
  144. * */
  145. private addPoint(p: SPoint, index?: number): void {
  146. if (index && this.canHandle(index)) {
  147. this.pointList.splice(index, 0, p);
  148. this.recordAction(SGraphPointListInsert, [
  149. this.pointList,
  150. p,
  151. index
  152. ]);
  153. } else {
  154. this.pointList.push(p);
  155. this.recordAction(SGraphPointListInsert, [this.pointList, p]);
  156. }
  157. this.update();
  158. } // Function addPoint()
  159. /**
  160. * 是否可以添加点到数组中
  161. *
  162. * @param index 要添加到的索引
  163. * @return boolean 是否可添加
  164. * */
  165. private canHandle(index: number): boolean {
  166. return index >= 0 && index <= this.pointList.length;
  167. } // Function canHandle()
  168. /**
  169. * 根据索引删除点
  170. *
  171. * @param index 删除点
  172. * */
  173. deletePoint(index: number): void {
  174. if (this.canHandle(index) && this.pointList.length > 2) {
  175. const p = new SPoint(
  176. this.pointList[this.curIndex].x,
  177. this.pointList[this.curIndex].y
  178. );
  179. this.pointList.splice(index, 1);
  180. this.recordAction(SGraphPointListDelete, [
  181. this.pointList,
  182. p,
  183. index
  184. ]);
  185. this.curIndex = -1;
  186. this.curPoint = null;
  187. this.update();
  188. }
  189. } // Function deletePoint
  190. /**
  191. * 鼠标按下事件
  192. *
  193. * @param event 鼠标事件
  194. * @return boolean 是否处理事件
  195. * */
  196. onMouseDown(event: SMouseEvent): boolean {
  197. this.curIndex = -1;
  198. this.curPoint = null;
  199. if (event.shiftKey) {
  200. event = this.compare(event);
  201. }
  202. if (event.buttons == 1) {
  203. if (this.status == SItemStatus.Create) {
  204. this.addPoint(new SPoint(event.x, event.y));
  205. return true;
  206. } else if (this.status == SItemStatus.Edit) {
  207. // 查询鼠标最近的索引
  208. this.findNearestPoint(new SPoint(event.x, event.y));
  209. // 增加点
  210. if (this.curIndex < 0) {
  211. this.findAddPos(new SPoint(event.x, event.y));
  212. }
  213. // 删除点
  214. if (event.altKey && this.canHandle(this.curIndex)) {
  215. this.deletePoint(this.curIndex);
  216. }
  217. this.update();
  218. return true;
  219. } else {
  220. return super.onMouseDown(event);
  221. }
  222. }
  223. return super.onMouseDown(event);
  224. } // Function onMouseDown()
  225. /**
  226. * 鼠标移动事件
  227. *
  228. * @param event 鼠标事件
  229. * @return boolean 是否处理事件
  230. * */
  231. onMouseMove(event: SMouseEvent): boolean {
  232. if (event.shiftKey) {
  233. event = this.compare(event);
  234. }
  235. if (this.status == SItemStatus.Create) {
  236. if (this.lastPoint) {
  237. this.lastPoint.x = event.x;
  238. this.lastPoint.y = event.y;
  239. } else {
  240. this.lastPoint = new SPoint(event.x, event.y);
  241. }
  242. this.update();
  243. return true;
  244. } else if (this.status == SItemStatus.Edit) {
  245. if (event.buttons == 1) {
  246. if (this.canHandle(this.curIndex)) {
  247. this.pointList[this.curIndex].x = event.x;
  248. this.pointList[this.curIndex].y = event.y;
  249. }
  250. }
  251. this.update();
  252. return true;
  253. } else {
  254. return super.onMouseMove(event);
  255. }
  256. } // Function onMouseMove()
  257. /**
  258. * 鼠标移动事件
  259. *
  260. * @param event 鼠标事件
  261. * @return boolean 是否处理事件
  262. * */
  263. onMouseUp(event: SMouseEvent): boolean {
  264. if (this.status == SItemStatus.Edit) {
  265. if (this.curIndex > -1) {
  266. const p = new SPoint(
  267. this.pointList[this.curIndex].x,
  268. this.pointList[this.curIndex].y
  269. );
  270. this.recordAction(SGraphPointListUpdate, [
  271. this.pointList,
  272. this.curPoint,
  273. p,
  274. this.curIndex
  275. ]);
  276. }
  277. } else if (this.status == SItemStatus.Normal) {
  278. this.moveToOrigin(this.x, this.y);
  279. return super.onMouseUp(event);
  280. }
  281. return true;
  282. } // Function onMouseUp()
  283. /**
  284. * 鼠标双击事件
  285. *
  286. * @param event 事件参数
  287. * @return boolean
  288. */
  289. onDoubleClick(event: SMouseEvent): boolean {
  290. if (this.status == SItemStatus.Normal) {
  291. this.status = SItemStatus.Edit;
  292. this.grabItem(this);
  293. } else if (this.status == SItemStatus.Edit) {
  294. this.status = SItemStatus.Normal;
  295. this.releaseItem();
  296. } else if (this.status == SItemStatus.Create) {
  297. if (this.pointList.length > 1) {
  298. this.status = SItemStatus.Normal;
  299. this.releaseItem();
  300. this.$emit("finishCreated");
  301. }
  302. }
  303. this.$emit("onDoubleClick", event);
  304. return true;
  305. } // Function onDoubleClick()
  306. /***
  307. * 键盘按键弹起事件
  308. *
  309. * @param event 事件参数
  310. */
  311. onKeyUp(event: KeyboardEvent): void {
  312. if (event.keyCode == SKeyCode.Enter) {
  313. if (this.pointList.length > 1) {
  314. if (this.status == SItemStatus.Create) {
  315. this.$emit("finishCreated");
  316. }
  317. this.status = SItemStatus.Normal;
  318. this.releaseItem();
  319. }
  320. }
  321. // delete删除点
  322. if (
  323. event.keyCode == SKeyCode.Delete &&
  324. this.status == SItemStatus.Edit
  325. ) {
  326. this.deletePoint(this.curIndex);
  327. }
  328. } // Function onKeyUp()
  329. /**
  330. * 移动后处理所有坐标,并肩原点置为场景原点
  331. *
  332. * @param x x坐标
  333. * @param y y坐标
  334. * */
  335. moveToOrigin(x: number, y: number): void {
  336. super.moveToOrigin(x, y);
  337. this.pointList = this.pointList.map(t => {
  338. t.x = t.x + x;
  339. t.y = t.y + y;
  340. return t;
  341. });
  342. this.x = 0;
  343. this.y = 0;
  344. } // Function moveToOrigin()
  345. /**
  346. * 获取点击点与点集中距离最近点
  347. *
  348. * @param p 鼠标点击点
  349. * */
  350. findNearestPoint(p: SPoint): void {
  351. let len = this.sceneDis;
  352. for (let i = 0; i < this.pointList.length; i++) {
  353. let dis = SMathUtil.pointDistance(
  354. p.x,
  355. p.y,
  356. this.pointList[i].x,
  357. this.pointList[i].y
  358. );
  359. if (dis < len) {
  360. len = dis;
  361. this.curIndex = i;
  362. this.curPoint = new SPoint(
  363. this.pointList[this.curIndex].x,
  364. this.pointList[this.curIndex].y
  365. );
  366. }
  367. }
  368. } // Function findNearestPoint()
  369. /**
  370. * 计算增加点的位置
  371. *
  372. * @param p 鼠标点击点
  373. * */
  374. findAddPos(p: SPoint): void {
  375. let len = SMathUtil.pointToLine(
  376. p,
  377. new SLine(this.pointList[0], this.pointList[1])
  378. ),
  379. index = 0;
  380. if (this.pointList.length > 2) {
  381. for (let i = 1; i < this.pointList.length - 1; i++) {
  382. let dis = SMathUtil.pointToLine(
  383. p,
  384. new SLine(this.pointList[i], this.pointList[i + 1])
  385. );
  386. if (dis.MinDis < len.MinDis) {
  387. len = dis;
  388. index = i;
  389. }
  390. }
  391. }
  392. if (len.MinDis < this.sceneDis) {
  393. if (len.Point) {
  394. this.addPoint(len.Point, index + 1);
  395. }
  396. }
  397. } // Function findAddPos()
  398. /**
  399. * shift垂直水平创建或编辑
  400. *
  401. * @param event 事件
  402. * @return 事件对象
  403. * */
  404. compare(event: SMouseEvent): SMouseEvent {
  405. if (this.pointList.length) {
  406. let last = new SPoint(event.x, event.y);
  407. if (this.status == SItemStatus.Create) {
  408. last = this.pointList[this.pointList.length - 1];
  409. } else if (this.status == SItemStatus.Edit) {
  410. if (this.curIndex > 1) {
  411. last = this.pointList[this.curIndex - 1];
  412. }
  413. }
  414. const dx = Math.abs(event.x - last.x);
  415. const dy = Math.abs(event.y - last.y);
  416. if (dy > dx) {
  417. event.x = last.x;
  418. } else {
  419. event.y = last.y;
  420. }
  421. }
  422. return event;
  423. } // Function compare()
  424. /**
  425. * 记录相关动作并推入栈中
  426. *
  427. * @param SGraphCommand 相关命令类
  428. * @param any 对应传入参数
  429. */
  430. protected recordAction(SGraphCommand: any, any: any[]): void {
  431. // 记录相关命令并推入堆栈中
  432. const command = new SGraphCommand(this.scene, this, ...any);
  433. this.undoStack.push(command);
  434. } // Function recordAction()
  435. /**
  436. * Item对象边界区域
  437. *
  438. * @return SRect 外接矩阵
  439. * */
  440. boundingRect(): SRect {
  441. if (this.pointList.length) {
  442. this.minX = this.pointList[0].x;
  443. this.maxX = this.pointList[0].x;
  444. this.minY = this.pointList[0].y;
  445. this.maxY = this.pointList[0].y;
  446. this.pointList.forEach(it => {
  447. let x = it.x,
  448. y = it.y;
  449. if (x < this.minX) {
  450. this.minX = x;
  451. }
  452. if (y < this.minY) {
  453. this.minY = y;
  454. }
  455. if (x > this.maxX) {
  456. this.maxX = x;
  457. }
  458. if (y > this.maxY) {
  459. this.maxY = y;
  460. }
  461. });
  462. }
  463. return new SRect(
  464. this.minX,
  465. this.minY,
  466. this.maxX - this.minX,
  467. this.maxY - this.minY
  468. );
  469. } // Function boundingRect()
  470. /**
  471. * 判断点是否在区域内
  472. *
  473. * @param x
  474. * @param y
  475. * @return true-是
  476. */
  477. contains(x: number, y: number): boolean {
  478. let p = new SPoint(x, y);
  479. for (let i = 1; i < this.pointList.length; i++) {
  480. let PTL = SMathUtil.pointToLine(
  481. p,
  482. new SLine(
  483. this.pointList[i - 1].x,
  484. this.pointList[i - 1].y,
  485. this.pointList[i].x,
  486. this.pointList[i].y
  487. )
  488. );
  489. if (PTL.MinDis < this.sceneDis) {
  490. return true;
  491. }
  492. }
  493. return false;
  494. } // Function contains()
  495. /**
  496. * 撤销操作
  497. *
  498. */
  499. undo(): void {
  500. if (this._status != SItemStatus.Normal) {
  501. this.undoStack.undo();
  502. }
  503. } // Function undo()
  504. /**
  505. * 重做操作
  506. *
  507. */
  508. redo(): void {
  509. if (this._status != SItemStatus.Normal) {
  510. this.undoStack.redo();
  511. }
  512. } // Function redo()
  513. /**
  514. * 取消操作执行
  515. *
  516. * */
  517. cancelOperate(): void {
  518. if (this.status == SItemStatus.Create) {
  519. this.parent = null;
  520. this.releaseItem();
  521. } else if (this.status == SItemStatus.Edit) {
  522. this.status = SItemStatus.Normal;
  523. this.releaseItem();
  524. }
  525. } // Function cancelOperate()
  526. /**
  527. * 绘制基本图形
  528. *
  529. * @param painter painter对象
  530. * */
  531. drawBaseLine(painter: SPainter): void {
  532. // 绘制基本图形
  533. if (this.lineStyle == SLineStyle.Dashed) {
  534. painter.pen.lineDash = [
  535. painter.toPx(this.lineWidth * 3),
  536. painter.toPx(this.lineWidth * 7)
  537. ];
  538. } else if (this.lineStyle == SLineStyle.Dotted) {
  539. painter.pen.lineDash = [
  540. painter.toPx(this.lineWidth),
  541. painter.toPx(this.lineWidth)
  542. ];
  543. }
  544. painter.pen.color = this.strokeColor;
  545. painter.drawPolyline(this.pointList);
  546. } // Function drawBaseLine()
  547. /**
  548. * Item绘制操作
  549. *
  550. * @param painter painter对象
  551. */
  552. onDraw(painter: SPainter): void {
  553. // 缓存场景长度
  554. this.sceneDis = painter.toPx(this.dis);
  555. // 创建状态
  556. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  557. if (this.status == SItemStatus.Create && this.lastPoint) {
  558. // 绘制基本图形
  559. this.drawBaseLine(painter);
  560. painter.drawLine(
  561. this.pointList[this.pointList.length - 1],
  562. this.lastPoint
  563. );
  564. this.pointList.forEach((t, i): void => {
  565. painter.brush.color = SColor.White;
  566. if (i == this.curIndex) {
  567. painter.brush.color = this.fillColor;
  568. }
  569. painter.drawCircle(t.x, t.y, painter.toPx(5));
  570. });
  571. } else if (this.status == SItemStatus.Edit) {
  572. // 绘制基本图形
  573. this.drawBaseLine(painter);
  574. // 编辑状态
  575. this.pointList.forEach((t, i): void => {
  576. painter.brush.color = SColor.White;
  577. if (i == this.curIndex) {
  578. painter.brush.color = this.fillColor;
  579. }
  580. painter.drawCircle(t.x, t.y, painter.toPx(5));
  581. });
  582. } else {
  583. // 查看状态
  584. if (this.selected) {
  585. painter.pen.lineWidth = painter.toPx(this.lineWidth * 2);
  586. painter.shadow.shadowBlur = 10;
  587. painter.shadow.shadowColor = new SColor(`#00000033`);
  588. painter.shadow.shadowOffsetX = 5;
  589. painter.shadow.shadowOffsetY = 5;
  590. }
  591. // 绘制基本图形
  592. this.drawBaseLine(painter);
  593. }
  594. } // Function onDraw()
  595. } // Class SPolylineItem