SPolylineItem.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. import { SColor, SLine, SPainter, SPoint, SRect } from "@saga-web/draw";
  2. import { SKeyCode, SMouseEvent, SUndoStack } from "@saga-web/base";
  3. import { SItemStatus } from "..";
  4. import { SMathUtil } from "../utils/SMathUtil";
  5. import {
  6. SGraphItem,
  7. SGraphPointListDelete,
  8. SGraphPointListInsert,
  9. SGraphPointListUpdate
  10. } from "@saga-web/graph/lib";
  11. /**
  12. * 直线item
  13. *
  14. * */
  15. export class SPolylineItem extends SGraphItem {
  16. /** X坐标最小值 */
  17. private minX = Number.MAX_SAFE_INTEGER;
  18. /** X坐标最大值 */
  19. private maxX = Number.MIN_SAFE_INTEGER;
  20. /** Y坐标最小值 */
  21. private minY = Number.MAX_SAFE_INTEGER;
  22. /** Y坐标最大值 */
  23. private maxY = Number.MIN_SAFE_INTEGER;
  24. /** 折点信息 */
  25. pointList: SPoint[] = [];
  26. /** 是否绘制完成 */
  27. _status: SItemStatus = SItemStatus.Normal;
  28. get status(): SItemStatus {
  29. return this._status;
  30. }
  31. set status(v: SItemStatus) {
  32. this._status = v;
  33. this.update();
  34. }
  35. /** 鼠标移动时的点 */
  36. private lastPoint: SPoint | null = null;
  37. /** 线条颜色 */
  38. _strokeColor: SColor = SColor.Black;
  39. get strokeColor(): SColor {
  40. return this._strokeColor;
  41. }
  42. set strokeColor(v: SColor) {
  43. this._strokeColor = v;
  44. this.update();
  45. }
  46. /** 填充色 */
  47. _fillColor: SColor = new SColor("#2196f3");
  48. get fillColor(): SColor {
  49. return this._fillColor;
  50. }
  51. set fillColor(v: SColor) {
  52. this._fillColor = v;
  53. this.update();
  54. }
  55. /** 线条宽度 */
  56. _lineWidth: number = 1;
  57. get lineWidth(): number {
  58. return this._lineWidth;
  59. }
  60. set lineWidth(v: number) {
  61. this._lineWidth = v;
  62. this.update();
  63. }
  64. /** 全局灵敏度 */
  65. dis: number = 10;
  66. /** 灵敏度转换为场景长度 */
  67. private sceneDis: number = 10;
  68. /** 当前点索引 */
  69. private curIndex: number = -1;
  70. /** 当前点索引 */
  71. private curPoint: SPoint | null = null;
  72. /** undo/redo堆栈 */
  73. private undoStack: SUndoStack | null = new SUndoStack();
  74. /**
  75. * 构造函数
  76. *
  77. * @param parent 父级
  78. * @param list 坐标集合
  79. * */
  80. constructor(parent: null | SGraphItem, list: SPoint[]);
  81. /**
  82. * 构造函数
  83. *
  84. * @param parent 父级
  85. * @param list 第一个坐标
  86. * */
  87. constructor(parent: null | SGraphItem, list: SPoint);
  88. /**
  89. * 构造函数
  90. *
  91. * @param parent 父级
  92. * @param list 第一个坐标|坐标集合
  93. * */
  94. constructor(parent: null | SGraphItem, list: SPoint | SPoint[]) {
  95. super(parent);
  96. if (list instanceof SPoint) {
  97. this.pointList.push(list);
  98. } else {
  99. this.pointList = list;
  100. }
  101. } // Constructor
  102. /**
  103. * 添加点至数组中
  104. *
  105. * @param p 添加的点
  106. * @param index 添加到的索引
  107. * */
  108. private addPoint(p: SPoint, index?: number): void {
  109. if (index && this.canHandle(index)) {
  110. this.pointList.splice(index, 0, p);
  111. this.recordAction(SGraphPointListInsert, [
  112. this.pointList,
  113. p,
  114. index
  115. ]);
  116. } else {
  117. this.pointList.push(p);
  118. this.recordAction(SGraphPointListInsert, [this.pointList, p]);
  119. }
  120. this.update();
  121. } // Function addPoint()
  122. /**
  123. * 是否可以添加点到数组中
  124. *
  125. * @param index 要添加到的索引
  126. * @return boolean 是否可添加
  127. * */
  128. private canHandle(index: number): boolean {
  129. return index >= 0 && index <= this.pointList.length;
  130. } // Function canHandle()
  131. /**
  132. * 根据索引删除点
  133. *
  134. * @param index 删除点
  135. * */
  136. deletePoint(index: number): void {
  137. if (this.canHandle(index) && this.pointList.length > 2) {
  138. const p = new SPoint(
  139. this.pointList[this.curIndex].x,
  140. this.pointList[this.curIndex].y
  141. );
  142. this.pointList.splice(index, 1);
  143. this.recordAction(SGraphPointListDelete, [
  144. this.pointList,
  145. p,
  146. index
  147. ]);
  148. this.curIndex = -1;
  149. this.curPoint = null;
  150. this.update();
  151. }
  152. } // Function deletePoint
  153. /**
  154. * 鼠标按下事件
  155. *
  156. * @param event 鼠标事件
  157. * @return boolean 是否处理事件
  158. * */
  159. onMouseDown(event: SMouseEvent): boolean {
  160. this.curIndex = -1;
  161. this.curPoint = null;
  162. if (event.buttons == 1) {
  163. if (this.status == SItemStatus.Create) {
  164. this.addPoint(new SPoint(event.x, event.y));
  165. return true;
  166. } else if (this.status == SItemStatus.Edit) {
  167. // 查询鼠标最近的索引
  168. this.findNearestPoint(new SPoint(event.x, event.y));
  169. // 增加点
  170. if (this.curIndex < 0) {
  171. this.findAddPos(new SPoint(event.x, event.y));
  172. }
  173. // 删除点
  174. if (event.altKey && this.canHandle(this.curIndex)) {
  175. this.deletePoint(this.curIndex);
  176. }
  177. this.update();
  178. return true;
  179. } else {
  180. return super.onMouseDown(event);
  181. }
  182. }
  183. return super.onMouseDown(event);
  184. } // Function onMouseDown()
  185. /**
  186. * 鼠标移动事件
  187. *
  188. * @param event 鼠标事件
  189. * @return boolean 是否处理事件
  190. * */
  191. onMouseMove(event: SMouseEvent): boolean {
  192. if (this.status == SItemStatus.Create) {
  193. if (this.lastPoint) {
  194. this.lastPoint.x = event.x;
  195. this.lastPoint.y = event.y;
  196. } else {
  197. this.lastPoint = new SPoint(event.x, event.y);
  198. }
  199. this.update();
  200. return true;
  201. } else if (this.status == SItemStatus.Edit) {
  202. if (event.buttons == 1) {
  203. if (this.canHandle(this.curIndex)) {
  204. this.pointList[this.curIndex].x = event.x;
  205. this.pointList[this.curIndex].y = event.y;
  206. }
  207. }
  208. this.update();
  209. return true;
  210. } else {
  211. return super.onMouseMove(event);
  212. }
  213. } // Function onMouseMove()
  214. /**
  215. * 鼠标移动事件
  216. *
  217. * @param event 鼠标事件
  218. * @return boolean 是否处理事件
  219. * */
  220. onMouseUp(event: SMouseEvent): boolean {
  221. if (this.status == SItemStatus.Edit) {
  222. if (this.curIndex > -1) {
  223. const p = new SPoint(
  224. this.pointList[this.curIndex].x,
  225. this.pointList[this.curIndex].y
  226. );
  227. this.recordAction(SGraphPointListUpdate, [
  228. this.pointList,
  229. this.curPoint,
  230. p,
  231. this.curIndex
  232. ]);
  233. }
  234. } else if (this.status == SItemStatus.Normal) {
  235. this.moveToOrigin(this.x, this.y);
  236. return super.onMouseUp(event);
  237. }
  238. return true;
  239. } // Function onMouseUp()
  240. /**
  241. * 鼠标双击事件
  242. *
  243. * @param event 事件参数
  244. * @return boolean
  245. */
  246. onDoubleClick(event: SMouseEvent): boolean {
  247. if (this.status == SItemStatus.Normal) {
  248. this.status = SItemStatus.Edit;
  249. this.grabItem(this);
  250. } else if (this.status == SItemStatus.Edit) {
  251. this.status = SItemStatus.Normal;
  252. this.releaseItem();
  253. } else if (this.status == SItemStatus.Create) {
  254. this.status = SItemStatus.Normal;
  255. this.releaseItem();
  256. this.$emit("finishCreated");
  257. }
  258. this.$emit("onDoubleClick", event);
  259. return true;
  260. } // Function onDoubleClick()
  261. /***
  262. * 键盘按键弹起事件
  263. *
  264. * @param event 事件参数
  265. */
  266. onKeyUp(event: KeyboardEvent): void {
  267. if (event.keyCode == SKeyCode.Enter) {
  268. this.status = SItemStatus.Normal;
  269. this.releaseItem();
  270. this.$emit("finishCreated");
  271. }
  272. // delete删除点
  273. if (
  274. event.keyCode == SKeyCode.Delete &&
  275. this.status == SItemStatus.Edit
  276. ) {
  277. this.deletePoint(this.curIndex);
  278. }
  279. } // Function onKeyUp()
  280. /**
  281. * 移动后处理所有坐标,并肩原点置为场景原点
  282. *
  283. * @param x x坐标
  284. * @param y y坐标
  285. * */
  286. moveToOrigin(x: number, y: number): void {
  287. super.moveToOrigin(x, y);
  288. this.pointList = this.pointList.map(t => {
  289. t.x = t.x + x;
  290. t.y = t.y + y;
  291. return t;
  292. });
  293. this.x = 0;
  294. this.y = 0;
  295. } // Function moveToOrigin()
  296. /**
  297. * 获取点击点与点集中距离最近点
  298. *
  299. * @param p 鼠标点击点
  300. * */
  301. findNearestPoint(p: SPoint): void {
  302. let len = this.sceneDis;
  303. for (let i = 0; i < this.pointList.length; i++) {
  304. let dis = SMathUtil.pointDistance(
  305. p.x,
  306. p.y,
  307. this.pointList[i].x,
  308. this.pointList[i].y
  309. );
  310. if (dis < len) {
  311. len = dis;
  312. this.curIndex = i;
  313. this.curPoint = new SPoint(
  314. this.pointList[this.curIndex].x,
  315. this.pointList[this.curIndex].y
  316. );
  317. }
  318. }
  319. } // Function findNearestPoint()
  320. /**
  321. * 计算增加点的位置
  322. *
  323. * @param p 鼠标点击点
  324. * */
  325. findAddPos(p: SPoint): void {
  326. let len = SMathUtil.pointToLine(
  327. p,
  328. new SLine(this.pointList[0], this.pointList[1])
  329. ),
  330. index = 0;
  331. if (this.pointList.length > 2) {
  332. for (let i = 1; i < this.pointList.length - 1; i++) {
  333. let dis = SMathUtil.pointToLine(
  334. p,
  335. new SLine(this.pointList[i], this.pointList[i + 1])
  336. );
  337. if (dis.MinDis < len.MinDis) {
  338. len = dis;
  339. index = i;
  340. }
  341. }
  342. }
  343. if (len.MinDis < this.sceneDis) {
  344. if (len.Point) {
  345. this.addPoint(len.Point, index + 1);
  346. }
  347. }
  348. } // Function findAddPos()
  349. /**
  350. * 记录相关动作并推入栈中
  351. *
  352. * @param SGraphCommand 相关命令类
  353. * @param any 对应传入参数
  354. */
  355. protected recordAction(SGraphCommand: any, any: any[]): void {
  356. // 记录相关命令并推入堆栈中
  357. const command = new SGraphCommand(this, ...any);
  358. if (this.undoStack) {
  359. this.undoStack.push(command);
  360. }
  361. } // Function recordAction()
  362. /**
  363. * Item对象边界区域
  364. *
  365. * @return SRect 外接矩阵
  366. * */
  367. boundingRect(): SRect {
  368. if (this.pointList.length) {
  369. this.minX = this.pointList[0].x;
  370. this.maxX = this.pointList[0].x;
  371. this.minY = this.pointList[0].y;
  372. this.maxY = this.pointList[0].y;
  373. this.pointList.forEach(it => {
  374. let x = it.x,
  375. y = it.y;
  376. if (x < this.minX) {
  377. this.minX = x;
  378. }
  379. if (y < this.minY) {
  380. this.minY = y;
  381. }
  382. if (x > this.maxX) {
  383. this.maxX = x;
  384. }
  385. if (y > this.maxY) {
  386. this.maxY = y;
  387. }
  388. });
  389. }
  390. return new SRect(
  391. this.minX,
  392. this.minY,
  393. this.maxX - this.minX,
  394. this.maxY - this.minY
  395. );
  396. } // Function boundingRect()
  397. /**
  398. * 判断点是否在区域内
  399. *
  400. * @param x
  401. * @param y
  402. * @return true-是
  403. */
  404. contains(x: number, y: number): boolean {
  405. let p = new SPoint(x, y);
  406. for (let i = 1; i < this.pointList.length; i++) {
  407. let PTL = SMathUtil.pointToLine(
  408. p,
  409. new SLine(
  410. this.pointList[i - 1].x,
  411. this.pointList[i - 1].y,
  412. this.pointList[i].x,
  413. this.pointList[i].y
  414. )
  415. );
  416. if (PTL.MinDis < this.sceneDis) {
  417. return true;
  418. }
  419. }
  420. return false;
  421. } // Function contains()
  422. /**
  423. * 撤销操作
  424. *
  425. */
  426. undo(): void {
  427. if (this._status != SItemStatus.Normal) {
  428. if (this.undoStack) {
  429. this.undoStack.undo();
  430. }
  431. }
  432. } // Function undo()
  433. /**
  434. * 重做操作
  435. *
  436. */
  437. redo(): void {
  438. if (this._status != SItemStatus.Normal) {
  439. if (this.undoStack) {
  440. this.undoStack.redo();
  441. }
  442. }
  443. } // Function redo()
  444. /**
  445. * 取消操作执行
  446. *
  447. * */
  448. cancelOperate(): void {
  449. if (this.status == SItemStatus.Create) {
  450. this.parent = null;
  451. this.releaseItem();
  452. } else if (this.status == SItemStatus.Edit) {
  453. this.status = SItemStatus.Normal;
  454. this.releaseItem();
  455. }
  456. } // Function cancelOperate()
  457. /**
  458. * 绘制基本图形
  459. * */
  460. drawBaseLine(painter: SPainter): void {
  461. // 绘制基本图形
  462. painter.pen.color = this.strokeColor;
  463. painter.drawPolyline(this.pointList);
  464. } // Function drawBaseLine()
  465. /**
  466. * Item绘制操作
  467. *
  468. * @param painter painter对象
  469. */
  470. onDraw(painter: SPainter): void {
  471. // 缓存场景长度
  472. this.sceneDis = painter.toPx(this.dis);
  473. // 创建状态
  474. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  475. if (this.status == SItemStatus.Create && this.lastPoint) {
  476. // 绘制基本图形
  477. this.drawBaseLine(painter);
  478. painter.drawLine(
  479. this.pointList[this.pointList.length - 1],
  480. this.lastPoint
  481. );
  482. // 编辑状态
  483. this.pointList.forEach((t, i): void => {
  484. painter.brush.color = SColor.White;
  485. if (i == this.curIndex) {
  486. painter.brush.color = this.fillColor;
  487. }
  488. painter.drawCircle(t.x, t.y, painter.toPx(5));
  489. });
  490. } else if (this.status == SItemStatus.Edit) {
  491. // 绘制基本图形
  492. this.drawBaseLine(painter);
  493. // 编辑状态
  494. this.pointList.forEach((t, i): void => {
  495. painter.brush.color = SColor.White;
  496. if (i == this.curIndex) {
  497. painter.brush.color = this.fillColor;
  498. }
  499. painter.drawCircle(t.x, t.y, painter.toPx(5));
  500. });
  501. } else {
  502. // 查看状态
  503. if (this.selected) {
  504. painter.pen.lineWidth = painter.toPx(this.lineWidth * 2);
  505. painter.shadow.shadowBlur = 10;
  506. painter.shadow.shadowColor = new SColor(`#00000060`);
  507. painter.shadow.shadowOffsetX = 5;
  508. painter.shadow.shadowOffsetY = 5;
  509. }
  510. // 绘制基本图形
  511. this.drawBaseLine(painter);
  512. }
  513. } // Function onDraw()
  514. } // Class SPolylineItem