SPolygonItem.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. } // Get getPointList
  64. set setPointList(arr: SPoint[]) {
  65. this.pointList = arr;
  66. this.update();
  67. } // Set getPointList
  68. /** 当前状态 */
  69. protected _status: number = SItemStatus.Normal;
  70. get status(): SItemStatus {
  71. return this._status;
  72. } // Get status
  73. set status(value: SItemStatus) {
  74. this._status = value;
  75. this.undoStack.clear();
  76. this.update();
  77. } // Set status
  78. /** 边框颜色 */
  79. _strokeColor: SColor = new SColor("#0091FF");
  80. /** 画笔颜色 */
  81. get strokeColor(): SColor {
  82. return this._strokeColor;
  83. } // Get strokeColor
  84. set strokeColor(v: SColor) {
  85. this._strokeColor = v;
  86. this.update();
  87. } // Set strokeColor
  88. /** 填充颜色 */
  89. _fillColor: SColor = new SColor("#1EE887");
  90. get fillColor(): SColor {
  91. return this._fillColor;
  92. } // Get fillColor
  93. set fillColor(v: SColor) {
  94. this._fillColor = v;
  95. this.update();
  96. } // Set fillColor
  97. /** 边框样式 */
  98. _lineStyle: SLineStyle = SLineStyle.Solid;
  99. get lineStyle(): SLineStyle {
  100. return this._lineStyle;
  101. } // Get lineStyle
  102. set lineStyle(v: SLineStyle) {
  103. this._lineStyle = v;
  104. this.update();
  105. } // Set lineStyle
  106. /** 边框的宽 只可输入像素宽 */
  107. _lineWidth: number = 4;
  108. get lineWidth(): number {
  109. return this._lineWidth;
  110. } // Get lineWidth
  111. set lineWidth(v: number) {
  112. this._lineWidth = v;
  113. this.update();
  114. } // Set lineWidth
  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. } // Constructor
  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. } // Function insertPoint()
  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. } // Function deletePoint()
  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. } // Function movePoint()
  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. if (this.selected) {
  233. painter.shadow.shadowBlur = 10;
  234. painter.shadow.shadowColor = new SColor(`#00000033`);
  235. painter.shadow.shadowOffsetX = 5;
  236. painter.shadow.shadowOffsetY = 5;
  237. } else {
  238. painter.shadow.shadowColor = SColor.Transparent;
  239. }
  240. painter.drawPolygon([...pointList]);
  241. painter.restore();
  242. } // Function drawShowPolygon()
  243. /**
  244. * 创建状态 --绘制多边形数组
  245. *
  246. * @param painter 绘制类
  247. * @param pointList 绘制多边形数组
  248. */
  249. protected drawCreatePolygon(painter: SPainter, pointList: SPoint[]): void {
  250. painter.pen.lineCapStyle = SLineCapStyle.Square;
  251. painter.pen.color = this.strokeColor;
  252. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  253. if (this.lastPoint && pointList.length) {
  254. painter.drawLine(
  255. pointList[pointList.length - 1].x,
  256. pointList[pointList.length - 1].y,
  257. this.lastPoint.x,
  258. this.lastPoint.y
  259. );
  260. }
  261. painter.drawPolyline(pointList);
  262. painter.pen.color = SColor.Transparent;
  263. painter.brush.color = new SColor(this.fillColor.value);
  264. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  265. if (this.lastPoint) {
  266. painter.drawPolygon([...pointList, this.lastPoint]);
  267. // 绘制顶点块
  268. painter.pen.color = SColor.Black;
  269. painter.brush.color = SColor.White;
  270. pointList.forEach(item => {
  271. painter.drawCircle(item.x, item.y, painter.toPx(this.len / 2));
  272. });
  273. // 如果最后一个点在第一个点的灵敏度范围内,第一个点填充变红
  274. if (this.pointList.length) {
  275. if (
  276. SMathUtil.pointDistance(
  277. this.lastPoint.x,
  278. this.lastPoint.y,
  279. this.pointList[0].x,
  280. this.pointList[0].y
  281. ) < this.scenceLen
  282. ) {
  283. // 绘制第一个点的顶点块
  284. painter.pen.color = SColor.Black;
  285. painter.brush.color = SColor.Red;
  286. painter.drawCircle(
  287. this.pointList[0].x,
  288. this.pointList[0].y,
  289. painter.toPx(this.len / 2)
  290. );
  291. }
  292. }
  293. } else {
  294. painter.drawPolygon(pointList);
  295. }
  296. } // Function drawCreatePolygon()
  297. /**
  298. * 编辑状态 --绘制多边形数组
  299. *
  300. * @param painter 绘制类
  301. * @param pointList 绘制多边形数组
  302. */
  303. protected drawEditPolygon(painter: SPainter, pointList: SPoint[]): void {
  304. // 展示多边形
  305. painter.pen.lineCapStyle = SLineCapStyle.Square;
  306. painter.pen.color = this.strokeColor;
  307. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  308. painter.brush.color = new SColor(this.fillColor.value);
  309. painter.drawPolygon([...pointList]);
  310. // 绘制顶点块
  311. painter.pen.color = SColor.Black;
  312. painter.brush.color = SColor.White;
  313. pointList.forEach((item, index) => {
  314. painter.brush.color = SColor.White;
  315. if (index == this.curIndex) {
  316. painter.brush.color = new SColor("#2196f3");
  317. }
  318. painter.drawCircle(item.x, item.y, painter.toPx(this.len / 2));
  319. });
  320. } // Function drawCreatePolygon()
  321. /**
  322. * 编辑状态操作多边形数组
  323. *
  324. * @param event 鼠标事件
  325. */
  326. protected editPolygonPoint(event: SMouseEvent): void {
  327. // 判断是否为删除状态 isAlt = true为删除状态
  328. if (this.isAlt) {
  329. // 1 判断是否点击在多边形顶点
  330. let lenIndex = -1; // 当前点击到的点位索引;
  331. let curenLen = this.scenceLen; // 当前的灵敏度
  332. this.pointList.forEach((item, index) => {
  333. let dis = SMathUtil.pointDistance(
  334. event.x,
  335. event.y,
  336. item.x,
  337. item.y
  338. );
  339. if (dis < curenLen) {
  340. curenLen = dis;
  341. lenIndex = index;
  342. }
  343. });
  344. // 若点击到,对该索引对应的点做删除
  345. if (lenIndex != -1) {
  346. if (this.pointList.length <= 3) {
  347. return;
  348. }
  349. const delePoint = new SPoint(
  350. this.pointList[lenIndex].x,
  351. this.pointList[lenIndex].y
  352. );
  353. this.deletePoint(lenIndex);
  354. // 记录顶点操作记录压入堆栈
  355. this.recordAction(SGraphPointListDelete, [
  356. this.pointList,
  357. delePoint,
  358. lenIndex
  359. ]);
  360. }
  361. } else {
  362. // 1 判断是否点击在多边形顶点
  363. this.curIndex = -1;
  364. this.curPoint = null;
  365. let lenIndex = -1; // 当前点击到的点位索引;
  366. let curenLen = this.scenceLen; // 当前的灵敏度
  367. this.pointList.forEach((item, index) => {
  368. let dis = SMathUtil.pointDistance(
  369. event.x,
  370. event.y,
  371. item.x,
  372. item.y
  373. );
  374. if (dis < curenLen) {
  375. curenLen = dis;
  376. lenIndex = index;
  377. }
  378. });
  379. this.curIndex = lenIndex;
  380. // 2判断是否点击在多边形得边上
  381. if (-1 == lenIndex) {
  382. let len = SMathUtil.pointToLine(
  383. new SPoint(event.x, event.y),
  384. new SLine(this.pointList[0], this.pointList[1])
  385. ),
  386. index = 0;
  387. if (this.pointList.length > 2) {
  388. for (let i = 1; i < this.pointList.length; i++) {
  389. let dis = SMathUtil.pointToLine(
  390. new SPoint(event.x, event.y),
  391. new SLine(this.pointList[i], this.pointList[i + 1])
  392. );
  393. if (i + 1 == this.pointList.length) {
  394. dis = SMathUtil.pointToLine(
  395. new SPoint(event.x, event.y),
  396. new SLine(this.pointList[i], this.pointList[0])
  397. );
  398. }
  399. if (dis.MinDis < len.MinDis) {
  400. len = dis;
  401. index = i;
  402. }
  403. }
  404. }
  405. // 判断是否有点
  406. if (len.Point) {
  407. // 点在了多边形的边上
  408. if (len.MinDis <= this.scenceLen) {
  409. this.pointList.splice(index + 1, 0, len.Point);
  410. // 记录新增顶点操作记录压入堆栈
  411. this.recordAction(SGraphPointListInsert, [
  412. this.pointList,
  413. len.Point,
  414. index + 1
  415. ]);
  416. } else {
  417. //没点在多边形边上也没点在多边形顶点上
  418. super.onMouseDown(event);
  419. }
  420. }
  421. } else {
  422. // 当捕捉到顶点后 ,记录当前点的xy坐标,用于undo、redo操作
  423. this.curPoint = new SPoint(
  424. this.pointList[this.curIndex].x,
  425. this.pointList[this.curIndex].y
  426. );
  427. }
  428. // 刷新视图
  429. this.update();
  430. }
  431. } // Function editPolygonPoint()
  432. /////////////////////
  433. // undo、redo相关操作
  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. } // Function recordAction()
  445. /**
  446. * 执行取消操作执行
  447. */
  448. undo(): void {
  449. if (this.status == SItemStatus.Normal) {
  450. return;
  451. }
  452. this.undoStack.undo();
  453. } // Function undo()
  454. /**
  455. * 执行重做操作执行
  456. */
  457. redo(): void {
  458. if (this.status == SItemStatus.Normal) {
  459. return;
  460. }
  461. this.undoStack.redo();
  462. } // Function redo()
  463. ///////////////////////////////
  464. // 以下为鼠标事件
  465. /**
  466. * 鼠标双击事件
  467. *
  468. * @param event 事件参数
  469. * @return 是否处理
  470. */
  471. onDoubleClick(event: SMouseEvent): boolean {
  472. // 如果位show状态 双击改对象则需改为编辑状态
  473. if (SItemStatus.Normal == this.status) {
  474. this.status = SItemStatus.Edit;
  475. this.grabItem(this);
  476. } else if (SItemStatus.Edit == this.status) {
  477. this.status = SItemStatus.Normal;
  478. this.releaseItem();
  479. }
  480. this.update();
  481. return true;
  482. } // Function onDoubleClick()
  483. /**
  484. * 键盘事件
  485. *
  486. * @param event 事件参数
  487. * @return 是否处理
  488. */
  489. onKeyDown(event: KeyboardEvent): boolean {
  490. if (this.status == SItemStatus.Normal) {
  491. return false;
  492. } else if (this.status == SItemStatus.Create) {
  493. if (event.code == "Enter") {
  494. // 当顶点大于二个时才又条件执行闭合操作并清空堆栈
  495. if (this.pointList.length > 2) {
  496. this.status = SItemStatus.Normal;
  497. //3 传递完成事件状态
  498. this.$emit("finishCreated");
  499. //1 grabItem 置为null
  500. this.releaseItem();
  501. }
  502. }
  503. } else if (this.status == SItemStatus.Edit) {
  504. if (event.key == "Alt") {
  505. this.isAlt = true;
  506. }
  507. }
  508. this.update();
  509. return true;
  510. } // Function onKeyDown()
  511. /**
  512. * 键盘键抬起事件
  513. *
  514. * @param event 事件参数
  515. * @return 是否处理
  516. */
  517. onKeyUp(event: KeyboardEvent): void {
  518. if (this.status == SItemStatus.Edit) {
  519. if (event.key == "Alt") {
  520. this.isAlt = false;
  521. } else if (event.keyCode == SKeyCode.Delete) {
  522. // 当多边形的顶点大于三个允许删除点
  523. if (this.pointList.length > 3) {
  524. this.deletePoint(this.curIndex);
  525. }
  526. }
  527. }
  528. this.update();
  529. } // Function onKeyUp()
  530. /**
  531. * 鼠标按下事件
  532. *
  533. * @param event 事件参数
  534. * @return 是否处理
  535. */
  536. onMouseDown(event: SMouseEvent): boolean {
  537. if (event.shiftKey) {
  538. event = this.compare(event);
  539. }
  540. // 如果状态为编辑状态则添加点
  541. if (this.status == SItemStatus.Create) {
  542. // 新增顶点
  543. let len = -1;
  544. if (this.pointList.length) {
  545. len = SMathUtil.pointDistance(
  546. event.x,
  547. event.y,
  548. this.pointList[0].x,
  549. this.pointList[0].y
  550. );
  551. }
  552. if (this.pointList.length > 2 && len > 0 && len < this.scenceLen) {
  553. this.status = SItemStatus.Normal;
  554. //3 传递完成事件状态
  555. this.$emit("finishCreated");
  556. //1 grabItem 置为null
  557. this.releaseItem();
  558. } else {
  559. this.insertPoint(event.x, event.y);
  560. // 记录新增顶点操作记录压入堆栈
  561. let pos = new SPoint(event.x, event.y);
  562. this.recordAction(SGraphPointListInsert, [this.pointList, pos]);
  563. }
  564. } else if (this.status == SItemStatus.Edit) {
  565. // 对多边形数组做编辑操作
  566. this.editPolygonPoint(event);
  567. } else {
  568. return super.onMouseDown(event);
  569. }
  570. return true;
  571. } // Function onMouseDown()
  572. /**
  573. * 鼠标移入事件
  574. *
  575. * @param event 事件参数
  576. * @return 是否处理
  577. */
  578. onMouseEnter(event: SMouseEvent): boolean {
  579. return true;
  580. } // Function onMouseEnter()
  581. /**
  582. * 鼠标移出事件
  583. *
  584. * @param event 事件参数
  585. * @return 是否处理
  586. */
  587. onMouseLeave(event: SMouseEvent): boolean {
  588. return true;
  589. } // Function onMouseLeave()
  590. /**
  591. * 鼠标移动事件
  592. *
  593. * @param event 事件参数
  594. * @return 是否处理
  595. */
  596. onMouseMove(event: SMouseEvent): boolean {
  597. if (event.shiftKey) {
  598. event = this.compare(event);
  599. }
  600. if (this.status == SItemStatus.Create) {
  601. this.lastPoint = new SPoint();
  602. this.lastPoint.x = event.x;
  603. this.lastPoint.y = event.y;
  604. this.update();
  605. } else if (this.status == SItemStatus.Edit) {
  606. if (event.buttons == 1) {
  607. if (-1 != this.curIndex) {
  608. this.pointList[this.curIndex].x = event.x;
  609. this.pointList[this.curIndex].y = event.y;
  610. }
  611. }
  612. this.update();
  613. } else {
  614. return super.onMouseMove(event);
  615. }
  616. return true;
  617. } // Function onMouseMove()
  618. /**
  619. * shift 垂直水平创建或编辑
  620. *
  621. * @param event 事件
  622. * @return 处理后的事件
  623. */
  624. compare(event: SMouseEvent): SMouseEvent {
  625. if (this.pointList.length) {
  626. let last = new SPoint(event.x, event.y);
  627. if (this.status == SItemStatus.Create) {
  628. last = this.pointList[this.pointList.length - 1];
  629. } else if (this.status == SItemStatus.Edit) {
  630. if (this.curIndex > 0) {
  631. last = this.pointList[this.curIndex - 1];
  632. }
  633. }
  634. const dx = Math.abs(event.x - last.x);
  635. const dy = Math.abs(event.y - last.y);
  636. if (dy > dx) {
  637. event.x = last.x;
  638. } else {
  639. event.y = last.y;
  640. }
  641. }
  642. return event;
  643. } // Function compare()
  644. /**
  645. * 鼠标抬起事件
  646. *
  647. * @param event 事件参数
  648. * @return 是否处理
  649. */
  650. onMouseUp(event: SMouseEvent): boolean {
  651. if (this.status == SItemStatus.Edit) {
  652. if (-1 != this.curIndex) {
  653. const pos = new SPoint(
  654. this.pointList[this.curIndex].x,
  655. this.pointList[this.curIndex].y
  656. );
  657. this.recordAction(SGraphPointListUpdate, [
  658. this.pointList,
  659. this.curPoint,
  660. pos,
  661. this.curIndex
  662. ]);
  663. }
  664. } else if (this.status == SItemStatus.Normal) {
  665. this.moveToOrigin(this.x, this.y);
  666. return super.onMouseUp(event);
  667. }
  668. return true;
  669. } // Function onMouseUp()
  670. /**
  671. * 移动后处理所有坐标,并肩原点置为场景原点
  672. *
  673. * @param x x 坐标
  674. * @param y y 坐标
  675. */
  676. moveToOrigin(x: number, y: number): void {
  677. super.moveToOrigin(x, y);
  678. this.pointList = this.pointList.map(t => {
  679. t.x = t.x + x;
  680. t.y = t.y + y;
  681. return t;
  682. });
  683. this.x = 0;
  684. this.y = 0;
  685. } // Function moveToOrigin()
  686. /**
  687. * 适配事件
  688. *
  689. * @param event 事件参数
  690. * @return 是否处理
  691. */
  692. onResize(event: SMouseEvent): boolean {
  693. return true;
  694. } // Function onResize()
  695. /**
  696. * 取消操作
  697. */
  698. cancelOperate(): void {
  699. // 当状态为展示状态
  700. if (this.status == SItemStatus.Create) {
  701. // 闭合多边形
  702. this.parent = null;
  703. } else if (this.status == SItemStatus.Edit) {
  704. // 编辑状态
  705. this.status = SItemStatus.Normal;
  706. }
  707. this.update();
  708. } // Function cancelOperate()
  709. /**
  710. * Item 对象边界区域
  711. *
  712. * @return 边界区域
  713. */
  714. boundingRect(): SRect {
  715. if (this.pointList.length) {
  716. this.minX = this.pointList[0].x;
  717. this.maxX = this.pointList[0].x;
  718. this.minY = this.pointList[0].y;
  719. this.maxY = this.pointList[0].y;
  720. this.pointList.forEach(it => {
  721. let x = it.x,
  722. y = it.y;
  723. if (x < this.minX) {
  724. this.minX = x;
  725. }
  726. if (y < this.minY) {
  727. this.minY = y;
  728. }
  729. if (x > this.maxX) {
  730. this.maxX = x;
  731. }
  732. if (y > this.maxY) {
  733. this.maxY = y;
  734. }
  735. });
  736. }
  737. return new SRect(
  738. this.minX,
  739. this.minY,
  740. this.maxX - this.minX,
  741. this.maxY - this.minY
  742. );
  743. } // Function boundingRect()
  744. /**
  745. * 判断点是否在区域内
  746. *
  747. * @param x x 坐标
  748. * @param y y 坐标
  749. * @return 是否在区域内
  750. */
  751. contains(x: number, y: number): boolean {
  752. let arr = this.pointList;
  753. if (arr.length < 3 || !SPolygonUtil.pointIn(x, y, arr)) {
  754. return false;
  755. }
  756. return true;
  757. } // Function contains()
  758. /**
  759. * Item 绘制操作
  760. *
  761. * @param painter 绘制对象
  762. */
  763. onDraw(painter: SPainter): void {
  764. this.scenceLen = painter.toPx(this.len);
  765. // 当状态为展示状态
  766. if (this.status == SItemStatus.Normal) {
  767. // 闭合多边形
  768. this.drawShowPolygon(painter, this.pointList);
  769. } else if (this.status == SItemStatus.Create) {
  770. // 创建状态
  771. this.drawCreatePolygon(painter, this.pointList);
  772. } else {
  773. // 编辑状态
  774. this.drawEditPolygon(painter, this.pointList);
  775. }
  776. } // Function onDraw()
  777. } // Class SPolygonItem