STextItem.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.STextItem = void 0;
  4. const SObjectItem_1 = require("./SObjectItem");
  5. const lib_1 = require("@persagy-web/draw/lib");
  6. const __1 = require("..");
  7. const __2 = require("..");
  8. class STextItem extends SObjectItem_1.SObjectItem {
  9. constructor(parent, str = "") {
  10. super(parent);
  11. this._painter = null;
  12. this._text = "";
  13. this._textArr = [];
  14. this._color = new lib_1.SColor("#333333");
  15. this._backgroundColor = lib_1.SColor.Transparent;
  16. this._strokeColor = lib_1.SColor.Transparent;
  17. this._lineWidth = 1;
  18. this._borderStyle = __1.SLineStyle.None;
  19. this._originPosition = __2.STextOrigin.LeftTop;
  20. this.maxWidth = undefined;
  21. this._text = str;
  22. this._font = new lib_1.SFont("sans-serif", 12);
  23. this.height = 12;
  24. }
  25. get text() {
  26. return this._text;
  27. }
  28. set text(v) {
  29. this._text = v;
  30. this._textArr = this.text.split(/\n/g);
  31. this.drawFormatText();
  32. this.update();
  33. }
  34. get color() {
  35. return this._color;
  36. }
  37. set color(v) {
  38. this._color = v;
  39. this.update();
  40. }
  41. get font() {
  42. return this._font;
  43. }
  44. set font(v) {
  45. this._font = v;
  46. this.drawFormatText();
  47. this.update();
  48. }
  49. get backgroundColor() {
  50. return this._backgroundColor;
  51. }
  52. set backgroundColor(v) {
  53. this._backgroundColor = v;
  54. this.update();
  55. }
  56. get strokeColor() {
  57. return this._strokeColor;
  58. }
  59. set strokeColor(v) {
  60. this._strokeColor = v;
  61. this.update();
  62. }
  63. get lineWidth() {
  64. return this._lineWidth;
  65. }
  66. set lineWidth(v) {
  67. this._lineWidth = v;
  68. this.update();
  69. }
  70. get borderStyle() {
  71. return this._borderStyle;
  72. }
  73. set borderStyle(v) {
  74. this._borderStyle = v;
  75. this.update();
  76. }
  77. get originPosition() {
  78. return this._originPosition;
  79. }
  80. set originPosition(v) {
  81. this._originPosition = v;
  82. this.update();
  83. }
  84. boundingRect() {
  85. return new lib_1.SRect(-this.origin.x, -this.origin.y, this.width, this.height);
  86. }
  87. moveToOrigin(x, y) {
  88. this.moveTo(this.x + x, this.y + y);
  89. }
  90. drawShowText(painter) {
  91. painter.translate(-this.origin.x, -this.origin.y);
  92. if (this.selected) {
  93. painter.shadow.shadowBlur = 10;
  94. painter.shadow.shadowColor = new lib_1.SColor(`#00000033`);
  95. painter.shadow.shadowOffsetX = 5;
  96. painter.shadow.shadowOffsetY = 5;
  97. }
  98. else {
  99. painter.shadow.shadowColor = lib_1.SColor.Transparent;
  100. }
  101. painter.brush.color = this.backgroundColor;
  102. painter.pen.lineWidth = this.lineWidth;
  103. painter.pen.color = this.strokeColor;
  104. painter.drawRect(0, 0, this.width, this.height);
  105. painter.brush.color = new lib_1.SColor(this.color);
  106. painter.shadow.shadowColor = lib_1.SColor.Transparent;
  107. painter.font = this.font;
  108. this._textArr.forEach((text, index) => {
  109. painter.drawText(text, this.font.size / 4, index * (this.font.size * 1.25) + this.font.size / 4, this.maxWidth);
  110. });
  111. }
  112. drawFormatText() {
  113. if (this._painter instanceof lib_1.SPainter) {
  114. this._painter.save();
  115. this._painter.font = this.font;
  116. let textMaxWidth = 0;
  117. let fontSize = this.font.size;
  118. this._textArr.forEach((text, index) => {
  119. let textWidth = this._painter
  120. ? this._painter.textWidth(text) + fontSize / 2
  121. : fontSize / 2;
  122. if (textWidth > textMaxWidth) {
  123. textMaxWidth = textWidth;
  124. }
  125. });
  126. this.width = textMaxWidth;
  127. this.height = fontSize * 1.25 * this._textArr.length + fontSize / 8;
  128. if (this.originPosition == __2.STextOrigin.Centrum) {
  129. this.origin = new lib_1.SPoint(this.width / 2, this.height / 2);
  130. }
  131. this._painter.restore();
  132. }
  133. }
  134. onDraw(painter) {
  135. if (!(this._painter instanceof lib_1.SPainter)) {
  136. this._painter = painter;
  137. this.drawFormatText();
  138. }
  139. this.drawShowText(painter);
  140. }
  141. }
  142. exports.STextItem = STextItem;