tldraw/state/sessions/brush-session.ts

193 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-05-10 12:16:57 +00:00
import { current } from "immer"
2021-05-12 21:11:17 +00:00
import { Bounds, Data, ShapeType } from "types"
2021-05-10 12:16:57 +00:00
import BaseSession from "./base-session"
2021-05-12 21:11:17 +00:00
import Shapes from "lib/shapes"
import { getBoundsFromPoints } from "utils/utils"
2021-05-10 12:16:57 +00:00
import * as vec from "utils/vec"
import {
intersectCircleBounds,
intersectPolylineBounds,
} from "utils/intersections"
2021-05-10 12:16:57 +00:00
interface BrushSnapshot {
2021-05-12 11:27:33 +00:00
selectedIds: Set<string>
shapes: { id: string; test: (bounds: Bounds) => boolean }[]
2021-05-10 12:16:57 +00:00
}
export default class BrushSession extends BaseSession {
origin: number[]
snapshot: BrushSnapshot
constructor(data: Data, point: number[]) {
super(data)
this.origin = vec.round(point)
this.snapshot = BrushSession.getSnapshot(data)
}
update = (data: Data, point: number[]) => {
const { origin, snapshot } = this
const brushBounds = getBoundsFromPoints(origin, point)
2021-05-10 12:16:57 +00:00
2021-05-12 11:27:33 +00:00
for (let { test, id } of snapshot.shapes) {
if (test(brushBounds)) {
data.selectedIds.add(id)
} else if (data.selectedIds.has(id)) {
data.selectedIds.delete(id)
}
}
2021-05-10 12:16:57 +00:00
data.brush = brushBounds
2021-05-10 12:16:57 +00:00
}
cancel = (data: Data) => {
data.brush = undefined
2021-05-12 11:27:33 +00:00
data.selectedIds = new Set(this.snapshot.selectedIds)
2021-05-10 12:16:57 +00:00
}
complete = (data: Data) => {
data.brush = undefined
}
2021-05-12 11:27:33 +00:00
/**
* Get a snapshot of the current selected ids, for each shape that is
* not already selected, the shape's id and a test to see whether the
* brush will intersect that shape. For tests, start broad -> fine.
* @param data
* @returns
*/
static getSnapshot(data: Data): BrushSnapshot {
2021-05-10 12:16:57 +00:00
const {
selectedIds,
2021-05-10 12:16:57 +00:00
document: { pages },
currentPageId,
} = current(data)
return {
2021-05-12 11:27:33 +00:00
selectedIds: new Set(data.selectedIds),
shapes: Object.values(pages[currentPageId].shapes)
2021-05-12 11:27:33 +00:00
.filter((shape) => !selectedIds.has(shape.id))
.map((shape) => {
switch (shape.type) {
case ShapeType.Dot: {
2021-05-12 21:11:17 +00:00
const bounds = Shapes[shape.type].getBounds(shape)
return {
2021-05-12 11:27:33 +00:00
id: shape.id,
test: (brushBounds: Bounds) =>
boundsContained(bounds, brushBounds) ||
intersectCircleBounds(shape.point, 4, brushBounds).length > 0,
}
}
case ShapeType.Circle: {
2021-05-12 21:11:17 +00:00
const bounds = Shapes[shape.type].getBounds(shape)
return {
2021-05-12 11:27:33 +00:00
id: shape.id,
test: (brushBounds: Bounds) =>
boundsContained(bounds, brushBounds) ||
intersectCircleBounds(
vec.addScalar(shape.point, shape.radius),
shape.radius,
brushBounds
).length > 0,
}
}
case ShapeType.Rectangle: {
2021-05-12 21:11:17 +00:00
const bounds = Shapes[shape.type].getBounds(shape)
return {
2021-05-12 11:27:33 +00:00
id: shape.id,
test: (brushBounds: Bounds) =>
boundsContained(bounds, brushBounds) ||
boundsCollide(bounds, brushBounds),
}
}
case ShapeType.Polyline: {
2021-05-12 21:11:17 +00:00
const bounds = Shapes[shape.type].getBounds(shape)
const points = shape.points.map((point) =>
vec.add(point, shape.point)
)
return {
2021-05-12 11:27:33 +00:00
id: shape.id,
test: (brushBounds: Bounds) =>
boundsContained(bounds, brushBounds) ||
2021-05-12 11:27:33 +00:00
(boundsCollide(bounds, brushBounds) &&
intersectPolylineBounds(points, brushBounds).length > 0),
}
}
default: {
return undefined
}
}
})
.filter(Boolean),
2021-05-10 12:16:57 +00:00
}
}
}
/**
* Get whether two bounds collide.
* @param a Bounds
* @param b Bounds
* @returns
*/
export function boundsCollide(a: Bounds, b: Bounds) {
return !(
a.maxX < b.minX ||
a.minX > b.maxX ||
a.maxY < b.minY ||
a.minY > b.maxY
)
}
/**
* Get whether the bounds of A contain the bounds of B. A perfect match will return true.
* @param a Bounds
* @param b Bounds
* @returns
*/
export function boundsContain(a: Bounds, b: Bounds) {
return (
a.minX < b.minX && a.minY < b.minY && a.maxY > b.maxY && a.maxX > b.maxX
)
}
/**
* Get whether the bounds of A are contained by the bounds of B.
* @param a Bounds
* @param b Bounds
* @returns
*/
export function boundsContained(a: Bounds, b: Bounds) {
return boundsContain(b, a)
}
/**
* Get whether two bounds are identical.
* @param a Bounds
* @param b Bounds
* @returns
*/
export function boundsAreEqual(a: Bounds, b: Bounds) {
return !(
b.maxX !== a.maxX ||
b.minX !== a.minX ||
b.maxY !== a.maxY ||
b.minY !== a.minY
)
}
/**
* Get whether a point is inside of a bounds.
* @param A
* @param b
* @returns
*/
export function pointInBounds(A: number[], b: Bounds) {
return !(A[0] < b.minX || A[0] > b.maxX || A[1] < b.minY || A[1] > b.maxY)
}