2021-05-28 14:37:23 +00:00
|
|
|
import { current } from 'immer'
|
|
|
|
import { Data, DrawShape } from 'types'
|
|
|
|
import BaseSession from './base-session'
|
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
2021-06-04 16:08:43 +00:00
|
|
|
import { getPage, getShape, updateParents } from 'utils/utils'
|
2021-05-28 14:37:23 +00:00
|
|
|
import * as vec from 'utils/vec'
|
|
|
|
import commands from 'state/commands'
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
let prevEndPoint: number[]
|
|
|
|
|
2021-05-27 17:59:40 +00:00
|
|
|
export default class BrushSession extends BaseSession {
|
|
|
|
origin: number[]
|
2021-05-27 20:11:48 +00:00
|
|
|
previous: number[]
|
2021-06-06 07:33:30 +00:00
|
|
|
last: number[]
|
2021-05-27 17:59:40 +00:00
|
|
|
points: number[][]
|
|
|
|
snapshot: DrawSnapshot
|
2021-05-30 21:24:31 +00:00
|
|
|
isLocked: boolean
|
|
|
|
lockedDirection: 'horizontal' | 'vertical'
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
constructor(data: Data, id: string, point: number[], isLocked = false) {
|
2021-05-27 17:59:40 +00:00
|
|
|
super(data)
|
|
|
|
this.origin = point
|
2021-05-27 20:11:48 +00:00
|
|
|
this.previous = point
|
2021-06-06 07:33:30 +00:00
|
|
|
this.last = point
|
2021-05-27 20:11:48 +00:00
|
|
|
this.points = []
|
2021-05-27 17:59:40 +00:00
|
|
|
this.snapshot = getDrawSnapshot(data, id)
|
|
|
|
|
|
|
|
const page = getPage(data)
|
|
|
|
const shape = page.shapes[id]
|
2021-06-04 16:08:43 +00:00
|
|
|
getShapeUtils(shape).translateTo(shape, point)
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 07:33:30 +00:00
|
|
|
update = (
|
|
|
|
data: Data,
|
|
|
|
point: number[],
|
|
|
|
pressure: number,
|
|
|
|
isLocked = false
|
|
|
|
) => {
|
2021-05-29 12:54:33 +00:00
|
|
|
const { snapshot } = this
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
const delta = vec.vec(this.origin, point)
|
|
|
|
|
|
|
|
if (isLocked) {
|
|
|
|
if (!this.isLocked && this.points.length > 1) {
|
|
|
|
this.isLocked = true
|
|
|
|
const returning = [...this.previous]
|
|
|
|
|
|
|
|
if (Math.abs(delta[0]) < Math.abs(delta[1])) {
|
|
|
|
this.lockedDirection = 'vertical'
|
|
|
|
returning[0] = this.origin[0]
|
|
|
|
} else {
|
|
|
|
this.lockedDirection = 'horizontal'
|
|
|
|
returning[1] = this.origin[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
this.previous = returning
|
|
|
|
this.points.push(vec.sub(returning, this.origin))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.isLocked) {
|
|
|
|
this.isLocked = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isLocked) {
|
|
|
|
if (this.lockedDirection === 'vertical') {
|
|
|
|
point[0] = this.origin[0]
|
|
|
|
} else {
|
|
|
|
point[1] = this.origin[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 07:33:30 +00:00
|
|
|
point = vec.med(this.previous, point)
|
|
|
|
|
2021-06-11 10:11:42 +00:00
|
|
|
const next = vec.round([...vec.sub(point, this.origin), pressure])
|
2021-05-30 21:24:31 +00:00
|
|
|
|
2021-06-06 07:33:30 +00:00
|
|
|
// Don't add duplicate points
|
|
|
|
if (vec.isEqual(this.last, next)) return
|
2021-05-30 21:24:31 +00:00
|
|
|
|
|
|
|
this.points.push(next)
|
2021-06-06 07:33:30 +00:00
|
|
|
|
|
|
|
this.last = next
|
2021-05-30 21:24:31 +00:00
|
|
|
this.previous = point
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
const shape = getShape(data, snapshot.id) as DrawShape
|
2021-05-29 12:54:33 +00:00
|
|
|
getShapeUtils(shape).setProperty(shape, 'points', [...this.points])
|
2021-06-04 16:14:01 +00:00
|
|
|
updateParents(data, [shape.id])
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel = (data: Data) => {
|
2021-05-29 12:54:33 +00:00
|
|
|
const { snapshot } = this
|
2021-06-04 16:08:43 +00:00
|
|
|
const shape = getShape(data, snapshot.id) as DrawShape
|
2021-05-29 12:54:33 +00:00
|
|
|
getShapeUtils(shape).setProperty(shape, 'points', snapshot.points)
|
2021-06-04 16:14:01 +00:00
|
|
|
updateParents(data, [shape.id])
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
complete = (data: Data) => {
|
2021-06-07 11:18:50 +00:00
|
|
|
const { snapshot } = this
|
|
|
|
const page = getPage(data)
|
|
|
|
const shape = page.shapes[snapshot.id] as DrawShape
|
2021-05-29 22:27:19 +00:00
|
|
|
|
2021-06-07 11:18:50 +00:00
|
|
|
getShapeUtils(shape).onSessionComplete(shape)
|
|
|
|
commands.draw(data, this.snapshot.id)
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDrawSnapshot(data: Data, shapeId: string) {
|
|
|
|
const page = getPage(current(data))
|
2021-06-02 11:50:34 +00:00
|
|
|
const { points } = page.shapes[shapeId] as DrawShape
|
|
|
|
|
2021-05-27 17:59:40 +00:00
|
|
|
return {
|
2021-05-29 12:54:33 +00:00
|
|
|
id: shapeId,
|
2021-05-27 17:59:40 +00:00
|
|
|
points,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DrawSnapshot = ReturnType<typeof getDrawSnapshot>
|