import {EventEmitter2} from "eventemitter2"; import {IDisposable, IServiceProvider, IComponent} from "../../interfaces/Interfaces"; import {BehaviorService} from "./BehaviorService"; import {RectF, SizeF} from "../../drawing/Unit"; import {DesignerHost} from "../DesignerHost"; import {SnapLine, SnapLineType, SnapLinePriority} from "./SnapLine"; import {Glyph} from "./Glyph"; import {ReportItemBase} from "../../items/ReportItemBase"; import {ReportItemBaseDesigner} from "../designer/ReportItemBaseDesigner"; import {DesignerStyleSettings} from "../DesignerStyleSettings"; import {SnapLineGlyph} from "./SnapLineGlyph"; import {SnapLineGlyphType} from "./Enums"; const InvalidDistance = 50000; export class SnapLineEngine implements IDisposable { private _events = new EventEmitter2(); private _serviceProvider:IServiceProvider; private _behaviorService:BehaviorService; private _dragItems:Array; private _host: DesignerHost; private _targetVerticalSnapLines: Array = []; private _targetHorizontalSnapLines: Array = []; private _verticalSnapLines: Array = []; private _horizontalSnapLines: Array = []; private _snapLineToBounds: {[key: string]: RectF} = {}; private _verticalDistances: Array = []; private _horizontalDistances: Array = []; private _snapTreshold = 0; private _snapOffsetX = 0; private _snapOffsetY = 0; private _dragOffset:RectF; private _hasSnapLine: boolean = false; constructor(serviceProvider:IServiceProvider, dragItem:Array, snapTreshold:number) { this._serviceProvider = serviceProvider; this._behaviorService = serviceProvider.getService(BehaviorService); this._dragItems = dragItem.map(item => item); this._snapTreshold = snapTreshold; this._host = DesignerHost.instance(); this.initialize(); } on(event: string, listener: EventEmitter2.Listener):void { this._events.on(event, listener); } dispose() { this._events.removeAllListeners(); this._events = null; this.stop(); } get snapLineGlyphs():Array { return this._behaviorService.snapLinesAdorner.glyphs; } stop() { this.snapLineGlyphs.length = 0; } snap(dragBounds:RectF, targetSnapLines:Array, snapped:{x:boolean, y:boolean}):SizeF { // console.log(dragBounds,targetSnapLines,snapped); if (targetSnapLines) { this._targetVerticalSnapLines.length = 0; this._targetHorizontalSnapLines.length = 0; for (let line of targetSnapLines) { if (line.isHorizontal) { this._targetHorizontalSnapLines.push(line); } else { this._targetVerticalSnapLines.push(line); } } } return this.snapOffsetTarget(dragBounds , targetSnapLines === null , snapped); } private initialize():void { this._dragOffset = RectF.Empty; let leadingItem:ReportItemBase = null; if (this._dragItems && this._dragItems.length > 0) { leadingItem = this._dragItems[0] as ReportItemBase; } if (leadingItem) { const designer = this._host.getDesigner(leadingItem) as ReportItemBaseDesigner; if (designer != null) { this.addSnapLines(designer , this._targetVerticalSnapLines , this._targetHorizontalSnapLines, true); this._dragOffset = designer.getBounds(); } } for (let component of this._host.container.getComponents()) { if (!this.canAddSnapLines(component, leadingItem)) continue; const baseDesigner = this._host.getDesigner(component) as ReportItemBaseDesigner; if (baseDesigner != null) { this.addSnapLines(baseDesigner , this._verticalSnapLines , this._horizontalSnapLines , false); } } this._verticalDistances = this._verticalSnapLines.map(item => InvalidDistance); this._horizontalDistances = this._horizontalSnapLines.map(item => InvalidDistance); this.snapLineGlyphs.length = 0; } private addSnapLines(designer:ReportItemBaseDesigner, verticalLines:Array, horizontalLines:Array, isTarget:boolean ):void { const bounds = designer.getBounds(); for (let line of designer.snapLines) { line.tag = designer.item.toString(); if (!isTarget) { this._snapLineToBounds[line.index] = bounds; } if (line.isHorizontal) { horizontalLines.push(line); } else { verticalLines.push(line); } } } private snapOffsetTarget(dragBounds:RectF, offsetTargetSnapLines:boolean, snapped:{x:boolean, y:boolean}):SizeF { // console.log(dragBounds, offsetTargetSnapLines, snapped); // console.log(this._targetHorizontalSnapLines); let isNeedRepaint = false; if (offsetTargetSnapLines) { const offset = new SizeF(dragBounds.x - this._dragOffset.x , dragBounds.y - this._dragOffset.y); for (let snapLine of this._targetHorizontalSnapLines) { snapLine.adjustOffset(offset.height); } for (let snapLine of this._targetVerticalSnapLines) { snapLine.adjustOffset(offset.width); } } this.snapLineGlyphs.length = 0; snapped.x = false; snapped.y = false; this._snapOffsetX = this.calculateMinDistance(this._targetVerticalSnapLines , this._verticalSnapLines , this._verticalDistances , dragBounds); this._snapOffsetY = this.calculateMinDistance(this._targetHorizontalSnapLines , this._horizontalSnapLines , this._horizontalDistances , dragBounds); if (Math.abs(this._snapOffsetX) - this._snapTreshold > DesignerStyleSettings.FloatEpsilon) { this._snapOffsetX = InvalidDistance; } if (Math.abs(this._snapOffsetY) - this._snapTreshold > DesignerStyleSettings.FloatEpsilon) { this._snapOffsetY = InvalidDistance; } if (this._snapOffsetX !== InvalidDistance) { this.addSnapLineGlyphs(this._verticalSnapLines , this._verticalDistances , this._snapOffsetX , dragBounds); snapped.x = true; isNeedRepaint = true; } if (this._snapOffsetY !== InvalidDistance) { this.addSnapLineGlyphs(this._horizontalSnapLines , this._horizontalDistances , this._snapOffsetY , dragBounds); snapped.y = true; isNeedRepaint = true; } const alsoNeedRepaint = this._hasSnapLine !== isNeedRepaint; if(isNeedRepaint || alsoNeedRepaint){ this.notifyRepaint(); this._hasSnapLine = isNeedRepaint; } this._dragOffset = dragBounds.clone(); return new SizeF(snapped.x ? -this._snapOffsetX : 0 , snapped.y ? -this._snapOffsetY : 0); } private calculateMinDistance(targetSnapLines:Array, snapLines:Array, distanceArray:Array, dragBounds:RectF):number { let minDistance = InvalidDistance; let priority = 0; for (let i = 0; i < snapLines.length; i++) { let snapLine = snapLines[i]; let minOffset = InvalidDistance; for (let targetSnapLine of targetSnapLines) { if (SnapLine.shouldSnap(snapLine , this._snapLineToBounds[snapLine.index] , targetSnapLine , dragBounds)) { const offset = targetSnapLine.offset - snapLine.offset; if (Math.abs(offset) < Math.abs(minOffset)) { minOffset = offset; } } } distanceArray[i] = minOffset; const delta = Math.abs(minOffset) - Math.abs(minDistance); if (delta < 0 || (Math.abs(delta) < DesignerStyleSettings.FloatEpsilon && snapLine.priority > priority)) { minDistance = minOffset; if (snapLine.priority !== SnapLinePriority.Always) { priority = snapLine.priority; } } } return minDistance; } private canAddSnapLines(component:IComponent, leadingItem:ReportItemBase):boolean { const item = component as ReportItemBase; if (null === item || (this._dragItems && this._dragItems.indexOf(item) >= 0) || this.isChildOf(item, leadingItem)) { return false; } return true; } private isChildOf(child:ReportItemBase, parent:ReportItemBase):boolean { if (child && parent) { while (child.parent) { if (child.parent === parent) { return true; } child = child.parent; } } return false; } private addSnapLineGlyphs(snapLines:Array , distanceArray:Array , distance:number , dragBounds:RectF) { let priority = 1; for (let i = 0; i < distanceArray.length; i++) { if (Math.abs(Math.abs(distanceArray[i]) - Math.abs(distance)) < DesignerStyleSettings.FloatEpsilon) { const pr = snapLines[i].priority; if (pr > priority && pr !== SnapLinePriority.Always) { priority = pr; } } } const baseDesigner = this._host.getDesigner(this._dragItems[0]) as ReportItemBaseDesigner; // console.log(baseDesigner); for (let i = 0; i < distanceArray.length; i++) { if (Math.abs(Math.abs(distanceArray[i]) - Math.abs(distance)) < DesignerStyleSettings.FloatEpsilon && (snapLines[i].priority === priority || snapLines[i].priority === SnapLinePriority.Always)) { this.addSnapLineGlyph(snapLines[i], dragBounds, baseDesigner); } } } private addSnapLineGlyph(snapLine:SnapLine, dragBounds:RectF, baseDesigner:ReportItemBaseDesigner):void { const bounds = this._snapLineToBounds[snapLine.index]; let type = SnapLineGlyphType.Standard; if (snapLine.snapLineType == SnapLineType.Baseline) { type = SnapLineGlyphType.Baseline; } else if (snapLine.filter && snapLine.filter.startsWith("Margin")) { type = SnapLineGlyphType.Margin; } else if (snapLine.filter && snapLine.filter.startsWith("Padding")) { type = SnapLineGlyphType.Padding; } let snapLineGlyph:SnapLineGlyph; if (snapLine.isVertical) { const minY = dragBounds.top - (this._snapOffsetY !== InvalidDistance ? this._snapOffsetY : 0); const maxY = dragBounds.bottom - (this._snapOffsetY !== InvalidDistance ? this._snapOffsetY : 0); snapLineGlyph = new SnapLineGlyph(type , snapLine.offset , Math.min(minY, bounds.top) , snapLine.offset , Math.max(maxY, bounds.bottom)); } else { const minX = dragBounds.left - (this._snapOffsetX !== InvalidDistance ? this._snapOffsetX : 0); const maxX = dragBounds.right - (this._snapOffsetX !== InvalidDistance ? this._snapOffsetX : 0); snapLineGlyph = new SnapLineGlyph(type , Math.min(minX, bounds.left) , snapLine.offset , Math.max(maxX, bounds.right) , snapLine.offset); } snapLineGlyph.originalBounds = bounds.clone(); snapLineGlyph.designer = baseDesigner; snapLineGlyph.tag = snapLine.filter; this.combineSnapLines(snapLineGlyph); } /** * 合并 SnapLine,避免重复绘制和显示 * @param snapLineGlyph */ private combineSnapLines(snapLineGlyph:SnapLineGlyph):void { let toAdd = true; for (let i = 0; i < this.snapLineGlyphs.length; i++){ const glyph = this.snapLineGlyphs[i] as SnapLineGlyph; const line1 = SnapLineGlyph.overlap(glyph, snapLineGlyph); if (line1){ this.snapLineGlyphs[i] = line1; toAdd = false; } } //console.log(snapLineGlyph); if (toAdd){ this.snapLineGlyphs.push(snapLineGlyph); } } notifyRepaint(): void{ this._events.emit("repaint",true); } }