2021-06-06 10:50:01 +00:00
|
|
|
import { PointerInfo } from 'types'
|
|
|
|
import {
|
|
|
|
getCameraZoom,
|
|
|
|
getCurrentCamera,
|
2021-06-07 11:18:50 +00:00
|
|
|
getSelectedIds,
|
2021-06-06 10:50:01 +00:00
|
|
|
screenToWorld,
|
2021-06-07 11:18:50 +00:00
|
|
|
setToArray,
|
2021-06-24 08:18:14 +00:00
|
|
|
} from 'utils'
|
2021-06-16 12:09:45 +00:00
|
|
|
import { freeze } from 'immer'
|
2021-06-06 10:50:01 +00:00
|
|
|
import session from './session'
|
|
|
|
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-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-06-23 12:46:16 +00:00
|
|
|
session.update<Session.DrawSession>(
|
2021-06-06 10:50:01 +00:00
|
|
|
data,
|
|
|
|
screenToWorld(info.point, data),
|
|
|
|
info.pressure,
|
|
|
|
info.shiftKey
|
|
|
|
)
|
|
|
|
|
2021-06-07 11:18:50 +00:00
|
|
|
const selectedId = setToArray(getSelectedIds(data))[0]
|
2021-06-06 10:50:01 +00:00
|
|
|
|
|
|
|
const shape = data.document.pages[data.currentPageId].shapes[selectedId]
|
|
|
|
|
|
|
|
data.document.pages[data.currentPageId].shapes[selectedId] = { ...shape }
|
|
|
|
|
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 }
|
|
|
|
const camera = getCurrentCamera(data)
|
|
|
|
camera.point = vec.sub(camera.point, vec.div(delta, camera.zoom))
|
|
|
|
|
|
|
|
data.pageStates[data.currentPageId].camera = { ...camera }
|
|
|
|
|
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 }
|
|
|
|
const camera = getCurrentCamera(data)
|
|
|
|
|
|
|
|
const next = camera.zoom - (delta / 100) * camera.zoom
|
|
|
|
|
|
|
|
const p0 = screenToWorld(point, data)
|
|
|
|
camera.zoom = getCameraZoom(next)
|
|
|
|
const p1 = screenToWorld(point, data)
|
|
|
|
camera.point = vec.add(camera.point, vec.sub(p1, p0))
|
|
|
|
|
|
|
|
data.pageStates[data.currentPageId].camera = { ...camera }
|
|
|
|
|
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 }
|
|
|
|
const camera = getCurrentCamera(data)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
const p0 = screenToWorld(point, data)
|
|
|
|
camera.zoom = getCameraZoom(next)
|
|
|
|
const p1 = screenToWorld(point, data)
|
|
|
|
camera.point = vec.add(camera.point, vec.sub(p1, p0))
|
|
|
|
|
2021-06-15 12:20:22 +00:00
|
|
|
const pageState = data.pageStates[data.currentPageId]
|
|
|
|
pageState.camera = { ...camera }
|
|
|
|
|
|
|
|
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-23 12:46:16 +00:00
|
|
|
session.update<Session.BrushSession>(data, screenToWorld(point, data))
|
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-13 13:55:37 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export function fastTranslate(info: PointerInfo): void {
|
2021-06-13 13:55:37 +00:00
|
|
|
const data = { ...state.data }
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
session.update<Session.TranslateSession>(
|
2021-06-13 13:55:37 +00:00
|
|
|
data,
|
|
|
|
screenToWorld(info.point, data),
|
|
|
|
info.shiftKey,
|
|
|
|
info.altKey
|
|
|
|
)
|
|
|
|
|
2021-06-16 12:09:45 +00:00
|
|
|
state.forceData(freeze(data))
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export function fastTransform(info: PointerInfo): void {
|
2021-06-16 12:09:45 +00:00
|
|
|
const data = { ...state.data }
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
session.update<Session.TransformSession | Session.TransformSingleSession>(
|
2021-06-16 12:09:45 +00:00
|
|
|
data,
|
|
|
|
screenToWorld(info.point, data),
|
2021-06-23 12:46:16 +00:00
|
|
|
info.shiftKey
|
2021-06-16 12:09:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
state.forceData(freeze(data))
|
2021-06-13 13:55:37 +00:00
|
|
|
}
|