@leafer/display-module 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-module
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@leafer/display-module",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/display-module",
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-module",
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
+ },
23
+ "devDependencies": {
24
+ "@leafer/interface": "1.0.0-alpha.1"
25
+ }
26
+ }
@@ -0,0 +1,119 @@
1
+ import { ILeafBoundsModule } from '@leafer/interface'
2
+ import { BoundsHelper } from '@leafer/math'
3
+
4
+
5
+ const { setByBoundsTimesMatrix } = BoundsHelper
6
+
7
+ export const LeafBounds: ILeafBoundsModule = {
8
+
9
+ __updateWorldBounds(): void {
10
+
11
+ if (this.__layout.boundsChanged) {
12
+
13
+ let resize: boolean
14
+ const layout = this.__layout
15
+
16
+
17
+ if (layout.boxBoundsChanged) {
18
+
19
+ this.__updatePath()
20
+ this.__updateRenderPath()
21
+
22
+ this.__updateBoxBounds()
23
+ layout.boxBoundsChanged = false
24
+ resize = true
25
+ }
26
+
27
+
28
+ if (layout.localBoxBoundsChanged) { // position change
29
+
30
+ this.__updateRelativeBoxBounds()
31
+ layout.localBoxBoundsChanged = false
32
+
33
+ if (layout.eventBoundsSpreadWidth) layout.eventBoundsChanged = true
34
+ if (layout.renderBoundsSpreadWidth) layout.renderBoundsChanged = true
35
+ this.parent?.__layout.boxBoundsChange()
36
+ }
37
+
38
+
39
+ if (layout.eventBoundsChanged) {
40
+
41
+ layout.eventBoundsSpreadWidth = this.__updateEventBoundsSpreadWidth()
42
+
43
+ if (layout.eventBoundsSpreadWidth) {
44
+
45
+ if (layout.eventBounds === layout.boxBounds) {
46
+ layout.eventBoundsSpread()
47
+ }
48
+
49
+ this.__updateEventBounds()
50
+ this.__updateRelativeEventBounds()
51
+ layout.eventBoundsChanged = false
52
+
53
+ if (layout.renderBoundsSpreadWidth) layout.renderBoundsChanged = true
54
+
55
+ } else {
56
+ layout.eventBoundsSpreadCancel()
57
+ }
58
+
59
+ this.parent?.__layout.eventBoundsChange()
60
+ resize || (resize = true)
61
+ }
62
+
63
+
64
+ if (layout.renderBoundsChanged) {
65
+
66
+ layout.renderBoundsSpreadWidth = this.__updateRenderBoundsSpreadWidth()
67
+
68
+ if (layout.renderBoundsSpreadWidth) {
69
+
70
+ if (layout.renderBounds === layout.boxBounds || layout.renderBounds === layout.eventBounds) {
71
+ layout.renderBoundsSpread()
72
+ }
73
+
74
+ this.__updateRenderBounds()
75
+ this.__updateRelativeRenderBounds()
76
+ layout.renderBoundsChanged = false
77
+
78
+ } else {
79
+ layout.renderBoundsSpreadCancel()
80
+ }
81
+
82
+ this.parent?.__layout.renderBoundsChange()
83
+ }
84
+
85
+
86
+ layout.boundsChanged = false
87
+
88
+ setByBoundsTimesMatrix(this.__world, this.__layout.renderBounds, this.__world)
89
+
90
+ if (resize) this.__onUpdateSize()
91
+
92
+ } else {
93
+ setByBoundsTimesMatrix(this.__world, this.__layout.renderBounds, this.__world)
94
+ }
95
+
96
+ },
97
+
98
+ __updateRelativeBoxBounds(): void {
99
+ setByBoundsTimesMatrix(this.__relative, this.__layout.boxBounds, this.__relative)
100
+ },
101
+
102
+ __updateRelativeEventBounds(): void {
103
+ setByBoundsTimesMatrix(this.__layout.relativeEventBounds, this.__layout.eventBounds, this.__relative)
104
+ },
105
+
106
+ __updateRelativeRenderBounds(): void {
107
+ setByBoundsTimesMatrix(this.__layout.relativeRenderBounds, this.__layout.renderBounds, this.__relative)
108
+ },
109
+
110
+
111
+ __updateBoxBounds(): void {
112
+ const b = this.__layout.boxBounds
113
+ b.x = 0
114
+ b.y = 0
115
+ b.width = this.__.width
116
+ b.height = this.__.height
117
+ }
118
+
119
+ }
@@ -0,0 +1,24 @@
1
+ import { ILeafDataProxyModule } from '@leafer/interface'
2
+ import { AttrEvent } from '@leafer/event'
3
+
4
+
5
+ export const LeafDataProxy: ILeafDataProxyModule = {
6
+
7
+ __set(attrName: string, newValue: unknown): void {
8
+ const oldValue = this.__.__getInput(attrName)
9
+ if (oldValue !== newValue) {
10
+ this.__[attrName] = newValue
11
+ this.root.emitEvent(new AttrEvent(AttrEvent.CHANGE, this, attrName, oldValue, newValue))
12
+ }
13
+ },
14
+
15
+ __get(attrName: string): unknown {
16
+ return this.__.__getInput(attrName)
17
+ },
18
+
19
+ __updateAttr(attrName: string): void {
20
+ const value = this.__.__getInput(attrName)
21
+ this.root?.emitEvent(new AttrEvent(AttrEvent.CHANGE, this, attrName, value, value))
22
+ }
23
+
24
+ }
@@ -0,0 +1,122 @@
1
+ import { IEventListener, IEventListenerOptions, IEventListenerMap, IEventListenerItem, IEventListenerId, IEvent, IObject, IEventTarget, ILeafEventerModule } from '@leafer/interface'
2
+
3
+
4
+ const empty = {}
5
+
6
+ export const LeafEventer: ILeafEventerModule = {
7
+
8
+ on(type: string | string[], listener: IEventListener, options?: IEventListenerOptions | boolean): void {
9
+ let capture: boolean, once: boolean
10
+ if (options) {
11
+ const isBoolean = typeof options === 'boolean'
12
+ capture = isBoolean ? options : options.capture
13
+ once = isBoolean ? false : options.once
14
+ }
15
+
16
+ let events: IEventListenerItem[]
17
+ const map = __getListenerMap(this, capture, true)
18
+ const typeList = typeof type === 'string' ? type.split(' ') : type
19
+ const item = once ? { listener, once } : { listener }
20
+
21
+ typeList.forEach(type => {
22
+ if (type) {
23
+ events = map[type]
24
+ if (events) {
25
+ if (events.findIndex(item => item.listener === listener) === -1) events.push(item)
26
+ } else {
27
+ map[type] = [item]
28
+ }
29
+ }
30
+ })
31
+ },
32
+
33
+ off(type: string | string[], listener: IEventListener, options?: IEventListenerOptions | boolean): void {
34
+ let capture: boolean
35
+ if (options) capture = typeof options === 'boolean' ? options : options.capture
36
+
37
+ let events: IEventListenerItem[], index: number
38
+ const map = __getListenerMap(this, capture)
39
+ const typeList = typeof type === 'string' ? type.split(' ') : type
40
+
41
+ typeList.forEach(type => {
42
+ if (type) {
43
+ events = map[type]
44
+ if (events) {
45
+ index = events.findIndex(item => item.listener === listener)
46
+ if (index > -1) events.splice(index, 1)
47
+ if (!events.length) delete map[type]
48
+ }
49
+ }
50
+ })
51
+ },
52
+
53
+ on__(type: string | string[], listener: IEventListener, bind?: IObject, options?: IEventListenerOptions | boolean): IEventListenerId {
54
+ if (bind) listener = listener.bind(bind)
55
+ this.on(type, listener, options)
56
+ return { type, listener, options }
57
+ },
58
+
59
+ off__(id: IEventListenerId | IEventListenerId[]): void {
60
+ if (!id) return
61
+ const list = id instanceof Array ? id : [id]
62
+ list.forEach(item => {
63
+ this.off(item.type, item.listener, item.options)
64
+ })
65
+ list.length = 0
66
+ },
67
+
68
+ once(type: string | string[], listener: IEventListener, capture?: boolean): void {
69
+ this.on(type, listener, { once: true, capture })
70
+ },
71
+
72
+ emit(type: string, event?: IEvent | IObject, capture?: boolean): void {
73
+ const map = __getListenerMap(this, capture)
74
+ const list = map[type]
75
+ if (list) {
76
+ let item: IEventListenerItem
77
+ for (let i = 0, len = list.length; i < len; i++) {
78
+ item = list[i]
79
+ item.listener(event)
80
+ if (item.once) this.off(type, item.listener, capture)
81
+ if (event && (event as IEvent).isStopNow) break
82
+ }
83
+ }
84
+ },
85
+
86
+ emitEvent(event: IEvent, capture?: boolean): void {
87
+ event.current = this
88
+ this.emit(event.type, event, capture)
89
+ },
90
+
91
+ hasEvent(type: string, capture?: boolean): boolean {
92
+ const { __bubbleMap: b, __captureMap: c } = this
93
+ if (capture === undefined) {
94
+ return !!((c && c[type]) || (b && b[type]))
95
+ } else {
96
+ return !!(capture ? (c && c[type]) : (b && b[type]))
97
+ }
98
+ },
99
+
100
+ }
101
+
102
+ function __getListenerMap(eventer: IEventTarget, capture?: boolean, create?: boolean): IEventListenerMap {
103
+ if (capture) {
104
+
105
+ const { __captureMap: c } = eventer
106
+ if (c) {
107
+ return c
108
+ } else {
109
+ return create ? eventer.__captureMap = {} : empty
110
+ }
111
+
112
+ } else {
113
+
114
+ const { __bubbleMap: b } = eventer
115
+ if (b) {
116
+ return b
117
+ } else {
118
+ return create ? eventer.__bubbleMap = {} : empty
119
+ }
120
+
121
+ }
122
+ }
package/src/LeafHit.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { ILeafHitModule, IRadiusPointData } from '@leafer/interface'
2
+ import { PointHelper } from '@leafer/math'
3
+
4
+
5
+ const { toLocalRadiusPoint } = PointHelper
6
+ const innerPoint = {} as IRadiusPointData
7
+
8
+ export const LeafHit: ILeafHitModule = {
9
+
10
+ __updateHitCanvas(): void {
11
+ if (!this.__hitCanvas) this.__hitCanvas = this.leafer.hitCanvasManager.getPathType(this)
12
+ this.__drawRenderPath(this.__hitCanvas)
13
+ },
14
+
15
+ __hitWorld(point: IRadiusPointData): boolean {
16
+ if (this.__layout.hitCanvasChanged) {
17
+ this.__updateHitCanvas()
18
+ this.__layout.hitCanvasChanged = false
19
+ }
20
+ toLocalRadiusPoint(point, this.__world, innerPoint)
21
+ return this.__hit(innerPoint)
22
+ }
23
+
24
+ }
@@ -0,0 +1,99 @@
1
+ import { ILeafMatrixModule } from '@leafer/interface'
2
+ import { OneRadian, MatrixHelper } from '@leafer/math'
3
+
4
+
5
+ const { defaultMatrix } = MatrixHelper
6
+ const { sin, cos } = Math
7
+
8
+ export const LeafMatrix: ILeafMatrixModule = {
9
+
10
+ __updateWorldMatrix(): void {
11
+
12
+ const pw = this.parent ? this.parent.__world : defaultMatrix
13
+ const r = this.__relative
14
+ const w = this.__world
15
+
16
+ if (this.__layout.matrixChanged) this.__updateRelativeMatrix()
17
+
18
+ if (this.__layout.affectScaleOrRotation) {
19
+ w.a = r.a * pw.a + r.b * pw.c
20
+ w.b = r.a * pw.b + r.b * pw.d
21
+ w.c = r.c * pw.a + r.d * pw.c
22
+ w.d = r.c * pw.b + r.d * pw.d
23
+ w.e = r.e * pw.a + r.f * pw.c + pw.e
24
+ w.f = r.e * pw.b + r.f * pw.d + pw.f
25
+ } else {
26
+ w.a = pw.a
27
+ w.b = pw.b
28
+ w.c = pw.c
29
+ w.d = pw.d
30
+ w.e = r.e * pw.a + r.f * pw.c + pw.e
31
+ w.f = r.e * pw.b + r.f * pw.d + pw.f
32
+ }
33
+ },
34
+
35
+ __updateRelativeMatrix(): void {
36
+
37
+ const r = this.__relative
38
+ const layout = this.__layout
39
+
40
+ if (layout.affectScaleOrRotation) {
41
+
42
+ const { scaleX, scaleY } = this.__
43
+
44
+ if (layout.affectRotation) {
45
+
46
+ if (layout.scaleChanged || layout.rotationChanged) {
47
+
48
+ let { rotation, skewX, skewY } = this.__
49
+
50
+ if (rotation || skewX || skewY) {
51
+
52
+ rotation *= OneRadian
53
+ if (skewX) skewX *= OneRadian
54
+ if (skewY) skewY *= OneRadian
55
+
56
+ r.a = scaleX * cos(rotation + skewY)
57
+ r.b = scaleX * sin(rotation + skewY)
58
+ r.c = scaleY * -sin(rotation - skewX)
59
+ r.d = scaleY * cos(rotation - skewX)
60
+
61
+ } else {
62
+
63
+ r.a = scaleX
64
+ r.b = 0
65
+ r.c = 0
66
+ r.d = scaleY
67
+
68
+ layout.affectRotation = false
69
+ }
70
+
71
+ layout.scaleChanged = false
72
+ layout.rotationChanged = false
73
+
74
+ }
75
+
76
+ } else {
77
+
78
+ if (layout.scaleChanged) {
79
+ r.a = scaleX
80
+ r.d = scaleY
81
+ layout.scaleChanged = false
82
+ }
83
+
84
+ }
85
+
86
+ }
87
+
88
+ if (layout.positionChanged) {
89
+ r.e = this.__.x
90
+ r.f = this.__.y
91
+ layout.positionChanged = false
92
+ }
93
+
94
+ this.__layout.matrixChanged = false
95
+ }
96
+
97
+ }
98
+
99
+
@@ -0,0 +1,19 @@
1
+ import { ILeaferCanvas, IRenderOptions, ILeafRenderModule } from '@leafer/interface'
2
+
3
+
4
+ export const LeafRender: ILeafRenderModule = {
5
+
6
+ __render(canvas: ILeaferCanvas, options: IRenderOptions): void {
7
+ if (this.__worldOpacity) {
8
+ canvas.setWorld(this.__world, options.matrix)
9
+ canvas.opacity = this.__worldOpacity
10
+ this.__complex ? this.__draw(canvas, options) : this.__drawFast(canvas, options)
11
+ }
12
+ },
13
+
14
+ __updateWorldOpacity(): void {
15
+ this.__worldOpacity = this.__.visible ? (this.parent ? this.parent.__worldOpacity * this.__.opacity : this.__.opacity) : 0
16
+ if (this.__layout.opacityChanged) this.__layout.opacityChanged = false
17
+ }
18
+
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export { LeafEventer } from './LeafEventer'
2
+ export { LeafDataProxy } from './LeafDataProxy'
3
+ export { LeafMatrix } from './LeafMatrix'
4
+ export { LeafBounds } from './LeafBounds'
5
+ export { LeafHit } from './LeafHit'
6
+ export { LeafRender } from './LeafRender'
7
+