@leafer/layout 1.0.0-alpha.9 → 1.0.0-beta

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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/src/LeafLayout.ts +115 -84
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/layout",
3
- "version": "1.0.0-alpha.9",
3
+ "version": "1.0.0-beta",
4
4
  "description": "@leafer/layout",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,9 +19,10 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/math": "1.0.0-alpha.9"
22
+ "@leafer/math": "1.0.0-beta",
23
+ "@leafer/platform": "1.0.0-beta"
23
24
  },
24
25
  "devDependencies": {
25
- "@leafer/interface": "1.0.0-alpha.9"
26
+ "@leafer/interface": "1.0.0-beta"
26
27
  }
27
28
  }
package/src/LeafLayout.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { ILeaf, ILeafLayout, ILayoutLocationType, ILayoutBoundsType, IBoundsData, IMatrixData, } from '@leafer/interface'
2
- import { BoundsHelper } from '@leafer/math'
1
+ import { ILeaf, ILeafLayout, ILayoutLocationType, ILayoutBoundsType, IBoundsData, IMatrixData, IMatrixDecompositionData } from '@leafer/interface'
2
+ import { BoundsHelper, MatrixHelper } from '@leafer/math'
3
+ import { Platform } from '@leafer/platform'
3
4
 
4
5
 
5
- const { setByBoundsTimesMatrix } = BoundsHelper
6
+ const { toOuterOf } = BoundsHelper
6
7
 
7
8
  export class LeafLayout implements ILeafLayout {
8
9
 
@@ -12,41 +13,40 @@ export class LeafLayout implements ILeafLayout {
12
13
 
13
14
  // local
14
15
 
15
- public boxBounds: IBoundsData // | content + padding |
16
- public eventBounds: IBoundsData // | boxBounds + border |
17
- public renderBounds: IBoundsData // | eventBounds + shadow |
16
+ public boxBounds: IBoundsData
17
+ public strokeBounds: IBoundsData
18
+ public renderBounds: IBoundsData
18
19
 
19
20
  // auto layout
20
- public marginBounds: IBoundsData // | eventBounds + margin |
21
- public contentBounds: IBoundsData // | content |
21
+ public marginBounds: IBoundsData
22
+ public contentBounds: IBoundsData
22
23
 
23
- // relative
24
+ // local
24
25
 
25
- //relativeBoxBounds: IBoundsData = leaf.__relative
26
- public relativeEventBounds: IBoundsData
27
- public relativeRenderBounds: IBoundsData
26
+ public localStrokeBounds: IBoundsData
27
+ public localRenderBounds: IBoundsData
28
28
 
29
29
  // world temp
30
+ protected _worldContentBounds: IBoundsData
30
31
  protected _worldBoxBounds: IBoundsData
31
- protected _worldEventBounds: IBoundsData
32
- // worldRenderBounds: IBoundsData = leaf.__world
32
+ protected _worldStrokeBounds: IBoundsData
33
33
 
34
34
  // state
35
35
 
36
36
  // matrix changed
37
- public matrixChanged: boolean // include positionChanged scaleChanged skewChanged
38
- public positionChanged: boolean // x, y
39
- public scaleChanged: boolean // scaleX scaleY
40
- public rotationChanged: boolean // rotaiton, skewX scaleY
37
+ public matrixChanged: boolean
38
+ public positionChanged: boolean
39
+ public scaleChanged: boolean
40
+ public rotationChanged: boolean
41
41
 
42
42
  // bounds
43
43
  public boundsChanged: boolean
44
44
 
45
- public boxBoundsChanged: boolean
46
- public eventBoundsChanged: boolean
47
- public renderBoundsChanged: boolean
45
+ public boxChanged: boolean
46
+ public strokeChanged: boolean
47
+ public renderChanged: boolean
48
48
 
49
- public localBoxBoundsChanged: boolean
49
+ public localBoxChanged: boolean
50
50
 
51
51
  // face
52
52
  public surfaceChanged: boolean
@@ -59,133 +59,164 @@ export class LeafLayout implements ILeafLayout {
59
59
  // keep state
60
60
  public affectScaleOrRotation: boolean
61
61
  public affectRotation: boolean
62
- public eventBoundsSpreadWidth: number
63
- public renderBoundsSpreadWidth: number
64
- public renderShapeBoundsSpreadWidth: number
62
+
63
+ public strokeSpread: number
64
+ public renderSpread: number
65
+ public strokeBoxSpread: number
66
+ public renderShapeSpread: number
65
67
 
66
68
 
67
69
  constructor(leaf: ILeaf) {
68
70
  this.leaf = leaf
69
- this.renderBounds = this.eventBounds = this.boxBounds = { x: 0, y: 0, width: 0, height: 0 }
70
- this.relativeRenderBounds = this.relativeEventBounds = leaf.__relative
71
+ this.renderBounds = this.strokeBounds = this.boxBounds = { x: 0, y: 0, width: 0, height: 0 }
72
+ this.localRenderBounds = this.localStrokeBounds = leaf.__local
73
+ this.boxChange()
71
74
  }
72
75
 
73
76
 
74
- public update(): void {
77
+ public checkUpdate(): void {
75
78
  const { leafer } = this.leaf
76
- if (leafer && leafer.watcher.changed) {
77
- if (!leafer.running) leafer.start()
78
- leafer.layouter.layout()
79
+ if (leafer) {
80
+ if (leafer.ready) {
81
+ if (leafer.watcher.changed) leafer.layouter.layout()
82
+ } else {
83
+ leafer.start()
84
+ leafer.layouter.layout()
85
+ }
86
+ } else {
87
+ let root = this.leaf
88
+ while (root.parent) { root = root.parent }
89
+ Platform.layout(root)
79
90
  }
80
91
  }
81
92
 
82
- public getTransform(type: ILayoutLocationType): IMatrixData {
83
- this.update()
84
- return type === 'world' ? this.leaf.__world : this.leaf.__relative
93
+ public getTransform(locationType: ILayoutLocationType): IMatrixData {
94
+ this.checkUpdate()
95
+ return locationType === 'world' ? this.leaf.__world : this.leaf.__local
85
96
  }
86
97
 
87
- public getBounds(type: ILayoutLocationType, boundsType: ILayoutBoundsType): IBoundsData {
98
+ public decomposeTransform(locationType: ILayoutLocationType): IMatrixDecompositionData {
99
+ this.checkUpdate()
100
+ return MatrixHelper.decompose(locationType === 'world' ? this.leaf.__world : this.leaf.__local)
101
+ }
88
102
 
89
- this.update()
103
+ public getBounds(type: ILayoutBoundsType, locationType: ILayoutLocationType): IBoundsData {
90
104
 
91
- if (type === 'world') {
105
+ this.checkUpdate()
92
106
 
93
- switch (boundsType) {
107
+ if (locationType === 'world') {
108
+
109
+ switch (type) {
94
110
  case 'render':
95
111
  return this.leaf.__world
112
+ case 'content':
113
+ if (this.contentBounds) return this.getWorldContentBounds()
114
+ case 'margin':
96
115
  case 'box':
97
116
  return this.getWorldBoxBounds()
98
- case 'event':
99
- return this.getWorldEventBounds()
117
+ case 'margin':
118
+ case 'stroke':
119
+ return this.getWorldStrokeBounds()
100
120
  }
101
121
 
102
- } else if (type === 'local') {
122
+ } else if (locationType === 'inner') {
103
123
 
104
- switch (boundsType) {
124
+ switch (type) {
105
125
  case 'render':
106
126
  return this.renderBounds
127
+ case 'content':
128
+ if (this.contentBounds) return this.contentBounds
129
+ case 'margin':
107
130
  case 'box':
108
131
  return this.boxBounds
109
- case 'event':
110
- return this.eventBounds
132
+ case 'stroke':
133
+ return this.strokeBounds
111
134
  }
112
135
 
113
136
  } else {
114
137
 
115
- switch (boundsType) {
138
+ switch (type) {
116
139
  case 'render':
117
- return this.relativeRenderBounds
140
+ return this.localRenderBounds
141
+ case 'margin':
142
+ case 'content':
118
143
  case 'box':
119
- return this.leaf.__relative
120
- case 'event':
121
- return this.relativeEventBounds
144
+ return this.leaf.__local
145
+ case 'stroke':
146
+ return this.localStrokeBounds
122
147
  }
123
148
 
124
149
  }
125
- return this.leaf.__world
150
+
126
151
  }
127
152
 
153
+ protected getWorldContentBounds(): IBoundsData {
154
+ this._worldContentBounds || (this._worldContentBounds = {} as IBoundsData)
155
+ toOuterOf(this.contentBounds, this.leaf.__world, this._worldContentBounds)
156
+ return this._worldContentBounds
157
+ }
128
158
 
129
159
  protected getWorldBoxBounds(): IBoundsData {
130
160
  this._worldBoxBounds || (this._worldBoxBounds = {} as IBoundsData)
131
- setByBoundsTimesMatrix(this._worldBoxBounds, this.boxBounds, this.leaf.__world)
161
+ toOuterOf(this.boxBounds, this.leaf.__world, this._worldBoxBounds)
132
162
  return this._worldBoxBounds
133
163
  }
134
164
 
135
- protected getWorldEventBounds(): IBoundsData {
136
- this._worldEventBounds || (this._worldEventBounds = {} as IBoundsData)
137
- setByBoundsTimesMatrix(this._worldEventBounds, this.eventBounds, this.leaf.__world)
138
- return this._worldEventBounds
165
+ protected getWorldStrokeBounds(): IBoundsData {
166
+ this._worldStrokeBounds || (this._worldStrokeBounds = {} as IBoundsData)
167
+ toOuterOf(this.strokeBounds, this.leaf.__world, this._worldStrokeBounds)
168
+ return this._worldStrokeBounds
139
169
  }
140
170
 
141
171
  // 独立 / 引用 boxBounds
142
172
 
143
- public eventBoundsSpreadCancel(): void {
144
- const same = this.renderBounds === this.eventBounds
145
- this.eventBounds = this.boxBounds
146
- this.relativeEventBounds = this.leaf.__relative
147
- if (same) this.renderBoundsSpreadCancel()
173
+ public spreadStrokeCancel(): void {
174
+ const same = this.renderBounds === this.strokeBounds
175
+ this.strokeBounds = this.boxBounds
176
+ this.localStrokeBounds = this.leaf.__local
177
+ if (same) this.spreadRenderCancel()
148
178
  }
149
- public renderBoundsSpreadCancel(): void {
150
- this.renderBounds = this.eventBounds
151
- this.relativeRenderBounds = this.relativeEventBounds
179
+ public spreadRenderCancel(): void {
180
+ this.renderBounds = this.strokeBounds
181
+ this.localRenderBounds = this.localStrokeBounds
152
182
  }
153
183
 
154
- public eventBoundsSpread(): void {
155
- const { x, y, width, height } = this.eventBounds
156
- this.eventBounds = { x, y, width, height }
157
- this.relativeEventBounds = { x, y, width, height }
158
- if (!this.renderBoundsSpreadWidth) this.renderBoundsSpreadCancel()
184
+ public spreadStroke(): void {
185
+ const { x, y, width, height } = this.strokeBounds
186
+ this.strokeBounds = { x, y, width, height }
187
+ this.localStrokeBounds = { x, y, width, height }
188
+ if (!this.renderSpread) this.spreadRenderCancel()
159
189
  }
160
- public renderBoundsSpread(): void {
190
+ public spreadRender(): void {
161
191
  const { x, y, width, height } = this.renderBounds
162
192
  this.renderBounds = { x, y, width, height }
163
- this.relativeRenderBounds = { x, y, width, height }
193
+ this.localRenderBounds = { x, y, width, height }
164
194
  }
165
195
 
166
196
 
167
197
  // bounds
168
198
 
169
- public boxBoundsChange(): void {
170
- this.boxBoundsChanged = true
171
- this.localBoxBoundsChanged || this.localBoxBoundsChange()
199
+ public boxChange(): void {
200
+ this.boxChanged = true
201
+ this.localBoxChanged || this.localBoxChange()
172
202
  this.hitCanvasChanged = true
173
203
  }
174
204
 
175
- public localBoxBoundsChange(): void {
176
- this.localBoxBoundsChanged = true
205
+ public localBoxChange(): void {
206
+ this.localBoxChanged = true
177
207
  this.boundsChanged = true
178
208
  }
179
209
 
180
- public eventBoundsChange(): void {
181
- this.eventBoundsChanged = true
182
- this.eventBoundsSpreadWidth || (this.eventBoundsSpreadWidth = 1)
210
+ public strokeChange(): void {
211
+ this.strokeChanged = true
212
+ this.strokeSpread || (this.strokeSpread = 1)
183
213
  this.boundsChanged = true
214
+ this.hitCanvasChanged = true
184
215
  }
185
216
 
186
- public renderBoundsChange(): void {
187
- this.renderBoundsChanged = true
188
- this.renderBoundsSpreadWidth || (this.renderBoundsSpreadWidth = 1)
217
+ public renderChange(): void {
218
+ this.renderChanged = true
219
+ this.renderSpread || (this.renderSpread = 1)
189
220
  this.boundsChanged = true
190
221
  }
191
222
 
@@ -195,7 +226,7 @@ export class LeafLayout implements ILeafLayout {
195
226
  public positionChange(): void {
196
227
  this.positionChanged = true
197
228
  this.matrixChanged = true
198
- this.localBoxBoundsChanged || this.localBoxBoundsChange()
229
+ this.localBoxChanged || this.localBoxChange()
199
230
  }
200
231
 
201
232
  public scaleChange(): void {
@@ -212,7 +243,7 @@ export class LeafLayout implements ILeafLayout {
212
243
  protected _scaleOrRotationChange() {
213
244
  this.affectScaleOrRotation = true
214
245
  this.matrixChanged = true
215
- this.localBoxBoundsChanged || this.localBoxBoundsChange()
246
+ this.localBoxChanged || this.localBoxChange()
216
247
  }
217
248
 
218
249
 
@@ -228,7 +259,7 @@ export class LeafLayout implements ILeafLayout {
228
259
  }
229
260
 
230
261
  public destroy(): void {
231
- this.leaf = undefined
262
+ this.leaf = null
232
263
  }
233
264
 
234
265
  }