2021-05-12 21:11:17 +00:00
|
|
|
import { v4 as uuid } from "uuid"
|
|
|
|
import * as vec from "utils/vec"
|
2021-05-22 20:35:01 +00:00
|
|
|
import { RectangleShape, ShapeType } from "types"
|
2021-05-20 09:49:40 +00:00
|
|
|
import { registerShapeUtils } from "./index"
|
2021-05-18 08:32:20 +00:00
|
|
|
import { boundsCollidePolygon, boundsContainPolygon } from "utils/bounds"
|
2021-05-19 09:35:00 +00:00
|
|
|
import {
|
|
|
|
getBoundsFromPoints,
|
|
|
|
getRotatedCorners,
|
|
|
|
translateBounds,
|
|
|
|
} from "utils/utils"
|
2021-05-12 21:11:17 +00:00
|
|
|
|
2021-05-20 09:49:40 +00:00
|
|
|
const rectangle = registerShapeUtils<RectangleShape>({
|
2021-05-14 12:44:23 +00:00
|
|
|
boundsCache: new WeakMap([]),
|
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
create(props) {
|
2021-05-12 21:11:17 +00:00
|
|
|
return {
|
|
|
|
id: uuid(),
|
|
|
|
type: ShapeType.Rectangle,
|
2021-05-15 13:02:13 +00:00
|
|
|
isGenerated: false,
|
2021-05-12 21:11:17 +00:00
|
|
|
name: "Rectangle",
|
|
|
|
parentId: "page0",
|
|
|
|
childIndex: 0,
|
|
|
|
point: [0, 0],
|
|
|
|
size: [1, 1],
|
|
|
|
rotation: 0,
|
2021-05-15 17:11:08 +00:00
|
|
|
style: {
|
2021-05-19 09:35:00 +00:00
|
|
|
fill: "#c6cacb",
|
2021-05-15 17:11:08 +00:00
|
|
|
stroke: "#000",
|
|
|
|
},
|
2021-05-12 21:11:17 +00:00
|
|
|
...props,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render({ id, size }) {
|
|
|
|
return <rect id={id} width={size[0]} height={size[1]} />
|
|
|
|
},
|
|
|
|
|
|
|
|
getBounds(shape) {
|
2021-05-18 08:32:20 +00:00
|
|
|
if (!this.boundsCache.has(shape)) {
|
|
|
|
const [width, height] = shape.size
|
|
|
|
const bounds = {
|
|
|
|
minX: 0,
|
|
|
|
maxX: width,
|
|
|
|
minY: 0,
|
|
|
|
maxY: height,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
|
|
|
|
this.boundsCache.set(shape, bounds)
|
2021-05-12 22:08:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 08:32:20 +00:00
|
|
|
return translateBounds(this.boundsCache.get(shape), shape.point)
|
|
|
|
},
|
|
|
|
|
|
|
|
getRotatedBounds(shape) {
|
2021-05-19 09:35:00 +00:00
|
|
|
return getBoundsFromPoints(
|
|
|
|
getRotatedCorners(this.getBounds(shape), shape.rotation)
|
|
|
|
)
|
2021-05-12 21:11:17 +00:00
|
|
|
},
|
|
|
|
|
2021-05-17 21:27:18 +00:00
|
|
|
getCenter(shape) {
|
2021-05-18 08:32:20 +00:00
|
|
|
const bounds = this.getRotatedBounds(shape)
|
2021-05-17 21:27:18 +00:00
|
|
|
return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
|
|
|
|
},
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
hitTest(shape) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
hitTestBounds(shape, brushBounds) {
|
2021-05-19 09:35:00 +00:00
|
|
|
const rotatedCorners = getRotatedCorners(
|
|
|
|
this.getBounds(shape),
|
|
|
|
shape.rotation
|
|
|
|
)
|
2021-05-18 08:32:20 +00:00
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
return (
|
2021-05-18 08:32:20 +00:00
|
|
|
boundsContainPolygon(brushBounds, rotatedCorners) ||
|
|
|
|
boundsCollidePolygon(brushBounds, rotatedCorners)
|
2021-05-13 18:22:16 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
rotate(shape) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
2021-05-13 06:44:52 +00:00
|
|
|
translate(shape, delta) {
|
|
|
|
shape.point = vec.add(shape.point, delta)
|
2021-05-12 21:11:17 +00:00
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
scale(shape, scale) {
|
2021-05-12 21:11:17 +00:00
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
|
2021-05-19 12:27:01 +00:00
|
|
|
if (shape.rotation === 0) {
|
2021-05-22 20:35:01 +00:00
|
|
|
shape.size = [bounds.width, bounds.height]
|
2021-05-22 20:35:53 +00:00
|
|
|
shape.point = [bounds.minX, bounds.minY]
|
2021-05-18 10:45:29 +00:00
|
|
|
} else {
|
2021-05-22 20:35:53 +00:00
|
|
|
shape.size = vec.mul(
|
|
|
|
initialShape.size,
|
|
|
|
Math.min(Math.abs(scaleX), Math.abs(scaleY))
|
|
|
|
)
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
shape.point = [
|
|
|
|
bounds.minX +
|
|
|
|
(bounds.width - shape.size[0]) *
|
|
|
|
(scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
|
|
|
|
bounds.minY +
|
|
|
|
(bounds.height - shape.size[1]) *
|
|
|
|
(scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
|
|
|
|
]
|
|
|
|
|
|
|
|
shape.rotation =
|
|
|
|
(scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
|
|
|
|
? -initialShape.rotation
|
|
|
|
: initialShape.rotation
|
2021-05-18 10:45:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
return shape
|
|
|
|
},
|
2021-05-15 13:02:13 +00:00
|
|
|
|
2021-05-19 12:27:01 +00:00
|
|
|
transformSingle(shape, bounds) {
|
2021-05-19 09:35:00 +00:00
|
|
|
shape.size = [bounds.width, bounds.height]
|
|
|
|
shape.point = [bounds.minX, bounds.minY]
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
2021-05-15 13:02:13 +00:00
|
|
|
canTransform: true,
|
2021-05-13 18:22:16 +00:00
|
|
|
})
|
2021-05-12 21:11:17 +00:00
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
export default rectangle
|