2021-06-24 22:09:36 +00:00
|
|
|
import {
|
|
|
|
ColorStyle,
|
|
|
|
DashStyle,
|
|
|
|
Mutable,
|
|
|
|
Shape,
|
|
|
|
ShapeUtility,
|
|
|
|
SizeStyle,
|
|
|
|
} from 'types'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { createShape, getShapeUtils } from 'state/shape-utils'
|
2021-07-09 08:59:43 +00:00
|
|
|
import { uniqueId } from 'utils'
|
2021-06-27 11:58:08 +00:00
|
|
|
import Vec from 'utils/vec'
|
2021-05-14 22:56:41 +00:00
|
|
|
|
2021-05-15 13:02:13 +00:00
|
|
|
export const codeShapes = new Set<CodeShape<Shape>>([])
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
function getOrderedShapes() {
|
2021-07-09 08:59:43 +00:00
|
|
|
return Array.from(codeShapes.values()).sort(
|
2021-06-24 22:09:36 +00:00
|
|
|
(a, b) => a.shape.childIndex - b.shape.childIndex
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-15 13:02:13 +00:00
|
|
|
/**
|
|
|
|
* A base class for code shapes. Note that creating a shape adds it to the
|
|
|
|
* shape map, while deleting it removes it from the collected shapes set
|
|
|
|
*/
|
2021-06-25 10:28:52 +00:00
|
|
|
|
|
|
|
/* ----------------- Start Copy Here ---------------- */
|
|
|
|
|
2021-05-14 22:56:41 +00:00
|
|
|
export default class CodeShape<T extends Shape> {
|
2021-06-06 11:05:51 +00:00
|
|
|
private _shape: Mutable<T>
|
2021-06-27 11:58:08 +00:00
|
|
|
protected utils: ShapeUtility<T>
|
2021-05-14 22:56:41 +00:00
|
|
|
|
|
|
|
constructor(props: T) {
|
2021-06-06 11:05:51 +00:00
|
|
|
this._shape = createShape(props.type, props) as Mutable<T>
|
2021-05-25 09:09:51 +00:00
|
|
|
this.utils = getShapeUtils<T>(this._shape)
|
2021-05-15 13:02:13 +00:00
|
|
|
codeShapes.add(this)
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* Destroy the shape.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.destroy()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
destroy = (): void => {
|
2021-05-15 13:02:13 +00:00
|
|
|
codeShapes.delete(this)
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* Move the shape to a point.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.moveTo(100,100)
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
moveTo = (point: number[]): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
return this.translateTo(point)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the shape to a point.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.translateTo([100,100])
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
translateTo = (point: number[]): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
this.utils.translateTo(this._shape, point)
|
2021-05-25 09:00:59 +00:00
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* Move the shape by a delta.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.translateBy([100,100])
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
translateBy = (delta: number[]): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
this.utils.translateTo(this._shape, delta)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotate the shape.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.rotateTo(Math.PI / 2)
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
rotateTo = (rotation: number): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
this.utils.rotateTo(this._shape, rotation, this.shape.rotation - rotation)
|
2021-05-25 09:00:59 +00:00
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* Rotate the shape by a delta.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.rotateBy(Math.PI / 2)
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
rotateBy = (rotation: number): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
this.utils.rotateBy(this._shape, rotation)
|
2021-05-25 09:00:59 +00:00
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* Get the shape's bounding box.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const bounds = shape.getBounds()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
getBounds = (): CodeShape<T> => {
|
2021-05-25 09:00:59 +00:00
|
|
|
this.utils.getBounds(this.shape)
|
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* Test whether a point is inside of the shape.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const isHit = shape.hitTest()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
hitTest = (point: number[]): CodeShape<T> => {
|
2021-06-23 22:32:21 +00:00
|
|
|
this.utils.hitTest(this.shape, point)
|
2021-05-25 09:00:59 +00:00
|
|
|
return this
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 11:58:08 +00:00
|
|
|
/**
|
|
|
|
* Duplicate this shape.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeB = shape.duplicate()
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
duplicate = (): CodeShape<T> => {
|
|
|
|
const duplicate = Object.assign(
|
|
|
|
Object.create(Object.getPrototypeOf(this)),
|
|
|
|
this
|
|
|
|
)
|
|
|
|
|
|
|
|
duplicate._shape = createShape(this._shape.type, {
|
|
|
|
...this._shape,
|
|
|
|
id: uniqueId(),
|
|
|
|
} as any)
|
|
|
|
|
|
|
|
codeShapes.add(duplicate)
|
|
|
|
return duplicate
|
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* Move the shape to the back of the painting order.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.moveToBack()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
moveToBack = (): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
const sorted = getOrderedShapes()
|
|
|
|
|
|
|
|
if (sorted.length <= 1) return
|
|
|
|
|
|
|
|
const first = sorted[0].childIndex
|
|
|
|
sorted.forEach((shape) => shape.childIndex++)
|
|
|
|
this.childIndex = first
|
|
|
|
|
|
|
|
codeShapes.clear()
|
|
|
|
sorted.forEach((shape) => codeShapes.add(shape))
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the shape to the top of the painting order.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.moveToFront()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
moveToFront = (): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
const sorted = getOrderedShapes()
|
|
|
|
|
|
|
|
if (sorted.length <= 1) return
|
|
|
|
|
|
|
|
const ahead = sorted.slice(sorted.indexOf(this))
|
|
|
|
const last = ahead[ahead.length - 1].childIndex
|
|
|
|
ahead.forEach((shape) => shape.childIndex--)
|
|
|
|
this.childIndex = last
|
|
|
|
|
|
|
|
codeShapes.clear()
|
|
|
|
sorted.forEach((shape) => codeShapes.add(shape))
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the shape backward in the painting order.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.moveBackward()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
moveBackward = (): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
const sorted = getOrderedShapes()
|
|
|
|
|
|
|
|
if (sorted.length <= 1) return
|
|
|
|
|
|
|
|
const next = sorted[sorted.indexOf(this) - 1]
|
|
|
|
|
|
|
|
if (!next) return
|
|
|
|
|
|
|
|
const index = next.childIndex
|
|
|
|
next.childIndex = this.childIndex
|
|
|
|
this.childIndex = index
|
|
|
|
|
|
|
|
codeShapes.clear()
|
|
|
|
sorted.forEach((shape) => codeShapes.add(shape))
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the shape forward in the painting order.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* shape.moveForward()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
moveForward = (): CodeShape<T> => {
|
2021-06-24 22:09:36 +00:00
|
|
|
const sorted = getOrderedShapes()
|
|
|
|
|
|
|
|
if (sorted.length <= 1) return
|
|
|
|
|
|
|
|
const next = sorted[sorted.indexOf(this) + 1]
|
|
|
|
|
|
|
|
if (!next) return
|
|
|
|
|
|
|
|
const index = next.childIndex
|
|
|
|
next.childIndex = this.childIndex
|
|
|
|
this.childIndex = index
|
|
|
|
|
|
|
|
codeShapes.clear()
|
|
|
|
sorted.forEach((shape) => codeShapes.add(shape))
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-06-25 11:01:22 +00:00
|
|
|
get id(): string {
|
|
|
|
return this._shape.id
|
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
2021-06-27 11:58:08 +00:00
|
|
|
* The shape's underlying shape (readonly).
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const underlyingShape = shape.shape
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
get shape(): Readonly<T> {
|
2021-05-14 22:56:41 +00:00
|
|
|
return this._shape
|
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
/**
|
|
|
|
* The shape's current point.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapePoint = shape.point()
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-21 21:35:28 +00:00
|
|
|
get point(): number[] {
|
2021-05-14 22:56:41 +00:00
|
|
|
return [...this.shape.point]
|
|
|
|
}
|
|
|
|
|
2021-06-24 22:09:36 +00:00
|
|
|
set point(point: number[]) {
|
2021-06-27 11:58:08 +00:00
|
|
|
this.utils.translateTo(this._shape, point)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shape's current x position.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeX = shape.x
|
|
|
|
*
|
|
|
|
* shape.x = 100
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
get x(): number {
|
|
|
|
return this.point[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
set x(x: number) {
|
|
|
|
this.utils.translateTo(this._shape, [x, this.y])
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shape's current y position.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeY = shape.y
|
|
|
|
*
|
|
|
|
* shape.y = 100
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
get y(): number {
|
|
|
|
return this.point[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
set y(y: number) {
|
|
|
|
this.utils.translateTo(this._shape, [this.x, y])
|
2021-06-24 22:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shape's rotation.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeRotation = shape.rotation
|
|
|
|
*
|
|
|
|
* shape.rotation = Math.PI / 2
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-21 21:35:28 +00:00
|
|
|
get rotation(): number {
|
2021-05-14 22:56:41 +00:00
|
|
|
return this.shape.rotation
|
|
|
|
}
|
2021-06-24 22:09:36 +00:00
|
|
|
|
|
|
|
set rotation(rotation: number) {
|
2021-06-27 11:58:08 +00:00
|
|
|
this.utils.rotateTo(this._shape, rotation, rotation - this.shape.rotation)
|
2021-06-24 22:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-27 11:58:08 +00:00
|
|
|
* The shape's color style (ColorStyle).
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeColor = shape.color
|
|
|
|
*
|
|
|
|
* shape.color = ColorStyle.Red
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
|
|
|
get color(): ColorStyle {
|
|
|
|
return this.shape.style.color
|
|
|
|
}
|
|
|
|
|
|
|
|
set color(color: ColorStyle) {
|
2021-06-27 11:58:08 +00:00
|
|
|
this.utils.applyStyles(this._shape, { color })
|
2021-06-24 22:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-27 11:58:08 +00:00
|
|
|
* The shape's dash style (DashStyle).
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeDash = shape.dash
|
|
|
|
*
|
|
|
|
* shape.dash = DashStyle.Dotted
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
|
|
|
get dash(): DashStyle {
|
|
|
|
return this.shape.style.dash
|
|
|
|
}
|
|
|
|
|
|
|
|
set dash(dash: DashStyle) {
|
2021-06-27 11:58:08 +00:00
|
|
|
this.utils.applyStyles(this._shape, { dash })
|
2021-06-24 22:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-27 11:58:08 +00:00
|
|
|
* The shape's size (SizeStyle).
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeSize = shape.size
|
|
|
|
*
|
|
|
|
* shape.size = SizeStyle.Large
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
2021-06-27 11:58:08 +00:00
|
|
|
get size(): SizeStyle {
|
2021-06-24 22:09:36 +00:00
|
|
|
return this.shape.style.size
|
|
|
|
}
|
|
|
|
|
2021-06-27 11:58:08 +00:00
|
|
|
set size(size: SizeStyle) {
|
|
|
|
this.utils.applyStyles(this._shape, { size })
|
2021-06-24 22:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shape's index in the painting order.
|
2021-06-27 11:58:08 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeChildIndex = shape.childIndex
|
|
|
|
*
|
|
|
|
* shape.childIndex = 10
|
|
|
|
* ```
|
2021-06-24 22:09:36 +00:00
|
|
|
*/
|
|
|
|
get childIndex(): number {
|
|
|
|
return this.shape.childIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
set childIndex(childIndex: number) {
|
2021-06-27 11:58:08 +00:00
|
|
|
this.utils.setProperty(this._shape, 'childIndex', childIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shape's center.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const shapeCenter = shape.center
|
|
|
|
*
|
|
|
|
* shape.center = [100, 100]
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
get center(): number[] {
|
|
|
|
return this.utils.getCenter(this.shape)
|
|
|
|
}
|
|
|
|
|
|
|
|
set center(center: number[]) {
|
|
|
|
const oldCenter = this.utils.getCenter(this.shape)
|
|
|
|
const delta = Vec.sub(center, oldCenter)
|
|
|
|
this.translateBy(delta)
|
2021-06-24 22:09:36 +00:00
|
|
|
}
|
2021-05-14 22:56:41 +00:00
|
|
|
}
|