| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import { SObjectItem } from "./SObjectItem";
- import { SPainter, SRect, SColor, SFont } from "@saga-web/draw/lib";
- import { SGraphItem } from "../SGraphItem";
- /**
- * 文本item
- *
- * @author 张宇(taohuzy@163.com)
- */
- export class STextItem extends SObjectItem {
- /** 文本内容 */
- private _text: string = "";
- get text(): string {
- return this._text;
- }
- set text(v: string) {
- this._text = v;
- this.update();
- }
- /** 文本颜色 */
- private _color: string = "#333333";
- get color(): string {
- return this._color;
- }
- set color(v: string) {
- this._color = v;
- this.update();
- }
- /** 字体 */
- private _font: SFont;
- get font(): SFont {
- return this._font;
- }
- set font(v: SFont) {
- this._font = v;
- this.update();
- }
- /** 背景色 */
- private _backgroundColor: string = "#00000000";
- get backgroundColor(): string {
- return this._backgroundColor;
- }
- set backgroundColor(v: string) {
- this._backgroundColor = v;
- this.update();
- }
- /** 边框色 */
- private _strokeColor: string = "#00000000";
- get strokeColor(): string {
- return this._strokeColor;
- }
- set strokeColor(v: string) {
- this._strokeColor = v;
- this.update();
- }
- private _showBorder: boolean = false;
- get showBorder(): boolean {
- return this._showBorder;
- }
- set showBorder(v: boolean) {
- if (this._showBorder === v) {
- return;
- }
- this._showBorder = v;
- this.update();
- }
- /** 文本绘制最大宽 */
- maxWidth: number | undefined = undefined;
- /**
- * 构造函数
- *
- * @param parent 指向父Item
- * @param str 文本内容
- */
- constructor(parent: SGraphItem | null, str: string = "") {
- super(parent);
- this._text = str;
- this._font = new SFont("sans-serif", 12);
- this.height = 12;
- } // Constructor
- /**
- * 对象边界区域
- *
- * @return 边界区域
- */
- boundingRect(): SRect {
- return new SRect(0, 0, this.width, this.height);
- } // Function boundingRect()
- /**
- * 绘制显示状态文本Item
- *
- * @param painter 绘制类
- */
- protected drawShowText(painter: SPainter): void {
- //绘制矩形轮廓
- if (this.showBorder) {
- painter.brush.color = new SColor(this.backgroundColor);
- painter.pen.color = new SColor(this.strokeColor);
- painter.drawRect(this.boundingRect());
- }
- //绘制文本
- painter.brush.color = new SColor(this.color);
- painter.font = this.font;
- this.drawFormatText(painter);
- } // Function drawShowText()
- /**
- * 根据换行切割文本,绘制多行并计算外轮廓宽高
- *
- * @param painter 绘制类
- */
- protected drawFormatText(painter: SPainter): void {
- let textArr: string[] = this.text.split(/\n/g);
- let textMaxWidth = 0;
- let textHeight: number = this.font.size;
- textArr.forEach((text: string, index: number) => {
- painter.drawText(
- text,
- 0,
- index * (textHeight + 2) + 2,
- this.maxWidth
- );
- let textWidth: number = painter.textWidth(text);
- if (textWidth > textMaxWidth) {
- textMaxWidth = textWidth;
- }
- });
- // 在绘制文本后计算文本的宽高
- this.width = textMaxWidth;
- this.height = (textHeight + 2) * textArr.length;
- } // Function drawFormatText()
- /**
- * Item绘制操作
- *
- * @param painter 绘画类
- */
- onDraw(painter: SPainter): void {
- this.drawShowText(painter);
- } // Function onDraw()
- } // Class STextItem
|