import {IDisposable, IAdonerWindowControl} from "../../interfaces/Interfaces"; import {Adorner} from "./Adorner"; import {AdornerLevels, ReportRootDesigner} from "../designer/ReportRootDesigner"; import {Unit, PointF, RectF} from "../../drawing/Unit"; import {DesignerHost} from "../DesignerHost"; import {Glyph} from "./Glyph"; import {ItemBodyGlyph} from "./ItemBodyGlyph"; import {Behavior} from "./Behavior"; import {DropSourceBehavior} from "./DropSourceBehavior"; import {DragEventArgs} from "../DragEventArgs"; import {Cursor} from "../../drawing/Cursor"; import {KeyStates, Keys} from "../../common/KeyStates"; import {HitMoveEvent} from "../../common/HitMoveEvent"; import {SelectionService} from "./SelectionService"; import {ReportItemBase} from "../../items/ReportItemBase"; import {SelectionBorderGlyph} from "./SelectionBorderGlyph"; import { SelectionBorderGlyphType} from "./Enums"; import SnackBar from '../../helper/snackbar'; /** * Created by luosong on 16/7/8. * BehaviorService 对设计过程中的事件统一调度给适合的 Behavior 进行处理. */ export class BehaviorService implements IDisposable { private _reportDesigner: ReportRootDesigner; private _host: DesignerHost; private _adorners: Array; private _hitGlyph:Glyph; private _behaviorStack: Array; private _captureBehavior:Behavior; private _adornerWindow: IAdonerWindowControl; private _validDragArgs:DragEventArgs = null; private _suspendHitTestCount = 0; private _primaryOffset:PointF; private _primaryKeyMoveItem: any; private _dropSourceBehavior: DropSourceBehavior; private _selectionService: SelectionService; private _snapLinesAdorner: Adorner; private _dimensionLinesAdorner: Adorner; private _rectSelectionAdorner: Adorner; private _itemsAdorner: Adorner; private _editingAdorner: Adorner; constructor() { this.initialize(); } protected initialize() { this._behaviorStack = []; this._host = DesignerHost.instance(); this._reportDesigner = this._host.designReport; this._adorners = []; this._snapLinesAdorner = new Adorner("Snap Lines", AdornerLevels.SnapLines); this._dimensionLinesAdorner = new Adorner("Dimension Lines", AdornerLevels.DimensionLines); this._rectSelectionAdorner = new Adorner("Rect Selection", AdornerLevels.RectSelection); this._editingAdorner = new Adorner("Editing Item", AdornerLevels.EditingItem); this._selectionService = this._host.getService(SelectionService) as SelectionService; this._adorners.push(this._snapLinesAdorner); this._adorners.push(this._dimensionLinesAdorner); this._adorners.push(this._rectSelectionAdorner); this._adorners.push(this._editingAdorner); } get host():DesignerHost { return this._host; } /** * 返回绘制 SnapLines (辅助线) 的 Adorner * @returns {Adorner} */ get snapLinesAdorner():Adorner { return this._snapLinesAdorner; } /** * 返回绘制 Dimensions (坐标线) 的 Adrorner * @returns {Adorner} */ get dimensionLinesAdorner():Adorner { return this._dimensionLinesAdorner; } /** * 返回绘制 RectSelection (矩形选择框) 的 Adrorner * @returns {Adorner} */ get rectSelectionAdorner():Adorner { return this._rectSelectionAdorner; } /** * 返回设计画布中是否显示 Dimensions (坐标线) * @returns {boolean} */ get showDimensions():boolean { return this._reportDesigner.showDimensions; } /** * 返回在 Move 或者 Resize 过程中是否需要自动对齐到最接近的辅助线. * @returns {boolean} */ get snapToSnapLines():boolean { return this._reportDesigner.snapToSnapLines; } /** * 返回在 Move 或者 Resize 过程中是否需要自动对齐到最接近的网格线. * @returns {boolean} */ get snapToGrid():boolean { return this._reportDesigner.snapToGrid; } /** * 返回是否应该按网格自动对齐 * @returns {boolean} */ get shouldSnap():boolean { return this.snapToGrid && !KeyStates.isDown(Keys.Shift); } /** * 返回正在处理 Drag Drop 中. * @returns {boolean} */ get isDragging():boolean { return (this.validDragArgs !== null); } /** * 返回 Drag 事件参数. * @returns {DragEventArgs} */ get validDragArgs():DragEventArgs { return this._validDragArgs; } /** * 设置 Drag 事件参数. * @param value */ set validDragArgs(value:DragEventArgs) { this._validDragArgs = value; } /** * 返回提供给 Adorner 绘制的 window. * @returns {IAdonerWindowControl} */ get adornerWindow():IAdonerWindowControl { return this._adornerWindow; } set adornerWindow(value:IAdonerWindowControl) { this._adornerWindow = value; } /** * 返回网格大小 * @returns {Unit} */ get snapGridSize():Unit { return this._reportDesigner.snapGridSize; } /** * 返回缩放比例 * @returns {number} */ get zoom():number { return this._reportDesigner.zoom; } set zoom(value:number) { this._reportDesigner.zoom = value; } get adorners():Array { return this._adorners; } set itemsAdorner(adorner: Adorner){ this._itemsAdorner = adorner; } get itemsAdorner(): Adorner{ return this._itemsAdorner; } get editingAdorner(): Adorner{ return this._editingAdorner; } sortAdorners():void { this._adorners = this._adorners.sort((a, b) => a.level - b.level); } redraw():void { this._reportDesigner.report.setNeedDisplay(); } /** * 冻结 propagateHitTest 行为. */ resumeHitTest():void { if (this._suspendHitTestCount > 0) { this._suspendHitTestCount--; } } /** * 恢复 propagateHitTest 行为. */ suspendHitTest():void { this._suspendHitTestCount++; } /** * 将 behavior 压入 behaviorStack 中并且将这个 behavior 作为 capture 事件 的 behavior。 * @param behavior */ pushCaptureBehavior(behavior:Behavior):void { this._behaviorStack.unshift(behavior); if (this._captureBehavior && this._captureBehavior !== behavior) { this.onLoseCapture(); } this._captureBehavior = behavior; // this.adornerWindow.capture = true; } popBehavior(behavior:Behavior):Behavior { if (this._behaviorStack.length === 0) { throw new Error("Cannot pop from empty behavior stack"); } const index = this._behaviorStack.indexOf(behavior); if (index === -1) { return null; } this._behaviorStack.splice(index, 1); if (behavior === this._captureBehavior) { // this.adornerWindow.capture = false; this.onLoseCapture(); } return behavior; } doDragDrop(dropSourceBehavior:DropSourceBehavior):void { const ctrl = this.adornerWindow; // 监听 Drag 过程中的反馈事件. ctrl.addEventListener("giveFeedback", dropSourceBehavior.giveFeedback.bind(dropSourceBehavior)); ctrl.addEventListener("queryContinueDrag", dropSourceBehavior.queryContinueDrag.bind(dropSourceBehavior)); // 准备 Drag ctrl.doDragDrop(dropSourceBehavior.dataObject); this._dropSourceBehavior = dropSourceBehavior; } onLoseCapture():void { const behavior = this._captureBehavior; this._captureBehavior = null; behavior.onLoseCapture(this._hitGlyph, null); } /** * 通过向 Adorners 中所有的 Adorner 传播 hitTest,来找到优先响应点击事件的 Glyph。 * 再通过找到响应的 Glyph 找到对应的 Behavior,调用 Behavior 的点击处理方法 * @param mousePos * @returns {boolean} */ propagateHitTest(mousePos:PointF):boolean { let cursor:Cursor = null; if (this._suspendHitTestCount <= 0 && mousePos.x >= 0 && mousePos.y >= 0) { // 从最顶上的 item 开始找,找到了 glyph 返回 for (let j = this.adorners.length - 1; j >=0; j--) { const adorner = this.adorners[j]; if (adorner.enabled && (!this.isDragging || adorner.level === AdornerLevels.Items)) { for (let i = adorner.glyphs.length - 1; i >=0; i--) { // 从最上层的 glyph 开始测试. const glyph = adorner.glyphs[i]; const cursor = this.hitTest(glyph, mousePos); if (cursor) { this.callMouseEnterLevel(this._hitGlyph, glyph, mousePos); if (! this.isDragging) { this.setAppropriateCursor(cursor); } // cursor 不为空,则表示这个 glyph 提供后续的 Behavior. this._hitGlyph = glyph; return true; } } } } } this.callMouseEnterLevel(this._hitGlyph, null, mousePos); if (!this.isDragging) { cursor = Cursor.Default; if (this._behaviorStack && this._behaviorStack.length > 0){ cursor = this._behaviorStack[0].cursor; } this.setAppropriateCursor(cursor); } // 消除原来记录的 hitGlyph this._hitGlyph = null; return false; } onMouseMove(evt:HitMoveEvent, mousePos:PointF):boolean { const behavior = this.getAppropreateBehavior(this._hitGlyph); if (behavior) { return behavior.onMouseMove(this._hitGlyph, evt, mousePos); } return false; } onMouseDown(evt:HitMoveEvent, mousePos:PointF):boolean { const behavior = this.getAppropreateBehavior(this._hitGlyph); if (behavior) { return behavior.onMouseDown(this._hitGlyph, evt, mousePos); } return false; } onDoubleClick(evt:MouseEvent, mousePos:PointF):boolean { const behavior = this.getAppropreateBehavior(this._hitGlyph); if (behavior) { return behavior.onDoubleClick(this._hitGlyph, evt, mousePos); } return false; } onMouseUp():boolean { this.validDragArgs = null; const behavior = this.getAppropreateBehavior(this._hitGlyph); if (behavior) { return behavior.onMouseUp(this._hitGlyph); } return false; } onMouseEnter(glyph:Glyph, mousePos:PointF):boolean { const behavior = this.getAppropreateBehavior(glyph); if (behavior) { return behavior.onMouseEnter(glyph, mousePos); } return false; } onMouseLeave(glyph:Glyph, mousePos:PointF):boolean { const behavior = this.getAppropreateBehavior(glyph); if (behavior) { return behavior.onMouseLeave(glyph, mousePos); } return false; } onMouseOver(mousePos:PointF):boolean { const behavior = this.getAppropreateBehavior(this._hitGlyph); if (behavior) { return behavior.onMouseOver(this._hitGlyph, mousePos); } return false; } onDragEnter(glyph:Glyph, dragArgs:DragEventArgs, mousePos:PointF):boolean { if (! glyph) { glyph = this._hitGlyph; } const behavior = this.getAppropreateBehavior(glyph); if (behavior) { return behavior.onDragEnter(glyph, dragArgs, mousePos); } return false; } onDragLeave(glyph:Glyph):boolean { if (! glyph) { glyph = this._hitGlyph; } const behavior = this.getAppropreateBehavior(glyph); if (behavior) { return behavior.onDragLeave(glyph); } return false; } onDragOver(dragArgs:DragEventArgs, mousePos:PointF):boolean { this._validDragArgs = dragArgs; const behavior = this.getAppropreateBehavior(this._hitGlyph); if (behavior) { return behavior.onDragOver(this._hitGlyph, dragArgs, mousePos); } return false; } onDragDrop(dragArgs:DragEventArgs):boolean { this._validDragArgs = null; const behavior = this.getAppropreateBehavior(this._hitGlyph); const ctrl = this.adornerWindow; ctrl.removeEventListener("giveFeedback",this._dropSourceBehavior.giveFeedback.bind(this._dropSourceBehavior)); ctrl.removeEventListener("queryContinueDrag",this._dropSourceBehavior.queryContinueDrag.bind(this._dropSourceBehavior)); //检查drop的结果, 判断是否有item放进了不能放置的容器中 const items = dragArgs.dataObject.dragComponents; let dragSuccess = true; items.map((dragComponent) => { const item = dragComponent.component; const parent = item.parent; const acceptResult = parent.canAcceptItem ? parent.canAcceptItem(item) : {result: true}; if(!acceptResult.result){ SnackBar.show(acceptResult.msg); dragSuccess = false; } }); if (behavior) { return behavior.onDragDrop(this._hitGlyph, dragArgs) && dragSuccess; } return false; } onKeyDown(evt: HitMoveEvent): boolean { const moveItem = this._selectionService.getPrimarySelection() as ReportItemBase; if(this.allowKeyMove() && moveItem) { // 不能根据固定位置来移动位置,要根据primary selection item 的position来移动,because overlap item const itemBounds = this._host.getDesigner(moveItem).getBounds() ; let pt = new PointF(itemBounds.x+this._primaryOffset.x, itemBounds.y+ this._primaryOffset.y); if(!this.isDragging) { // 连续按下key时,为true const tmp = this._hitGlyph; this.propagateHitTest(pt); if(this._hitGlyph instanceof ItemBodyGlyph && !(this._hitGlyph as any).designer.isParentDesigner() ) { if((this._hitGlyph).item != this._primaryKeyMoveItem) { this._hitGlyph = tmp; } this.onMouseDown(evt, pt); }else if(this.checkHitGlyph(pt)) { // 点击到border上时,为false this.onMouseDown(evt, pt); }else { this.onKeyDown(evt); return false; } } this.onKeyMove(evt, pt); } return false; } checkHitGlyph(pt) { if(this._hitGlyph instanceof SelectionBorderGlyph) { const glyph = this._hitGlyph as SelectionBorderGlyph; let minOffset = PointF.Empty; switch (glyph.borderType) { case SelectionBorderGlyphType.Top: minOffset = new PointF(0,2); break; case SelectionBorderGlyphType.Bottom: minOffset = new PointF(0,-2); break; case SelectionBorderGlyphType.Left: minOffset = new PointF(2,0); break; case SelectionBorderGlyphType.Right: minOffset = new PointF(-2,0); break; } this.setPrimaryCursor(new PointF(pt.x+minOffset.x, pt.y+minOffset.y)); return false; } return true; } onKeyMove(evt: HitMoveEvent, pt: PointF) { // 在adorner window中有对于key move的触发处理 const step = this.computeDragStep(evt) as PointF; this.adornerWindow.doKeyMove(evt,new PointF(pt.x + step.x, pt.y + step.y)); } allowKeyMove(): boolean { return KeyStates.isDown(Keys.Up) || KeyStates.isDown(Keys.Down) || KeyStates.isDown(Keys.Left) || KeyStates.isDown(Keys.Right); } setPrimaryCursor(pt: PointF) { const defaultItem = this._selectionService.getPrimarySelection() as ReportItemBase; if(defaultItem) { this._primaryKeyMoveItem = defaultItem; const itemBounds = this._host.getDesigner(defaultItem).getBounds() ; this._primaryOffset = new PointF(pt.x-itemBounds.x,pt.y-itemBounds.y); } } private computeDragStep(evt:HitMoveEvent): PointF { if(KeyStates.isDown(Keys.Up)) { return new PointF(0, 0-evt.moveStep); } else if(KeyStates.isDown(Keys.Down)) { return new PointF(0, evt.moveStep); } else if( KeyStates.isDown(Keys.Left) ) { return new PointF(0-evt.moveStep,0); } else if(KeyStates.isDown(Keys.Right) ) { return new PointF(evt.moveStep,0); } } /** * 发生在从 leaveGlyph 离开开始进入 enterGlyph 的时机 * @param leaveGlyph * @param enterGlyph * @param mousePos */ private callMouseEnterLevel(leaveGlyph:Glyph, enterGlyph:Glyph, mousePos:PointF) { if (leaveGlyph === enterGlyph) { return; } if (leaveGlyph) { if (this.isDragging) { this.onDragLeave(leaveGlyph); } else { this.onMouseLeave(leaveGlyph, mousePos); } } if (enterGlyph) { if (this.isDragging) { this.onDragEnter(enterGlyph, this.validDragArgs, mousePos); } else { this.onMouseEnter(enterGlyph, mousePos); } } } /** * 返回后续处理的 Behavior * @param glyph * @returns {any} */ private getAppropreateBehavior(glyph:Glyph):Behavior { if (this._behaviorStack.length > 0) { // 如果行为栈中还有 Behavior,则返回最初压入栈的 Behavior. return this._behaviorStack[0]; } if (glyph) { return glyph.behavior; } return null; } private hitTest(glyph:Glyph, mousePos:PointF) { const cursor = glyph.hitTest(mousePos); if (cursor && this.isDragging) { if (glyph instanceof ItemBodyGlyph) { if (glyph.allowDrop) { // Drag 过程中只有允许 Drop 的元素可以处理 return cursor; } } return null; } return cursor; } private setAppropriateCursor(cursor) { this._adornerWindow.setAppropriateCursor(cursor); } dispose() { } }