SBaseArrowEdit.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 { SArrowStyleType, SColor, SLine, SPainter, SPoint, SRect } from "@persagy-web/draw";
  27. import { SMouseButton, SMouseEvent, SUndoStack } from "@persagy-web/base";
  28. import { SItemStatus } from "@persagy-web/big";;
  29. import {
  30. SLineStyle,
  31. SGraphItem, SGraphPointListInsert, SGraphPointListUpdate
  32. } from "@persagy-web/graph/";
  33. import { SGraphEdit } from "..";
  34. import { SMathUtil } from '@persagy-web/big/lib/utils/SMathUtil';
  35. import { Marker } from "./../type/Marker";
  36. /**
  37. * 折线编辑类
  38. *
  39. * @author 韩耀龙 <han_yao_long@163.com>
  40. */
  41. export class SBaseArrowEdit extends SGraphEdit {
  42. /** 编辑相关操作的数据 */
  43. data: Marker;
  44. /** undo/redo 堆栈 */
  45. private undoStack: SUndoStack = new SUndoStack();
  46. /** 是否绘制完成 */
  47. _status: SItemStatus = SItemStatus.Normal;
  48. get status(): SItemStatus {
  49. return this._status;
  50. } // Get status
  51. set status(v: SItemStatus) {
  52. this._status = v;
  53. if (this.undoStack) {
  54. this.undoStack.clear();
  55. }
  56. this.update();
  57. } // Set status
  58. /** 矩形两个对角 */
  59. private _line: SPoint[] = [];
  60. get line(): SPoint[] {
  61. return this._line;
  62. } // Get line
  63. set line(arr: SPoint[]) {
  64. this._line = arr;
  65. this.update();
  66. } // Get line
  67. /** 矩形左上角 */
  68. private _leftTop: SPoint = new SPoint();
  69. /** 矩形右下角 */
  70. private _rightBottom: SPoint = new SPoint();
  71. /** 拖动灵敏度 */
  72. dis: number = 5;
  73. /** 拖动灵敏度 */
  74. private sceneDis: number = 5;
  75. /** 是否垂直水平绘制 */
  76. private _verAndLeve: Boolean = false;
  77. get verAndLeve(): Boolean {
  78. return this._verAndLeve;
  79. } // Get verAndLeve
  80. set verAndLeve(bool: Boolean) {
  81. this._verAndLeve = bool;
  82. this.update();
  83. } // Set verAndLeve
  84. /** 当前点索引 */
  85. curIndex: number = -1;
  86. /** 当前点坐标 */
  87. private curPoint: SPoint | null = null;
  88. /** 线条颜色 */
  89. _strokeColor: SColor = SColor.Black;
  90. get strokeColor(): SColor {
  91. return this._strokeColor;
  92. } // Get strokeColor
  93. set strokeColor(v: SColor) {
  94. this._strokeColor = v;
  95. this.update();
  96. } // Set strokeColor
  97. /** 填充色 */
  98. _fillColor: SColor = new SColor("#2196f3");
  99. get fillColor(): SColor {
  100. return this._fillColor;
  101. } // Get fillColor
  102. set fillColor(v: SColor) {
  103. this._fillColor = v;
  104. this.update();
  105. } // Set fillColor
  106. /** 选中端点填充色 */
  107. private _activeFillColor: SColor = new SColor("#2196f3");
  108. get activeFillColor(): SColor {
  109. return this._activeFillColor;
  110. } // Get activeFillColor
  111. set activeFillColor(v: SColor) {
  112. this._activeFillColor = v;
  113. this.update();
  114. } // Set activeFillColor
  115. /** 边框样式 */
  116. _lineStyle: SLineStyle = SLineStyle.Solid;
  117. get lineStyle(): SLineStyle {
  118. return this._lineStyle;
  119. } // Get lineStyle
  120. set lineStyle(v: SLineStyle) {
  121. this._lineStyle = v;
  122. this.update();
  123. } // Set lineStyle
  124. /** 线条宽度 */
  125. _lineWidth: number = 1;
  126. get lineWidth(): number {
  127. return this._lineWidth;
  128. } // Get lineWidth
  129. set lineWidth(v: number) {
  130. this._lineWidth = v;
  131. this.update();
  132. } // Set lineWidth
  133. /**
  134. * 构造函数
  135. *
  136. * @param parent 指向父对象
  137. * @param data 数据
  138. */
  139. constructor(parent: SGraphItem | null, data: Marker) {
  140. super(parent);
  141. this.showSelect = false;
  142. this.data = data;
  143. if (data.style && data.style.default) {
  144. // 关于顶点
  145. if (data.style.default.line) {
  146. let setPointList: SPoint[];
  147. setPointList = data.style.default.line.map(i => {
  148. return new SPoint(i.x, i.y)
  149. });
  150. this.line = setPointList;
  151. }
  152. // 颜色
  153. if (data.style.default.strokeColor) {
  154. this.strokeColor = new SColor(data.style.default.strokeColor)
  155. }
  156. // 线宽
  157. if (data.style.default.LineWidth) {
  158. this.lineWidth = data.style.default.lineWidth
  159. }
  160. // 线样式
  161. if (data.style.default.LineStyle) {
  162. this.lineStyle = data.style.default.lineStyle
  163. }
  164. }
  165. } // Constructor
  166. /**
  167. * 鼠标双击事件
  168. *
  169. * @param event 事件参数
  170. * @return 是否处理该事件
  171. */
  172. onDoubleClick(event: SMouseEvent): boolean {
  173. if (this.status == SItemStatus.Normal) {
  174. this.status = SItemStatus.Edit;
  175. this.grabItem(this);
  176. } else if (this.status == SItemStatus.Edit) {
  177. this.status = SItemStatus.Normal;
  178. this.releaseItem();
  179. }
  180. this.update();
  181. return true;
  182. } // Function onDoubleClick()
  183. /**
  184. * 鼠标按下事件
  185. *
  186. * @param event 鼠标事件
  187. * @return 是否处理该事件
  188. */
  189. onMouseDown(event: SMouseEvent): boolean {
  190. this.curIndex = -1;
  191. this.curPoint = null;
  192. if (event.shiftKey || this._verAndLeve) {
  193. event = this.compare(event);
  194. }
  195. if (event.buttons == SMouseButton.LeftButton) {
  196. if (this.status == SItemStatus.Normal) {
  197. return super.onMouseDown(event);
  198. } else if (this.status == SItemStatus.Edit) {
  199. // 判断是否点击到端点上(获取端点索引值)
  200. this.findNearestPoint(new SPoint(event.x, event.y));
  201. } else if (this.status == SItemStatus.Create) {
  202. this.addPoint(new SPoint(event.x, event.y));
  203. return true;
  204. }
  205. }
  206. return true;
  207. } // Function onMouseDown()
  208. /**
  209. * shift 垂直水平创建或编辑
  210. *
  211. * @param event 事件
  212. * @return 返回鼠标事件
  213. */
  214. compare(event: SMouseEvent): SMouseEvent {
  215. if (this.line.length) {
  216. let last = new SPoint(event.x, event.y);
  217. if (this.status == SItemStatus.Create) {
  218. last = this.line[0];
  219. } else if (this.status == SItemStatus.Edit) {
  220. if (this.curIndex == 1) {
  221. last = this.line[0];
  222. } else if (this.curIndex == 0) {
  223. last = this.line[1];
  224. }
  225. }
  226. const dx = Math.abs(event.x - last.x);
  227. const dy = Math.abs(event.y - last.y);
  228. if (dy > dx) {
  229. event.x = last.x;
  230. } else {
  231. event.y = last.y;
  232. }
  233. }
  234. return event;
  235. } // Function compare()
  236. /**
  237. * 获取点击点与 Point[] 中的点距离最近点
  238. *
  239. * @param p 鼠标点击点
  240. */
  241. findNearestPoint(p: SPoint): void {
  242. let len = this.sceneDis;
  243. for (let i = 0; i < this.line.length; i++) {
  244. let dis = SMathUtil.pointDistance(
  245. p.x,
  246. p.y,
  247. this.line[i].x,
  248. this.line[i].y
  249. );
  250. if (dis < len) {
  251. len = dis;
  252. this.curIndex = i;
  253. this.curPoint = new SPoint(this.line[this.curIndex]);
  254. }
  255. }
  256. } // Function findNearestPoint()
  257. /**
  258. * 鼠标移动事件
  259. *
  260. * @param event 鼠标事件
  261. * @return 是否处理事件
  262. */
  263. onMouseMove(event: SMouseEvent): boolean {
  264. if (event.shiftKey || this._verAndLeve) {
  265. event = this.compare(event);
  266. }
  267. if (this.status == SItemStatus.Create) {
  268. if (this.line[0] instanceof SPoint) {
  269. this.line[1] = new SPoint(event.x, event.y);
  270. this.calRect()
  271. }
  272. } else if (this.status == SItemStatus.Edit) {
  273. if (-1 != this.curIndex) {
  274. this.line[this.curIndex].x = event.x;
  275. this.line[this.curIndex].y = event.y;
  276. this.calRect()
  277. }
  278. } else {
  279. return super.onMouseMove(event);
  280. }
  281. this.update();
  282. return true;
  283. } // Function onMouseMove()
  284. /**
  285. * 鼠标抬起事件
  286. *
  287. * @param event 事件参数
  288. * @return 是否处理该事件
  289. */
  290. onMouseUp(event: SMouseEvent): boolean {
  291. if (this.status == SItemStatus.Edit) {
  292. if (this.curIndex > -1) {
  293. const p = new SPoint(
  294. this.line[this.curIndex].x,
  295. this.line[this.curIndex].y
  296. );
  297. this.recordAction(SGraphPointListUpdate, [
  298. this.line,
  299. this.curPoint,
  300. p,
  301. this.curIndex
  302. ]);
  303. }
  304. } else if (this.status == SItemStatus.Normal) {
  305. super.onMouseUp(event)
  306. return true;
  307. }
  308. this.curIndex = -1;
  309. this.curPoint = null;
  310. return true;
  311. } // Function onMouseUp()
  312. /**
  313. * 计算矩形的左上角和右下角
  314. */
  315. private calRect(): void {
  316. if (this.line.length > 1) {
  317. const fi = this.line[0];
  318. const se = this.line[1];
  319. let minx, maxx, miny, maxy;
  320. if (fi.x < se.x) {
  321. minx = fi.x;
  322. maxx = se.x;
  323. } else {
  324. minx = se.x;
  325. maxx = fi.x;
  326. }
  327. if (fi.y < se.y) {
  328. miny = fi.y;
  329. maxy = se.y;
  330. } else {
  331. miny = se.y;
  332. maxy = fi.y;
  333. }
  334. this._leftTop = new SPoint(minx, miny)
  335. this._rightBottom = new SPoint(maxx, maxy)
  336. }
  337. } // Function calRect
  338. /**
  339. * 添加点至数组中
  340. *
  341. * @param p 添加的点
  342. */
  343. private addPoint(p: SPoint): void {
  344. if (this.line.length < 2) {
  345. this.line.push(p);
  346. this.recordAction(SGraphPointListInsert, [this.line, p]);
  347. } else {
  348. this.line[1] = p;
  349. this.recordAction(SGraphPointListInsert, [this.line, p, 1]);
  350. this.status = SItemStatus.Normal;
  351. this.releaseItem();
  352. this.$emit("finishCreated");
  353. }
  354. this.calRect()
  355. this.update();
  356. } // Function addPoint()
  357. /**
  358. * 移动后处理所有坐标,并肩原点置为场景原点
  359. */
  360. moveToOrigin(): void {
  361. this.line = this.line.map(t => {
  362. t.x = t.x + this.x;
  363. t.y = t.y + this.y;
  364. return t;
  365. });
  366. this.x = 0;
  367. this.y = 0;
  368. } // Function moveToOrigin()
  369. /**
  370. * 记录相关动作并推入栈中
  371. *
  372. * @param SGraphCommand 相关命令类
  373. * @param any 对应传入参数
  374. */
  375. protected recordAction(SGraphCommand: any, any: any[]): void {
  376. // 记录相关命令并推入堆栈中
  377. const command = new SGraphCommand(this.scene, this, ...any);
  378. this.undoStack.push(command);
  379. } // Function recordAction()
  380. /**
  381. * 判断点是否在区域内
  382. *
  383. * @param x x 横向坐标
  384. * @param y y 纵向坐标
  385. * @return 在区域内返回 true, 否则 false
  386. */
  387. contains(x: number, y: number): boolean {
  388. if (this.line.length == 2) {
  389. let p = new SPoint(x, y);
  390. if (
  391. SMathUtil.pointToLine(p, new SLine(this.line[0], this.line[1]))
  392. .MinDis < this.dis
  393. ) {
  394. return true;
  395. }
  396. }
  397. return false;
  398. } // Function contains()
  399. /**
  400. * Item 对象边界区域
  401. *
  402. * @return 外接矩阵
  403. */
  404. boundingRect(): SRect {
  405. if (this.line.length > 1) {
  406. this.calRect()
  407. return new SRect(this._leftTop, this._rightBottom);
  408. }
  409. return new SRect()
  410. } // Function boundingRect()
  411. /**
  412. * 撤销操作
  413. */
  414. undo(): void {
  415. if (this._status != SItemStatus.Normal) {
  416. this.undoStack.undo();
  417. }
  418. } // Function undo()
  419. /**
  420. * 重做操作
  421. */
  422. redo(): void {
  423. if (this._status != SItemStatus.Normal) {
  424. this.undoStack.redo();
  425. }
  426. } // Function redo()
  427. /**
  428. * 取消操作执行
  429. */
  430. cancelOperate(): void {
  431. if (this.status == SItemStatus.Create) {
  432. this.parent = null;
  433. this.releaseItem();
  434. } else if (this.status == SItemStatus.Edit) {
  435. this.status = SItemStatus.Normal;
  436. this.releaseItem();
  437. }
  438. } // Function cancelOperate()
  439. /**
  440. * Item绘制操作
  441. *
  442. * @param painter 绘制对象
  443. */
  444. onDraw(painter: SPainter): void {
  445. painter.pen.color = this.strokeColor;
  446. if (this.line.length == 2) {
  447. painter.brush.color = this.fillColor;
  448. painter.pen.lineWidth = this.lineWidth
  449. if (this.lineStyle == SLineStyle.Dashed) {
  450. painter.pen.lineDash = [
  451. painter.toPx(this.lineWidth * 3),
  452. painter.toPx(this.lineWidth * 7)
  453. ];
  454. } else if (this.lineStyle == SLineStyle.Dotted) {
  455. painter.pen.lineDash = [
  456. painter.toPx(this.lineWidth * 2),
  457. painter.toPx(this.lineWidth * 2)
  458. ];
  459. }
  460. if (this.selected && this.status == SItemStatus.Normal) {
  461. painter.pen.lineWidth = painter.toPx(this.lineWidth * 2);
  462. painter.shadow.shadowBlur = 10;
  463. painter.shadow.shadowColor = new SColor(`#00000033`);
  464. painter.shadow.shadowOffsetX = 5;
  465. painter.shadow.shadowOffsetY = 5;
  466. }
  467. painter.drawArrowLine(this.line[0], this.line[1], { begin: SArrowStyleType.None, end: SArrowStyleType.Basic });
  468. if (
  469. this.status == SItemStatus.Edit ||
  470. this.status == SItemStatus.Create
  471. ) {
  472. // 绘制端点
  473. this.line.forEach((p, i): void => {
  474. painter.brush.color = this.fillColor;
  475. if (i == this.curIndex) {
  476. painter.brush.color = this.activeFillColor;
  477. }
  478. painter.drawCircle(p.x, p.y, painter.toPx(5));
  479. });
  480. }
  481. } else if (this.line.length == 1) {
  482. if (
  483. this.status == SItemStatus.Edit ||
  484. this.status == SItemStatus.Create
  485. ) {
  486. // 绘制端点
  487. painter.brush.color = this.fillColor;
  488. painter.drawCircle(
  489. this.line[0].x,
  490. this.line[0].y,
  491. painter.toPx(5)
  492. );
  493. }
  494. }
  495. } // Function onDraw()
  496. /**
  497. * 返回对象储存的相关数据
  498. *
  499. * @return 实体类保存得数据
  500. */
  501. toData(): any {
  502. this.moveToOrigin()
  503. const Line = [{ x: this.line[0].x, y: this.line[0].y }, { x: this.line[1].x, y: this.line[1].y }];
  504. this.data.style.default.line = Line;
  505. this.data.style.default.lineWidth = this.lineWidth;
  506. this.data.style.default.lineStyle = this.lineStyle;
  507. this.data.style.default.strokeColor = this.strokeColor;
  508. return this.data
  509. } // Function toData()
  510. } // Class SArrowEdit