/** * Created by TonyJiang on 16/8/26. */ import {RectF} from "../../../drawing/Unit"; import {hasProperty} from "../../../items/decorates"; import {BoxProperty} from "../../../items/properties/define"; import {BoxItem} from "../../../items/BoxItem"; import {Border} from "../../../items/properties/classes/Border"; import {KonvaMapping} from "../../../drawing/KonvaMapping"; import {DesignerHost} from "../../DesignerHost"; import {GroupObjectPool, LineObjectPool} from "../../../drawing/KonvaObjectPools"; const perfectPosition = function(borderWidth:number, position:Array, offset:number = 0.5):Array { return position.map(p => borderWidth === 1 ? p + offset : p); }; export const BorderDrawer = function (obj, prop, descriptor) { const originalFunction = descriptor.value; descriptor.value = function (parentNode:any, rect: RectF):void { const bounds = rect.clone(); //如果没有border属性, 则不做任何处理 if (!hasProperty(this.item.constructor, BoxProperty.border)) { console.log(`${obj}没有属性${BoxProperty.border.name}`); return originalFunction.call(this, parentNode, bounds); } //绘制border, 并更新bounds大小; const item = this.item as BoxItem; const border: Border = item.border; const borderTopVisible = border.top.borderVisible; const borderRightVisible = border.right.borderVisible; const borderBottomVisible = border.bottom.borderVisible; const borderLeftVisible = border.left.borderVisible; if (!borderTopVisible && !borderLeftVisible && !borderBottomVisible && !borderRightVisible) { return originalFunction.call(this, parentNode, bounds); } // let groupProps = { // x: Math.floor(bounds.x), // y: Math.floor(bounds.y), // width: Math.floor(bounds.width), // height: Math.floor(bounds.height) // }; const left = Math.floor(bounds.x); const top = Math.floor(bounds.y); const right = left + Math.floor(bounds.width); const bottom = top + Math.floor(bounds.height); const zoom = DesignerHost.instance().designReport.zoom; // const groupNode = GroupObjectPool.get(groupProps); if (borderTopVisible && border.top.borderWidth.value > 0) { const borderTopWidth = border.top.borderWidth.pixels * zoom; const borderTopStyle = border.top.borderStyle; const borderTopColor = border.top.borderColor; const rectNode = LineObjectPool.get({ points: perfectPosition(borderTopWidth, [left, top, right, top]), stroke: borderTopColor, strokeWidth: borderTopWidth, dash: KonvaMapping.getLineStyleValue(borderTopStyle) }); parentNode.add(rectNode); } if (borderRightVisible && border.right.borderWidth.value > 0) { const borderRightWidth = border.right.borderWidth.pixels * zoom; const borderRightStyle = border.right.borderStyle; const borderRightColor = border.right.borderColor; const rectNode = LineObjectPool.get({ points: perfectPosition(borderRightWidth, [right, top, right, bottom]), stroke: borderRightColor, strokeWidth: borderRightWidth, dash: KonvaMapping.getLineStyleValue(borderRightStyle) }); parentNode.add(rectNode); } if (borderBottomVisible && border.bottom.borderWidth.value > 0) { const borderBottomWidth = border.bottom.borderWidth.pixels * zoom; const borderBottomStyle = border.bottom.borderStyle; const borderBottomColor = border.bottom.borderColor; const rectNode = LineObjectPool.get({ points: perfectPosition(borderBottomWidth, [left, bottom, right, bottom]), stroke: borderBottomColor, strokeWidth: borderBottomWidth, dash: KonvaMapping.getLineStyleValue(borderBottomStyle) }); parentNode.add(rectNode); } if (borderLeftVisible && border.left.borderWidth.value > 0) { const borderLeftWidth = border.left.borderWidth.pixels * zoom; const borderLeftStyle = border.left.borderStyle; const borderLeftColor = border.left.borderColor; const rectNode = LineObjectPool.get({ points: perfectPosition(borderLeftWidth, [left, top, left, bottom]), stroke: borderLeftColor, strokeWidth: borderLeftWidth, dash: KonvaMapping.getLineStyleValue(borderLeftStyle) }); parentNode.add(rectNode); } // bounds.x = 0; // bounds.y = 0; // bounds.width = groupProps.width; // bounds.height = groupProps.height; originalFunction.call(this, parentNode, bounds); // if (childNode) { // groupNode.add(childNode); // } // return groupNode; } }