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"
|
2021-05-11 10:13:07 +00:00
|
|
|
import { getBoundsFromPoints } from "utils/utils"
|
2021-05-10 12:16:57 +00:00
|
|
|
import * as vec from "utils/vec"
|
2021-05-12 09:18:11 +00:00
|
|
|
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
|
|
|
|
|
2021-05-11 10:13:07 +00:00
|
|
|
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
|
|
|
|
2021-05-11 10:13:07 +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
|
|
|
|
*/
|
2021-05-12 09:18:11 +00:00
|
|
|
static getSnapshot(data: Data): BrushSnapshot {
|
2021-05-10 12:16:57 +00:00
|
|
|
const {
|
2021-05-11 10:13:07 +00:00
|
|
|
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),
|
2021-05-11 10:13:07 +00:00
|
|
|
shapes: Object.values(pages[currentPageId].shapes)
|
2021-05-12 11:27:33 +00:00
|
|
|
.filter((shape) => !selectedIds.has(shape.id))
|
2021-05-11 10:13:07 +00:00
|
|
|
.map((shape) => {
|
|
|
|
switch (shape.type) {
|
|
|
|
case ShapeType.Dot: {
|
2021-05-12 21:11:17 +00:00
|
|
|
const bounds = Shapes[shape.type].getBounds(shape)
|
2021-05-12 09:18:11 +00:00
|
|
|
|
2021-05-11 10:13:07 +00:00
|
|
|
return {
|
2021-05-12 11:27:33 +00:00
|
|
|
id: shape.id,
|
2021-05-12 09:18:11 +00:00
|
|
|
test: (brushBounds: Bounds) =>
|
|
|
|
boundsContained(bounds, brushBounds) ||
|
|
|
|
intersectCircleBounds(shape.point, 4, brushBounds).length > 0,
|
2021-05-11 10:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case ShapeType.Circle: {
|
2021-05-12 21:11:17 +00:00
|
|
|
const bounds = Shapes[shape.type].getBounds(shape)
|
2021-05-12 09:18:11 +00:00
|
|
|
|
2021-05-11 10:13:07 +00:00
|
|
|
return {
|
2021-05-12 11:27:33 +00:00
|
|
|
id: shape.id,
|
2021-05-12 09:18:11 +00:00
|
|
|
test: (brushBounds: Bounds) =>
|
|
|
|
boundsContained(bounds, brushBounds) ||
|
2021-05-12 09:48:35 +00:00
|
|
|
intersectCircleBounds(
|
|
|
|
vec.addScalar(shape.point, shape.radius),
|
|
|
|
shape.radius,
|
|
|
|
brushBounds
|
|
|
|
).length > 0,
|
2021-05-11 10:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case ShapeType.Rectangle: {
|
2021-05-12 21:11:17 +00:00
|
|
|
const bounds = Shapes[shape.type].getBounds(shape)
|
2021-05-12 09:18:11 +00:00
|
|
|
|
|
|
|
return {
|
2021-05-12 11:27:33 +00:00
|
|
|
id: shape.id,
|
2021-05-12 09:18:11 +00:00
|
|
|
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)
|
2021-05-12 09:18:11 +00:00
|
|
|
const points = shape.points.map((point) =>
|
|
|
|
vec.add(point, shape.point)
|
|
|
|
)
|
|
|
|
|
2021-05-11 10:13:07 +00:00
|
|
|
return {
|
2021-05-12 11:27:33 +00:00
|
|
|
id: shape.id,
|
2021-05-12 09:18:11 +00:00
|
|
|
test: (brushBounds: Bounds) =>
|
|
|
|
boundsContained(bounds, brushBounds) ||
|
2021-05-12 11:27:33 +00:00
|
|
|
(boundsCollide(bounds, brushBounds) &&
|
|
|
|
intersectPolylineBounds(points, brushBounds).length > 0),
|
2021-05-11 10:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean),
|
2021-05-10 12:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 10:13:07 +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)
|
|
|
|
}
|