@vyr/engine 0.0.33 → 0.0.35

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 (137) hide show
  1. package/package.json +6 -3
  2. package/src/ArrayUtils.ts +63 -65
  3. package/src/AsyncTask.ts +67 -71
  4. package/src/Category.ts +69 -86
  5. package/src/Color.ts +3 -111
  6. package/src/Engine.ts +12 -24
  7. package/src/Executor.ts +109 -0
  8. package/src/Generate.ts +23 -40
  9. package/src/InputSystem.ts +106 -108
  10. package/src/Listener.ts +58 -59
  11. package/src/ObjectPool.ts +83 -84
  12. package/src/ObjectUtils.ts +97 -49
  13. package/src/Scriptable.ts +82 -0
  14. package/src/Serialization.ts +4 -6
  15. package/src/Traverser.ts +41 -39
  16. package/src/actor/Actor.ts +23 -27
  17. package/src/actor/AnimationUnitActor.ts +22 -36
  18. package/src/actor/DivActor.ts +21 -41
  19. package/src/actor/FragmentActor.ts +1 -5
  20. package/src/actor/HTMLActor.ts +81 -0
  21. package/src/actor/HTMLServiceActor.ts +49 -0
  22. package/src/actor/{HTMTransformControllerActor.ts → HTMLTransformControllerActor.ts} +178 -71
  23. package/src/actor/InputActor.ts +50 -0
  24. package/src/actor/TextActor.ts +51 -0
  25. package/src/actor/index.ts +6 -5
  26. package/src/asset/Asset.ts +23 -68
  27. package/src/asset/AssetGraph.ts +21 -28
  28. package/src/descriptor/AnimationUnit.ts +81 -0
  29. package/src/descriptor/Camera.ts +12 -0
  30. package/src/descriptor/Controller.ts +6 -0
  31. package/src/descriptor/Descriptor.ts +133 -113
  32. package/src/descriptor/Div.ts +29 -0
  33. package/src/descriptor/HTML.ts +22 -0
  34. package/src/descriptor/HTMLService.ts +42 -0
  35. package/src/descriptor/HTMLTransformController.ts +67 -0
  36. package/src/descriptor/Input.ts +29 -0
  37. package/src/descriptor/Interaction.ts +80 -0
  38. package/src/descriptor/Node.ts +98 -0
  39. package/src/descriptor/Scene.ts +110 -0
  40. package/src/descriptor/Service.ts +11 -0
  41. package/src/descriptor/Store.ts +136 -0
  42. package/src/descriptor/Text.ts +36 -0
  43. package/src/descriptor/index.ts +14 -15
  44. package/src/graphics/Collection.ts +1 -5
  45. package/src/graphics/Compilation.ts +15 -19
  46. package/src/graphics/Graphics.ts +41 -54
  47. package/src/graphics/Observer.ts +6 -14
  48. package/src/graphics/Unit.ts +3 -9
  49. package/src/graphics/VariableProxy.ts +11 -15
  50. package/src/index.ts +5 -5
  51. package/src/interpreter/AnimationUnitInterpreter.ts +7 -9
  52. package/src/interpreter/DivInterpreter.ts +10 -13
  53. package/src/interpreter/FragmentInterpreter.ts +2 -6
  54. package/src/interpreter/HTMLServiceInterpreter.ts +25 -15
  55. package/src/interpreter/HTMLTransformControllerInterpreter.ts +9 -12
  56. package/src/interpreter/InputInterpreter.ts +41 -0
  57. package/src/interpreter/Interpreter.ts +3 -4
  58. package/src/interpreter/SceneInterpreter.ts +93 -0
  59. package/src/interpreter/ServiceInterpreter.ts +8 -15
  60. package/src/interpreter/StoreInterpreter.ts +7 -0
  61. package/src/interpreter/TextInterpreter.ts +41 -0
  62. package/src/interpreter/index.ts +5 -7
  63. package/src/locale/Language.ts +1 -5
  64. package/src/locale/LanguageProvider.ts +164 -21
  65. package/src/math/Euler.ts +2 -5
  66. package/src/math/Matrix4.ts +2 -4
  67. package/src/math/Quaternion.ts +2 -4
  68. package/src/math/Vector2.ts +4 -4
  69. package/src/math/Vector3.ts +2 -4
  70. package/src/math/utils.ts +1 -5
  71. package/src/schema/AnimationUnit.ts +68 -0
  72. package/src/schema/Asset.ts +13 -0
  73. package/src/schema/Descriptor.ts +41 -0
  74. package/src/schema/HTML.ts +261 -0
  75. package/src/schema/Interaction.ts +50 -0
  76. package/src/schema/Scene.ts +138 -0
  77. package/src/schema/Store.ts +8 -0
  78. package/src/schema/index.ts +7 -0
  79. package/src/scripts/ConditionScriptable.ts +196 -0
  80. package/src/scripts/FetchScriptable.ts +51 -0
  81. package/src/scripts/FindScriptable.ts +23 -0
  82. package/src/scripts/InvokeScriptable.ts +13 -0
  83. package/src/scripts/SwitchSceneScriptable.ts +18 -0
  84. package/src/scripts/UpdateScriptable.ts +60 -0
  85. package/src/scripts/index.ts +6 -0
  86. package/src/utils/AssetProvider.ts +4 -77
  87. package/src/utils/DOM.ts +37 -0
  88. package/src/utils/HTML.ts +5 -0
  89. package/src/utils/Service.ts +40 -0
  90. package/src/utils/constants.ts +1 -5
  91. package/src/utils/http.ts +2 -21
  92. package/src/utils/index.ts +3 -1
  93. package/src/actor/HTMActor.ts +0 -169
  94. package/src/actor/HTMServiceActor.ts +0 -57
  95. package/src/actor/StyleActor.ts +0 -96
  96. package/src/descriptor/AnimationUnitDescriptor.ts +0 -65
  97. package/src/descriptor/CameraDescriptor.ts +0 -12
  98. package/src/descriptor/ControllerDescriptor.ts +0 -16
  99. package/src/descriptor/DatasetDescriptor.ts +0 -90
  100. package/src/descriptor/DivDescriptor.ts +0 -18
  101. package/src/descriptor/DynamicDescriptor.ts +0 -27
  102. package/src/descriptor/HTMLDescriptor.ts +0 -87
  103. package/src/descriptor/HTMLServiceDescriptor.ts +0 -19
  104. package/src/descriptor/HTMLTransformControllerDescriptor.ts +0 -34
  105. package/src/descriptor/NodeDescriptor.ts +0 -32
  106. package/src/descriptor/PrefabDescriptor.ts +0 -53
  107. package/src/descriptor/PrefabInstanceDescriptor.ts +0 -32
  108. package/src/descriptor/ServiceDescriptor.ts +0 -32
  109. package/src/descriptor/ServiceSchedulerDescriptor.ts +0 -32
  110. package/src/descriptor/StyleDescriptor.ts +0 -213
  111. package/src/interaction/InteractionDescriptor.ts +0 -96
  112. package/src/interaction/InteractionExecutor.ts +0 -84
  113. package/src/interaction/Scriptable.ts +0 -44
  114. package/src/interaction/index.ts +0 -3
  115. package/src/interpreter/DatasetInterpreter.ts +0 -11
  116. package/src/interpreter/DynamicInterpreter.ts +0 -207
  117. package/src/interpreter/PrefaInterpreter.ts +0 -11
  118. package/src/interpreter/PrefabInstanceInterpreter.ts +0 -12
  119. package/src/interpreter/ServiceSchedulerInterpreter.ts +0 -42
  120. package/src/interpreter/StyleInterpreter.ts +0 -66
  121. package/src/preset/execute/dataset/index.ts +0 -1
  122. package/src/preset/execute/dataset/update.ts +0 -51
  123. package/src/preset/execute/graphics/index.ts +0 -1
  124. package/src/preset/execute/graphics/invoke.ts +0 -48
  125. package/src/preset/execute/index.ts +0 -4
  126. package/src/preset/execute/net/http.ts +0 -52
  127. package/src/preset/execute/net/index.ts +0 -1
  128. package/src/preset/execute/scheduler/index.ts +0 -1
  129. package/src/preset/execute/scheduler/switch.ts +0 -46
  130. package/src/preset/index.ts +0 -7
  131. package/src/preset/interaction/graphics/index.ts +0 -1
  132. package/src/preset/interaction/graphics/invoke.ts +0 -27
  133. package/src/preset/interaction/index.ts +0 -2
  134. package/src/preset/interaction/scheduler/index.ts +0 -1
  135. package/src/preset/interaction/scheduler/switch.ts +0 -27
  136. package/src/setup/index.ts +0 -17
  137. package/src/utils/compile.ts +0 -50
@@ -1,32 +0,0 @@
1
- import { Euler, Vector3 } from "../math"
2
- import { DeserializationObject } from "../Serialization"
3
- import { Descriptor } from "./Descriptor"
4
-
5
- class NodeDescriptor extends Descriptor {
6
- static type = 'Node'
7
- readonly scale: Vector3
8
- readonly position: Vector3
9
- readonly rotation: Euler
10
- layer = 0
11
- visible = true
12
- renderOrder = 0
13
- castShadow = false
14
- receiveShadow = false
15
-
16
- constructor(descriptor: Partial<DeserializationObject<NodeDescriptor>> = {}) {
17
- super(descriptor)
18
- this.scale = descriptor.scale ? Vector3.create(descriptor.scale) : Vector3.create(1, 1, 1)
19
- this.position = Vector3.create(descriptor.position)
20
- this.rotation = Euler.create(descriptor.rotation)
21
- this.layer = descriptor.layer ?? 0
22
- this.visible = descriptor.visible ?? true
23
- this.renderOrder = descriptor.renderOrder ?? 0
24
- this.castShadow = descriptor.castShadow ?? false
25
- this.receiveShadow = descriptor.receiveShadow ?? false
26
- }
27
- }
28
- Descriptor.register(NodeDescriptor)
29
-
30
- export {
31
- NodeDescriptor
32
- }
@@ -1,53 +0,0 @@
1
- import { DeserializationObject } from "../Serialization"
2
- import { Descriptor } from "./Descriptor"
3
-
4
- class PrefabeDescriptor extends Descriptor {
5
- static type = 'Prefab'
6
-
7
- static add(parent: Descriptor, sub: Descriptor) {
8
- parent.add(sub)
9
-
10
- const instanceCollection = this.getInstanceCollection(parent.uuid)
11
- if (instanceCollection === null) return
12
- for (const instance of instanceCollection) {
13
- const instanceDescriptor = this.get<Descriptor>(instance)
14
- instanceDescriptor.add(sub.clone(true))
15
- }
16
- }
17
-
18
- static remove(parent: Descriptor, sub: Descriptor) {
19
- parent.remove(sub)
20
-
21
- const instanceCollection = this.getInstanceCollection(sub.uuid)
22
- if (instanceCollection === null) return
23
- for (const instance of instanceCollection) {
24
- const instanceDescriptor = this.get<Descriptor>(instance)
25
- const ancestor = instanceDescriptor.traceAncestor(false)
26
- if (ancestor.parent === null) continue
27
- ancestor.parent.remove(instanceDescriptor)
28
- }
29
- }
30
-
31
- static update(descriptor: Descriptor) {
32
- descriptor.setNeedsUpdate()
33
-
34
- const instanceCollection = this.getInstanceCollection(descriptor.uuid)
35
- if (instanceCollection === null) return
36
- for (const instance of instanceCollection) {
37
- const instanceDescriptor = this.get<Descriptor>(instance)
38
- instanceDescriptor.syncWith(descriptor)
39
- instanceDescriptor.setNeedsUpdate()
40
- }
41
- }
42
-
43
- category: 'html' | 'scene'
44
-
45
- constructor(descriptor: Partial<DeserializationObject<PrefabeDescriptor>> = {}) {
46
- super(descriptor)
47
- this.category = descriptor.category ?? 'html'
48
- }
49
- }
50
-
51
- Descriptor.register(PrefabeDescriptor)
52
-
53
- export { PrefabeDescriptor }
@@ -1,32 +0,0 @@
1
- import { DeserializationObject } from "../Serialization"
2
- import { Descriptor } from "./Descriptor"
3
- import { PrefabeDescriptor } from "./PrefabDescriptor"
4
-
5
- class PrefabInstanceDescriptor extends Descriptor {
6
- static type = 'PrefabInstance'
7
-
8
- static fromPrefab(url: string, descriptor: PrefabeDescriptor) {
9
- const clone = Descriptor.cloneAndFixReferences(descriptor)
10
-
11
- const prefabInstance = new PrefabInstanceDescriptor({
12
- name: clone.name ? `${clone.name}-prefab` : 'prefab',
13
- interactions: clone.interactions,
14
- prefab: clone.prefab,
15
- url,
16
- })
17
- prefabInstance.add(...clone.children)
18
-
19
- return prefabInstance
20
- }
21
-
22
- readonly url: string
23
-
24
- constructor(descriptor: Partial<DeserializationObject<PrefabInstanceDescriptor>> = {}) {
25
- super(descriptor)
26
- this.url = descriptor.url ?? ''
27
- }
28
- }
29
-
30
- Descriptor.register(PrefabInstanceDescriptor)
31
-
32
- export { PrefabInstanceDescriptor }
@@ -1,32 +0,0 @@
1
- import { DeserializationObject } from "../Serialization"
2
- import { DefaultStyleColor, StyleColor } from "../Color"
3
- import { Descriptor } from "./Descriptor"
4
- import { HTMLDescriptor } from "./HTMLDescriptor"
5
-
6
- abstract class ServiceDescriptor extends HTMLDescriptor {
7
- static type = 'Service'
8
-
9
- backgroundColor: StyleColor
10
-
11
- /**
12
- * 获取当前描述器节点的父服务节点
13
- *
14
- * 注意:若你需要在渲染流程中获取父服务节点,你应该调用`Graphics`的同名方法,因为一些动态节点可能并未直接被插入到节点树中。
15
- */
16
- static traceService(descriptor: Descriptor | null): ServiceDescriptor | null {
17
- if (descriptor === null) return null
18
- if (descriptor instanceof ServiceDescriptor) {
19
- return descriptor instanceof ServiceDescriptor ? descriptor : null
20
- } else {
21
- const ancestor = descriptor.traceAncestor(false)
22
- return this.traceService(ancestor.parent)
23
- }
24
- }
25
-
26
- constructor(descriptor: Partial<DeserializationObject<ServiceDescriptor>> = {}) {
27
- super(descriptor)
28
- this.backgroundColor = descriptor.backgroundColor ? DefaultStyleColor.create(descriptor.backgroundColor) : new DefaultStyleColor({ opacity: 0 })
29
- }
30
- }
31
-
32
- export { ServiceDescriptor }
@@ -1,32 +0,0 @@
1
- import { Vector2, Vector3 } from "../math";
2
- import { DeserializationObject } from "../Serialization"
3
- import { Descriptor } from "./Descriptor"
4
- import { ServiceDescriptor } from "./ServiceDescriptor";
5
-
6
- interface RayIntersection {
7
- object: Descriptor
8
- distance: number
9
- point: Vector3
10
- normal: Vector2 | null
11
- uv: Vector2 | null
12
- uv1: Vector2 | null
13
- faceIndex: number | null
14
- instanceId: number | null
15
- }
16
-
17
- class ServiceSchedulerDescriptor extends Descriptor {
18
- static type = 'ServiceScheduler'
19
- /**调度器的默认调度脚本 */
20
- declare children: ServiceDescriptor[]
21
- /**调度器渲染场景时使用的相机 */
22
- camera: string
23
-
24
- constructor(descriptor: Partial<DeserializationObject> = {}) {
25
- super(descriptor)
26
- this.camera = descriptor.camera ?? ''
27
- }
28
- }
29
-
30
- Descriptor.register(ServiceSchedulerDescriptor)
31
-
32
- export { RayIntersection, ServiceSchedulerDescriptor }
@@ -1,213 +0,0 @@
1
- import { DeserializationObject } from "../Serialization"
2
- import { BasicStyleColor, DefaultStyleColor, StyleColor } from "../Color"
3
- import { Descriptor } from "./Descriptor"
4
-
5
- class StyleShadow {
6
- /**阴影类型
7
- * - 0 box-shadow
8
- * - 1 text-shadow
9
- */
10
- type: number
11
- /** 阴影颜色 */
12
- color: DefaultStyleColor
13
- /** 阴影是否内嵌 */
14
- inset: boolean
15
- /** 阴影水平偏移 */
16
- offsetX: number
17
- /** 阴影垂直偏移 */
18
- offsetY: number
19
- /** 阴影模糊半径 */
20
- blur: number
21
- /** 阴影扩展半径 */
22
- spread: number
23
-
24
- constructor(shadow: Partial<DeserializationObject<StyleShadow>> = {}) {
25
- this.type = shadow.type ?? 0
26
- this.color = new DefaultStyleColor(shadow.color)
27
- this.inset = shadow.inset ?? false
28
- this.offsetX = shadow.offsetX ?? 0
29
- this.offsetY = shadow.offsetY ?? 0
30
- this.blur = shadow.blur ?? 0
31
- this.spread = shadow.spread ?? 0
32
- }
33
-
34
- getStyleValue(unit = 'px') {
35
- if (this.type === 0) {
36
- const inset = this.inset ? 'inset' : ''
37
- return `${inset} ${this.offsetX}${unit} ${this.offsetY}${unit} ${this.blur}${unit} ${this.spread}${unit} ${this.color.getColor()}`
38
- } else {
39
- return ` ${this.offsetX}${unit} ${this.offsetY}${unit} ${this.spread}${unit} ${this.color.getColor()}`
40
- }
41
- }
42
- }
43
-
44
- class StyleSpacing {
45
- left: number
46
- top: number
47
- right: number
48
- bottom: number
49
-
50
- constructor(border: Partial<DeserializationObject<StyleSpacing>> = {}) {
51
- this.left = border.left ?? 0
52
- this.top = border.top ?? 0
53
- this.right = border.right ?? 0
54
- this.bottom = border.bottom ?? 0
55
- }
56
-
57
- getStyleValue(unit = 'px') {
58
- return `${this.top}${unit} ${this.right}${unit} ${this.bottom}${unit} ${this.left}${unit}`
59
- }
60
- }
61
-
62
- type StyleRuleType = StyleColor | StyleShadow | StyleSpacing | number | string
63
-
64
- class StyleRule<T extends StyleRuleType = StyleRuleType> {
65
- static clone(rule: DeserializationObject<StyleRule>) {
66
- let value: StyleRuleType
67
- if (['box-shadow', 'text-shadow'].includes(rule.name)) {
68
- //@ts-ignore
69
- value = new StyleShadow(rule.value)
70
- } else if (['background', 'border-color', 'color'].includes(rule.name)) {
71
- //@ts-ignore
72
- value = BasicStyleColor.create(rule.value)
73
- } else if (['border-width', 'padding', 'margin'].includes(rule.name)) {
74
- //@ts-ignore
75
- value = new StyleSpacing(rule.value)
76
- } else {
77
- value = rule.value
78
- }
79
- return new StyleRule(rule.name, value, rule.inherit, rule.unit)
80
- }
81
-
82
- name: string
83
- value: T
84
- inherit: boolean
85
- unit: string
86
-
87
- constructor(name: string, value: T, inherit: boolean = false, unit = '') {
88
- this.name = name
89
- this.value = value
90
- this.inherit = inherit
91
- this.unit = unit
92
- }
93
-
94
- getValue() {
95
- if (this.value instanceof BasicStyleColor) {
96
- return this.value.getColor()
97
- } else if (this.value instanceof StyleShadow) {
98
- return this.value.getStyleValue(this.unit)
99
- } else if (this.value instanceof StyleSpacing) {
100
- return this.value.getStyleValue(this.unit)
101
- } else if (this.name === 'transition') {
102
- return `all ${this.value}s`
103
- } else {
104
- return this.value + this.unit
105
- }
106
- }
107
- }
108
-
109
- let _basicStyle: StyleDescriptor | null = null
110
- class StyleDescriptor extends Descriptor {
111
- static type = 'Style'
112
- static basicName = 'vyr_basic_style'
113
- static activeName = 'vyr_active_style'
114
- static get basicStyle() {
115
- if (_basicStyle === null) {
116
- _basicStyle = new StyleDescriptor({ name: 'basic', rules: [] })
117
- const rules = StyleDescriptor.generateRule(false)
118
- for (const rule of rules) _basicStyle.addRule(rule)
119
- }
120
- return _basicStyle
121
- }
122
- static generateRule = (inherit = true) => {
123
- const rules: StyleRule[] = []
124
- rules.push(new StyleRule('cursor', 'default', inherit))
125
- rules.push(new StyleRule('padding', new StyleSpacing(), inherit, 'px'))
126
- rules.push(new StyleRule('box-sizing', 'border-box', inherit))
127
- rules.push(new StyleRule('overflow-x', 'visible', inherit))
128
- rules.push(new StyleRule('overflow-y', 'visible', inherit))
129
- rules.push(new StyleRule('vertical-align', 'baseline', inherit))
130
-
131
- rules.push(new StyleRule('transition', 0.5, inherit))
132
-
133
- rules.push(new StyleRule('background', new DefaultStyleColor({ opacity: 0 }), inherit))
134
- rules.push(new StyleRule('background-blend-mode', 'normal', inherit))
135
- rules.push(new StyleRule('background-clip', 'border-box', inherit))
136
-
137
- rules.push(new StyleRule('box-shadow', new StyleShadow({ type: 0 }), inherit, 'px'))
138
- rules.push(new StyleRule('text-shadow', new StyleShadow({ type: 1 }), inherit, 'px'))
139
-
140
- rules.push(new StyleRule('border-style', 'solid', inherit))
141
- rules.push(new StyleRule('border-color', new DefaultStyleColor(), inherit))
142
- rules.push(new StyleRule('border-width', new StyleSpacing(), inherit, 'px'))
143
-
144
- rules.push(new StyleRule('color', new DefaultStyleColor({ value: '#000000' }), inherit))
145
- rules.push(new StyleRule('font-size', 16, inherit, 'px'))
146
- rules.push(new StyleRule('text-align', 'left', inherit))
147
- rules.push(new StyleRule('font-weight', 'normal', inherit))
148
- rules.push(new StyleRule('line-height', 1.2, inherit))
149
- rules.push(new StyleRule('font-style', 'normal', inherit))
150
- rules.push(new StyleRule('text-decoration', 'none', inherit))
151
- rules.push(new StyleRule('letter-spacing', 0, inherit, 'px'))
152
- rules.push(new StyleRule('white-space', 'normal', inherit))
153
- rules.push(new StyleRule('font-family', 'unset', inherit))
154
-
155
- return rules
156
- }
157
-
158
- /**样式继承 */
159
- inherit: string
160
- /**样式规则 */
161
- readonly rules: StyleRule[]
162
- readonly hoverRules: StyleRule[]
163
- readonly activeRules: StyleRule[]
164
-
165
- constructor(descriptor: Partial<DeserializationObject<StyleDescriptor>> = {}) {
166
- super(descriptor)
167
- this.inherit = descriptor.inherit ?? ''
168
-
169
- if (Array.isArray(descriptor.rules)) {
170
- this.rules = []
171
- for (const rule of descriptor.rules) this.addRule(StyleRule.clone(rule), this.rules)
172
- } else {
173
- this.rules = StyleDescriptor.generateRule(true)
174
- }
175
-
176
- if (Array.isArray(descriptor.hoverRules)) {
177
- this.hoverRules = []
178
- for (const rule of descriptor.hoverRules) this.addRule(StyleRule.clone(rule), this.hoverRules)
179
- } else {
180
- this.hoverRules = StyleDescriptor.generateRule(true)
181
- }
182
-
183
- if (Array.isArray(descriptor.activeRules)) {
184
- this.activeRules = []
185
- for (const rule of descriptor.activeRules) this.addRule(StyleRule.clone(rule), this.activeRules)
186
- } else {
187
- this.activeRules = StyleDescriptor.generateRule(true)
188
- }
189
- }
190
-
191
- addRule(rule: StyleRule, rules = this.rules) {
192
- for (const item of rules) {
193
- if (rule.name === item.name) return
194
- }
195
- rules.push(rule)
196
- }
197
-
198
- removeRule(rule: StyleRule, rules = this.rules) {
199
- for (let i = 0; i < rules.length; i++) {
200
- const item = this.rules[i]
201
- if (rule.name !== item.name) continue
202
- rules.splice(i, 1)
203
- return
204
- }
205
- }
206
-
207
- clearRule(rules = this.rules) {
208
- rules.length = 0
209
- }
210
- }
211
- Descriptor.register(StyleDescriptor)
212
-
213
- export { StyleShadow, StyleSpacing, StyleRuleType, StyleRule, StyleDescriptor }
@@ -1,96 +0,0 @@
1
- import { DeserializationObject, Serialization } from "../Serialization";
2
- import { Descriptor } from "../descriptor";
3
- import { AssetProperty, AssetPropertyCollection } from "../asset";
4
- import { Graphics } from "../graphics";
5
- import { InteractionExecutor } from "./InteractionExecutor";
6
-
7
- interface InteractionInput {
8
- [param: string]: {
9
- type?: 'data' | string
10
- value: any
11
- }
12
- }
13
-
14
- interface InteractionInputCollection {
15
- [id: string]: InteractionInput
16
- }
17
-
18
- interface InteractionProperty extends AssetProperty {
19
- uuid: string
20
- type: string
21
- inputs: InteractionInputCollection
22
- }
23
-
24
- interface InteractionPropertyCollection extends AssetPropertyCollection<InteractionProperty> { }
25
-
26
- interface InteractionNode {
27
- id: string
28
- label: string
29
- position: { x: number; y: number }
30
- interaction: string
31
- url: string
32
- input: InteractionInput
33
- }
34
-
35
- interface InteractionMapper {
36
- source: string
37
- target: string
38
- }
39
-
40
- const privateState = {
41
- executorPool: new WeakMap<Descriptor, InteractionExecutor>()
42
- }
43
-
44
- class InteractionDescriptor extends Descriptor {
45
- static type = 'Interaction'
46
- roots: string[]
47
- nodes: InteractionNode[]
48
- mappers: InteractionMapper[]
49
-
50
- constructor(descriptor: Partial<DeserializationObject<InteractionDescriptor>> = {}) {
51
- super(descriptor)
52
- this.roots = descriptor.roots ? Serialization.deepClone(descriptor.roots) : []
53
- this.nodes = descriptor.nodes ? Serialization.deepClone(descriptor.nodes) : []
54
- this.mappers = descriptor.mappers ? Serialization.deepClone(descriptor.mappers) : []
55
- }
56
-
57
- addNode(nodes: InteractionNode[]) {
58
- const count = nodes.length
59
- const max = count - 1
60
- for (let i = 0; i < count; i++) {
61
- const node = nodes[i]
62
- this.nodes.push(node)
63
- if (i < max) {
64
- this.mappers.push({ source: node.id, target: nodes[i + 1].id })
65
- }
66
- }
67
- }
68
-
69
- async execute(customInputs: InteractionInputCollection, graphics: Graphics, trigger: Descriptor, otherArgs?: { [k: string]: any }) {
70
- let executor = privateState.executorPool.get(this)
71
- if (executor === undefined) {
72
- executor = new InteractionExecutor()
73
- executor.update(this)
74
- privateState.executorPool.set(this, executor)
75
- }
76
- await executor.execute(customInputs, this, graphics, trigger,otherArgs)
77
- }
78
-
79
- setNeedsUpdate() {
80
- const executor = privateState.executorPool.get(this)
81
- if (executor === undefined) return
82
- executor.update(this)
83
- }
84
- }
85
-
86
- Descriptor.register(InteractionDescriptor)
87
-
88
- export {
89
- InteractionInput,
90
- InteractionInputCollection,
91
- InteractionProperty,
92
- InteractionPropertyCollection,
93
- InteractionNode,
94
- InteractionMapper,
95
- InteractionDescriptor,
96
- }
@@ -1,84 +0,0 @@
1
- import { Asset } from "../asset"
2
- import { Scriptable } from "./Scriptable"
3
- import { InteractionDescriptor, InteractionInputCollection, InteractionNode } from "./InteractionDescriptor"
4
- import { Graphics } from "../graphics/Graphics"
5
-
6
- type ExecuteResult = { [k: string]: any } | null
7
-
8
- class InteractionExecutor {
9
- readonly collection = new Map<string, InteractionNode>()
10
- readonly linkCollection = new Map<string, string[]>()
11
-
12
- update(interaction: InteractionDescriptor) {
13
- this.collection.clear()
14
- this.linkCollection.clear()
15
-
16
- for (const node of interaction.nodes) {
17
- this.collection.set(node.id, node)
18
- }
19
-
20
- for (const mapper of interaction.mappers) {
21
- let link = this.linkCollection.get(mapper.source)
22
- if (link === undefined) {
23
- link = []
24
- this.linkCollection.set(mapper.source, link)
25
- }
26
- link.push(mapper.target)
27
- }
28
- }
29
-
30
- async doBranch(branch: string, previous: string, customInputs: InteractionInputCollection, interaction: InteractionDescriptor, graphics: Graphics, result: Map<string, ExecuteResult>, trigger: any, otherArgs?: any) {
31
- await this.doExecute(branch, previous, customInputs, interaction, graphics, result, trigger, otherArgs)
32
- }
33
-
34
- async doCondition(condition: string, previous: string, customInputs: InteractionInputCollection, interaction: InteractionDescriptor, graphics: Graphics, result: Map<string, ExecuteResult>, trigger: any, otherArgs?: any) {
35
- const preResult = result.get(previous)
36
- const conditionResult = result.get(condition)
37
- if (preResult !== conditionResult?.condition.value) return
38
-
39
- await this.doExecute(condition, previous, customInputs, interaction, graphics, result, trigger, otherArgs)
40
- }
41
-
42
- async doExecute(execute: string, previous: string, customInputs: InteractionInputCollection, interaction: InteractionDescriptor, graphics: Graphics, result: Map<string, ExecuteResult>, trigger: any, otherArgs?: any) {
43
- const nexts = this.linkCollection.get(execute)
44
- if (nexts === undefined) return
45
-
46
- for (const next of nexts) {
47
- await this.doNext(next, execute, customInputs, interaction, graphics, result, trigger, otherArgs)
48
- }
49
- }
50
-
51
- async doNext(current: string, previous: string, customInputs: InteractionInputCollection, interaction: InteractionDescriptor, graphics: Graphics, result: Map<string, ExecuteResult>, trigger: any, otherArgs?: any) {
52
- const active = this.collection.get(current)
53
- if (active === undefined) return
54
-
55
- const currentInput = customInputs[current] ?? {}
56
- const script = Asset.get<Scriptable>(active.url)
57
- if (script) {
58
- const args = { input: { previous, default: active.input, user: currentInput }, result, trigger }
59
- if (otherArgs) Object.assign(args, otherArgs)
60
- const executeResult = await script.execute(graphics, args)
61
- result.set(current, executeResult)
62
- }
63
-
64
- if (active.interaction === 'Branch') {
65
- await this.doBranch(current, previous, customInputs, interaction, graphics, result, trigger, otherArgs)
66
- } else if (active.interaction === 'Condition') {
67
- if (result.has(current) === false) result.set(current, active.input)
68
- await this.doCondition(current, previous, customInputs, interaction, graphics, result, trigger, otherArgs)
69
- } else {
70
- await this.doExecute(current, previous, customInputs, interaction, graphics, result, trigger, otherArgs)
71
- }
72
- }
73
-
74
- async execute(customInputs: InteractionInputCollection, interaction: InteractionDescriptor, graphics: Graphics, trigger: any, otherArgs?: any) {
75
- for (const root of interaction.roots) {
76
- const resultCollection = new Map()
77
- await this.doNext(root, '', customInputs, interaction, graphics, resultCollection, trigger, otherArgs)
78
- }
79
- }
80
- }
81
-
82
- export {
83
- InteractionExecutor
84
- }
@@ -1,44 +0,0 @@
1
- import { Descriptor } from "../descriptor"
2
- import { Graphics } from "../graphics/Graphics"
3
- import { InteractionInput } from "./InteractionDescriptor";
4
-
5
- interface ScriptableInput<T = InteractionInput> {
6
- previous: string;
7
- default: T,
8
- user: T
9
- }
10
-
11
- interface ScriptableArgs<T = InteractionInput, R = Map<string, any>, I = ScriptableInput<T>> {
12
- input: I,
13
- result: R,
14
- trigger: Descriptor
15
- }
16
-
17
- /**
18
- * 可编程脚本(脚本不会在服务端实例化)
19
- */
20
- class Scriptable<T extends ScriptableArgs<any, any, any> = ScriptableArgs> {
21
- readonly uuid
22
- /**脚本文件的url路径 */
23
- get url() {
24
- return this.uuid
25
- }
26
-
27
- constructor(url: string) {
28
- this.uuid = url
29
- }
30
-
31
- getInputValue(key: string, input: ScriptableInput) {
32
- return input?.user?.[key]?.value ?? input.default[key].value
33
- }
34
-
35
- /**脚本的准备事件,该方法在脚本被实例化后立即执行
36
- *
37
- * 一般在该事件中进行脚本的准备工作,如从服务端获取资源或数据
38
- */
39
- async ready() { }
40
-
41
- execute(graphics: Graphics, args: T): any { }
42
- }
43
-
44
- export { ScriptableArgs, Scriptable }
@@ -1,3 +0,0 @@
1
- export * from './InteractionDescriptor'
2
- export * from './InteractionExecutor'
3
- export * from './Scriptable'
@@ -1,11 +0,0 @@
1
- import { DatasetDescriptor } from "../descriptor";
2
- import { Interpreter } from "./Interpreter";
3
-
4
- class DatasetInterpreter extends Interpreter {
5
- static type = DatasetDescriptor.type
6
- }
7
- Interpreter.register(DatasetInterpreter)
8
-
9
- export {
10
- DatasetInterpreter
11
- }