import { SObjectItem } from "./SObjectItem"; import { SPainter, SRect, SColor, SFont } from "@saga-web/draw/lib"; import { SMouseEvent } from "@saga-web/base/lib"; /** * 文本item * * @author 郝建龙(1061851420@qq.com) */ export class STextItem extends SObjectItem { /** 文本内容 */ _text: string = ""; get text(): string { return this._text; } set text(v: string) { this._text = v; this.update(); } /** 文本所占高度 */ private height: number = 0; /** 文本所占宽度 */ private width: number = 0; /** 字体 */ _font: SFont = new SFont(); get font(): SFont { return this._font; } set font(v: SFont) { this._font = v; this.update(); } /** 文本绘制最大宽 */ maxWidth: number | undefined = undefined; /** 文本颜色 */ private _color: string = "#000000"; get color(): string { return this._color; } set color(v: string) { this._color = v; this.update(); } /** * Item对象边界区域 * * @return SRect */ boundingRect(): SRect { return new SRect( -this.width / 2, -this.height / 2, this.width, this.height ); } /** * 判断点是否在区域内 * * @param x * @param y */ contains(x: number, y: number): boolean { return this.boundingRect().contains(x, y); } // Function contains() /** * 鼠标单击事件 * * @param event 事件参数 * @return boolean */ onMouseDown(event: SMouseEvent): boolean { console.log("textDown"); this.$emit("click", event); return true; } // Function onMouseDown() /** * 鼠标抬起事件 * * @param event 事件参数 * @return boolean */ onMouseUp(event: SMouseEvent): boolean { console.log("textup"); return super.onMouseUp(event); } // Function onClick() /** * Item绘制操作 * * @param painter painter对象 */ onDraw(painter: SPainter): void { this.width = painter.toPx(painter.textWidth(this.text)); this.height = painter.toPx(this.font.size); painter.font.textBaseLine = this.font.textBaseLine; painter.font.textDirection = this.font.textDirection; painter.font.textAlign = this.font.textAlign; painter.font.name = this.font.name; painter.font.size = painter.toPx(this.font.size); painter.brush.color = new SColor(this.color); painter.drawText(this.text, 0, 0, this.maxWidth); } // Function onDraw() } // Class STextItem