SDoorItem.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { Door } from "../../types/floor/Door";
  27. import { SPainter, SPoint, SRect } from "@persagy-web/draw";
  28. import { SMathUtil } from "../../utils/SMathUtil";
  29. import { ItemOrder, ItemColor } from "../..";
  30. import { SGraphItem } from "@persagy-web/graph";
  31. /**
  32. * 门 item
  33. *
  34. * @author 郝建龙 <haojianlong@sagacloud.cn>
  35. */
  36. export class SDoorItem extends SGraphItem {
  37. /** 门数据 */
  38. data: Door;
  39. /** 门轮廓线坐标 list */
  40. private readonly pointArr: SPoint[] = [];
  41. /** 门长度 */
  42. private readonly r: number = 0;
  43. /** 角度 */
  44. private readonly ang: number = 0;
  45. /** 旋转点 */
  46. private readonly p: SPoint = new SPoint(0, 0);
  47. /** 旋转起始角度,结束角度 + Math.PI / 2 */
  48. private readonly startAng: number = -Math.PI / 2;
  49. /** X 坐标最小值 */
  50. private minX = Number.MAX_SAFE_INTEGER;
  51. /** X 坐标最大值 */
  52. private maxX = Number.MIN_SAFE_INTEGER;
  53. /** Y 坐标最小值 */
  54. private minY = Number.MAX_SAFE_INTEGER;
  55. /** Y 坐标最大值 */
  56. private maxY = Number.MIN_SAFE_INTEGER;
  57. /**
  58. * 构造函数
  59. *
  60. * @param parent 指向父对象
  61. * @param data 门数据
  62. */
  63. constructor(parent: SGraphItem | null, data: Door) {
  64. super(parent);
  65. this.data = data;
  66. this.zOrder = ItemOrder.doorOrder;
  67. if (this.data.OutLine.length) {
  68. this.pointArr = this.data.OutLine[0].map(
  69. (t): SPoint => {
  70. let x = t.X;
  71. let y = -t.Y;
  72. if (x < this.minX) {
  73. this.minX = x;
  74. }
  75. if (y < this.minY) {
  76. this.minY = y;
  77. }
  78. if (x > this.maxX) {
  79. this.maxX = x;
  80. }
  81. if (y > this.maxY) {
  82. this.maxY = y;
  83. }
  84. return new SPoint(t.X, -t.Y);
  85. }
  86. );
  87. let p1 = this.pointArr[0];
  88. let p2 = this.pointArr[1];
  89. // 旋转点
  90. this.p = p1;
  91. const HX = (this.data.HandDirection.X = Number(
  92. this.data.HandDirection.X.toFixed()
  93. ));
  94. const HY = (this.data.HandDirection.Y = Number(
  95. this.data.HandDirection.Y.toFixed()
  96. ));
  97. const FX = (this.data.FaceDirection.X = Number(
  98. this.data.FaceDirection.X.toFixed()
  99. ));
  100. const FY = (this.data.FaceDirection.Y = Number(
  101. this.data.FaceDirection.Y.toFixed()
  102. ));
  103. // 向量点乘 => x1 * x2 + y1 * y2,大于0同向
  104. let dotProduct = (p2.x - p1.x) * HX + (p2.y - p1.y) * -HY;
  105. if (dotProduct > 0) {
  106. this.p = p2;
  107. p2 = p1;
  108. p1 = this.p;
  109. }
  110. // 两点间距离
  111. this.r = SMathUtil.pointDistance(p1.x, p1.y, p2.x, p2.y);
  112. // 门朝向角度
  113. let fo = Math.atan(-FY / FX);
  114. this.ang = FX > 0 ? fo : fo + Math.PI;
  115. // 向量叉乘 => x1 * y2 - x2 * y1,小于0是顺时针
  116. let direction = (p2.x - p1.x) * -FY - (p2.y - p1.y) * FX;
  117. if (direction > 0) {
  118. this.startAng = 0;
  119. }
  120. }
  121. } // Constructor
  122. /**
  123. * Item 对象边界区域
  124. *
  125. * @return 边界矩形
  126. */
  127. boundingRect(): SRect {
  128. return new SRect(0, 0, this.r, this.r);
  129. } // Function boundingRect()
  130. /**
  131. * Item 绘制操作
  132. *
  133. * @param painter 绘制对象
  134. */
  135. onDraw(painter: SPainter): void {
  136. painter.translate(this.p.x, this.p.y);
  137. painter.rotate(this.ang);
  138. painter.pen.lineWidth = 100;
  139. painter.pen.color = ItemColor.doorColor;
  140. painter.drawLine(0, 0, this.r, 0);
  141. painter.pen.lineDash = [50, 100];
  142. painter.pen.lineWidth = painter.toPx(1);
  143. // painter.drawArc(
  144. // -this.r,
  145. // -this.r,
  146. // this.r * 2,
  147. // this.r * 2,
  148. // this.startAng,
  149. // this.startAng + Math.PI / 2
  150. // );
  151. } // Function onDraw()
  152. } // Class SDoorItem