123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- import { SPainter, SRect, SColor, SFont, SPoint, SSize } from "@persagy-web/draw";
- import { SGraphItem, STextOrigin, SLineStyle, SAnchorItem } from "@persagy-web/graph/";
- import { SItemStatus } from "@persagy-web/big";
- import { SGraphEdit } from ".."
- import { Marker } from "../type/Marker";
- import { ItemOrder } from '@persagy-web/big/lib';
- import { SMouseEvent } from '@persagy-web/base/lib';
- export class SBaseTextEdit extends SGraphEdit {
-
-
-
- data: Marker | null = null;
-
- propertyData: any
-
- startItem: SGraphItem | null = null;
-
- endItem: SGraphItem | null = null;
-
- protected _status: SItemStatus = SItemStatus.Normal;
- get status(): SItemStatus {
- return this._status;
- }
- set status(value: SItemStatus) {
- const oldStatus = this._status;
- const newStatus = value;
- this._status = value;
-
- this.$emit('StatusChange', oldStatus, newStatus)
- this.update();
- }
-
- private _painter: SPainter | null = null;
-
- private _text: string = "";
- get text(): string {
- return this._text;
- }
- set text(v: string) {
- this._text = v;
- this._textArr = this.text.split(/\n/g);
- this.drawFormatText();
- this.update();
- }
-
- private _textArr: string[] = [];
-
- anchorList: SAnchorItem[] = [];
-
- private _width: number = 64;
- get width(): number {
- return this._width;
- }
- set width(v: number) {
- if (v > 0) {
- if (v != this._width) {
- let w = this._width;
- this._width = v;
- this.onResize(
- new SSize(w, this._height),
- new SSize(this._width, this._height)
- );
- }
- }
- }
-
- private _height: number = 64;
- get height(): number {
- return this._height;
- }
- set height(v: number) {
- if (v > 0) {
- if (v != this._height) {
- let h = this._height;
- this._height = v;
- this.onResize(
- new SSize(this._width, h),
- new SSize(this._width, this._height)
- );
- }
- }
- }
-
- origin = new SPoint();
-
- private _color: SColor = new SColor("#333333");
- get color(): SColor {
- return this._color;
- }
- set color(v: SColor) {
- this._color = v;
- this.update();
- }
-
- private _font: SFont = new SFont("sans-serif", 12);
- get font(): SFont {
- return this._font;
- }
- set font(v: SFont) {
- this._font = v;
- this.drawFormatText();
- this.update();
- }
-
- private _backgroundColor: SColor = SColor.Transparent;
- get backgroundColor(): SColor {
- return this._backgroundColor;
- }
- set backgroundColor(v: SColor) {
- this._backgroundColor = v;
- this.update();
- }
-
- private _strokeColor: SColor = SColor.Transparent;
- get strokeColor(): SColor {
- return this._strokeColor;
- }
- set strokeColor(v: SColor) {
- this._strokeColor = v;
- this.update();
- }
-
- private _lineWidth: number = 1;
- get lineWidth(): number {
- return this._lineWidth;
- }
- set lineWidth(v: number) {
- this._lineWidth = v;
- this.update();
- }
-
- private _borderStyle: SLineStyle = SLineStyle.None;
- get borderStyle(): SLineStyle {
- return this._borderStyle;
- }
- set borderStyle(v: SLineStyle) {
- this._borderStyle = v;
- this.update();
- }
-
- private _originPosition: STextOrigin = STextOrigin.LeftTop;
- get originPosition(): STextOrigin {
- return this._originPosition;
- }
- set originPosition(v: STextOrigin) {
- this._originPosition = v;
- this.update();
- }
-
- maxWidth: number | undefined = undefined;
-
- constructor(parent: SGraphItem | null, data: Marker | null) {
- super(parent);
- if (data) {
- this.initData(data)
- }
- }
-
- initData(data: Marker) {
- this.showSelect = false;
- this.zOrder = ItemOrder.textOrder;
- this.isTransform = false;
- this.data = data;
- this.name = data.name;
- this.moveTo(data.pos.x, data.pos.y);
- if (data.size) {
- this.width = data.size.width;
- this.height = data.size.height;
- }
- if (data.style && data.style.default) {
-
- if (data.style.default.zorder) {
- this.zOrder = data.style.default.zorder;
- }
-
- if (data.style.default.text) {
- this.text = data.style.default.text;
- }
-
- if (data.style.default.strokeColor) {
- this.strokeColor = new SColor(data.style.default.strokeColor);
- }
-
- if (data.style.default.font) {
- this.font = new SFont("sans-serif", data.style.default.font);
- }
-
- if (data.style.default.backgroundColor) {
- this.backgroundColor = new SColor(data.style.default.backgroundColor);
- }
- }
- }
-
- protected onResize(oldSize: SSize, newSize: SSize) { }
-
- boundingRect(): SRect {
- return new SRect(
- -this.origin.x,
- -this.origin.y,
- this.width,
- this.height
- );
- }
-
-
-
-
-
- onMouseDown(event: SMouseEvent): boolean {
- this.$emit('textSelect')
- super.onMouseDown(event);
- return this.moveable;
- }
-
- onMouseUp(event: SMouseEvent): boolean {
- super.onMouseUp(event);
- return this.moveable;
- }
-
- protected drawShowText(painter: SPainter): void {
- painter.translate(-this.origin.x, -this.origin.y);
-
- if (this.selected) {
- painter.shadow.shadowBlur = 10;
- painter.shadow.shadowColor = new SColor(`#00000033`);
- painter.shadow.shadowOffsetX = 5;
- painter.shadow.shadowOffsetY = 5;
- } else {
- painter.shadow.shadowColor = SColor.Transparent;
- }
- painter.brush.color = this.backgroundColor;
- painter.pen.lineWidth = this.lineWidth;
- painter.pen.color = this.strokeColor;
- painter.drawRect(0, 0, this.width, this.height);
-
- painter.brush.color = new SColor(this.color);
- painter.shadow.shadowColor = SColor.Transparent;
- painter.font = this.font;
- this._textArr.forEach((text: string, index: number) => {
- painter.drawText(
- text,
- this.font.size / 4,
- index * (this.font.size * 1.25) + this.font.size / 4,
- this.maxWidth
- );
- });
- }
-
- protected drawFormatText(): void {
- if (this._painter instanceof SPainter) {
- this._painter.save();
- this._painter.font = this.font;
- let textMaxWidth = 0;
- let fontSize: number = this.font.size;
- this._textArr.forEach((text: string, index: number) => {
- let textWidth: number = this._painter
- ? this._painter.textWidth(text) + fontSize / 2
- : fontSize / 2;
- if (textWidth > textMaxWidth) {
- textMaxWidth = textWidth;
- }
- });
-
- this.width = textMaxWidth;
- this.height = fontSize * 1.25 * this._textArr.length + fontSize / 8;
-
- if (this.originPosition == STextOrigin.Center) {
- this.origin = new SPoint(this.width / 2, this.height / 2);
- }
- this._painter.restore();
- }
- }
-
- toData(): any {
- if (this.data.size) {
- this.data.size.width = this.width;
- this.data.size.height = this.height;
- }
- this.data.pos.x = this.pos.x;
- this.data.pos.y = this.pos.y;
- this.data.style.default.zorder = this.zOrder;
- this.data.style.default.text = this.text;
- this.data.style.default.strokeColor = this.strokeColor.value;
- this.data.style.default.font = this.font.size;
- this.data.style.default.backgroundColor = this.backgroundColor.value;
- return this.data;
- }
-
- onDraw(painter: SPainter): void {
- if (!(this._painter instanceof SPainter)) {
- this._painter = painter;
- this.drawFormatText();
- }
- this.drawShowText(painter);
- }
- }
|