SBasePolylineEdit.ts 20 KB

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