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 { PolylineShape, ShapeType } from "types"
|
2021-05-14 12:44:23 +00:00
|
|
|
import { createShape } from "./index"
|
2021-05-13 18:22:16 +00:00
|
|
|
import { intersectPolylineBounds } from "utils/intersections"
|
|
|
|
import { boundsCollide, boundsContained } from "utils/bounds"
|
2021-05-12 21:11:17 +00:00
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
const polyline = createShape<PolylineShape>({
|
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.Polyline,
|
|
|
|
name: "Polyline",
|
|
|
|
parentId: "page0",
|
|
|
|
childIndex: 0,
|
|
|
|
point: [0, 0],
|
|
|
|
points: [[0, 0]],
|
|
|
|
rotation: 0,
|
|
|
|
style: {},
|
|
|
|
...props,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render({ id, points }) {
|
|
|
|
return <polyline id={id} points={points.toString()} />
|
|
|
|
},
|
|
|
|
|
|
|
|
getBounds(shape) {
|
2021-05-14 12:44:23 +00:00
|
|
|
if (this.boundsCache.has(shape)) {
|
|
|
|
return this.boundsCache.get(shape)
|
2021-05-12 22:08:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
let minX = 0
|
|
|
|
let minY = 0
|
|
|
|
let maxX = 0
|
|
|
|
let maxY = 0
|
|
|
|
|
|
|
|
for (let [x, y] of shape.points) {
|
|
|
|
minX = Math.min(x, minX)
|
|
|
|
minY = Math.min(y, minY)
|
|
|
|
maxX = Math.max(x, maxX)
|
|
|
|
maxY = Math.max(y, maxY)
|
|
|
|
}
|
|
|
|
|
2021-05-12 22:08:53 +00:00
|
|
|
const bounds = {
|
2021-05-12 21:11:17 +00:00
|
|
|
minX: minX + shape.point[0],
|
|
|
|
minY: minY + shape.point[1],
|
|
|
|
maxX: maxX + shape.point[0],
|
|
|
|
maxY: maxY + shape.point[1],
|
|
|
|
width: maxX - minX,
|
|
|
|
height: maxY - minY,
|
|
|
|
}
|
2021-05-12 22:08:53 +00:00
|
|
|
|
2021-05-14 12:44:23 +00:00
|
|
|
this.boundsCache.set(shape, bounds)
|
2021-05-12 22:08:53 +00:00
|
|
|
return bounds
|
2021-05-12 21:11:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hitTest(shape) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
2021-05-13 18:22:16 +00:00
|
|
|
hitTestBounds(this, shape, bounds) {
|
|
|
|
const shapeBounds = this.getBounds(shape)
|
|
|
|
return (
|
|
|
|
boundsContained(shapeBounds, bounds) ||
|
|
|
|
(boundsCollide(shapeBounds, bounds) &&
|
|
|
|
intersectPolylineBounds(
|
|
|
|
shape.points.map((point) => vec.add(point, shape.point)),
|
|
|
|
bounds
|
|
|
|
).length > 0)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
|
|
|
scale(shape, scale: number) {
|
|
|
|
return shape
|
|
|
|
},
|
|
|
|
|
|
|
|
stretch(shape, scaleX: number, scaleY: number) {
|
|
|
|
return shape
|
|
|
|
},
|
2021-05-14 12:44:23 +00:00
|
|
|
|
|
|
|
transform(shape, bounds) {
|
|
|
|
const currentBounds = this.getBounds(shape)
|
|
|
|
|
|
|
|
const scaleX = bounds.width / currentBounds.width
|
|
|
|
const scaleY = bounds.height / currentBounds.height
|
|
|
|
|
|
|
|
shape.points = shape.points.map((point) => {
|
|
|
|
let pt = vec.mulV(point, [scaleX, scaleY])
|
|
|
|
return pt
|
|
|
|
})
|
|
|
|
|
|
|
|
shape.point = [bounds.minX, bounds.minY]
|
|
|
|
return shape
|
|
|
|
},
|
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 polyline
|