|
| 1 | +import { Entity, System } from '@lastolivegames/becsy'; |
| 2 | +import { |
| 3 | + Children, |
| 4 | + Circle, |
| 5 | + ComputedBounds, |
| 6 | + FillSolid, |
| 7 | + Opacity, |
| 8 | + Parent, |
| 9 | + Rect, |
| 10 | + Renderable, |
| 11 | + Selected, |
| 12 | + SizeAttenuation, |
| 13 | + Stroke, |
| 14 | + ToBeDeleted, |
| 15 | + Transform, |
| 16 | + UI, |
| 17 | + ZIndex, |
| 18 | +} from '../components'; |
| 19 | +import { Commands } from '../commands'; |
| 20 | +import { getDescendants, getSceneRoot } from './Transform'; |
| 21 | + |
| 22 | +/** |
| 23 | + * @see https://github.com/konvajs/konva/blob/master/src/shapes/Transformer.ts |
| 24 | + */ |
| 25 | +export class RenderTransformer extends System { |
| 26 | + private readonly commands = new Commands(this); |
| 27 | + |
| 28 | + private readonly selected = this.query((q) => |
| 29 | + q.added.and.removed.with(Selected), |
| 30 | + ); |
| 31 | + |
| 32 | + #transformers = new WeakMap<Entity, Entity>(); |
| 33 | + |
| 34 | + constructor() { |
| 35 | + super(); |
| 36 | + |
| 37 | + this.query( |
| 38 | + (q) => |
| 39 | + q |
| 40 | + .using(ComputedBounds) |
| 41 | + .read.and.using( |
| 42 | + UI, |
| 43 | + Selected, |
| 44 | + Transform, |
| 45 | + Parent, |
| 46 | + Children, |
| 47 | + Renderable, |
| 48 | + FillSolid, |
| 49 | + Opacity, |
| 50 | + Stroke, |
| 51 | + Rect, |
| 52 | + Circle, |
| 53 | + ZIndex, |
| 54 | + SizeAttenuation, |
| 55 | + ToBeDeleted, |
| 56 | + ).write, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + execute() { |
| 61 | + this.selected.added.forEach((entity) => { |
| 62 | + console.log('add', entity); |
| 63 | + |
| 64 | + const { geometryBounds } = entity.read(ComputedBounds); |
| 65 | + const { minX, minY, maxX, maxY } = geometryBounds; |
| 66 | + const width = maxX - minX; |
| 67 | + const height = maxY - minY; |
| 68 | + const { rotation } = entity.read(Transform); |
| 69 | + |
| 70 | + const transformer = this.commands |
| 71 | + .spawn( |
| 72 | + new UI(), |
| 73 | + new Transform({ |
| 74 | + rotation, |
| 75 | + }), |
| 76 | + new Renderable(), |
| 77 | + new FillSolid('white'), // --spectrum-blue-100 |
| 78 | + new Opacity({ fillOpacity: 0 }), |
| 79 | + new Stroke({ width: 1, color: '#147af3' }), // --spectrum-thumbnail-border-color-selected |
| 80 | + new Rect({ |
| 81 | + x: minX, |
| 82 | + y: minY, |
| 83 | + width, |
| 84 | + height, |
| 85 | + }), |
| 86 | + new ZIndex(Infinity), |
| 87 | + // new SizeAttenuation(), |
| 88 | + ) |
| 89 | + .id() |
| 90 | + .hold(); |
| 91 | + |
| 92 | + const tlAnchor = this.createAnchor(minX, minY); |
| 93 | + const trAnchor = this.createAnchor(maxX, minY); |
| 94 | + const blAnchor = this.createAnchor(minX, maxY); |
| 95 | + const brAnchor = this.createAnchor(maxX, maxY); |
| 96 | + |
| 97 | + const transformEntity = this.commands.entity(transformer); |
| 98 | + transformEntity.appendChild(this.commands.entity(tlAnchor)); |
| 99 | + transformEntity.appendChild(this.commands.entity(trAnchor)); |
| 100 | + transformEntity.appendChild(this.commands.entity(blAnchor)); |
| 101 | + transformEntity.appendChild(this.commands.entity(brAnchor)); |
| 102 | + |
| 103 | + this.commands.execute(); |
| 104 | + |
| 105 | + const camera = this.commands.entity(getSceneRoot(entity)); |
| 106 | + camera.appendChild(this.commands.entity(transformer)); |
| 107 | + |
| 108 | + this.commands.execute(); |
| 109 | + |
| 110 | + this.#transformers.set(entity, transformer); |
| 111 | + }); |
| 112 | + |
| 113 | + this.selected.removed.forEach((entity) => { |
| 114 | + console.log('remove', entity); |
| 115 | + |
| 116 | + if (this.#transformers.has(entity)) { |
| 117 | + const transformer = this.#transformers.get(entity); |
| 118 | + |
| 119 | + transformer.add(ToBeDeleted); |
| 120 | + getDescendants(transformer).forEach((child) => { |
| 121 | + child.add(ToBeDeleted); |
| 122 | + }); |
| 123 | + |
| 124 | + this.#transformers.delete(entity); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + private createAnchor(cx: number, cy: number) { |
| 130 | + return this.commands |
| 131 | + .spawn( |
| 132 | + new UI(), |
| 133 | + new Transform(), |
| 134 | + new Renderable(), |
| 135 | + new FillSolid('#fff'), |
| 136 | + new Stroke({ width: 1, color: '#147af3' }), |
| 137 | + new Circle({ |
| 138 | + cx, |
| 139 | + cy, |
| 140 | + r: 5, |
| 141 | + }), |
| 142 | + new SizeAttenuation(), |
| 143 | + ) |
| 144 | + .id() |
| 145 | + .hold(); |
| 146 | + } |
| 147 | +} |
0 commit comments