STextItem.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { SObjectItem } from "./SObjectItem";
  2. import { SPainter, SRect, SColor, SFont } from "@saga-web/draw/lib";
  3. import { SGraphItem } from "../SGraphItem";
  4. /**
  5. * 文本item
  6. *
  7. * @author 张宇(taohuzy@163.com)
  8. */
  9. export class STextItem extends SObjectItem {
  10. /** 文本内容 */
  11. private _text: string = "";
  12. get text(): string {
  13. return this._text;
  14. }
  15. set text(v: string) {
  16. this._text = v;
  17. this.update();
  18. }
  19. /** 文本颜色 */
  20. private _color: string = "#333333";
  21. get color(): string {
  22. return this._color;
  23. }
  24. set color(v: string) {
  25. this._color = v;
  26. this.update();
  27. }
  28. /** 字体 */
  29. private _font: SFont;
  30. get font(): SFont {
  31. return this._font;
  32. }
  33. set font(v: SFont) {
  34. this._font = v;
  35. this.update();
  36. }
  37. /** 背景色 */
  38. private _backgroundColor: string = "#00000000";
  39. get backgroundColor(): string {
  40. return this._backgroundColor;
  41. }
  42. set backgroundColor(v: string) {
  43. this._backgroundColor = v;
  44. this.update();
  45. }
  46. /** 边框色 */
  47. private _strokeColor: string = "#00000000";
  48. get strokeColor(): string {
  49. return this._strokeColor;
  50. }
  51. set strokeColor(v: string) {
  52. this._strokeColor = v;
  53. this.update();
  54. }
  55. private _showBorder: boolean = false;
  56. get showBorder(): boolean {
  57. return this._showBorder;
  58. }
  59. set showBorder(v: boolean) {
  60. if (this._showBorder === v) {
  61. return;
  62. }
  63. this._showBorder = v;
  64. this.update();
  65. }
  66. /** 文本绘制最大宽 */
  67. maxWidth: number | undefined = undefined;
  68. /**
  69. * 构造函数
  70. *
  71. * @param parent 指向父Item
  72. * @param str 文本内容
  73. */
  74. constructor(parent: SGraphItem | null, str: string = "") {
  75. super(parent);
  76. this._text = str;
  77. this._font = new SFont("sans-serif", 12);
  78. this.height = 12;
  79. } // Constructor
  80. /**
  81. * 对象边界区域
  82. *
  83. * @return 边界区域
  84. */
  85. boundingRect(): SRect {
  86. return new SRect(0, 0, this.width, this.height);
  87. } // Function boundingRect()
  88. /**
  89. * 绘制显示状态文本Item
  90. *
  91. * @param painter 绘制类
  92. */
  93. protected drawShowText(painter: SPainter): void {
  94. //绘制矩形轮廓
  95. if (this.showBorder) {
  96. painter.brush.color = new SColor(this.backgroundColor);
  97. painter.pen.color = new SColor(this.strokeColor);
  98. painter.drawRect(this.boundingRect());
  99. }
  100. //绘制文本
  101. painter.brush.color = new SColor(this.color);
  102. painter.font = this.font;
  103. this.drawFormatText(painter);
  104. } // Function drawShowText()
  105. /**
  106. * 根据换行切割文本,绘制多行并计算外轮廓宽高
  107. *
  108. * @param painter 绘制类
  109. */
  110. protected drawFormatText(painter: SPainter): void {
  111. let textArr: string[] = this.text.split(/\n/g);
  112. let textMaxWidth = 0;
  113. let textHeight: number = this.font.size;
  114. textArr.forEach((text: string, index: number) => {
  115. painter.drawText(
  116. text,
  117. 0,
  118. index * (textHeight + 2) + 2,
  119. this.maxWidth
  120. );
  121. let textWidth: number = painter.textWidth(text);
  122. if (textWidth > textMaxWidth) {
  123. textMaxWidth = textWidth;
  124. }
  125. });
  126. // 在绘制文本后计算文本的宽高
  127. this.width = textMaxWidth;
  128. this.height = (textHeight + 2) * textArr.length;
  129. } // Function drawFormatText()
  130. /**
  131. * Item绘制操作
  132. *
  133. * @param painter 绘画类
  134. */
  135. onDraw(painter: SPainter): void {
  136. this.drawShowText(painter);
  137. } // Function onDraw()
  138. } // Class STextItem