SPolylineItem.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 郝建龙 <haojianlong@sagacloud.cn>
  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. }
  58. set status(v: SItemStatus) {
  59. this._status = v;
  60. this.undoStack.clear();
  61. this.update();
  62. }
  63. /** 鼠标移动时的点 */
  64. protected lastPoint: SPoint | null = null;
  65. /** 线条颜色 */
  66. _strokeColor: SColor = SColor.Black;
  67. get strokeColor(): SColor {
  68. return this._strokeColor;
  69. }
  70. set strokeColor(v: SColor) {
  71. this._strokeColor = v;
  72. this.update();
  73. }
  74. /** 填充色 */
  75. _fillColor: SColor = new SColor("#2196f3");
  76. get fillColor(): SColor {
  77. return this._fillColor;
  78. }
  79. set fillColor(v: SColor) {
  80. this._fillColor = v;
  81. this.update();
  82. }
  83. /** 线条宽度 */
  84. _lineWidth: number = 1;
  85. get lineWidth(): number {
  86. return this._lineWidth;
  87. }
  88. set lineWidth(v: number) {
  89. this._lineWidth = v;
  90. this.update();
  91. }
  92. /** 边框样式 */
  93. _lineStyle: SLineStyle = SLineStyle.Solid;
  94. get lineStyle(): SLineStyle {
  95. return this._lineStyle;
  96. }
  97. set lineStyle(v: SLineStyle) {
  98. this._lineStyle = v;
  99. this.update();
  100. }
  101. /** 全局灵敏度 */
  102. dis: number = 10;
  103. /** 灵敏度转换为场景长度 */
  104. protected sceneDis: number = 10;
  105. /** 当前点索引 */
  106. protected 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. }
  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. }
  159. /**
  160. * 是否可以添加点到数组中
  161. *
  162. * @param index 要添加到的索引
  163. * @return 是否可添加
  164. */
  165. private canHandle(index: number): boolean {
  166. return index >= 0 && index <= this.pointList.length;
  167. }
  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. }
  190. /**
  191. * 鼠标按下事件
  192. *
  193. * @param event 鼠标事件
  194. * @return 是否处理事件
  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. }
  225. /**
  226. * 鼠标移动事件
  227. *
  228. * @param event 鼠标事件
  229. * @return 是否处理事件
  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. }
  257. /**
  258. * 鼠标移动事件
  259. *
  260. * @param event 鼠标事件
  261. * @return 是否处理事件
  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. }
  283. /**
  284. * 鼠标双击事件
  285. *
  286. * @param event 事件参数
  287. * @return 是否处理
  288. */
  289. onDoubleClick(event: SMouseEvent): boolean {
  290. // 如果为show状态 双击改对象则需改为编辑状态
  291. if (this.status == SItemStatus.Normal) {
  292. this.status = SItemStatus.Edit;
  293. this.grabItem(this);
  294. } else if (this.status == SItemStatus.Edit) {
  295. // 编辑状态
  296. this.status = SItemStatus.Normal;
  297. this.releaseItem();
  298. } else if (this.status == SItemStatus.Create) {
  299. // 创建状态
  300. if (this.pointList.length > 1) {
  301. this.status = SItemStatus.Normal;
  302. this.releaseItem();
  303. this.$emit("finishCreated");
  304. }
  305. }
  306. this.$emit("onDoubleClick", event);
  307. return true;
  308. }
  309. /***
  310. * 键盘按键弹起事件
  311. *
  312. * @param event 事件参数
  313. */
  314. onKeyUp(event: KeyboardEvent): void {
  315. if (event.keyCode == SKeyCode.Enter) {
  316. if (this.pointList.length > 1) {
  317. if (this.status == SItemStatus.Create) {
  318. this.$emit("finishCreated");
  319. }
  320. this.status = SItemStatus.Normal;
  321. this.releaseItem();
  322. }
  323. }
  324. // delete删除点
  325. if (
  326. event.keyCode == SKeyCode.Delete &&
  327. this.status == SItemStatus.Edit
  328. ) {
  329. this.deletePoint(this.curIndex);
  330. }
  331. }
  332. /**
  333. * 移动后处理所有坐标,并肩原点置为场景原点
  334. *
  335. * @param x x 坐标
  336. * @param y y 坐标
  337. */
  338. moveToOrigin(x: number, y: number): void {
  339. super.moveToOrigin(x, y);
  340. this.pointList = this.pointList.map(
  341. (t): SPoint => {
  342. t.x = t.x + x;
  343. t.y = t.y + y;
  344. return t;
  345. }
  346. );
  347. this.x = 0;
  348. this.y = 0;
  349. }
  350. /**
  351. * 获取点击点与点集中距离最近点
  352. *
  353. * @param p 鼠标点击点
  354. */
  355. findNearestPoint(p: SPoint): void {
  356. let len = this.sceneDis;
  357. for (let i = 0; i < this.pointList.length; i++) {
  358. let dis = SMathUtil.pointDistance(
  359. p.x,
  360. p.y,
  361. this.pointList[i].x,
  362. this.pointList[i].y
  363. );
  364. if (dis < len) {
  365. len = dis;
  366. this.curIndex = i;
  367. this.curPoint = new SPoint(
  368. this.pointList[this.curIndex].x,
  369. this.pointList[this.curIndex].y
  370. );
  371. }
  372. }
  373. }
  374. /**
  375. * 计算增加点的位置
  376. *
  377. * @param p 鼠标点击点
  378. */
  379. findAddPos(p: SPoint): void {
  380. let len = SMathUtil.pointToLine(
  381. p,
  382. new SLine(this.pointList[0], this.pointList[1])
  383. ),
  384. index = 0;
  385. if (this.pointList.length > 2) {
  386. for (let i = 1; i < this.pointList.length - 1; i++) {
  387. let dis = SMathUtil.pointToLine(
  388. p,
  389. new SLine(this.pointList[i], this.pointList[i + 1])
  390. );
  391. if (dis.MinDis < len.MinDis) {
  392. len = dis;
  393. index = i;
  394. }
  395. }
  396. }
  397. if (len.MinDis < this.sceneDis && len.Point) {
  398. this.addPoint(len.Point, index + 1);
  399. }
  400. }
  401. /**
  402. * shift 垂直水平创建或编辑
  403. *
  404. * @param event 事件
  405. * @return 事件对象
  406. */
  407. compare(event: SMouseEvent): SMouseEvent {
  408. if (this.pointList.length) {
  409. let last = new SPoint(event.x, event.y);
  410. if (this.status == SItemStatus.Create) {
  411. last = this.pointList[this.pointList.length - 1];
  412. } else if (this.status == SItemStatus.Edit) {
  413. if (this.curIndex > 1) {
  414. last = this.pointList[this.curIndex - 1];
  415. }
  416. }
  417. const dx = Math.abs(event.x - last.x);
  418. const dy = Math.abs(event.y - last.y);
  419. if (dy > dx) {
  420. event.x = last.x;
  421. } else {
  422. event.y = last.y;
  423. }
  424. }
  425. return event;
  426. }
  427. /**
  428. * 记录相关动作并推入栈中
  429. *
  430. * @param SGraphCommand 相关命令类
  431. * @param any 对应传入参数
  432. */
  433. protected recordAction(SGraphCommand: any, any: any[]): void {
  434. // 记录相关命令并推入堆栈中
  435. const command = new SGraphCommand(this.scene, this, ...any);
  436. this.undoStack.push(command);
  437. }
  438. /**
  439. * Item 对象边界区域
  440. *
  441. * @return 外接矩阵
  442. */
  443. boundingRect(): SRect {
  444. if (this.pointList.length) {
  445. this.minX = this.pointList[0].x;
  446. this.maxX = this.pointList[0].x;
  447. this.minY = this.pointList[0].y;
  448. this.maxY = this.pointList[0].y;
  449. this.pointList.forEach((it): void => {
  450. let x = it.x,
  451. y = it.y;
  452. // 如果数据 x 坐标小于 x 坐标最小值
  453. if (x < this.minX) {
  454. this.minX = x;
  455. }
  456. // 如果数据 y 坐标小于 y 坐标最小值
  457. if (y < this.minY) {
  458. this.minY = y;
  459. }
  460. // 如果数据 x 坐标大于 x 坐标最小值
  461. if (x > this.maxX) {
  462. this.maxX = x;
  463. }
  464. // 如果数据 y 坐标大于 y 坐标最小值
  465. if (y > this.maxY) {
  466. this.maxY = y;
  467. }
  468. });
  469. }
  470. return new SRect(
  471. this.minX,
  472. this.minY,
  473. this.maxX - this.minX,
  474. this.maxY - this.minY
  475. );
  476. }
  477. /**
  478. * 判断点是否在区域内
  479. *
  480. * @param x x 坐标
  481. * @param y y 坐标
  482. * @return 是否在区域内
  483. */
  484. contains(x: number, y: number): boolean {
  485. let p = new SPoint(x, y);
  486. for (let i = 1; i < this.pointList.length; i++) {
  487. let PTL = SMathUtil.pointToLine(
  488. p,
  489. new SLine(
  490. this.pointList[i - 1].x,
  491. this.pointList[i - 1].y,
  492. this.pointList[i].x,
  493. this.pointList[i].y
  494. )
  495. );
  496. if (PTL.MinDis < this.sceneDis) {
  497. return true;
  498. }
  499. }
  500. return false;
  501. }
  502. /**
  503. * 撤销操作
  504. */
  505. undo(): void {
  506. if (this._status != SItemStatus.Normal) {
  507. this.undoStack.undo();
  508. }
  509. }
  510. /**
  511. * 重做操作
  512. */
  513. redo(): void {
  514. if (this._status != SItemStatus.Normal) {
  515. this.undoStack.redo();
  516. }
  517. }
  518. /**
  519. * 取消操作执行
  520. */
  521. cancelOperate(): void {
  522. if (this.status == SItemStatus.Create) {
  523. this.parent = null;
  524. this.releaseItem();
  525. } else if (this.status == SItemStatus.Edit) {
  526. this.status = SItemStatus.Normal;
  527. this.releaseItem();
  528. }
  529. }
  530. /**
  531. * 绘制基本图形
  532. *
  533. * @param painter 绘制对象
  534. */
  535. drawBaseLine(painter: SPainter): void {
  536. // 绘制基本图形
  537. if (this.lineStyle == SLineStyle.Dashed) {
  538. painter.pen.lineDash = [
  539. painter.toPx(this.lineWidth * 3),
  540. painter.toPx(this.lineWidth * 7)
  541. ];
  542. } else if (this.lineStyle == SLineStyle.Dotted) {
  543. painter.pen.lineDash = [
  544. painter.toPx(this.lineWidth),
  545. painter.toPx(this.lineWidth)
  546. ];
  547. }
  548. painter.pen.color = this.strokeColor;
  549. painter.drawPolyline(this.pointList);
  550. }
  551. /**
  552. * Item 绘制操作
  553. *
  554. * @param painter 绘制对象
  555. */
  556. onDraw(painter: SPainter): void {
  557. // 缓存场景长度
  558. this.sceneDis = painter.toPx(this.dis);
  559. // 创建状态
  560. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  561. if (this.status == SItemStatus.Create && this.lastPoint) {
  562. // 绘制基本图形
  563. this.drawBaseLine(painter);
  564. painter.drawLine(
  565. this.pointList[this.pointList.length - 1],
  566. this.lastPoint
  567. );
  568. this.pointList.forEach((t, i): void => {
  569. painter.brush.color = SColor.White;
  570. if (i == this.curIndex) {
  571. painter.brush.color = this.fillColor;
  572. }
  573. painter.drawCircle(t.x, t.y, painter.toPx(5));
  574. });
  575. } else if (this.status == SItemStatus.Edit) {
  576. // 绘制基本图形
  577. this.drawBaseLine(painter);
  578. // 编辑状态
  579. this.pointList.forEach((t, i): void => {
  580. painter.brush.color = SColor.White;
  581. if (i == this.curIndex) {
  582. painter.brush.color = this.fillColor;
  583. }
  584. painter.drawCircle(t.x, t.y, painter.toPx(5));
  585. });
  586. } else {
  587. // 查看状态,是否选中
  588. if (this.selected) {
  589. painter.pen.lineWidth = painter.toPx(this.lineWidth * 2);
  590. painter.shadow.shadowBlur = 10;
  591. painter.shadow.shadowColor = new SColor(`#00000033`);
  592. painter.shadow.shadowOffsetX = 5;
  593. painter.shadow.shadowOffsetY = 5;
  594. }
  595. // 绘制基本图形
  596. this.drawBaseLine(painter);
  597. }
  598. }
  599. }