2021-05-12 21:11:17 +00:00
|
|
|
import { v4 as uuid } from "uuid"
|
|
|
|
import * as vec from "utils/vec"
|
2021-05-13 18:22:16 +00:00
|
|
|
import { RectangleShape, ShapeType } from "types"
|
2021-05-14 12:44:23 +00:00
|
|
|
import { createShape } from "./index"
|
2021-05-18 08:32:20 +00:00
|
|
|
import { boundsCollidePolygon, boundsContainPolygon } from "utils/bounds"
|
|
|
|
import { getBoundsFromPoints, rotateBounds, translateBounds } from "utils/utils"
|
2021-05-12 21:11:17 +00:00
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
const rectangle = createShape<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-17 21:27:18 +00:00
|
|
|
fill: "rgba(142, 143, 142, 1.000)",
|
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) {
|
|
|
|
const b = this.getBounds(shape)
|
|
|
|
const center = [b.minX + b.width / 2, b.minY + b.height / 2]
|
2021-05-12 22:08:53 +00:00
|
|
|
|
2021-05-18 08:32:20 +00:00
|
|
|
// Rotate corners of the shape, then find the minimum among those points.
|
|
|
|
const rotatedCorners = [
|
|
|
|
[b.minX, b.minY],
|
|
|
|
[b.maxX, b.minY],
|
|
|
|
[b.maxX, b.maxY],
|
|
|
|
[b.minX, b.maxY],
|
|
|
|
].map((point) => vec.rotWith(point, center, shape.rotation))
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-05-18 08:32:20 +00:00
|
|
|
return getBoundsFromPoints(rotatedCorners)
|
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-18 08:32:20 +00:00
|
|
|
const b = this.getBounds(shape)
|
|
|
|
const center = [b.minX + b.width / 2, b.minY + b.height / 2]
|
|
|
|
|
|
|
|
const rotatedCorners = [
|
|
|
|
[b.minX, b.minY],
|
|
|
|
[b.maxX, b.minY],
|
|
|
|
[b.maxX, b.maxY],
|
|
|
|
[b.minX, b.maxY],
|
|
|
|
].map((point) => vec.rotWith(point, center, shape.rotation))
|
|
|
|
|
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-14 12:44:23 +00:00
|
|
|
transform(shape, bounds) {
|
|
|
|
shape.point = [bounds.minX, bounds.minY]
|
|
|
|
shape.size = [bounds.width, bounds.height]
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
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
|