@leafer/layouter 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 +21 -0
- package/README.md +1 -0
- package/package.json +30 -0
- package/src/LayoutBlockData.ts +52 -0
- package/src/Layouter.ts +189 -0
- package/src/LayouterHelper.ts +67 -0
- package/src/index.ts +1 -0
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/layouter
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leafer/layouter",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"description": "@leafer/canvas",
|
|
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/canvas/canvas",
|
|
14
|
+
"bugs": "https://github.com/leaferjs/leafer/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"leafer",
|
|
17
|
+
"leaferjs"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@leafer/event": "1.0.0-alpha.1",
|
|
21
|
+
"@leafer/math": "1.0.0-alpha.1",
|
|
22
|
+
"@leafer/list": "1.0.0-alpha.1",
|
|
23
|
+
"@leafer/data": "1.0.0-alpha.1",
|
|
24
|
+
"@leafer/helper": "1.0.0-alpha.1",
|
|
25
|
+
"@leafer/debug": "1.0.0-alpha.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@leafer/interface": "1.0.0-alpha.1"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { IBounds, ILayoutBlockData, ILeafList, ILeaf } from '@leafer/interface'
|
|
2
|
+
import { Bounds, BoundsHelper } from '@leafer/math'
|
|
3
|
+
import { LeafBoundsHelper } from '@leafer/helper'
|
|
4
|
+
import { LeafList } from '@leafer/list'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const { worldBounds } = LeafBoundsHelper
|
|
8
|
+
const { setByListWithHandle } = BoundsHelper
|
|
9
|
+
|
|
10
|
+
export class LayoutBlockData implements ILayoutBlockData {
|
|
11
|
+
|
|
12
|
+
public updatedList: ILeafList
|
|
13
|
+
public updatedBounds: IBounds = new Bounds()
|
|
14
|
+
|
|
15
|
+
public beforeBounds: IBounds = new Bounds()
|
|
16
|
+
public afterBounds: IBounds = new Bounds()
|
|
17
|
+
|
|
18
|
+
constructor(list: ILeafList | ILeaf[]) {
|
|
19
|
+
if (list instanceof Array) list = new LeafList(list)
|
|
20
|
+
this.updatedList = list
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public setBefore(): void {
|
|
24
|
+
setByListWithHandle(this.beforeBounds, this.updatedList.list, worldBounds)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public setAfter(): void {
|
|
28
|
+
setByListWithHandle(this.afterBounds, this.updatedList.list, worldBounds)
|
|
29
|
+
this.__computeChange()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public merge(data: ILayoutBlockData): void {
|
|
33
|
+
this.updatedList.pushList(data.updatedList.list)
|
|
34
|
+
this.beforeBounds.add(data.beforeBounds)
|
|
35
|
+
this.afterBounds.add(data.afterBounds)
|
|
36
|
+
this.updatedBounds.add(data.updatedBounds)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected __computeChange(): void {
|
|
40
|
+
const { updatedBounds: changedBounds } = this
|
|
41
|
+
changedBounds.setByList([this.beforeBounds, this.afterBounds])
|
|
42
|
+
if (!changedBounds.isEmpty()) {
|
|
43
|
+
changedBounds.spread(2)
|
|
44
|
+
changedBounds.ceil()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public destroy(): void {
|
|
49
|
+
this.updatedList = undefined
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|
package/src/Layouter.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { ILayouter, ILeaf, ILayoutBlockData, IEventListenerId, ILayouterConfig, ILeafList } from '@leafer/interface'
|
|
2
|
+
import { LayoutEvent, RenderEvent, WatchEvent } from '@leafer/event'
|
|
3
|
+
import { LeafLevelList, LeafList } from '@leafer/list'
|
|
4
|
+
import { BranchHelper, LeafHelper } from '@leafer/helper'
|
|
5
|
+
import { DataHelper } from '@leafer/data'
|
|
6
|
+
import { Run } from '@leafer/debug'
|
|
7
|
+
|
|
8
|
+
import { updateBounds, updateMatrix, updateChange } from './LayouterHelper'
|
|
9
|
+
import { LayoutBlockData } from './LayoutBlockData'
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const { updateAllWorldMatrix, updateAllChange } = LeafHelper
|
|
13
|
+
const { pushAllBranchStack, updateWorldBoundsByBranchStack } = BranchHelper
|
|
14
|
+
|
|
15
|
+
export class Layouter implements ILayouter {
|
|
16
|
+
|
|
17
|
+
public target: ILeaf
|
|
18
|
+
public layoutedBlocks: ILayoutBlockData[]
|
|
19
|
+
public totalTimes: number = 0
|
|
20
|
+
public times: number
|
|
21
|
+
public changed: boolean = true
|
|
22
|
+
|
|
23
|
+
public config: ILayouterConfig = {
|
|
24
|
+
partLayout: {
|
|
25
|
+
maxTimes: 3
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public running: boolean
|
|
30
|
+
|
|
31
|
+
protected updateList: ILeafList
|
|
32
|
+
protected levelList: LeafLevelList = new LeafLevelList()
|
|
33
|
+
protected eventIds: IEventListenerId[]
|
|
34
|
+
|
|
35
|
+
constructor(target: ILeaf, userConfig?: ILayouterConfig) {
|
|
36
|
+
this.target = target
|
|
37
|
+
if (userConfig) this.config = DataHelper.default(userConfig, this.config)
|
|
38
|
+
this.listenEvents()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public start(): void {
|
|
42
|
+
this.running = true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public stop(): void {
|
|
46
|
+
this.running = false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected listenEvents(): void {
|
|
50
|
+
const { target } = this
|
|
51
|
+
this.eventIds = [
|
|
52
|
+
target.on__(LayoutEvent.REQUEST, this.layout, this),
|
|
53
|
+
target.on__(LayoutEvent.AGAIN, this.layoutOnce, this),
|
|
54
|
+
target.on__(WatchEvent.DATA, this.onReceiveWatchData, this),
|
|
55
|
+
target.on__(RenderEvent.REQUEST, this.onChange, this),
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected removeListenEvents(): void {
|
|
60
|
+
this.target.off__(this.eventIds)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected onReceiveWatchData(event: WatchEvent): void {
|
|
64
|
+
this.updateList = event.data.updatedList
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected onChange(): void {
|
|
68
|
+
this.changed = true
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public layout(): void {
|
|
72
|
+
if (!this.running) return
|
|
73
|
+
const { target } = this
|
|
74
|
+
const { START, LAYOUT, END } = LayoutEvent
|
|
75
|
+
this.times = 0
|
|
76
|
+
this.changed = false
|
|
77
|
+
target.emit(START)
|
|
78
|
+
this.layoutOnce()
|
|
79
|
+
target.emitEvent(new LayoutEvent(LAYOUT, this.layoutedBlocks))
|
|
80
|
+
target.emitEvent(new LayoutEvent(END, this.layoutedBlocks))
|
|
81
|
+
this.layoutedBlocks = undefined
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public layoutOnce(): void {
|
|
85
|
+
|
|
86
|
+
this.totalTimes++
|
|
87
|
+
this.times++
|
|
88
|
+
|
|
89
|
+
this.target.emit(WatchEvent.REQUEST)
|
|
90
|
+
if (this.totalTimes > 1) {
|
|
91
|
+
this.partLayout()
|
|
92
|
+
} else {
|
|
93
|
+
this.fullLayout()
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public partLayout(): void {
|
|
98
|
+
if (!this.updateList?.length) return
|
|
99
|
+
|
|
100
|
+
const t = Run.start('part layout')
|
|
101
|
+
const { target, updateList } = this
|
|
102
|
+
const { BEFORE_ONCE, ONCE, AFTER_ONCE } = LayoutEvent
|
|
103
|
+
|
|
104
|
+
const blocks = this.getBlocks(updateList)
|
|
105
|
+
blocks.forEach(item => { item.setBefore() })
|
|
106
|
+
target.emitEvent(new LayoutEvent(BEFORE_ONCE, blocks))
|
|
107
|
+
|
|
108
|
+
updateList.sort()
|
|
109
|
+
updateMatrix(updateList, this.levelList)
|
|
110
|
+
updateBounds(this.levelList)
|
|
111
|
+
updateChange(updateList)
|
|
112
|
+
|
|
113
|
+
blocks.forEach(item => { item.setAfter() })
|
|
114
|
+
target.emitEvent(new LayoutEvent(ONCE, blocks))
|
|
115
|
+
target.emitEvent(new LayoutEvent(AFTER_ONCE, blocks))
|
|
116
|
+
|
|
117
|
+
this.setBlocks(blocks)
|
|
118
|
+
|
|
119
|
+
this.levelList.reset()
|
|
120
|
+
this.updateList = undefined
|
|
121
|
+
Run.end(t)
|
|
122
|
+
|
|
123
|
+
this.__checkAgain()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public fullLayout(): void {
|
|
127
|
+
const t = Run.start('full layout')
|
|
128
|
+
|
|
129
|
+
const { target } = this
|
|
130
|
+
const { BEFORE_ONCE, ONCE, AFTER_ONCE } = LayoutEvent
|
|
131
|
+
|
|
132
|
+
const blocks = this.getBlocks(new LeafList(target))
|
|
133
|
+
target.emitEvent(new LayoutEvent(BEFORE_ONCE, blocks))
|
|
134
|
+
|
|
135
|
+
Layouter.fullLayout(target)
|
|
136
|
+
|
|
137
|
+
blocks.forEach(item => { item.setAfter() })
|
|
138
|
+
target.emitEvent(new LayoutEvent(ONCE, blocks))
|
|
139
|
+
target.emitEvent(new LayoutEvent(AFTER_ONCE, blocks))
|
|
140
|
+
|
|
141
|
+
this.setBlocks(blocks)
|
|
142
|
+
|
|
143
|
+
Run.end(t)
|
|
144
|
+
|
|
145
|
+
this.__checkAgain()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
protected __checkAgain(): void {
|
|
149
|
+
if (this.changed && this.times <= this.config.partLayout.maxTimes) this.target.emit(LayoutEvent.AGAIN) // 防止更新布局过程中产生了属性修改
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
public createBlock(data: ILeafList | ILeaf[]): ILayoutBlockData {
|
|
154
|
+
return new LayoutBlockData(data)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
public getBlocks(list: ILeafList): ILayoutBlockData[] {
|
|
158
|
+
return [this.createBlock(list)]
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public setBlocks(current: ILayoutBlockData[]) {
|
|
162
|
+
this.layoutedBlocks ? this.layoutedBlocks.push(...current) : this.layoutedBlocks = current
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
static fullLayout(target: ILeaf): void {
|
|
167
|
+
updateAllWorldMatrix(target)
|
|
168
|
+
|
|
169
|
+
if (target.__isBranch) {
|
|
170
|
+
const branchStack: ILeaf[] = [target]
|
|
171
|
+
pushAllBranchStack(target, branchStack)
|
|
172
|
+
updateWorldBoundsByBranchStack(branchStack)
|
|
173
|
+
} else {
|
|
174
|
+
target.__updateWorldBounds()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
updateAllChange(target)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
destroy(): void {
|
|
181
|
+
if (this.target) {
|
|
182
|
+
this.removeListenEvents()
|
|
183
|
+
this.target = undefined
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ILeaf, ILeafLayout, ILeafLevelList, ILeafList } from '@leafer/interface'
|
|
2
|
+
import { BranchHelper, LeafHelper } from '@leafer/helper'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const { updateAllWorldMatrix, updateAllWorldOpacity } = LeafHelper
|
|
6
|
+
const { pushAllChildBranch, pushAllParent } = BranchHelper
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export function updateMatrix(updateList: ILeafList, levelList: ILeafLevelList): void {
|
|
10
|
+
|
|
11
|
+
let layout: ILeafLayout
|
|
12
|
+
updateList.list.forEach(leaf => { // 更新矩阵, 所有子元素,和父元素都需要更新bounds
|
|
13
|
+
layout = leaf.__layout
|
|
14
|
+
if (levelList.without(leaf)) { // 防止重复, 子元素可能已经被父元素更新过
|
|
15
|
+
|
|
16
|
+
if (layout.matrixChanged) {
|
|
17
|
+
|
|
18
|
+
updateAllWorldMatrix(leaf)
|
|
19
|
+
|
|
20
|
+
levelList.push(leaf)
|
|
21
|
+
if (leaf.__isBranch) pushAllChildBranch(leaf, levelList)
|
|
22
|
+
pushAllParent(leaf, levelList)
|
|
23
|
+
|
|
24
|
+
} else if (layout.boundsChanged) {
|
|
25
|
+
|
|
26
|
+
levelList.push(leaf)
|
|
27
|
+
if (leaf.__isBranch) leaf.__tempNumber = 0 // 标识需要更新子Leaf元素的WorldBounds分支, 0表示不需要更新
|
|
28
|
+
pushAllParent(leaf, levelList)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export function updateBounds(boundsList: ILeafLevelList): void {
|
|
37
|
+
|
|
38
|
+
let itemList: ILeaf[], branch: ILeaf
|
|
39
|
+
boundsList.sort(true)
|
|
40
|
+
boundsList.levels.forEach(level => {
|
|
41
|
+
itemList = boundsList.levelMap[level]
|
|
42
|
+
for (let i = 0, len = itemList.length; i < len; i++) {
|
|
43
|
+
branch = itemList[i]
|
|
44
|
+
|
|
45
|
+
// 标识了需要更新子元素
|
|
46
|
+
if (branch.__isBranch && branch.__tempNumber) {
|
|
47
|
+
for (let j = 0, jLen = branch.children.length; j < jLen; j++) {
|
|
48
|
+
if (!branch.children[j].__isBranch) {
|
|
49
|
+
branch.children[j].__updateWorldBounds()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
branch.__updateWorldBounds()
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
export function updateChange(updateList: ILeafList): void {
|
|
63
|
+
updateList.list.forEach(leaf => {
|
|
64
|
+
if (leaf.__layout.opacityChanged) updateAllWorldOpacity(leaf)
|
|
65
|
+
leaf.__updateChange()
|
|
66
|
+
})
|
|
67
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Layouter } from './Layouter'
|