2021-05-28 16:25:43 +00:00
|
|
|
import { Data } from 'types'
|
|
|
|
import * as vec from 'utils/vec'
|
|
|
|
import BaseSession from './base-session'
|
|
|
|
import commands from 'state/commands'
|
|
|
|
import { current } from 'immer'
|
2021-05-22 15:45:24 +00:00
|
|
|
import {
|
2021-05-23 08:30:20 +00:00
|
|
|
clampToRotationToSegments,
|
2021-05-22 15:45:24 +00:00
|
|
|
getBoundsCenter,
|
|
|
|
getCommonBounds,
|
|
|
|
getPage,
|
|
|
|
getSelectedShapes,
|
|
|
|
getShapeBounds,
|
2021-05-28 16:25:43 +00:00
|
|
|
} from 'utils/utils'
|
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
const PI2 = Math.PI * 2
|
2021-05-17 21:27:18 +00:00
|
|
|
|
|
|
|
export default class RotateSession extends BaseSession {
|
|
|
|
delta = [0, 0]
|
|
|
|
origin: number[]
|
|
|
|
snapshot: RotateSnapshot
|
|
|
|
|
|
|
|
constructor(data: Data, point: number[]) {
|
|
|
|
super(data)
|
|
|
|
this.origin = point
|
|
|
|
this.snapshot = getRotateSnapshot(data)
|
|
|
|
}
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
update(data: Data, point: number[], isLocked: boolean) {
|
|
|
|
const { boundsCenter, shapes } = this.snapshot
|
2021-05-17 21:27:18 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const page = getPage(data)
|
2021-05-18 08:32:20 +00:00
|
|
|
const a1 = vec.angle(boundsCenter, this.origin)
|
|
|
|
const a2 = vec.angle(boundsCenter, point)
|
2021-05-17 21:27:18 +00:00
|
|
|
|
2021-05-23 08:30:20 +00:00
|
|
|
let rot = a2 - a1
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
if (isLocked) {
|
2021-05-23 08:30:20 +00:00
|
|
|
rot = clampToRotationToSegments(rot, 24)
|
2021-05-22 15:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data.boundsRotation = (PI2 + (this.snapshot.boundsRotation + rot)) % PI2
|
2021-05-18 08:32:20 +00:00
|
|
|
|
|
|
|
for (let { id, center, offset, rotation } of shapes) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = page.shapes[id]
|
2021-05-25 09:00:59 +00:00
|
|
|
|
|
|
|
getShapeUtils(shape)
|
2021-05-25 17:52:02 +00:00
|
|
|
.rotateTo(
|
|
|
|
shape,
|
|
|
|
(PI2 +
|
|
|
|
(isLocked
|
|
|
|
? clampToRotationToSegments(rotation + rot, 24)
|
|
|
|
: rotation + rot)) %
|
|
|
|
PI2
|
|
|
|
)
|
2021-05-25 11:38:21 +00:00
|
|
|
.translateTo(
|
2021-05-25 09:00:59 +00:00
|
|
|
shape,
|
|
|
|
vec.sub(vec.rotWith(center, boundsCenter, rot % PI2), offset)
|
|
|
|
)
|
2021-05-17 21:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel(data: Data) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const page = getPage(data, this.snapshot.currentPageId)
|
2021-05-17 21:27:18 +00:00
|
|
|
|
2021-05-18 08:32:20 +00:00
|
|
|
for (let { id, point, rotation } of this.snapshot.shapes) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = page.shapes[id]
|
2021-05-25 11:38:21 +00:00
|
|
|
getShapeUtils(shape).rotateTo(shape, rotation).translateTo(shape, point)
|
2021-05-17 21:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
complete(data: Data) {
|
|
|
|
commands.rotate(data, this.snapshot, getRotateSnapshot(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getRotateSnapshot(data: Data) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const shapes = getSelectedShapes(current(data))
|
2021-05-17 21:27:18 +00:00
|
|
|
|
|
|
|
const shapesBounds = Object.fromEntries(
|
2021-05-22 15:45:24 +00:00
|
|
|
shapes.map((shape) => [shape.id, getShapeBounds(shape)])
|
2021-05-17 21:27:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const bounds = getCommonBounds(...Object.values(shapesBounds))
|
|
|
|
|
|
|
|
return {
|
2021-05-22 15:45:24 +00:00
|
|
|
currentPageId: data.currentPageId,
|
|
|
|
boundsRotation: data.boundsRotation,
|
2021-05-25 17:52:02 +00:00
|
|
|
boundsCenter: getBoundsCenter(bounds),
|
2021-05-18 08:32:20 +00:00
|
|
|
shapes: shapes.map(({ id, point, rotation }) => {
|
|
|
|
const bounds = shapesBounds[id]
|
|
|
|
const offset = [bounds.width / 2, bounds.height / 2]
|
2021-05-22 15:45:24 +00:00
|
|
|
const center = getBoundsCenter(bounds)
|
2021-05-18 08:32:20 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
point,
|
|
|
|
rotation,
|
|
|
|
offset,
|
|
|
|
center,
|
|
|
|
}
|
|
|
|
}),
|
2021-05-17 21:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type RotateSnapshot = ReturnType<typeof getRotateSnapshot>
|