2021-06-27 09:07:20 +00:00
|
|
|
import { DrawShape, PointerInfo } from 'types'
|
2021-07-09 08:59:43 +00:00
|
|
|
import { deepClone } from 'utils'
|
2021-06-29 12:00:59 +00:00
|
|
|
import tld from 'utils/tld'
|
2021-06-16 12:09:45 +00:00
|
|
|
import { freeze } from 'immer'
|
2021-06-06 10:50:01 +00:00
|
|
|
import session from './session'
|
2021-07-02 12:04:45 +00:00
|
|
|
import coopState from 'state/coop/coop-state'
|
2021-06-06 10:50:01 +00:00
|
|
|
import state from './state'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-23 12:46:16 +00:00
|
|
|
import * as Session from './sessions'
|
2021-06-06 10:50:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* While a user is drawing with the draw tool, we want to update the shape without
|
|
|
|
* going through the trouble of updating the entire state machine. Speciifcally, we
|
|
|
|
* do not want to push the change through immer. Instead, we'll push the change
|
|
|
|
* directly to the state using `forceData`.
|
|
|
|
* @param info
|
|
|
|
*/
|
2021-07-04 18:45:07 +00:00
|
|
|
|
|
|
|
export function fastTranslate(info: PointerInfo): void {
|
|
|
|
const data = { ...state.data }
|
|
|
|
|
|
|
|
session.update<Session.TranslateSession>(
|
|
|
|
data,
|
|
|
|
tld.screenToWorld(info.point, data),
|
|
|
|
info.shiftKey,
|
|
|
|
info.altKey
|
|
|
|
)
|
|
|
|
|
|
|
|
state.forceData(freeze(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function fastTransform(info: PointerInfo): void {
|
|
|
|
const data = { ...state.data }
|
|
|
|
|
|
|
|
session.update<Session.TransformSession | Session.TransformSingleSession>(
|
|
|
|
data,
|
|
|
|
tld.screenToWorld(info.point, data),
|
|
|
|
info.shiftKey
|
|
|
|
)
|
|
|
|
|
|
|
|
state.forceData(freeze(data))
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export function fastDrawUpdate(info: PointerInfo): void {
|
2021-06-06 10:50:01 +00:00
|
|
|
const data = { ...state.data }
|
|
|
|
|
2021-07-04 18:45:07 +00:00
|
|
|
coopState.send('MOVED_CURSOR', {
|
2021-07-02 12:04:45 +00:00
|
|
|
pageId: data.currentPageId,
|
|
|
|
point: info.point,
|
|
|
|
})
|
2021-06-30 20:31:29 +00:00
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
session.update<Session.DrawSession>(
|
2021-06-06 10:50:01 +00:00
|
|
|
data,
|
2021-06-29 12:00:59 +00:00
|
|
|
tld.screenToWorld(info.point, data),
|
2021-06-06 10:50:01 +00:00
|
|
|
info.pressure,
|
|
|
|
info.shiftKey
|
|
|
|
)
|
|
|
|
|
2021-07-09 08:59:43 +00:00
|
|
|
const selectedId = [...tld.getSelectedIds(data)][0]
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-29 18:30:57 +00:00
|
|
|
const { shapes } = data.document.pages[data.currentPageId]
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-29 18:30:57 +00:00
|
|
|
const shape = shapes[selectedId] as DrawShape
|
|
|
|
|
|
|
|
shapes[selectedId] = deepClone(shape)
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
state.forceData(freeze(data))
|
2021-06-06 10:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export function fastPanUpdate(delta: number[]): void {
|
2021-06-06 10:50:01 +00:00
|
|
|
const data = { ...state.data }
|
2021-06-29 12:00:59 +00:00
|
|
|
const camera = tld.getCurrentCamera(data)
|
2021-06-06 10:50:01 +00:00
|
|
|
camera.point = vec.sub(camera.point, vec.div(delta, camera.zoom))
|
|
|
|
|
2021-06-29 18:08:25 +00:00
|
|
|
data.pageStates[data.currentPageId].camera = deepClone(camera)
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
state.forceData(freeze(data))
|
2021-06-06 10:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export function fastZoomUpdate(point: number[], delta: number): void {
|
2021-06-06 10:50:01 +00:00
|
|
|
const data = { ...state.data }
|
2021-06-29 12:00:59 +00:00
|
|
|
const camera = tld.getCurrentCamera(data)
|
2021-06-06 10:50:01 +00:00
|
|
|
|
|
|
|
const next = camera.zoom - (delta / 100) * camera.zoom
|
|
|
|
|
2021-06-29 12:00:59 +00:00
|
|
|
const p0 = tld.screenToWorld(point, data)
|
|
|
|
camera.zoom = tld.getCameraZoom(next)
|
|
|
|
const p1 = tld.screenToWorld(point, data)
|
2021-06-06 10:50:01 +00:00
|
|
|
camera.point = vec.add(camera.point, vec.sub(p1, p0))
|
|
|
|
|
2021-06-29 18:08:25 +00:00
|
|
|
data.pageStates[data.currentPageId].camera = deepClone(camera)
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
state.forceData(freeze(data))
|
2021-06-06 10:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function fastPinchCamera(
|
|
|
|
point: number[],
|
|
|
|
delta: number[],
|
2021-06-21 21:35:28 +00:00
|
|
|
distanceDelta: number
|
|
|
|
): void {
|
2021-06-06 10:50:01 +00:00
|
|
|
const data = { ...state.data }
|
2021-06-29 12:00:59 +00:00
|
|
|
const camera = tld.getCurrentCamera(data)
|
2021-06-06 10:50:01 +00:00
|
|
|
|
|
|
|
camera.point = vec.sub(camera.point, vec.div(delta, camera.zoom))
|
|
|
|
|
2021-06-18 15:19:10 +00:00
|
|
|
const next = camera.zoom - (distanceDelta / 350) * camera.zoom
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-29 12:00:59 +00:00
|
|
|
const p0 = tld.screenToWorld(point, data)
|
|
|
|
camera.zoom = tld.getCameraZoom(next)
|
|
|
|
const p1 = tld.screenToWorld(point, data)
|
2021-06-06 10:50:01 +00:00
|
|
|
camera.point = vec.add(camera.point, vec.sub(p1, p0))
|
|
|
|
|
2021-06-15 12:20:22 +00:00
|
|
|
const pageState = data.pageStates[data.currentPageId]
|
2021-06-29 17:58:07 +00:00
|
|
|
|
|
|
|
pageState.camera = deepClone(camera)
|
2021-06-15 12:20:22 +00:00
|
|
|
|
|
|
|
data.pageStates[data.currentPageId] = { ...pageState }
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
state.forceData(freeze(data))
|
2021-06-06 10:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export function fastBrushSelect(point: number[]): void {
|
2021-06-06 10:50:01 +00:00
|
|
|
const data = { ...state.data }
|
2021-06-13 13:55:37 +00:00
|
|
|
|
2021-06-29 12:00:59 +00:00
|
|
|
session.update<Session.BrushSession>(data, tld.screenToWorld(point, data))
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-29 17:58:07 +00:00
|
|
|
data.brush = deepClone(data.brush)
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
state.forceData(freeze(data))
|
2021-06-06 10:50:01 +00:00
|
|
|
}
|