@asyra/render 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-test$colon$local.log +45 -0
- package/README.md +1 -0
- package/dist/__tests__/render.test.d.ts +2 -0
- package/dist/__tests__/render.test.d.ts.map +1 -0
- package/dist/__tests__/render.test.js +148 -0
- package/dist/__tests__/render.test.js.map +1 -0
- package/dist/handlers/element-selection.d.ts +4 -0
- package/dist/handlers/element-selection.d.ts.map +1 -0
- package/dist/handlers/element-selection.js +7 -0
- package/dist/handlers/element-selection.js.map +1 -0
- package/dist/handlers/index.d.ts +2 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +2 -0
- package/dist/handlers/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/render-layer/element-interaction-handler.d.ts +8 -0
- package/dist/render-layer/element-interaction-handler.d.ts.map +1 -0
- package/dist/render-layer/element-interaction-handler.js +20 -0
- package/dist/render-layer/element-interaction-handler.js.map +1 -0
- package/dist/render-layer/index.d.ts +2 -0
- package/dist/render-layer/index.d.ts.map +1 -0
- package/dist/render-layer/index.js +2 -0
- package/dist/render-layer/index.js.map +1 -0
- package/dist/render-layer/render-layer.d.ts +41 -0
- package/dist/render-layer/render-layer.d.ts.map +1 -0
- package/dist/render-layer/render-layer.js +189 -0
- package/dist/render-layer/render-layer.js.map +1 -0
- package/dist/render.d.ts +53 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +116 -0
- package/dist/render.js.map +1 -0
- package/dist/selection-layer/index.d.ts +3 -0
- package/dist/selection-layer/index.d.ts.map +1 -0
- package/dist/selection-layer/index.js +3 -0
- package/dist/selection-layer/index.js.map +1 -0
- package/dist/selection-layer/selection-layer.d.ts +29 -0
- package/dist/selection-layer/selection-layer.d.ts.map +1 -0
- package/dist/selection-layer/selection-layer.js +63 -0
- package/dist/selection-layer/selection-layer.js.map +1 -0
- package/dist/selection-layer/utils.d.ts +13 -0
- package/dist/selection-layer/utils.d.ts.map +1 -0
- package/dist/selection-layer/utils.js +27 -0
- package/dist/selection-layer/utils.js.map +1 -0
- package/dist/stores/scene-tree.d.ts +16 -0
- package/dist/stores/scene-tree.d.ts.map +1 -0
- package/dist/stores/scene-tree.js +57 -0
- package/dist/stores/scene-tree.js.map +1 -0
- package/dist/stores/selection.d.ts +13 -0
- package/dist/stores/selection.d.ts.map +1 -0
- package/dist/stores/selection.js +31 -0
- package/dist/stores/selection.js.map +1 -0
- package/dist/subscribes/index.d.ts +2 -0
- package/dist/subscribes/index.d.ts.map +1 -0
- package/dist/subscribes/index.js +11 -0
- package/dist/subscribes/index.js.map +1 -0
- package/dist/subscribes/scene-tree.d.ts +4 -0
- package/dist/subscribes/scene-tree.d.ts.map +1 -0
- package/dist/subscribes/scene-tree.js +56 -0
- package/dist/subscribes/scene-tree.js.map +1 -0
- package/dist/subscribes/selection.d.ts +3 -0
- package/dist/subscribes/selection.d.ts.map +1 -0
- package/dist/subscribes/selection.js +31 -0
- package/dist/subscribes/selection.js.map +1 -0
- package/dist/subscribes/system.d.ts +2 -0
- package/dist/subscribes/system.d.ts.map +1 -0
- package/dist/subscribes/system.js +15 -0
- package/dist/subscribes/system.js.map +1 -0
- package/dist/subscribes/viewport.d.ts +2 -0
- package/dist/subscribes/viewport.d.ts.map +1 -0
- package/dist/subscribes/viewport.js +36 -0
- package/dist/subscribes/viewport.js.map +1 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/viewport-layer/index.d.ts +4 -0
- package/dist/viewport-layer/index.d.ts.map +1 -0
- package/dist/viewport-layer/index.js +4 -0
- package/dist/viewport-layer/index.js.map +1 -0
- package/dist/viewport-layer/types.d.ts +17 -0
- package/dist/viewport-layer/types.d.ts.map +1 -0
- package/dist/viewport-layer/types.js +2 -0
- package/dist/viewport-layer/types.js.map +1 -0
- package/dist/viewport-layer/utils.d.ts +8 -0
- package/dist/viewport-layer/utils.d.ts.map +1 -0
- package/dist/viewport-layer/utils.js +49 -0
- package/dist/viewport-layer/utils.js.map +1 -0
- package/dist/viewport-layer/viewport-layer.d.ts +59 -0
- package/dist/viewport-layer/viewport-layer.d.ts.map +1 -0
- package/dist/viewport-layer/viewport-layer.js +125 -0
- package/dist/viewport-layer/viewport-layer.js.map +1 -0
- package/package.json +32 -0
- package/src/__tests__/render.test.ts +209 -0
- package/src/handlers/element-selection.ts +7 -0
- package/src/handlers/index.ts +1 -0
- package/src/index.ts +7 -0
- package/src/render-layer/element-interaction-handler.ts +31 -0
- package/src/render-layer/index.ts +1 -0
- package/src/render-layer/render-layer.ts +238 -0
- package/src/render.ts +161 -0
- package/src/selection-layer/index.ts +2 -0
- package/src/selection-layer/selection-layer.ts +90 -0
- package/src/selection-layer/utils.ts +37 -0
- package/src/stores/scene-tree.ts +79 -0
- package/src/stores/selection.ts +40 -0
- package/src/subscribes/index.ts +11 -0
- package/src/subscribes/scene-tree.ts +72 -0
- package/src/subscribes/selection.ts +40 -0
- package/src/subscribes/system.ts +18 -0
- package/src/subscribes/viewport.ts +54 -0
- package/src/types.ts +15 -0
- package/src/viewport-layer/index.ts +3 -0
- package/src/viewport-layer/types.ts +18 -0
- package/src/viewport-layer/utils.ts +55 -0
- package/src/viewport-layer/viewport-layer.ts +172 -0
- package/test-global.d.ts +11 -0
- package/test-setup.ts +28 -0
- package/tsconfig.json +22 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Container, Graphics, FederatedPointerEvent } from 'pixi.js'
|
|
2
|
+
import { ElementSelectionHandlers } from '../handlers'
|
|
3
|
+
|
|
4
|
+
export class ElementInteractionHandler {
|
|
5
|
+
bindElementEvents(element: Container | Graphics) {
|
|
6
|
+
element.eventMode = 'static'
|
|
7
|
+
element.cursor = 'pointer'
|
|
8
|
+
|
|
9
|
+
element.on('pointerenter', (e) => this.handlePointerEnter(element, e))
|
|
10
|
+
element.on('pointerleave', (e) => this.handlePointerLeave(element, e))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
unbindElementEvents(element: Container | Graphics) {
|
|
14
|
+
element.eventMode = 'none'
|
|
15
|
+
element.removeAllListeners()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private handlePointerEnter(
|
|
19
|
+
element: Container | Graphics,
|
|
20
|
+
e: FederatedPointerEvent
|
|
21
|
+
) {
|
|
22
|
+
ElementSelectionHandlers.updateHoveredElement(element.label)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private handlePointerLeave(
|
|
26
|
+
element: Container | Graphics,
|
|
27
|
+
e: FederatedPointerEvent
|
|
28
|
+
) {
|
|
29
|
+
ElementSelectionHandlers.updateHoveredElement(null)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './render-layer'
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { Container, Graphics, Point } from 'pixi.js'
|
|
2
|
+
import { SceneElement, RenderContainerData, RenderElementData } from '../types'
|
|
3
|
+
import { DataTypes, EntityTypes } from '@asyra/utils'
|
|
4
|
+
import { ElementInteractionHandler } from './element-interaction-handler'
|
|
5
|
+
|
|
6
|
+
export class RenderLayer {
|
|
7
|
+
private currentWorkspace: Container
|
|
8
|
+
private _elements: Map<string, SceneElement> = new Map()
|
|
9
|
+
private _deleteMap: Map<string, SceneElement> = new Map()
|
|
10
|
+
private interactionHandler = new ElementInteractionHandler()
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.currentWorkspace = new Container()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get view() {
|
|
17
|
+
return this.currentWorkspace
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
addToMap(elementId: string, instance: SceneElement) {
|
|
21
|
+
this._elements.set(elementId, instance)
|
|
22
|
+
this.removeFromDeleteMap(elementId)
|
|
23
|
+
this.interactionHandler.bindElementEvents(instance)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
removeFromMap(elementId: string) {
|
|
27
|
+
const instance = this.getElementById(elementId) as SceneElement
|
|
28
|
+
this.interactionHandler.unbindElementEvents(instance)
|
|
29
|
+
this._elements.delete(elementId)
|
|
30
|
+
this.addToDeleteMap(elementId, instance)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
addToDeleteMap(elementId: string, instance: SceneElement) {
|
|
34
|
+
this._deleteMap.set(elementId, instance)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
removeFromDeleteMap(elementId: string) {
|
|
38
|
+
this._deleteMap.delete(elementId)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getAllElements() {
|
|
42
|
+
return this._elements
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getElementById(elementId: string): SceneElement | undefined {
|
|
46
|
+
return this._elements.get(elementId)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
getRestoreElement(elementId: string): SceneElement | undefined {
|
|
50
|
+
return this._deleteMap.get(elementId)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
switchWorkspace(workspaceData: RenderContainerData) {
|
|
54
|
+
this.currentWorkspace.label = workspaceData.label
|
|
55
|
+
this.currentWorkspace.x = workspaceData.x
|
|
56
|
+
this.currentWorkspace.y = workspaceData.y
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
addContainer(containerData: RenderContainerData) {
|
|
60
|
+
const container = new Container(containerData)
|
|
61
|
+
this._elements.set(containerData.label, container)
|
|
62
|
+
this.currentWorkspace.addChild(container)
|
|
63
|
+
|
|
64
|
+
return container
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
addElement(data: RenderElementData) {
|
|
68
|
+
const element = this.getRestoreElement(data.id)
|
|
69
|
+
if (element) {
|
|
70
|
+
this.addToMap(data.id, element)
|
|
71
|
+
this.currentWorkspace.addChild(element)
|
|
72
|
+
return element
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const graphic = new Graphics()
|
|
76
|
+
graphic.label = data.id
|
|
77
|
+
|
|
78
|
+
switch (data.type) {
|
|
79
|
+
case EntityTypes.RECTANGLE:
|
|
80
|
+
graphic.rect(0, 0, data.width, data.height).fill(randomHexColorCode())
|
|
81
|
+
graphic.x = data.x
|
|
82
|
+
graphic.y = data.y
|
|
83
|
+
break
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this.addToMap(data.id, graphic)
|
|
87
|
+
this.currentWorkspace.addChild(graphic)
|
|
88
|
+
|
|
89
|
+
return graphic
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
removeElement(elementId: string, parentId?: string) {
|
|
93
|
+
const parent =
|
|
94
|
+
(this.getElementById(parentId as string) as Container) ||
|
|
95
|
+
this.currentWorkspace
|
|
96
|
+
const element = this.getElementById(elementId)
|
|
97
|
+
|
|
98
|
+
if (parent && element) {
|
|
99
|
+
this.removeFromMap(elementId)
|
|
100
|
+
parent.removeChild(element)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return element
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
updateElement(
|
|
107
|
+
elementId: string,
|
|
108
|
+
key: string,
|
|
109
|
+
before: DataTypes,
|
|
110
|
+
after: DataTypes
|
|
111
|
+
) {
|
|
112
|
+
const element = this.getElementById(elementId)
|
|
113
|
+
if (!element) {
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
switch (key) {
|
|
118
|
+
case 'children': {
|
|
119
|
+
const oldList = new Set(before as string[])
|
|
120
|
+
let deleteCount = 0
|
|
121
|
+
// Add element
|
|
122
|
+
;(after as string[]).forEach((childId, index) => {
|
|
123
|
+
const child = this.getElementById(childId)
|
|
124
|
+
if (!child) {
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (oldList.has(childId)) {
|
|
129
|
+
oldList.delete(childId)
|
|
130
|
+
deleteCount++
|
|
131
|
+
} else {
|
|
132
|
+
element.addChildAt(child, index - deleteCount)
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
// Remove element
|
|
137
|
+
oldList.forEach((childId) => {
|
|
138
|
+
const child = this.getElementById(childId)
|
|
139
|
+
if (!child) {
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
element.removeChild(child)
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
// Move element
|
|
147
|
+
element.children.forEach((child, index) => {
|
|
148
|
+
const newIndex = (after as string[]).indexOf(child.label)
|
|
149
|
+
if (newIndex !== index) {
|
|
150
|
+
element.setChildIndex(child, newIndex)
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
break
|
|
154
|
+
}
|
|
155
|
+
default:
|
|
156
|
+
this.updateElementProperties(element, key, after)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
updateElementProperties(
|
|
161
|
+
element: Container | Graphics,
|
|
162
|
+
key: string,
|
|
163
|
+
after: DataTypes
|
|
164
|
+
) {
|
|
165
|
+
switch (key) {
|
|
166
|
+
case 'x':
|
|
167
|
+
element.x = after as number
|
|
168
|
+
break
|
|
169
|
+
case 'y':
|
|
170
|
+
element.y = after as number
|
|
171
|
+
break
|
|
172
|
+
case 'width':
|
|
173
|
+
element.width = after as number
|
|
174
|
+
break
|
|
175
|
+
case 'height':
|
|
176
|
+
element.height = after as number
|
|
177
|
+
break
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Calculates the combined bounding box of all visible elements in the workspace,
|
|
183
|
+
* expressed in the local coordinate space of the workspace (i.e., ignoring zoom and pan).
|
|
184
|
+
*
|
|
185
|
+
* This method ensures consistent bounding box results regardless of the current
|
|
186
|
+
* zoom or pan applied to the workspace.
|
|
187
|
+
*
|
|
188
|
+
* @param workspace - The container holding all elements (typically zoomed/panned).
|
|
189
|
+
* @returns A bounding box object containing { minX, minY, maxX, maxY } in local space.
|
|
190
|
+
*/
|
|
191
|
+
getAllElementsBounds(workspace: Container) {
|
|
192
|
+
const bounds = {
|
|
193
|
+
minX: Infinity,
|
|
194
|
+
minY: Infinity,
|
|
195
|
+
maxX: -Infinity,
|
|
196
|
+
maxY: -Infinity
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const topLeft = new Point()
|
|
200
|
+
const topRight = new Point()
|
|
201
|
+
const bottomLeft = new Point()
|
|
202
|
+
const bottomRight = new Point()
|
|
203
|
+
|
|
204
|
+
for (const [, element] of this._elements) {
|
|
205
|
+
if (element instanceof Graphics && element.visible) {
|
|
206
|
+
// Get the element's local bounds (before transform)
|
|
207
|
+
const localBounds = element.getLocalBounds()
|
|
208
|
+
|
|
209
|
+
// Update corner points only when necessary
|
|
210
|
+
topLeft.set(localBounds.x, localBounds.y)
|
|
211
|
+
topRight.set(localBounds.x + localBounds.width, localBounds.y)
|
|
212
|
+
bottomLeft.set(localBounds.x, localBounds.y + localBounds.height)
|
|
213
|
+
bottomRight.set(
|
|
214
|
+
localBounds.x + localBounds.width,
|
|
215
|
+
localBounds.y + localBounds.height
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
// Convert each corner to workspace local space and update bounding box
|
|
219
|
+
const corners = [topLeft, topRight, bottomLeft, bottomRight]
|
|
220
|
+
for (const corner of corners) {
|
|
221
|
+
const localCorner = workspace.toLocal(element.toGlobal(corner))
|
|
222
|
+
bounds.minX = Math.min(bounds.minX, localCorner.x)
|
|
223
|
+
bounds.minY = Math.min(bounds.minY, localCorner.y)
|
|
224
|
+
bounds.maxX = Math.max(bounds.maxX, localCorner.x)
|
|
225
|
+
bounds.maxY = Math.max(bounds.maxY, localCorner.y)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return bounds
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// REMOVE: test data
|
|
235
|
+
const randomHexColorCode = () => {
|
|
236
|
+
const n = (Math.random() * 0xfffff * 1000000).toString(16)
|
|
237
|
+
return '#' + n.slice(0, 6)
|
|
238
|
+
}
|
package/src/render.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { Application, Container, Graphics, Ticker } from 'pixi.js'
|
|
2
|
+
import { DataTypes, MouseData } from '@asyra/utils'
|
|
3
|
+
import { RenderElementData, RenderContainerData, SceneElement } from './types'
|
|
4
|
+
import { ViewportLayer } from './viewport-layer'
|
|
5
|
+
import { SelectionLayer } from './selection-layer'
|
|
6
|
+
import renderSelection from './stores/selection'
|
|
7
|
+
|
|
8
|
+
const ticker = Ticker.shared
|
|
9
|
+
|
|
10
|
+
class Render {
|
|
11
|
+
app: Application | null = null
|
|
12
|
+
viewport: ViewportLayer
|
|
13
|
+
selection: SelectionLayer
|
|
14
|
+
|
|
15
|
+
constructor() {
|
|
16
|
+
this.viewport = new ViewportLayer()
|
|
17
|
+
this.selection = new SelectionLayer({
|
|
18
|
+
getSelectedElements: this.getSelectedElements.bind(this),
|
|
19
|
+
getHoverElement: () => null
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
this.run()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
run() {
|
|
26
|
+
ticker.add(() => {
|
|
27
|
+
const animate = (time: number) => {
|
|
28
|
+
this.updateLayers()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
animate(performance.now())
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
updateLayers() {
|
|
36
|
+
this.selection.update()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private createApplication() {
|
|
40
|
+
const app = new Application()
|
|
41
|
+
|
|
42
|
+
return app
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async init(width: number, height: number, backgroundColor: number) {
|
|
46
|
+
const app = this.createApplication()
|
|
47
|
+
|
|
48
|
+
await app.init({
|
|
49
|
+
width,
|
|
50
|
+
height,
|
|
51
|
+
backgroundColor,
|
|
52
|
+
resolution: Math.min(window.devicePixelRatio, 2),
|
|
53
|
+
resizeTo: window,
|
|
54
|
+
antialias: true,
|
|
55
|
+
autoDensity: true
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
this.app = app
|
|
59
|
+
this.app.stage.eventMode = 'static'
|
|
60
|
+
|
|
61
|
+
this._setupStageLayers()
|
|
62
|
+
|
|
63
|
+
return this.app
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private _setupStageLayers() {
|
|
67
|
+
this.app?.stage.addChild(this.viewport.view)
|
|
68
|
+
this.app?.stage.addChild(this.selection.view)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
getSelectedElements(): SceneElement[] {
|
|
72
|
+
return [...renderSelection.elementSelection].map((elementId) =>
|
|
73
|
+
this.viewport.getElementById(elementId)
|
|
74
|
+
) as SceneElement[]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
switchWorkspace(workspaceData: RenderContainerData) {
|
|
78
|
+
this.viewport.switchWorkspace(workspaceData)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
addContainer(containerData: RenderContainerData) {
|
|
82
|
+
return this.viewport.addContainer(containerData)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
addElement(data: RenderElementData) {
|
|
86
|
+
return this.viewport.addElement(data)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
removeElement(elementId: string, parentId?: string) {
|
|
90
|
+
return this.viewport.removeElement(elementId, parentId)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
updateElement(
|
|
94
|
+
elementId: string,
|
|
95
|
+
key: string,
|
|
96
|
+
before: DataTypes,
|
|
97
|
+
after: DataTypes
|
|
98
|
+
) {
|
|
99
|
+
this.viewport.updateElement(elementId, key, before, after)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
updateElementProperties(
|
|
103
|
+
element: Container | Graphics,
|
|
104
|
+
key: string,
|
|
105
|
+
after: DataTypes
|
|
106
|
+
) {
|
|
107
|
+
this.viewport.updateElementProperties(element, key, after)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
zoomFit(uiBounds: DOMRect) {
|
|
111
|
+
this.viewport.zoomFit(uiBounds)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Move the canvas to the specified position
|
|
116
|
+
* @param x - The x-coordinate to move the canvas to
|
|
117
|
+
* @param y - The y-coordinate to move the canvas to
|
|
118
|
+
* @returns void
|
|
119
|
+
*/
|
|
120
|
+
panTo(x: number, y: number) {
|
|
121
|
+
this.viewport.panTo(x, y)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Set the canvas zoom level
|
|
126
|
+
* @param scale - The zoom scale factor. A value of 1.0 represents 100% zoom.
|
|
127
|
+
* Values greater than 1.0 zoom in, values less than 1.0 zoom out.
|
|
128
|
+
* @returns void
|
|
129
|
+
*/
|
|
130
|
+
zoomTo(scale: number) {
|
|
131
|
+
this.viewport.zoomTo(scale)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Set the canvas zoom level centered on a specific point
|
|
136
|
+
* @param scale - The zoom scale factor
|
|
137
|
+
* @param centerX - The x-coordinate of the zoom center
|
|
138
|
+
* @param centerY - The y-coordinate of the zoom center
|
|
139
|
+
* @returns void
|
|
140
|
+
*/
|
|
141
|
+
zoomToCenter(scale: number, centerX: number, centerY: number) {
|
|
142
|
+
this.viewport.zoomToCenter(scale, centerX, centerY)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getViewportPosition() {
|
|
146
|
+
return this.viewport.getPosition()
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getViewportScale() {
|
|
150
|
+
return this.viewport.getScale()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
getMousePosInWorkspace(mousePos: MouseData) {
|
|
154
|
+
return this.viewport.getMousePosInWorkspace(mousePos)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const render = new Render()
|
|
159
|
+
|
|
160
|
+
export default render
|
|
161
|
+
export { Render }
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Container, Graphics } from 'pixi.js'
|
|
2
|
+
import { getSelectionLocalBounds } from './utils'
|
|
3
|
+
import { SceneElement } from '../types'
|
|
4
|
+
|
|
5
|
+
interface SelectionLayerOptions {
|
|
6
|
+
getSelectedElements: () => SceneElement[]
|
|
7
|
+
getHoverElement: () => SceneElement | null
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A special layer responsible for rendering selection boxes.
|
|
12
|
+
* Includes both selected and hover targets.
|
|
13
|
+
*/
|
|
14
|
+
export class SelectionLayer {
|
|
15
|
+
layer: Container
|
|
16
|
+
private selectedBox: Graphics
|
|
17
|
+
private hoverBox: Graphics
|
|
18
|
+
private getSelectedElements: () => SceneElement[]
|
|
19
|
+
private getHoverElement: () => SceneElement | null
|
|
20
|
+
|
|
21
|
+
constructor(options: SelectionLayerOptions) {
|
|
22
|
+
this.layer = new Container()
|
|
23
|
+
|
|
24
|
+
this.selectedBox = new Graphics()
|
|
25
|
+
this.selectedBox.label = 'SelectedBox'
|
|
26
|
+
this.hoverBox = new Graphics()
|
|
27
|
+
this.hoverBox.label = 'HoverBox'
|
|
28
|
+
|
|
29
|
+
this.layer.addChild(this.selectedBox)
|
|
30
|
+
this.layer.addChild(this.hoverBox)
|
|
31
|
+
|
|
32
|
+
this.getSelectedElements = options.getSelectedElements
|
|
33
|
+
this.getHoverElement = options.getHoverElement
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get view() {
|
|
37
|
+
return this.layer
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Manually trigger a redraw of selection boxes.
|
|
42
|
+
*/
|
|
43
|
+
update() {
|
|
44
|
+
this.updateSelectedBox()
|
|
45
|
+
this.updateHoverBox()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
updateSelected() {
|
|
49
|
+
this.updateSelectedBox()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private updateSelectedBox() {
|
|
53
|
+
const selectedElements = this.getSelectedElements()
|
|
54
|
+
|
|
55
|
+
this.selectedBox.clear()
|
|
56
|
+
if (selectedElements.length === 0) return
|
|
57
|
+
|
|
58
|
+
if (selectedElements.length === 1) {
|
|
59
|
+
const element = selectedElements[0]
|
|
60
|
+
const bounds = element.getBounds()
|
|
61
|
+
|
|
62
|
+
this.selectedBox.rect(0, 0, bounds.width, bounds.height).stroke({
|
|
63
|
+
width: 1,
|
|
64
|
+
color: 0x1e90ff
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
this.selectedBox.x = bounds.x
|
|
68
|
+
this.selectedBox.y = bounds.y
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private updateHoverBox() {
|
|
73
|
+
const hoverElement = this.getHoverElement()
|
|
74
|
+
this.hoverBox.clear()
|
|
75
|
+
|
|
76
|
+
if (!hoverElement) return
|
|
77
|
+
|
|
78
|
+
const bounds = getSelectionLocalBounds(hoverElement)
|
|
79
|
+
|
|
80
|
+
this.hoverBox.lineStyle(1, 0xffa500, 1) // Orange color
|
|
81
|
+
this.hoverBox.drawRect(bounds.x, bounds.y, bounds.width, bounds.height)
|
|
82
|
+
|
|
83
|
+
const matrix = hoverElement.worldTransform.clone()
|
|
84
|
+
this.hoverBox.setTransform(matrix)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get viewportZoom() {
|
|
88
|
+
return this.layer.parent.scale.x
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Rectangle } from 'pixi.js'
|
|
2
|
+
import { SceneElement } from '../types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Calculate the world bounds that cover all given elements.
|
|
6
|
+
* Used for multi-selection bounding box.
|
|
7
|
+
*/
|
|
8
|
+
export function getSelectionWorldBounds(
|
|
9
|
+
elements: SceneElement[]
|
|
10
|
+
): Rectangle | null {
|
|
11
|
+
if (elements.length === 0) return null
|
|
12
|
+
|
|
13
|
+
let minX = Infinity,
|
|
14
|
+
minY = Infinity,
|
|
15
|
+
maxX = -Infinity,
|
|
16
|
+
maxY = -Infinity
|
|
17
|
+
|
|
18
|
+
for (const el of elements) {
|
|
19
|
+
const bounds = el.getBounds()
|
|
20
|
+
|
|
21
|
+
minX = Math.min(minX, bounds.x)
|
|
22
|
+
minY = Math.min(minY, bounds.y)
|
|
23
|
+
maxX = Math.max(maxX, bounds.x + bounds.width)
|
|
24
|
+
maxY = Math.max(maxY, bounds.y + bounds.height)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return new Rectangle(minX, minY, maxX - minX, maxY - minY)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get the local bounds of a single element.
|
|
32
|
+
* Used for single-selection bounding box.
|
|
33
|
+
*/
|
|
34
|
+
export const getSelectionLocalBounds = (element: SceneElement): Rectangle => {
|
|
35
|
+
const bounds = element.getLocalBounds()
|
|
36
|
+
return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height)
|
|
37
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { DataTypes, ElementRawData, WorkspaceRawData } from '@asyra/utils'
|
|
2
|
+
import { EntityTypes } from '@asyra/utils'
|
|
3
|
+
import sceneTree from '@asyra/scene-tree'
|
|
4
|
+
import { RenderElementData } from '../types'
|
|
5
|
+
|
|
6
|
+
import render from '../render'
|
|
7
|
+
|
|
8
|
+
class RenderSceneTree {
|
|
9
|
+
private _workspace: WorkspaceRawData | null
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
this._workspace = null
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
reload() {
|
|
16
|
+
if (!sceneTree.currentWorkspace) return
|
|
17
|
+
|
|
18
|
+
const currentWorkspaceData =
|
|
19
|
+
sceneTree.currentWorkspace.save() as WorkspaceRawData
|
|
20
|
+
this._workspace = currentWorkspaceData
|
|
21
|
+
|
|
22
|
+
// Create root render node
|
|
23
|
+
render.switchWorkspace({
|
|
24
|
+
label: currentWorkspaceData.id,
|
|
25
|
+
x: 0,
|
|
26
|
+
y: 0
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// Create all element render node
|
|
30
|
+
sceneTree.getAllElements().forEach((element) => {
|
|
31
|
+
const renderElementData = this._getRenderData(element.get('id'))
|
|
32
|
+
if (element.get('type') !== EntityTypes.WORKSPACE && renderElementData) {
|
|
33
|
+
this.addElement(renderElementData)
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private _getRenderData(id: string) {
|
|
39
|
+
const element = sceneTree.getElementById(id)
|
|
40
|
+
if (!element) return null
|
|
41
|
+
|
|
42
|
+
const elementComputedData = element.getAllComputedData()
|
|
43
|
+
const elementData = {
|
|
44
|
+
...element.save(),
|
|
45
|
+
...elementComputedData
|
|
46
|
+
} as RenderElementData
|
|
47
|
+
|
|
48
|
+
return elementData
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
addElementById(id: string) {
|
|
52
|
+
const renderElementData = this._getRenderData(id)
|
|
53
|
+
if (renderElementData) {
|
|
54
|
+
this.addElement(renderElementData)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
addElement(data: RenderElementData) {
|
|
59
|
+
render.addElement(data)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
removeElement(data: ElementRawData, parentId?: string) {
|
|
63
|
+
render.removeElement(data.id, parentId)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
updateElement(
|
|
67
|
+
elementId: string,
|
|
68
|
+
key: string,
|
|
69
|
+
before: DataTypes,
|
|
70
|
+
after: DataTypes
|
|
71
|
+
) {
|
|
72
|
+
render.updateElement(elementId, key, before, after)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { RenderSceneTree }
|
|
77
|
+
|
|
78
|
+
const renderSceneTree = new RenderSceneTree()
|
|
79
|
+
export default renderSceneTree
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import selectionManager, { SelectionManager } from '@asyra/selection'
|
|
2
|
+
import { SELECTION_TYPES } from '@asyra/utils'
|
|
3
|
+
|
|
4
|
+
class RenderSelection {
|
|
5
|
+
selectionManager: SelectionManager
|
|
6
|
+
elementSelection: Set<string>
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
this.selectionManager = selectionManager
|
|
10
|
+
this.elementSelection = new Set()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getElementSelection() {
|
|
14
|
+
const selection = this.selectionManager.get(SELECTION_TYPES.ELEMENT)
|
|
15
|
+
return selection ? [...selection.getSelectedIds()] : []
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
updateSelection(type: SELECTION_TYPES) {
|
|
19
|
+
const selection = this.selectionManager.get(type)
|
|
20
|
+
if (!selection) {
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const selectedIds = selection.getSelectedIds()
|
|
25
|
+
|
|
26
|
+
switch (type) {
|
|
27
|
+
case SELECTION_TYPES.ELEMENT: {
|
|
28
|
+
this.elementSelection = new Set(selectedIds)
|
|
29
|
+
break
|
|
30
|
+
}
|
|
31
|
+
case SELECTION_TYPES.VERTEX:
|
|
32
|
+
break
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { RenderSelection }
|
|
38
|
+
|
|
39
|
+
const renderStore = new RenderSelection()
|
|
40
|
+
export default renderStore
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { initSystemContext } from './system'
|
|
2
|
+
import { initSceneTreeDataContext } from './scene-tree'
|
|
3
|
+
import { initSelectionContext } from './selection'
|
|
4
|
+
import { initViewportContext } from './viewport'
|
|
5
|
+
|
|
6
|
+
export const initDataContexts = () => {
|
|
7
|
+
initSystemContext()
|
|
8
|
+
initSceneTreeDataContext()
|
|
9
|
+
initSelectionContext()
|
|
10
|
+
initViewportContext()
|
|
11
|
+
}
|