import {IDisposable, IServiceProvider, IComponent} from "../../interfaces/Interfaces"; import {ReportItemBase} from "../../items/ReportItemBase"; import {PointF, SizeF, RectF, Unit} from "../../drawing/Unit"; import {DesignerHost} from "../DesignerHost"; import {BehaviorService} from "./BehaviorService"; import {ReportItemBaseDesigner} from "../designer/ReportItemBaseDesigner"; import {SelectionService} from "./SelectionService"; import {SelectionTypes, MouseButtons, SelectionRules} from "./Enums"; import {action} from "mobx/lib/mobx"; import {Behavior} from "./Behavior"; import {SelectionGlyphBase} from "./SelectionGlyphBase"; import {DesignerUtils} from "../DesignerUtils"; import {ReportBandBase} from "../../items/band/ReportBandBase"; import {Report} from "../../items/Report"; import {Glyph} from "./Glyph"; import {SnappingEngine} from "./SnappingEngine"; import {SnapLineType, SnapLine, SnapLinePriority} from "./SnapLine"; import {HitMoveEvent} from "../../common/HitMoveEvent"; /** * 调整元素大小的行为 * 在onMouseDown时候通过 SelectionService 得到选中的 ReportItems * 如果开始 resize 处理,则将 ResizeBehavior 设置为 BehaviorService 的 CaptureBehavior。 * 在 onMouseMove 中根据当前鼠标位置结合 snapGrid 的设置计算每一个选中的 ReportItems 最终的位置。 * 在 onMouseUp 的时候调用 onLoseCapture 结束捕获事件的过程 * 在 onLoseCapture 中清理捕获事件过程中产生的临时状态。 */ export class ResizeBehavior extends Behavior{ private _host: DesignerHost; private _behaviorService: BehaviorService; private _snappingEngine: SnappingEngine; private _designer:ReportItemBaseDesigner; private _resizing:boolean; private _cancel:boolean; private _dragging:boolean; private _selectionRules:SelectionRules; private _selectionService: SelectionService; private _primaryItem: ReportItemBase; private _startPos:PointF; private _lastPos:PointF; private _resizeItems:Array; constructor(designer:ReportItemBaseDesigner, serviceProvider:IServiceProvider) { super(serviceProvider); this._designer = designer; this._host = DesignerHost.instance(); this._behaviorService = serviceProvider.getService(BehaviorService) as BehaviorService; this._selectionService = serviceProvider.getService(SelectionService) as SelectionService; } onMouseDown(glyph:Glyph, evt:HitMoveEvent, pt:PointF):boolean { this._resizing = false; this._cancel = true; if (evt.button === MouseButtons.Left) { // 将点中的元素变为首选 (Primary) 状态 this._selectionRules = SelectionRules.None; if (glyph instanceof SelectionGlyphBase) { this._designer.resizing = glyph.rules; this._selectionRules = glyph.rules; if (glyph.item && glyph.item !== this._selectionService.getPrimarySelection()) { this._selectionService.setSelectedItems([glyph.item], SelectionTypes.Primary); } } } if (SelectionRules.None != this._selectionRules) { this._startPos = pt.clone(); this._lastPos = pt.clone(); this._primaryItem = this._selectionService.getPrimarySelection() as ReportItemBase; const dragComponents = []; this._selectionService.getSelectedItems() .filter(item => item instanceof ReportItemBase) .forEach(item => dragComponents.push(item)); if (dragComponents.length > 0) { this._resizeItems = []; for (let item of dragComponents) { const rd = this._host.getDesigner(item) as ReportItemBaseDesigner; if (rd) { // 为每一个选择的元素创建一个 ResizeItem 执行具体的 Resize 行为. this._resizeItems.push(new ResizeItem(rd)); } } this._resizing = true; this._behaviorService.pushCaptureBehavior(this); this._snappingEngine = new SnappingEngine(this._host, dragComponents); } } return false; } onMouseMove(glyph:Glyph, evt:HitMoveEvent, pt:PointF):boolean { if (!this._resizing) { return; } if (!this._lastPos.equals(pt) && pt.x > 0 && pt.y > 0) { if (!this._dragging) { if (!DesignerUtils.isValidDrag(this._startPos, pt)) { return false; } this.initiateResize(); this._dragging = true; } this._lastPos = pt.clone(); const leadingItem = this._resizeItems[0]; // 根据 snap 需求调整位置的值 const pt1 = this.snap(pt, leadingItem); const bounds = this.calculateBounds(leadingItem, pt1); const zoom = this._behaviorService.zoom; const initBounds = leadingItem.originalBounds; const offsetX = (bounds.x - initBounds.x); const offsetY = (bounds.y - initBounds.y); const offsetWidth = (bounds.width - initBounds.width); const offsetHeight = (bounds.height - initBounds.height); for (let item of this._resizeItems) { item.resize(offsetX, offsetY, offsetWidth, offsetHeight, this._selectionRules, zoom); } } return false; } onMouseUp(glyph:Glyph):boolean { this._cancel = true; this.onLoseCapture(glyph, null); return true; } calculateBounds(item:ResizeItem, pt:PointF):RectF { const offset = new SizeF(pt.x - this._startPos.x, pt.y - this._startPos.y); return item.calculateBounds(this._selectionRules, offset); } private snap(mousePos:PointF, leadingItem:ResizeItem):PointF { let pt1 = mousePos.clone(); if (this._snappingEngine) { const pointToSnap = new PointF(0, 0); const lines = this.createSnapLines(this._selectionRules, leadingItem, mousePos, pointToSnap); const snapOffset = this._snappingEngine.snap(pointToSnap, leadingItem.designer.getBounds(), lines); pt1 = pt1.add(snapOffset); } return pt1; } private createSnapLines(rules:SelectionRules, resizeItem:ResizeItem, pt:PointF, snapPoint:PointF):Array { const lines:Array = []; const bounds = this.calculateBounds(resizeItem, pt); const margins = resizeItem.item.margins; const zoom = this._behaviorService.zoom; // 报表根容器元素是无法支持 snapLine 的. const itemSupportsSnapLines = !(resizeItem.item instanceof Report); if ((rules & SelectionRules.TopSizeable) != SelectionRules.None) { if (itemSupportsSnapLines) { lines.push(new SnapLine(SnapLineType.Top, bounds.top)); lines.push(new SnapLine(SnapLineType.HorizontalMiddle, bounds.top + bounds.height / 2.0, SnapLinePriority.Low, SnapLine.MiddleHorizontal)); lines.push(new SnapLine(SnapLineType.Horizontal, bounds.top - zoom * margins.top.fPixels, SnapLinePriority.Always, "Margin.Top")); } snapPoint.y = bounds.top; } else if ((rules & SelectionRules.BottomSizeable) != SelectionRules.None) { if (itemSupportsSnapLines) { lines.push(new SnapLine(SnapLineType.Bottom, bounds.bottom)); lines.push(new SnapLine(SnapLineType.HorizontalMiddle, bounds.bottom - bounds.height / 2.0, SnapLinePriority.Low, SnapLine.MiddleHorizontal)); lines.push(new SnapLine(SnapLineType.Horizontal, bounds.bottom + zoom * margins.bottom.fPixels, SnapLinePriority.Always, "Margin.Bottom")); } snapPoint.y = bounds.bottom; } else if ((rules & SelectionRules.LeftSizeable) != SelectionRules.None) { if (itemSupportsSnapLines) { lines.push(new SnapLine(SnapLineType.Left, bounds.left)); lines.push(new SnapLine(SnapLineType.VerticalMiddle, bounds.left + bounds.width / 2.0, SnapLinePriority.Low, SnapLine.MiddleVertical)); lines.push(new SnapLine(SnapLineType.Vertical, bounds.left - zoom * margins.left.fPixels, SnapLinePriority.Always, "Margin.Left")); } snapPoint.x = bounds.left; } else if ((rules & SelectionRules.RightSizeable) != SelectionRules.None) { if (itemSupportsSnapLines) { lines.push(new SnapLine(SnapLineType.Right, bounds.right)); lines.push(new SnapLine(SnapLineType.VerticalMiddle, bounds.left + bounds.width / 2.0, SnapLinePriority.Low, SnapLine.MiddleVertical)); lines.push(new SnapLine(SnapLineType.Vertical, bounds.right + zoom * margins.right.fPixels, SnapLinePriority.Always, "Margin.Right")); } snapPoint.x = bounds.right; } return lines; } initiateResize():void { // 在 Resize 过程中将 BehaviorService 的 hitTest 暂时挂起,避免产生干扰. this._behaviorService.suspendHitTest(); } onLoseCapture(glyph:Glyph, evt):void { this._designer.resizing = SelectionRules.None; super.onLoseCapture(glyph, evt); if (this._snappingEngine) { this._snappingEngine.dispose(); this._snappingEngine = null; } if (this._resizing) { this._resizing = false; if (this._dragging) { this._dragging = false; } DesignerHost.instance().historyService.snap(); this._behaviorService.popBehavior(this); // 恢复 BehaviorService 的 hitTest this._behaviorService.resumeHitTest(); } } } enum ResizeRules { Left, Right, Top, Bottom } enum UpdateRules { None = 0, Left = 1, Width = 2, Top = 4, Height = 8 } class ResizeItem { private _designer:ReportItemBaseDesigner; private _originalBounds:RectF; private _minSize:SizeF; private _left:Unit; private _top:Unit; private _width:Unit; private _height:Unit; constructor(designer:ReportItemBaseDesigner) { this._designer = designer; this._originalBounds = designer.getBounds().clone(); // 计算可以允许 resize 的 minSize. this._minSize = designer.calculateAllowedShrinkSize(); this._left = designer.item['left'] || Unit.Empty; this._top = designer.item['top'] || Unit.Empty; this._width = designer.item['width'] || Unit.Empty; this._height = designer.item['height'] || Unit.Empty; } get originalBounds():RectF { return this._originalBounds; } get item():ReportItemBase { return this.designer.item; } get designer():ReportItemBaseDesigner { return this._designer; } static canResize(designer:ReportItemBaseDesigner, selectionRules:SelectionRules, resizeRule:ResizeRules):boolean { let rule = SelectionRules.None; // 锁定状态,不允许进行 resize if (designer.locked) { return false; } if (designer.item) { switch (resizeRule) { case ResizeRules.Left: if (designer.item['left'] || designer.item['width']) { rule = SelectionRules.LeftSizeable; } break; case ResizeRules.Right: if (designer.item['width']) { rule = SelectionRules.RightSizeable; } break; case ResizeRules.Top: if (designer.item['top'] || designer.item['height']) { rule = SelectionRules.TopSizeable; } break; case ResizeRules.Bottom: if (designer.item['height']) { rule = SelectionRules.BottomSizeable; } break; } } return ( (selectionRules & rule) != SelectionRules.None && (designer.selectionRules & rule) != SelectionRules.None ); } canResize(selectionRules:SelectionRules, rule:ResizeRules):boolean { return ResizeItem.canResize(this._designer, selectionRules, rule); } /** * 根据 rules 和偏移量计算 resize 后的尺寸 * @param selectionRules * @param offset * @returns {RectF} */ calculateBounds(selectionRules:SelectionRules, offset:SizeF):RectF { let parentBounds = RectF.Empty; let parentDesigner = this._designer.parentDesigner; if (parentDesigner) { parentBounds = parentDesigner.getClientRectangle(true); } const rc = this.originalBounds.clone(); // 检查每一边 resize 后是否超过了 容器的限制. if (this.canResize(selectionRules, ResizeRules.Left)) { if (!parentBounds.isEmpty && (rc.left + offset.width < parentBounds.left)) { offset.width = parentBounds.left - rc.x; } rc.x += offset.width; rc.width -= offset.width; } if (this.canResize(selectionRules, ResizeRules.Right)) { rc.width += offset.width; } if (this.canResize(selectionRules, ResizeRules.Top)) { if (!parentBounds.isEmpty && (rc.y + offset.height < parentBounds.top)) { offset.height = parentBounds.top - rc.y; } rc.y += offset.height; rc.height -= offset.height; } if (this.canResize(selectionRules, ResizeRules.Bottom)) { rc.height += offset.height; } return rc; } /** * 执行 resize ,调整元素的尺寸. * @param offsetX * @param offsetY * @param offsetWidth * @param offsetHeight * @param selectionRules * @param zoom * @returns {boolean} */ resize(offsetX:number, offsetY:number, offsetWidth:number, offsetHeight:number, selectionRules:SelectionRules, zoom:number):boolean { let parentBounds = RectF.Empty; let parentDesigner = this._designer.parentDesigner; if (parentDesigner) { parentBounds = parentDesigner.getClientRectangle(true); } let updateRules = UpdateRules.None; let left = Unit.Empty, top = Unit.Empty, width = Unit.Empty, height = Unit.Empty; if (this.canResize(selectionRules, ResizeRules.Left)) { var offset = offsetX; var x = this.originalBounds.left; if (x + offset < parentBounds.left) { offset = parentBounds.left - x; } if (this.originalBounds.width - offset < this._minSize.width) { offset = this.originalBounds.width - this._minSize.width; } if (offset != offsetX) { offsetWidth += (offsetX - offset); offsetX = offset; } updateRules |= UpdateRules.Left | UpdateRules.Width; left = this._left.add(Unit.pixel(offsetX / zoom)); width = this._width.add(Unit.pixel(offsetWidth / zoom)); } if (this.canResize(selectionRules, ResizeRules.Top)) { var y = this.originalBounds.top; var offset = offsetY; if (y + offset < parentBounds.top) { offset = parentBounds.top - y; } if (this.originalBounds.height - offset < this._minSize.height) { offset = this.originalBounds.height - this._minSize.height; } if (offset != offsetY) { offsetHeight += (offsetY - offset); offsetY = offset; } updateRules |= UpdateRules.Top | UpdateRules.Height; top = this._top.add(Unit.pixel(offsetY / zoom)); height = this._height.add(Unit.pixel(offsetHeight / zoom)); } if (this.canResize(selectionRules, ResizeRules.Right)) { var w = this.originalBounds.width; if (w + offsetWidth < this._minSize.width) { offsetWidth = this._minSize.width - w; } if (parentDesigner && !(parentDesigner.item instanceof Report)) { var r = this.originalBounds.right; if (!ResizeItem.canResize(parentDesigner, selectionRules, ResizeRules.Right) && (r + offsetWidth > parentBounds.right)) { offsetWidth = parentBounds.right - r; } } updateRules |= UpdateRules.Width; width = this._width.add(Unit.pixel(offsetWidth / zoom)); } if (this.canResize(selectionRules, ResizeRules.Bottom)) { var h = this.originalBounds.height; if (h + offsetHeight < this._minSize.height) { offsetHeight = this._minSize.height - h; } if (parentDesigner != null && !(parentDesigner.item instanceof Report)) { var b = this.originalBounds.bottom; if (!ResizeItem.canResize(parentDesigner, selectionRules, ResizeRules.Bottom) && (b + offsetHeight > parentBounds.bottom)) { offsetHeight = parentBounds.bottom - b; } } updateRules |= UpdateRules.Height; height = this._height.add(Unit.pixel(offsetHeight / zoom)); } this.updateItem(left, top, width, height, updateRules); if (null != parentDesigner) { // 同步即时更新容器的大小. if (parentDesigner.updateMinBounds()) { return true; } } return (this._designer.item instanceof ReportBandBase); } updateItem(left:Unit, top:Unit, width:Unit, height:Unit, rules:UpdateRules) { if ((rules & UpdateRules.Left) != 0) { this.left = left; } if ((rules & UpdateRules.Width) != 0) { this.width = width; } if ((rules & UpdateRules.Top) != 0) { this.top = top; } if ((rules & UpdateRules.Height) != 0) { this.height = height; } } private set left(value:Unit) { this._designer.item['left'] = value; } private set top(value:Unit) { this._designer.item['top'] = value; } private set width(value:Unit) { this._designer.item['width'] = value; } private set height(value:Unit) { this._designer.item['height'] = value; } }