import {RectF} from "../../drawing/Unit"; import {DesignerStyleSettings} from "../DesignerStyleSettings"; export enum SnapLineType { Top, Bottom, Left, Right, Horizontal, Vertical, HorizontalMiddle, VerticalMiddle, Baseline } export enum SnapLinePriority { Low = 1 , Medium , High , Always } let SnapLineCount = 0; export class SnapLine { static Margin = "Margin"; static MarginBottom = "Margin.Bottom"; static MarginLeft = "Margin.Left"; static MarginRight = "Margin.Right"; static MarginTop = "Margin.Top"; static Padding = "Padding"; static PaddingBottom = "Padding.Bottom"; static PaddingLeft = "Padding.Left"; static PaddingRight = "Padding.Right"; static PaddingTop = "Padding.Top"; static Middle = "Middle"; static MiddleHorizontal = "Middle.Horizontal"; static MiddleVertical = "Middle.Vertical"; private _offset:number; private _filter = ''; private _priority: SnapLinePriority; private _type: SnapLineType; private _index = `${SnapLineCount++}`; public tag:string; constructor(type:SnapLineType, offset:number, priority:SnapLinePriority = SnapLinePriority.Low, filter = '') { this._offset = offset; this._filter = filter; this._priority = priority; this._type = type; } get index():string { return this._index; } get offset():number { return this._offset; } get filter():string { return this._filter; } get priority():SnapLinePriority { return this._priority; } get snapLineType():SnapLineType { return this._type; } get isHorizontal():boolean { return (this._type == SnapLineType.Top || this._type == SnapLineType.Bottom || this._type == SnapLineType.HorizontalMiddle || this._type == SnapLineType.Horizontal || this._type == SnapLineType.Baseline); } get isVertical():boolean { return (this._type == SnapLineType.Left || this._type == SnapLineType.Right || this._type == SnapLineType.VerticalMiddle || this._type == SnapLineType.Vertical); } adjustOffset(adjustment:number) { this._offset += adjustment; } static shouldSnap(line1:SnapLine, bounds1:RectF, line2:SnapLine, bounds2:RectF):boolean { if (line1.snapLineType !== line2.snapLineType) { return false; } if (line1.filter === null && line2.filter === null) { return true; } if (line1.filter === null || line2.filter === null) { return false; } // if (line1.filter.indexOf(SnapLine.Middle) >= 0) // { // switch (line1.filter) // { // case SnapLine.MiddleVertical: // return Math.abs(Math.abs(bounds1.center.x) - Math.abs(bounds2.center.x)) < DesignerStyleSettings.FloatEpsilon // && line2.filter == SnapLine.MiddleVertical; // // case SnapLine.MiddleHorizontal: // return Math.abs(Math.abs(bounds1.center.y) - Math.abs(bounds2.center.y)) < DesignerStyleSettings.FloatEpsilon // && line2.filter == SnapLine.MiddleHorizontal; // } // } if (line1.filter.indexOf(SnapLine.Margin) >= 0) { switch (line1.filter) { case SnapLine.MarginRight: return SnapLine.horizontalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginLeft || line2.filter == SnapLine.PaddingRight); case SnapLine.MarginLeft: return SnapLine.horizontalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginRight || line2.filter == SnapLine.PaddingLeft); case SnapLine.MarginTop: return SnapLine.verticalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginBottom || line2.filter == SnapLine.PaddingTop); case SnapLine.MarginBottom: return SnapLine.verticalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginTop || line2.filter == SnapLine.PaddingBottom); } } if (line1.filter.indexOf(SnapLine.Padding) >= 0) { switch (line1.filter) { case SnapLine.PaddingTop: return SnapLine.verticalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginTop); case SnapLine.PaddingBottom: return SnapLine.verticalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginBottom); case SnapLine.PaddingLeft: return SnapLine.horizontalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginLeft); case SnapLine.PaddingRight: return SnapLine.horizontalIntersects(bounds1, bounds2) && (line2.filter == SnapLine.MarginRight); } } return line1.filter == line2.filter; } static horizontalIntersects(rc1:RectF, rc2:RectF):boolean { return ((rc1.top <= rc2.top && rc2.top <= rc1.bottom) || (rc2.top <= rc1.top && rc1.top <= rc2.bottom)); } static verticalIntersects(rc1:RectF, rc2:RectF):boolean { return ((rc1.left <= rc2.left && rc2.left <= rc1.right) || (rc2.left <= rc1.left && rc1.left <= rc2.right)); } }