import * as React from "react"; import {Glyph} from "./Glyph"; import {RectF, PointF, Unit} from "../../drawing/Unit"; import {DesignerStyleSettings} from "../DesignerStyleSettings"; import {Cursor} from "../../drawing/Cursor"; import {SnapLineType} from "./SnapLine"; const Konva = require('konva'); /** * 显示元素距离值的绘图单元 */ export class ItemLocationGlyph extends Glyph { x1:number; y1:number; x2:number; y2:number; value:string; lineType:SnapLineType; constructor(x1:number, y1:number, x2:number, y2:number, value:Unit, lineType:SnapLineType) { super(null); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.value = value.toString(); this.lineType = lineType; } hitTest(pt:PointF):Cursor { return null; } isVertical():boolean { // return this.x1 === this.x2; return this.lineType === SnapLineType.Bottom || this.lineType === SnapLineType.Top; } render(parentNode):void { const font = DesignerStyleSettings.DimensionFont; // glyphs.push(Glyph.drawLine(DesignerStyleSettings.SelectionPenHalf, this.x1, this.y1, this.x2, this.y2)); parentNode.add(Glyph.drawLine(DesignerStyleSettings.SelectionPenHalf, this.x1, this.y1, this.x2, this.y2)); let sz = Glyph.measureString(this.value, font); const rc = new RectF(); const textConfig = { align:'center', rotation:null }; if (this.isVertical()) { // 旋转 90 度显示文本 textConfig.rotation = 90; rc.x = this.x1 + sz.height / 2; if ((sz.width + 20) < Math.abs(this.y2 - this.y1)) { rc.y = this.y1 + (this.y2 - this.y1 - sz.width) / 2; } else { if (this.lineType === SnapLineType.Top) { rc.y = this.y1 - sz.width - 3; } else { rc.y = this.y1 + 3; } } rc.width = sz.width; rc.height = sz.height; } else { if ((sz.width + 20) < Math.abs(this.x2 - this.x1)) { rc.x = this.x1 + (this.x2 - this.x1 - sz.width) / 2; } else { if (this.lineType === SnapLineType.Left) { rc.x = this.x1 - sz.width - 3; } else { rc.x = this.x1 + 3; } } rc.y = this.y1 - sz.height / 2; rc.size = sz; } // glyphs.push(Glyph.fillRectangle(DesignerStyleSettings.TextBackgroundBrush, rc, {rotation:textConfig.rotation})); // glyphs.push(Glyph.drawString(this.value, font, DesignerStyleSettings.SelectionBrushHalf, rc, textConfig)); parentNode.add(Glyph.fillRectangle(DesignerStyleSettings.TextBackgroundBrush, rc, {rotation:textConfig.rotation})); parentNode.add(Glyph.drawString(this.value, font, DesignerStyleSettings.SelectionBrushHalf, rc, textConfig)); } }