@leafer/display 1.0.0-alpha.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer/display
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@leafer/display",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/display",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "files": ["src"],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leaferjs/leafer.git"
12
+ },
13
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/display",
14
+ "bugs": "https://github.com/leaferjs/leafer/issues",
15
+ "keywords": [
16
+ "leafer",
17
+ "leaferjs"
18
+ ],
19
+ "dependencies": {
20
+ "@leafer/math": "1.0.0-alpha.1",
21
+ "@leafer/data": "1.0.0-alpha.1",
22
+ "@leafer/layout": "1.0.0-alpha.1",
23
+ "@leafer/module": "1.0.0-alpha.1",
24
+ "@leafer/event": "1.0.0-alpha.1",
25
+ "@leafer/decorator": "1.0.0-alpha.1",
26
+ "@leafer/helper": "1.0.0-alpha.1"
27
+ },
28
+ "devDependencies": {
29
+ "@leafer/interface": "1.0.0-alpha.1"
30
+ }
31
+ }
package/src/Branch.ts ADDED
@@ -0,0 +1,137 @@
1
+ import { ILeaf, ILeaferCanvas, IRenderOptions } from '@leafer/interface'
2
+ import { ChildEvent } from '@leafer/event'
3
+ import { BoundsHelper } from '@leafer/math'
4
+ import { BranchHelper, LeafBoundsHelper } from '@leafer/helper'
5
+
6
+ import { Leaf } from './Leaf'
7
+
8
+
9
+ const { setByListWithHandle } = BoundsHelper
10
+ const { sort } = BranchHelper
11
+ const { relativeBoxBounds: localBoxBounds, relativeEventBounds: localEventBounds, relativeRenderBounds: localRenderBounds } = LeafBoundsHelper
12
+
13
+ export class Branch extends Leaf {
14
+
15
+ constructor() {
16
+ super()
17
+ this.__isBranch = true
18
+ this.children = []
19
+ }
20
+
21
+ // overwrite
22
+
23
+ public __updateEventBoundsSpreadWidth(): number {
24
+ const { children } = this
25
+ for (let i = 0, len = children.length; i < len; i++) {
26
+ if (children[i].__layout.eventBoundsSpreadWidth) return 1
27
+ }
28
+ return 0
29
+ }
30
+
31
+ public __updateRenderBoundsSpreadWidth(): number {
32
+ const { children } = this
33
+ for (let i = 0, len = children.length; i < len; i++) {
34
+ if (children[i].__layout.renderBoundsSpreadWidth) return 1
35
+ }
36
+ return 0
37
+ }
38
+
39
+
40
+ public __updateBoxBounds(): void {
41
+ setByListWithHandle(this.__layout.boxBounds, this.children, localBoxBounds)
42
+ }
43
+
44
+ public __updateEventBounds(): void {
45
+ setByListWithHandle(this.__layout.eventBounds, this.children, localEventBounds)
46
+ }
47
+
48
+ public __updateRenderBounds(): void {
49
+ setByListWithHandle(this.__layout.renderBounds, this.children, localRenderBounds)
50
+ }
51
+
52
+ public __updateChange(): void {
53
+ const { __layout: layout } = this
54
+ if (layout.childrenSortChanged) {
55
+ this.__updateSortChildren()
56
+ layout.childrenSortChanged = false
57
+ }
58
+ }
59
+
60
+
61
+ // own
62
+
63
+ public __updateSortChildren(): void {
64
+ const { children } = this
65
+ if (children.length > 1) {
66
+ for (let i = 0, len = children.length; i < len; i++) {
67
+ children[i].__tempNumber = i
68
+ }
69
+ children.sort(sort)
70
+ }
71
+ }
72
+
73
+ public __render(canvas: ILeaferCanvas, options: IRenderOptions): void {
74
+
75
+ if (this.__worldOpacity) {
76
+ let child: ILeaf
77
+ const { bounds, hideBounds } = options, { children } = this
78
+ for (let i = 0, len = children.length; i < len; i++) {
79
+ child = children[i]
80
+ if (bounds && !bounds.hit(child.__world, options.matrix)) continue
81
+ if (hideBounds && hideBounds.includes(child.__world)) continue
82
+ child.__render(canvas, options)
83
+ }
84
+ }
85
+
86
+ }
87
+
88
+ public add(child: ILeaf, index?: number): void {
89
+
90
+ if (child.parent) {
91
+ if (child.parent !== this) console.warn('child had other parent, can not add to this, child innerId:' + child.innerId)
92
+ return
93
+ }
94
+
95
+ child.parent = this
96
+
97
+ index === undefined ? this.children.push(child) : this.children.splice(index, 0, child)
98
+ if (child.__isBranch) this.__childBranchNumber ? this.__childBranchNumber++ : this.__childBranchNumber = 1
99
+
100
+ if (this.root) {
101
+ child.__bindRoot(this.root)
102
+
103
+ const event = new ChildEvent(ChildEvent.ADD, child, this)
104
+ if (this.hasEvent(ChildEvent.ADD)) this.emitEvent(event)
105
+ this.root.emitEvent(event)
106
+ }
107
+
108
+ if (child.__parentWait) child.__runParentWait()
109
+ }
110
+
111
+ public remove(child?: Leaf): void {
112
+
113
+ if (child) {
114
+ const index = this.children.indexOf(child)
115
+ if (index > -1) {
116
+ this.children.splice(index, 1)
117
+
118
+ if (child.__isBranch) this.__childBranchNumber > 1 ? this.__childBranchNumber-- : this.__childBranchNumber = 0
119
+
120
+ if (this.root) {
121
+ const event = new ChildEvent(ChildEvent.REMOVE, child, this)
122
+ if (this.hasEvent(ChildEvent.REMOVE)) this.emitEvent(event)
123
+ this.root.emitEvent(event)
124
+ child.root = undefined
125
+ }
126
+
127
+ child.parent = undefined
128
+ this.__layout.boxBoundsChange()
129
+ }
130
+ } else {
131
+ if (this.parent) this.parent.remove(this)
132
+ }
133
+
134
+ }
135
+ }
136
+
137
+
package/src/Leaf.ts ADDED
@@ -0,0 +1,266 @@
1
+ import { ILeafer, ILeaf, ILeafInputData, ILeafData, ILeaferCanvas, IRenderOptions, IMatrixWithBoundsData, __Number, __Boolean, ILeafLayout, InnerId, IHitCanvas, IRadiusPointData, IEventListenerMap, IEventListener, IEventListenerOptions, IEventListenerId, IEvent, IObject, IFunction, IBoundsData, IMatrixData, __String } from '@leafer/interface'
2
+ import { IncrementId } from '@leafer/math'
3
+ import { LeafData } from '@leafer/data'
4
+ import { LeafLayout } from '@leafer/layout'
5
+ import { LeafDataProxy, LeafMatrix, LeafBounds, LeafHit, LeafEventer, LeafRender } from '@leafer/display-module'
6
+ import { useModule } from '@leafer/decorator'
7
+
8
+
9
+ const { LEAF, create } = IncrementId
10
+
11
+ @useModule(LeafDataProxy)
12
+ @useModule(LeafMatrix)
13
+ @useModule(LeafBounds)
14
+ @useModule(LeafHit)
15
+ @useModule(LeafEventer)
16
+ @useModule(LeafRender)
17
+ export class Leaf implements ILeaf {
18
+
19
+ public get tag(): string { return this.constructor.name }
20
+ public readonly innerId: InnerId // 内部唯一标识
21
+ public get __DataProcessor() { return LeafData }
22
+ public get __LayoutProcessor() { return LeafLayout }
23
+
24
+ public leafer?: ILeafer
25
+ public root?: ILeaf
26
+ public parent?: ILeaf
27
+
28
+ public __isRoot: boolean
29
+ public __isBranch: boolean
30
+ public __isBranchLeaf: boolean
31
+
32
+ public __: ILeafData
33
+ public __layout: ILeafLayout
34
+
35
+ public __relative: IMatrixWithBoundsData
36
+ public __world: IMatrixWithBoundsData
37
+
38
+ public __worldOpacity: number
39
+ public __renderTime: number // μs 1000微秒 = 1毫秒
40
+ public __complex: boolean // 外观是否复杂
41
+
42
+ public __interactionOff: boolean
43
+ public __childrenInteractionOff: boolean
44
+
45
+ public __level: number // 所在层级 0 -> 高
46
+ public __tempNumber: number // 用于排序,记录最后一次在parent中的排序索引,可用于移除之后回退
47
+
48
+ public __hitCanvas?: IHitCanvas
49
+
50
+ // event
51
+ public __captureMap?: IEventListenerMap
52
+ public __bubbleMap?: IEventListenerMap
53
+
54
+ public __parentWait?: IFunction[]
55
+
56
+
57
+ // branch
58
+ public children?: ILeaf[]
59
+ public __childBranchNumber?: number
60
+
61
+ constructor(data?: ILeafInputData) {
62
+
63
+ this.innerId = create(LEAF)
64
+
65
+ this.__relative = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0, x: 0, y: 0, width: 0, height: 0 }
66
+ this.__world = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0, x: 0, y: 0, width: 0, height: 0 }
67
+
68
+ this.__worldOpacity = 1
69
+ this.__renderTime = 2
70
+
71
+ this.__ = new this.__DataProcessor(this)
72
+ this.__layout = new this.__LayoutProcessor(this)
73
+
74
+ if (data) Object.assign(this, data)
75
+ }
76
+
77
+
78
+ public __addParentWait(item: IFunction): void {
79
+ this.__parentWait ? this.__parentWait.push(item) : this.__parentWait = [item]
80
+ }
81
+
82
+ public __runParentWait(): void {
83
+ const len = this.__parentWait.length
84
+ for (let i = 0; i < len; i++) {
85
+ this.__parentWait[i]()
86
+ }
87
+ this.__parentWait = undefined
88
+ }
89
+
90
+ public __setAsLeafer(): void {
91
+ this.leafer = this as unknown as ILeafer
92
+ }
93
+
94
+ public __setAsRoot(): void {
95
+ this.__bindRoot(this)
96
+ this.__isRoot = true
97
+ }
98
+
99
+ public __bindRoot(root: ILeaf): void {
100
+ if (this.__isRoot) return
101
+
102
+ this.root = root
103
+ this.leafer = root.leafer
104
+ this.__level = this.parent ? this.parent.__level + 1 : 1
105
+
106
+ if (this.__isBranch) {
107
+ const { children } = this
108
+ for (let i = 0, len = children.length; i < len; i++) {
109
+ children[i].__bindRoot(root)
110
+ }
111
+ }
112
+ }
113
+
114
+
115
+ // LeafDataProxy rewrite
116
+
117
+ public __set(attrName: string, newValue: unknown): void { }
118
+
119
+ public __get(attrName: string): unknown { return undefined }
120
+
121
+ public __updateAttr(attrName: string): void { }
122
+
123
+ // ---
124
+
125
+
126
+ // worldOpacity rewrite, include visible
127
+
128
+ public __updateWorldOpacity(): void { }
129
+
130
+ // ---
131
+
132
+
133
+
134
+ // LeafMatrix rewrite
135
+
136
+ public __updateWorldMatrix(): void { }
137
+
138
+ public __updateRelativeMatrix(): void { }
139
+
140
+ // ---
141
+
142
+
143
+ // LeafBounds rewrite
144
+
145
+ public __updateWorldBounds(): void { }
146
+
147
+
148
+ public __updateRelativeBoxBounds(): void { }
149
+
150
+ public __updateRelativeEventBounds(): void { }
151
+
152
+ public __updateRelativeRenderBounds(): void { }
153
+
154
+ // box
155
+
156
+ public __updateBoxBounds(): void { }
157
+
158
+ public __updateEventBounds(): void { }
159
+
160
+ public __updateRenderBounds(): void { }
161
+
162
+
163
+ public __updateEventBoundsSpreadWidth(): number { return 0 }
164
+
165
+ public __updateRenderBoundsSpreadWidth(): number { return 0 }
166
+
167
+
168
+ public __onUpdateSize(): void { }
169
+
170
+ // ---
171
+
172
+
173
+ // LeafHit rewrite
174
+
175
+ public __hitWorld(point: IRadiusPointData): boolean { return true }
176
+
177
+ public __hit(local: IRadiusPointData): boolean { return true }
178
+
179
+ public __updateHitCanvas(): void { }
180
+
181
+ // ---
182
+
183
+
184
+ // LeafRender rewrite
185
+
186
+ public __render(canvas: ILeaferCanvas, options: IRenderOptions): void { }
187
+
188
+ public __drawFast(canvas: ILeaferCanvas, options: IRenderOptions): void { }
189
+
190
+ public __draw(canvas: ILeaferCanvas, options: IRenderOptions): void { }
191
+
192
+ public __updateChange(): void { }
193
+
194
+ // ---
195
+
196
+
197
+ // path
198
+
199
+ public __drawPath(canvas: ILeaferCanvas): void { }
200
+
201
+ public __drawRenderPath(canvas: ILeaferCanvas): void { }
202
+
203
+ public __updatePath(): void { }
204
+
205
+ public __updateRenderPath(): void { }
206
+
207
+
208
+ // Branch rewrite
209
+
210
+ public __updateSortChildren(): void { }
211
+
212
+ public add(child: ILeaf, index?: number): void { }
213
+
214
+ public remove(child?: ILeaf): void { }
215
+
216
+ // ---
217
+
218
+
219
+ // LeafEventer rewrite
220
+
221
+ public on(type: string | string[], listener: IEventListener, options?: IEventListenerOptions | boolean): void { }
222
+
223
+ public off(type: string | string[], listener: IEventListener, options?: IEventListenerOptions | boolean): void { }
224
+
225
+ public on__(type: string | string[], listener: IEventListener, bind?: IObject, options?: IEventListenerOptions | boolean): IEventListenerId { return undefined }
226
+
227
+ public off__(id: IEventListenerId | IEventListenerId[]): void { }
228
+
229
+ public once(type: string | string[], listener: IEventListener, capture?: boolean): void { }
230
+
231
+ public emit(type: string, event?: IEvent | IObject, capture?: boolean): void { }
232
+
233
+ public emitEvent(event?: IEvent, capture?: boolean): void { }
234
+
235
+ public hasEvent(type: string, capture?: boolean): boolean { return false }
236
+
237
+ // ---
238
+
239
+ public destroy(): void {
240
+ if (this.__) {
241
+
242
+ if (this.__isBranch) {
243
+ this.children.forEach(child => { child.destroy() })
244
+ this.children = undefined
245
+ }
246
+
247
+ if (this.__hitCanvas) {
248
+ this.__hitCanvas.destroy()
249
+ this.__hitCanvas = undefined
250
+ }
251
+
252
+ this.leafer = undefined
253
+ this.root = undefined
254
+ this.parent = undefined
255
+
256
+ this.__.destroy()
257
+ this.__layout.destroy()
258
+ this.__ = undefined
259
+ this.__layout = undefined
260
+
261
+ this.__captureMap = undefined
262
+ this.__bubbleMap = undefined
263
+ }
264
+ }
265
+
266
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { Leaf } from './Leaf'
2
+ export { Branch } from './Branch'
3
+