SArrowPoly.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 { SLineItem } from "./SLineItem";
  27. import { SColor, SLine, SPainter, SPoint } from "@persagy-web/draw";
  28. import { SMathUtil } from "@persagy-web/graph/lib/utils/SMathUtil";
  29. import { SItemStatus } from "..";
  30. /**
  31. * 箭头多边形类型
  32. *
  33. * @author 郝建龙 <haojianlong@sagacloud.cn>
  34. */
  35. export class SArrowPoly extends SLineItem {
  36. /** 多边形数组 */
  37. pointList: SPoint[] = [];
  38. /** 绘制时需要旋转的角度 */
  39. private _ang: number = 0;
  40. /** 箭头中间粗细 */
  41. arrowWidth: number = 8;
  42. /** 是否为单向箭头 是-只绘制末端箭头 */
  43. _isSingle: boolean = true;
  44. get isSingle(): boolean {
  45. return this._isSingle;
  46. } // Get isSingle
  47. set isSingle(v: boolean) {
  48. this._isSingle = v;
  49. this.update();
  50. } // Set isSingle
  51. /** 多边形填充色 */
  52. private _polyFillColor: SColor = SColor.White;
  53. get polyFillColor(): SColor {
  54. return this._polyFillColor;
  55. } // Get polyFillColor
  56. set polyFillColor(v: SColor) {
  57. this._polyFillColor = v;
  58. this.update();
  59. } // Set polyFillColor
  60. /**
  61. * 根据两个顶点生成多边形数组
  62. */
  63. pointChange(): void {
  64. if (this.line.length > 1) {
  65. const line = new SLine(this.line[0], this.line[1]);
  66. const dis = SMathUtil.pointDistance(
  67. this.line[0].x,
  68. this.line[0].y,
  69. this.line[1].x,
  70. this.line[1].y
  71. );
  72. if (line.dx != 0) {
  73. const tempFo = Math.atan(line.dy / line.dx);
  74. this._ang = line.dx > 0 ? tempFo : tempFo + Math.PI;
  75. } else {
  76. this._ang = line.dy > 0 ? Math.PI / 2 : (3 * Math.PI) / 2;
  77. }
  78. if (this.isSingle) {
  79. this.pointList = [
  80. new SPoint(0, this.arrowWidth / 2),
  81. new SPoint(dis - this.arrowWidth, this.arrowWidth / 2),
  82. new SPoint(dis - this.arrowWidth, this.arrowWidth),
  83. new SPoint(dis, 0),
  84. new SPoint(dis - this.arrowWidth, -this.arrowWidth),
  85. new SPoint(dis - this.arrowWidth, -this.arrowWidth / 2),
  86. new SPoint(0, -this.arrowWidth / 2)
  87. ];
  88. } else {
  89. this.pointList = [
  90. new SPoint(0, 0),
  91. new SPoint(this.arrowWidth, this.arrowWidth),
  92. new SPoint(this.arrowWidth, this.arrowWidth / 2),
  93. new SPoint(dis - this.arrowWidth, this.arrowWidth / 2),
  94. new SPoint(dis - this.arrowWidth, this.arrowWidth),
  95. new SPoint(dis, 0),
  96. new SPoint(dis - this.arrowWidth, -this.arrowWidth),
  97. new SPoint(dis - this.arrowWidth, -this.arrowWidth / 2),
  98. new SPoint(this.arrowWidth, -this.arrowWidth / 2),
  99. new SPoint(this.arrowWidth, -this.arrowWidth)
  100. ];
  101. }
  102. }
  103. } // Function pointChange()
  104. /**
  105. * 绘制
  106. *
  107. * @param painter 绘制对象
  108. */
  109. onDraw(painter: SPainter): void {
  110. if (this.line.length) {
  111. painter.save();
  112. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  113. painter.translate(this.line[0].x, this.line[0].y);
  114. painter.rotate((this._ang * 180) / Math.PI);
  115. painter.pen.color = this.strokeColor;
  116. painter.brush.color = this.polyFillColor;
  117. painter.drawPolygon(this.pointList);
  118. painter.restore();
  119. if (
  120. this.status == SItemStatus.Edit ||
  121. this.status == SItemStatus.Create
  122. ) {
  123. this.line.forEach((p, i) => {
  124. painter.brush.color = this.fillColor;
  125. if (i == this.curIndex) {
  126. painter.brush.color = this.activeFillColor;
  127. }
  128. painter.drawCircle(p.x, p.y, painter.toPx(5));
  129. });
  130. }
  131. }
  132. } // Function onDraw()
  133. } // Class SArrowPoly