STextItem.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 { SObjectItem } from "./SObjectItem";
  27. import { SPainter, SRect, SColor, SFont, SPoint } from "@persagy-web/draw";
  28. import { SGraphItem } from "../SGraphItem";
  29. import { SLineStyle } from "..";
  30. import { STextOrigin } from "..";
  31. /**
  32. * 文本 item
  33. *
  34. * @author 张宇 <taohuzy@163.com>
  35. */
  36. export class STextItem extends SObjectItem {
  37. /** 记录 painter */
  38. private _painter: SPainter | null = null;
  39. /** 文本内容 */
  40. private _text: string = "";
  41. get text(): string {
  42. return this._text;
  43. }
  44. set text(v: string) {
  45. this._text = v;
  46. this._textArr = this.text.split(/\n/g);
  47. this.drawFormatText();
  48. this.update();
  49. }
  50. /** 切割后的文本数组 */
  51. private _textArr: string[] = [];
  52. /** 文本颜色 */
  53. private _color: SColor = new SColor("#333333");
  54. get color(): SColor {
  55. return this._color;
  56. }
  57. set color(v: SColor) {
  58. this._color = v;
  59. this.update();
  60. }
  61. /** 字体 */
  62. private _font: SFont;
  63. get font(): SFont {
  64. return this._font;
  65. }
  66. set font(v: SFont) {
  67. this._font = v;
  68. this.drawFormatText();
  69. this.update();
  70. }
  71. /** 背景色 */
  72. private _backgroundColor: SColor = SColor.Transparent;
  73. get backgroundColor(): SColor {
  74. return this._backgroundColor;
  75. }
  76. set backgroundColor(v: SColor) {
  77. this._backgroundColor = v;
  78. this.update();
  79. }
  80. /** 边框色 */
  81. private _strokeColor: SColor = SColor.Transparent;
  82. get strokeColor(): SColor {
  83. return this._strokeColor;
  84. }
  85. set strokeColor(v: SColor) {
  86. this._strokeColor = v;
  87. this.update();
  88. }
  89. /** 边框宽度 */
  90. private _lineWidth: number = 1;
  91. get lineWidth(): number {
  92. return this._lineWidth;
  93. }
  94. set lineWidth(v: number) {
  95. this._lineWidth = v;
  96. this.update();
  97. }
  98. /** 边框样式 */
  99. private _borderStyle: SLineStyle = SLineStyle.None;
  100. get borderStyle(): SLineStyle {
  101. return this._borderStyle;
  102. }
  103. set borderStyle(v: SLineStyle) {
  104. this._borderStyle = v;
  105. this.update();
  106. }
  107. /** 原点位置 */
  108. private _originPosition: STextOrigin = STextOrigin.LeftTop;
  109. get originPosition(): STextOrigin {
  110. return this._originPosition;
  111. }
  112. set originPosition(v: STextOrigin) {
  113. this._originPosition = v;
  114. this.update();
  115. }
  116. /** 文本绘制最大宽 */
  117. maxWidth: number | undefined = undefined;
  118. /**
  119. * 构造函数
  120. *
  121. * @param parent 指向父 Item
  122. * @param str 文本内容
  123. */
  124. constructor(parent: SGraphItem | null, str: string = "") {
  125. super(parent);
  126. this.text = str;
  127. this._font = new SFont("sans-serif", 12);
  128. this.height = 12;
  129. }
  130. /**
  131. * 对象边界区域
  132. *
  133. * @return 边界区域
  134. */
  135. boundingRect(): SRect {
  136. return new SRect(
  137. -this.origin.x,
  138. -this.origin.y,
  139. this.width,
  140. this.height
  141. );
  142. }
  143. /**
  144. * 移动后处理所有坐标,并肩原点置为场景原点
  145. *
  146. * @param x x 坐标
  147. * @param y y 坐标
  148. */
  149. moveToOrigin(x: number, y: number): void {
  150. this.moveTo(this.x + x, this.y + y);
  151. }
  152. /**
  153. * 绘制显示状态文本 Item
  154. *
  155. * @param painter 绘制对象
  156. */
  157. protected drawShowText(painter: SPainter): void {
  158. painter.translate(-this.origin.x, -this.origin.y);
  159. //绘制矩形轮廓,是否选中
  160. if (this.selected) {
  161. painter.shadow.shadowBlur = 10;
  162. painter.shadow.shadowColor = new SColor(`#00000033`);
  163. painter.shadow.shadowOffsetX = 5;
  164. painter.shadow.shadowOffsetY = 5;
  165. } else {
  166. painter.shadow.shadowColor = SColor.Transparent;
  167. }
  168. painter.brush.color = this.backgroundColor;
  169. painter.pen.lineWidth = this.lineWidth;
  170. painter.pen.color = this.strokeColor;
  171. painter.drawRect(0, 0, this.width, this.height);
  172. //绘制文本
  173. painter.brush.color = new SColor(this.color);
  174. painter.shadow.shadowColor = SColor.Transparent;
  175. painter.font = this.font;
  176. this._textArr.forEach((text: string, index: number): void => {
  177. painter.drawText(
  178. text,
  179. this.font.size / 4,
  180. index * (this.font.size * 1.25) + this.font.size / 4,
  181. this.maxWidth
  182. );
  183. });
  184. }
  185. /**
  186. * 根据换行切割文本,绘制多行并计算外轮廓宽高
  187. */
  188. protected drawFormatText(): void {
  189. if (this._painter instanceof SPainter) {
  190. this._painter.save();
  191. this._painter.font = this.font;
  192. let textMaxWidth = 0;
  193. let fontSize: number = this.font.size;
  194. this._textArr.forEach((text: string, index: number): void => {
  195. let textWidth: number = this._painter
  196. ? this._painter.textWidth(text) + fontSize / 2
  197. : fontSize / 2;
  198. if (textWidth > textMaxWidth) {
  199. textMaxWidth = textWidth;
  200. }
  201. });
  202. // 在绘制文本后计算文本的宽高
  203. this.width = textMaxWidth;
  204. this.height = fontSize * 1.25 * this._textArr.length + fontSize / 8;
  205. // 设置原点位置(默认左上角)
  206. if (this.originPosition == STextOrigin.Center) {
  207. this.origin = new SPoint(this.width / 2, this.height / 2);
  208. }
  209. this._painter.restore();
  210. }
  211. }
  212. /**
  213. * Item 绘制操作
  214. *
  215. * @param painter 绘画对象
  216. */
  217. onDraw(painter: SPainter): void {
  218. if (!(this._painter instanceof SPainter)) {
  219. this._painter = painter;
  220. this.drawFormatText();
  221. }
  222. this.drawShowText(painter);
  223. }
  224. }