import * as React from "react"; import {Glyph} from "./Glyph"; import {RectF, PointF, Unit} from "../../drawing/Unit"; import {DesignerStyleSettings} from "../DesignerStyleSettings"; import {Edges} from "./Enums"; import {Cursor} from "../../drawing/Cursor"; const Konva = require('konva'); /** * 维度(宽高)绘图单元. * 在 Resize 报表元素的时候辅助让用户更直接的了解 Resize 元素的变化. */ export class DimensionsGlyph extends Glyph { // 绘制维度线时距离测量主体元素的偏移量. // 正数在元素外围绘制维度值 // 负数在元素内部绘制维度值 static addedLineLength = 18; // 绘制维度线时额外偏移量. static extraOffset = 6; private bounds:RectF; private edges:Edges; private width:Unit; private height:Unit; /** * 实例化一个绘图单元 * @param bounds 维度的大小, 提供绘制的位置和大小. * @param edges 显示哪些边的维度数据信息 * @param width 维度的宽度, 包含计量单位的 Unit 值 * @param height 维度的高度, 包含计量单位的 Unit 值 */ constructor(bounds:RectF, edges:Edges, width:Unit, height:Unit) { super(null); this.bounds = bounds.clone(); this.edges = edges; this.width = width; this.height = height; } hitTest(pt:PointF):Cursor { // 我们绘制的图元不需要响应任何点击事件. return null; } render(parentNode: any):void { const font = DesignerStyleSettings.DimensionFont; const glyphs = []; if ((this.edges & Edges.Left) != Edges.None) { glyphs.push(DimensionsGlyph.draw(parentNode, font, this.bounds, this.width, Edges.Left)); } if ((this.edges & Edges.Right) != Edges.None) { glyphs.push(DimensionsGlyph.draw(parentNode, font, this.bounds, this.width, Edges.Right)); } if ((this.edges & Edges.Top) != Edges.None) { glyphs.push(DimensionsGlyph.draw(parentNode, font, this.bounds, this.height, Edges.Top)); } if ((this.edges & Edges.Bottom) != Edges.None) { glyphs.push(DimensionsGlyph.draw(parentNode, font, this.bounds, this.height, Edges.Bottom)); } } static draw(parentNode: any, font:any, bounds:RectF, value:Unit, edge:Edges):any { let s = "-"; if (!value.isEmpty) { // 将单位转为可读的文本. s = value.toString(); } switch (edge) { case Edges.Left: case Edges.Right: return DimensionsGlyph.drawHorizDimension(parentNode , font , s , bounds.left - DesignerStyleSettings.SelectionBorderSize , bounds.right + DesignerStyleSettings.SelectionBorderSize , bounds.bottom , DimensionsGlyph.addedLineLength); case Edges.Top: case Edges.Bottom: return DimensionsGlyph.drawVertDimension(parentNode , font , s , bounds.right , bounds.top - DesignerStyleSettings.SelectionBorderSize , bounds.bottom + DesignerStyleSettings.SelectionBorderSize , DimensionsGlyph.addedLineLength); } return null; } static drawHorizDimension(parentNode, font:any, s:string, x1:number, x2:number, y:number, offset:number):any { let y1 = y + Math.sign(offset) * DimensionsGlyph.extraOffset; const glyphs = []; glyphs.push(Glyph.drawLine(DesignerStyleSettings.SelectionPen, x1, y1, x1, y1 + offset)); glyphs.push(Glyph.drawLine(DesignerStyleSettings.SelectionPen, x2, y1, x2, y1 + offset)); y1 = y + offset + Math.sign(offset) * DimensionsGlyph.extraOffset / 2; glyphs.push(Glyph.drawLine(DesignerStyleSettings.SelectionPen, x1, y1, x2, y1)); let sz = Glyph.measureString(s, font); if (sz.width > (x2 - x1)) { if (offset > 0) { y1 += (DimensionsGlyph.extraOffset + 1); } else { y1 -= (DimensionsGlyph.extraOffset + 1 + sz.height); } } else { y1 -= sz.height / 2; } let rc = new RectF(x1 + (x2 - x1 - sz.width) / 2 , y1 , sz.width , sz.height); // glyphs.push(Glyph.fillRectangle(DesignerStyleSettings.TextBackgroundBrush, rc)); // glyphs.push(Glyph.drawString(s, font, DesignerStyleSettings.SelectionBrush, rc, {align:'center'})); parentNode.add(Glyph.fillRectangle(DesignerStyleSettings.TextBackgroundBrush, rc)); parentNode.add(Glyph.drawString(s, font, DesignerStyleSettings.SelectionBrush, rc, {align:'center'})); return glyphs; } static drawVertDimension(parentNode: any, font:any, s:string, x:number, y1:number, y2:number, offset:number):any { let x1 = x + Math.sign(offset) * DimensionsGlyph.extraOffset; const glyphs = []; glyphs.push(Glyph.drawLine(DesignerStyleSettings.SelectionPen, x1, y1, x1 + offset, y1)); glyphs.push(Glyph.drawLine(DesignerStyleSettings.SelectionPen, x1, y2, x1 + offset, y2)); x1 = x + offset + Math.sign(offset) * DimensionsGlyph.extraOffset / 2; glyphs.push(Glyph.drawLine(DesignerStyleSettings.SelectionPen, x1, y1, x1, y2)); let sz = Glyph.measureString(s, font); if (sz.width > (y2 - y1)) { if (offset > 0) { x1 += (DimensionsGlyph.extraOffset + 1); } else { x1 -= (DimensionsGlyph.extraOffset + 1 + sz.height); } } else { x1 -= sz.height / 2; } let rc = new RectF(x1 , y1 + (y2 - y1 - sz.width) / 2 , sz.width , sz.height); // glyphs.push(Glyph.fillRectangle(DesignerStyleSettings.TextBackgroundBrush, rc, {rotation:'90'})); // glyphs.push(Glyph.drawString(s, font, DesignerStyleSettings.SelectionBrush, rc, {align:'center', rotation:'90'})); parentNode.add(Glyph.fillRectangle(DesignerStyleSettings.TextBackgroundBrush, rc, {rotation:'90'})); parentNode.add(Glyph.drawString(s, font, DesignerStyleSettings.SelectionBrush, rc, {align:'center', rotation:'90'})); return glyphs; } }