import { Matrix, Rectangle } from '@gedit/math'; import type { PositionSchema, TransformSchema } from '../schema'; function prepareBoundsOutput(output: PositionSchema, target: TransformSchema, matrix?: Matrix): PositionSchema { // if (target.rotation !== 0) { // rotateAround(output, target.position.x, target.position.y, target.rotation); // } if (matrix) { matrix.apply(output, output); } // fix: -0 if (output.x === 0) output.x = 0; if (output.y === 0) output.y = 0; return output; } export namespace Bounds { /** * 获取外围边界矩形 * @param target * @param matrix */ export function getBounds(target: TransformSchema, matrix?: Matrix): Rectangle { const output = new Rectangle(); const topLeft = getTopLeft(target, matrix); const topRight = getTopRight(target, matrix); const bottomLeft = getBottomLeft(target, matrix); const bottomRight = getBottomRight(target, matrix); output.x = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x) || 0; output.y = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y) || 0; output.width = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x) - output.x || 0; output.height = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y) - output.y || 0; return output; } /** * 找到边框中最左边的点 * @param target * @param matrix */ export function getLeftPointFromBounds(target: TransformSchema, matrix?: Matrix): PositionSchema { const topLeft = getTopLeft(target, matrix); const topRight = getTopRight(target, matrix); const bottomLeft = getBottomLeft(target, matrix); const bottomRight = getBottomRight(target, matrix); const items = [topLeft, topRight, bottomLeft, bottomRight].sort((p1, p2) => p1.x - p2.x); return items[0]; } /** * 找到边框中最上边的点 * @param target * @param matrix */ export function getTopPointFromBounds(target: TransformSchema, matrix?: Matrix): PositionSchema { const topLeft = getTopLeft(target, matrix); const topRight = getTopRight(target, matrix); const bottomLeft = getBottomLeft(target, matrix); const bottomRight = getBottomRight(target, matrix); const items = [topLeft, topRight, bottomLeft, bottomRight].sort((p1, p2) => p1.y - p2.y); return items[0]; } export function getCenter(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x) + (size.width / 2), y: - (size.height * origin.y) + (size.height / 2) }; return prepareBoundsOutput(output, target, matrix); } export function getTopLeft(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x), y: - (size.height * origin.y), }; return prepareBoundsOutput(output, target, matrix); } export function getTopCenter(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x) + (size.width / 2), y: - (size.height * origin.y) }; return prepareBoundsOutput(output, target, matrix); } export function getTopRight(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x) + size.width, y: - (size.height * origin.y), }; return prepareBoundsOutput(output, target, matrix); } export function getLeftCenter(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x), y: - (size.height * origin.y) + (size.height / 2), }; return prepareBoundsOutput(output, target, matrix); } export function getRightCenter(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x) + size.width, y: - (size.height * origin.y) + (size.height / 2), }; return prepareBoundsOutput(output, target, matrix); } export function getBottomLeft(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x), y: - (size.height * origin.y) + size.height, }; return prepareBoundsOutput(output, target, matrix); } export function getBottomCenter(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x) + (size.width / 2), y: - (size.height * origin.y) + size.height, }; return prepareBoundsOutput(output, target, matrix); } export function getBottomRight(target: TransformSchema, matrix?: Matrix): PositionSchema { const { size, origin } = target; const output = { x: - (size.width * origin.x) + size.width, y: - (size.height * origin.y) + size.height, }; return prepareBoundsOutput(output, target, matrix); } }