SPolygonItem.ts 20 KB

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