import {IDisposable, IServiceProvider, IComponent, IAdonerWindowControl} from "../../interfaces/Interfaces"; import {PointF} from "../../drawing/Unit"; import {DesignerHost} from "../DesignerHost"; import {Report} from "../../items/Report"; import {BehaviorService} from "./BehaviorService"; import {SelectionService} from "./SelectionService"; import {SelectionTypes} from "./Enums"; import {Glyph} from "./Glyph"; import {RectSelectionGlyph} from "./RectSelectionGlyph"; import {ReportItemBaseDesigner} from "../designer/ReportItemBaseDesigner"; import {ReportBandDesigner} from "../designer/ReportBandDesigner"; import {ReportBandBase} from "../../items/band/ReportBandBase"; export class RectSelectionEngine implements IDisposable { private _serviceProvider:IServiceProvider; private _selectionService: SelectionService; private _behaviorService:BehaviorService; private _host: DesignerHost; private _endPos: PointF; private _selectedItems: Array; constructor(serviceProvider:IServiceProvider) { this._serviceProvider = serviceProvider; this._selectionService = serviceProvider.getService(SelectionService) as SelectionService; this._behaviorService = serviceProvider.getService(BehaviorService) as BehaviorService; this._host = DesignerHost.instance(); this.initialize(); } initialize() { } get rectSelectionGlyphs():Array { return this._behaviorService.rectSelectionAdorner.glyphs; } dispose() { this.stop(); } stop() { this.rectSelectionGlyphs.length = 0; this._behaviorService.host.designReport.report.setNeedDisplay(); } private get report():Report { return this._host.designReport.report; } rectSelection(startPos:PointF, endPos:PointF, evtPos:PointF) { let rectSelectionGlyph:RectSelectionGlyph; let reactEndPos:PointF; if (endPos.x >= 0 && endPos.y >= 0) { reactEndPos = endPos; } else { const stageInfo = (this._behaviorService.adornerWindow as any).getStagePosition(); const stagePos = stageInfo.position; const mousePos = new PointF(evtPos.x - stagePos.x, evtPos.y - stagePos.y); const x = mousePos.x > stageInfo.width ? stageInfo.width : (mousePos.x <0 ? 0 : mousePos.x); const y = mousePos.y > stageInfo.height ? stageInfo.height : (mousePos.y <0 ? 0 : mousePos.y); reactEndPos = new PointF(x as number, y as number); } if (! this._endPos || this._endPos.x !== reactEndPos.x || this._endPos.y !== reactEndPos.y){ this._endPos = reactEndPos; //清空上一次的选择 this._selectedItems = []; // this._selectionService.clear(); //判断当前选中的 const centerPos = new PointF((reactEndPos.x + startPos.x)/2 , (reactEndPos.y + startPos.y)/2); const selectionWidth = Math.abs(reactEndPos.x - startPos.x); const selectionHeight = Math.abs(reactEndPos.y - startPos.y); let hasPrimary = false; let primaryItem = this._selectionService.getPrimarySelection(); if(primaryItem) { hasPrimary = true; } this.checkSelectedItems(this.report.items, centerPos, selectionWidth, selectionHeight); this._selectionService.setSelectedItems(this._selectedItems, SelectionTypes.Replace); //记录下选中primary时的坐标 let curPrimaryItem = this._selectionService.getPrimarySelection(); if((!hasPrimary && curPrimaryItem)|| (curPrimaryItem && curPrimaryItem != primaryItem)) { var itemBounds = this._host.getDesigner(curPrimaryItem).getBounds(); this._behaviorService.setPrimaryCursor(new PointF(itemBounds.x,itemBounds.y)); } rectSelectionGlyph = new RectSelectionGlyph(startPos, reactEndPos); //清空glyphs,push这一次选取的glyphs this.rectSelectionGlyphs.length = 0; this.rectSelectionGlyphs.push(rectSelectionGlyph); //重新绘制 this.report.setNeedDisplay(); } } checkSelectedItems(items:Array, pos:PointF, width:number, height:number):void { items.map( item => { if (! (item instanceof ReportBandBase)) { const designer = this._host.getDesigner(item) as ReportItemBaseDesigner; const bounds = designer.getBounds(); const itemCenter = new PointF(bounds.x + bounds.width/2, bounds.y + bounds.height/2); const xlen = Math.abs(itemCenter.x - pos.x); const ylen = Math.abs(itemCenter.y - pos.y); //判断是否交叉 if (xlen <= (bounds.width+width)/2 && ylen <= (bounds.height+height)/2 ) { this._selectedItems.push(item); } } //递归 if(item.items){ this.checkSelectedItems(item.items, pos, width, height); } }); } }