@vyr/engine 0.0.33 → 0.0.34

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
@@ -4,41 +4,35 @@ import { ArrayUtils } from "../ArrayUtils"
4
4
  import { ObjectPool } from "../ObjectPool";
5
5
  import { Descriptor } from "../descriptor";
6
6
 
7
- interface Edge {
7
+ export interface Edge {
8
8
  key: string
9
9
  asset: string
10
10
  category: string
11
11
  }
12
12
 
13
- interface Port {
13
+ export interface Port {
14
14
  descriptor: string
15
15
  edges: Edge[]
16
16
  }
17
17
 
18
- interface Cell {
18
+ export type Provider<T extends Descriptor = Descriptor> = (descriptor: T, port: Port) => void
19
+
20
+ export interface Cell {
19
21
  url: string
20
22
  uuid: string
21
23
  ports: Port[]
22
24
  }
23
25
 
24
- interface UrlCell {
26
+ export interface UrlCell {
25
27
  url: string
26
28
  cells: Cell[]
27
29
  }
28
30
 
29
- type Provider<T extends Descriptor = Descriptor> = (descriptor: T, port: Port) => void
30
-
31
- const privateState = {
32
- providerCollection: new Map<string, Provider>(),
33
- }
34
-
35
- class Dependencide {
31
+ export class Dependencide {
36
32
  all: string[] = []
37
33
  descriptor: string[] = []
34
+ store: string[] = []
38
35
  interaction: string[] = []
39
- prefab: string[] = []
40
- dataset: string[] = []
41
- style: string[] = []
42
36
  texture: string[] = []
43
37
  material: string[] = []
44
38
  geometry: string[] = []
@@ -46,14 +40,16 @@ class Dependencide {
46
40
  add(type: keyof Dependencide, url: string) {
47
41
  const collection = this[type] as string[]
48
42
  if (collection === undefined) return console.warn(type, url)
43
+ if(Array.isArray(url)){
44
+ console.log(url)
45
+ }
49
46
  collection.push(url)
50
47
  this.descriptor.push(url)
51
48
  }
52
49
 
53
50
  forEachForGraphics(cb: (url: string) => void) {
54
- //Graphics遍历资产时,跳过 interaction prefab
55
- for (const url of this.dataset) cb(url)
56
- for (const url of this.style) cb(url)
51
+ //Graphics遍历资产时,跳过 interaction
52
+ for (const url of this.store) cb(url)
57
53
  for (const url of this.texture) cb(url)
58
54
  for (const url of this.material) cb(url)
59
55
  for (const url of this.geometry) cb(url)
@@ -62,9 +58,7 @@ class Dependencide {
62
58
  clear() {
63
59
  this.all.length = 0
64
60
  this.descriptor.length = 0
65
- this.prefab.length = 0
66
- this.dataset.length = 0
67
- this.style.length = 0
61
+ this.store.length = 0
68
62
  this.interaction.length = 0
69
63
  this.texture.length = 0
70
64
  this.material.length = 0
@@ -73,12 +67,13 @@ class Dependencide {
73
67
  }
74
68
 
75
69
  /**资产关系图,表示资产和资产之间的依赖关系 */
76
- class AssetGraph {
70
+ export class AssetGraph {
71
+ private static collection = new Map<string, Provider>()
77
72
 
78
- private static build(descriptor: Descriptor, port: Port) {
73
+ static build(descriptor: Descriptor, port: Port) {
79
74
  let Prototype = descriptor.constructor as typeof Descriptor
80
75
  while (Prototype.prototype instanceof ObjectPool) {
81
- const provider = privateState.providerCollection.get(Prototype.type)
76
+ const provider = this.collection.get(Prototype.type)
82
77
  Prototype = Object.getPrototypeOf(Prototype)
83
78
  if (provider === undefined) continue
84
79
  provider(descriptor, port)
@@ -86,7 +81,7 @@ class AssetGraph {
86
81
  }
87
82
 
88
83
  static register<T extends Descriptor = Descriptor>(type: string, provider: Provider<T>) {
89
- privateState.providerCollection.set(type, provider as Provider)
84
+ this.collection.set(type, provider as Provider)
90
85
  }
91
86
 
92
87
  static get(descriptor: Descriptor, ports: Port[] = []) {
@@ -191,7 +186,7 @@ class AssetGraph {
191
186
  if (ports.length === 0) return dependencide
192
187
  for (const port of ports) {
193
188
  for (const edge of port.edges) {
194
- const isDescriptor = Category.isDescriptor(edge.category, 'category')
189
+ const isDescriptor = Category.isDescriptor(edge.category)
195
190
  this.collection(edge.asset, dependencide, isDescriptor, edge.category as keyof Dependencide)
196
191
  }
197
192
  }
@@ -241,6 +236,4 @@ class AssetGraph {
241
236
 
242
237
  return false
243
238
  }
244
- }
245
-
246
- export { Edge, Port, Cell, UrlCell, Dependencide, AssetGraph }
239
+ }
@@ -0,0 +1,81 @@
1
+ import { AnimationFrameData, parseFrameData, Vector2FrameDataSchema } from "../schema"
2
+ import { DeserializationObject } from "../Serialization"
3
+ import { Descriptor } from "./Descriptor"
4
+
5
+ /**
6
+ * 动画单元类
7
+ * 用于定义和控制单个动画的参数和行为
8
+ *
9
+ * 作用:表示一个独立的动画片段,包含动画的播放状态、持续时间、循环设置、目标属性路径和关键帧数据
10
+ * 可用于控制场景中对象的各种属性动画,如位置、旋转、缩放、颜色等
11
+ */
12
+ export class AnimationUnit extends Descriptor {
13
+ /**类类型标识 */
14
+ static type = 'AnimationUnit'
15
+
16
+ /**
17
+ * 是否播放动画
18
+ * 控制动画的启动和暂停状态
19
+ * @default false
20
+ */
21
+ play: boolean
22
+
23
+ /**
24
+ * 动画持续时间(秒)
25
+ * 定义动画从开始到结束的总时长
26
+ * @default 0
27
+ */
28
+ duration: number
29
+
30
+ /**
31
+ * 循环次数
32
+ * 定义动画重复播放的次数,Infinity表示无限循环
33
+ * @default Infinity
34
+ */
35
+ count: number
36
+
37
+ /**
38
+ * 循环模式
39
+ * - 1 顺序循环:动画播放完成后从头开始
40
+ * - 2 往返循环:动画播放完成后反向播放
41
+ * @default 1
42
+ */
43
+ mode: number
44
+
45
+ /**
46
+ * 动画开始时间(秒)
47
+ * 定义动画在何时开始播放
48
+ * @default 0
49
+ */
50
+ startTime: number
51
+
52
+ /**
53
+ * 动画目标属性路径
54
+ * 指向要进行动画的对象属性路径
55
+ * @default ""
56
+ */
57
+ propertyPath: string
58
+
59
+ /**
60
+ * 动画帧数据
61
+ * 包含动画的关键帧信息,定义了动画的起始值和结束值
62
+ */
63
+ frameData: AnimationFrameData
64
+
65
+ constructor(descriptor: Partial<DeserializationObject<AnimationUnit>> = {}) {
66
+ super(descriptor)
67
+ this.play = descriptor.play ?? false
68
+ this.duration = descriptor.duration ?? 0
69
+ this.count = descriptor.count ?? Infinity
70
+ this.mode = descriptor.mode ?? 1
71
+ this.startTime = descriptor.startTime ?? 0
72
+ this.propertyPath = descriptor.propertyPath ?? ''
73
+ this.frameData = descriptor.frameData ? parseFrameData(descriptor.frameData) : Vector2FrameDataSchema.parse({})
74
+ }
75
+ }
76
+
77
+ Descriptor.register(AnimationUnit)
78
+
79
+ export type {
80
+ AnimationFrameData
81
+ }
@@ -0,0 +1,12 @@
1
+ import { Node } from "./Node"
2
+
3
+ /**
4
+ * 相机接口
5
+ * 继承自Node,用于定义场景中的相机对象
6
+ *
7
+ * 作用:相机负责定义场景的观察视角和投影方式
8
+ * 提供场景渲染时的视角控制
9
+ */
10
+ export interface Camera extends Node {
11
+
12
+ }
@@ -0,0 +1,6 @@
1
+ import { Descriptor } from "./Descriptor"
2
+
3
+ export interface Controller extends Descriptor {
4
+
5
+ enabled: boolean
6
+ }
@@ -1,76 +1,25 @@
1
1
  import { language } from '../locale'
2
+ import { DescriptorAdjacency, DescriptorAncestor, DescriptorVariables, DescriptorVariablesSchema, InteractionPropertyArray, InteractionPropertyArraySchame } from '../schema'
2
3
  import { listenInstance, ObjectPool } from '../ObjectPool'
3
- import { ArrayUtils } from '../ArrayUtils'
4
4
  import { Generate } from '../Generate'
5
5
  import { Traverser } from '../Traverser'
6
6
  import { ObjectUtils } from '../ObjectUtils'
7
7
  import { DeserializationObject, Serialization, SerializationObject } from '../Serialization'
8
- import { InteractionPropertyCollection } from '../interaction'
9
8
  import { observer } from '../graphics/Observer'
10
9
 
11
- /**描述器祖先节点 */
12
- interface DescriptorAncestor<T extends Descriptor = Descriptor> {
13
- parent: T | null,
14
- uuids: string[]
15
- collection: Descriptor[]
16
- }
17
-
18
- /**当前描述器节点的邻接关系 */
19
- interface DescriptorAdjacency {
20
- /**当前节点的父节点 */
21
- parent: string
22
- /**当前节点的前一个节点 */
23
- prev: string
24
- /**当前节点的后一个节点 */
25
- next: string
26
- }
27
-
28
- interface DescriptorPrefab {
29
- root: string
30
- uuid: string
31
- }
32
-
33
- interface VariabConfig {
34
- type: 'key' | 'custom'
35
- enabled: boolean
36
- value: string
37
- }
38
-
39
- interface DescriptorVariables {
40
- [k: string]: VariabConfig
41
- }
42
-
43
10
  const privateState = {
44
11
  classCollection: new Map<string, typeof Descriptor>(),
45
- prefabToInstanceCollection: new Map<string, string[]>(),
46
- instanceToPrefabCollection: new Map<string, string>(),
47
12
  refGraph: new Map<string, string>(),
48
13
  getParent: <T extends Descriptor = Descriptor>(uuid: string) => {
49
14
  const parent = privateState.refGraph.get(uuid)
50
15
  if (parent === undefined) return null
51
16
  return Descriptor.get<T>(parent)
52
17
  },
53
- setPrefabInstance(instance: Descriptor) {
54
- if (instance.prefab === false) return
55
-
56
- let instanceCollection = this.prefabToInstanceCollection.get(instance.prefab.uuid)
57
- if (instanceCollection === undefined) {
58
- instanceCollection = []
59
- this.prefabToInstanceCollection.set(instance.prefab.uuid, instanceCollection)
60
- }
61
-
62
- ArrayUtils.insert(instanceCollection, instance.uuid)
63
- this.instanceToPrefabCollection.set(instance.uuid, instance.prefab.uuid)
64
- },
65
18
  markclone: (descriptor: Descriptor, root: string, map: Map<string, Descriptor>, childs: Descriptor[]) => {
66
19
  const clone = descriptor.clone(false)
67
20
  map.set(descriptor.uuid, clone)
68
21
  if (clone.interactions.length > 0) childs.push(clone)
69
22
 
70
- if (clone.prefab === false) {
71
- clone.prefab = { root, uuid: descriptor.uuid }
72
- }
73
-
74
23
  for (const sub of descriptor.children) {
75
24
  const child = privateState.markclone(sub, root, map, childs)
76
25
  clone.add(child)
@@ -80,7 +29,6 @@ const privateState = {
80
29
  }
81
30
  }
82
31
 
83
-
84
32
  /**
85
33
  * 描述引擎所使用的场景、资产对象
86
34
  *
@@ -90,44 +38,72 @@ const privateState = {
90
38
  *
91
39
  * 不建议将一个描述器作为另一个描述器的属性,若要保持引用关系,将描述器的uuid作为属性即可
92
40
  */
93
- class Descriptor extends ObjectPool {
41
+ export class Descriptor extends ObjectPool {
42
+ /**类类型标识 */
94
43
  static type = 'Descriptor'
95
- /**注册描述器的派生类 */
44
+ /**
45
+ * 注册描述器的派生类
46
+ * @param DerivedClass 派生类
47
+ */
96
48
  static register(DerivedClass: typeof Descriptor) {
97
49
  privateState.classCollection.set(DerivedClass.type, DerivedClass)
98
50
  }
99
- /**获取注册的派生类 */
51
+ /**
52
+ * 获取注册的派生类
53
+ * @param type 类类型标识
54
+ * @returns 注册的派生类,如果不存在返回null
55
+ */
100
56
  static getClass<T extends typeof Descriptor>(type: string) {
101
57
  return (privateState.classCollection.get(type) ?? null) as T | null
102
58
  }
103
- /**使用数据动态的创建描述器 */
59
+ /**
60
+ * 使用数据动态的创建描述器
61
+ * @param descriptor 描述器配置对象
62
+ * @returns 创建的描述器实例
63
+ */
104
64
  static create<T extends Descriptor = Descriptor>(descriptor: Partial<DeserializationObject<Descriptor>>) {
105
65
  const Class = this.getClass(descriptor.type ?? Descriptor.type)
106
66
  if (Class === null) throw new Error(language.get('descriptor.notRegister', { type: descriptor.type }))
107
67
  return new Class(descriptor) as T
108
68
  }
109
- /**将对象序列化成json字符 */
69
+ /**
70
+ * 将对象序列化成json字符
71
+ * @param descriptor 要序列化的对象
72
+ * @returns 序列化后的json字符串
73
+ */
110
74
  static serialization(descriptor: SerializationObject) {
111
75
  return Serialization.stringify(descriptor)
112
76
  }
113
- /**将json字符反序列化成对象 */
77
+ /**
78
+ * 将json字符反序列化成对象
79
+ * @param str 要反序列化的json字符串
80
+ * @returns 反序列化后的对象
81
+ */
114
82
  static deserialization<T = any>(str: string) {
115
83
  return Serialization.parse(str) as T
116
84
  }
117
- /**深度克隆传入的JSON对象 */
118
- static deepClone<T = any>(descriptor: SerializationObject) {
85
+ /**
86
+ * 深度克隆传入的JSON对象
87
+ * @param descriptor 要克隆的对象
88
+ * @returns 克隆后的对象
89
+ */
90
+ static deepClone<T extends any = any>(descriptor: SerializationObject) {
119
91
  return Serialization.deepClone<T>(descriptor)
120
92
  }
121
- /**修复引用关系 */
93
+ /**
94
+ * 修复引用关系
95
+ * @param childs 子描述器数组
96
+ * @param map UUID映射表
97
+ */
122
98
  static fixReferences(childs: Descriptor[], map: Map<string, Descriptor>) {
123
99
  for (const child of childs) {
124
100
  for (const interaction of child.interactions) {
125
- const keys = Object.keys(interaction.inputs)
101
+ const keys = Object.keys(interaction.nodes)
126
102
  for (const key of keys) {
127
- const input = interaction.inputs[key]
128
- const attrs = Object.keys(input)
103
+ const node = interaction.nodes[key]
104
+ const attrs = Object.keys(node)
129
105
  for (const attr of attrs) {
130
- const cur = input[attr]
106
+ const cur = node[attr]
131
107
  const clone = map.get(cur.value)
132
108
  if (clone === undefined) continue
133
109
  cur.value = clone.uuid
@@ -136,7 +112,11 @@ class Descriptor extends ObjectPool {
136
112
  }
137
113
  }
138
114
  }
139
- /**创建镜像副本并修复引用关系 */
115
+ /**
116
+ * 创建镜像副本并修复引用关系
117
+ * @param descriptor 要克隆的描述器
118
+ * @returns 克隆后的描述器
119
+ */
140
120
  static cloneAndFixReferences(descriptor: Descriptor) {
141
121
  const map = new Map()
142
122
  const childs: Descriptor[] = []
@@ -146,48 +126,64 @@ class Descriptor extends ObjectPool {
146
126
  return clone
147
127
  }
148
128
 
149
- static getPrefab(uuid: string) {
150
- return privateState.instanceToPrefabCollection.get(uuid) ?? null
151
- }
152
- static getInstanceCollection(uuid: string) {
153
- return privateState.prefabToInstanceCollection.get(uuid) ?? null
154
- }
155
-
129
+ /**
130
+ * 类型标识
131
+ */
156
132
  readonly type: string
133
+ /**
134
+ * 子描述器数组
135
+ */
157
136
  readonly children: Descriptor[]
158
- generatedBy = ''
137
+ /**
138
+ * 名称
139
+ */
159
140
  name: string
160
- dataset: string
141
+ /**
142
+ * 数据容器
143
+ */
144
+ store: string
145
+ /**
146
+ * 变量配置
147
+ */
161
148
  variables: DescriptorVariables
162
- interactions: InteractionPropertyCollection
149
+ /**
150
+ * 交互属性集合
151
+ */
152
+ interactions: InteractionPropertyArray
153
+ /**
154
+ * 是否可选择
155
+ */
163
156
  selectable: boolean
164
- prefab: DescriptorPrefab | false
165
157
 
166
158
  constructor(descriptor: Partial<DeserializationObject<Descriptor>> = {}) {
167
159
  super(descriptor.uuid)
168
160
 
169
161
  this.type = (this.constructor as typeof Descriptor).type
170
162
  this.name = descriptor.name ?? ''
171
- this.dataset = descriptor.dataset ?? ''
172
- this.variables = descriptor.variables ? Descriptor.deepClone(descriptor.variables) : {}
173
- this.interactions = descriptor.interactions ? Descriptor.deepClone(descriptor.interactions) : []
163
+ this.store = descriptor.store ?? ''
164
+ this.variables = DescriptorVariablesSchema.parse(descriptor.variables)
165
+ this.interactions = InteractionPropertyArraySchame.parse(descriptor.interactions)
174
166
  this.selectable = descriptor.selectable ?? true
175
- this.prefab = descriptor.prefab ? Descriptor.deepClone(descriptor.prefab) : false
176
167
  //@ts-ignore
177
168
  this.children = []
178
169
 
179
- privateState.setPrefabInstance(this)
180
-
181
170
  if (Array.isArray(descriptor.children) && descriptor.children.length > 0) {
182
171
  for (const subDescriptor of descriptor.children) this.add(Descriptor.create(subDescriptor))
183
172
  }
184
173
  }
185
174
 
175
+ /**
176
+ * 实例化时的回调
177
+ * @param instance 实例描述器
178
+ */
186
179
  protected beenInstantiated(instance: Descriptor): void {
187
180
  instance.clear()
188
181
  }
189
182
 
190
- /**将`descriptor`添加到此节点中*/
183
+ /**
184
+ * 将`descriptor`添加到此节点中
185
+ * @param descriptor 要添加的描述器
186
+ */
191
187
  add(...descriptor: Descriptor[]) {
192
188
 
193
189
  for (const sub of descriptor) {
@@ -202,7 +198,10 @@ class Descriptor extends ObjectPool {
202
198
  observer.trigger('add', { self: sub.uuid, puid: this.uuid })
203
199
  }
204
200
  }
205
- /**将`descriptor`从此节点中移除*/
201
+ /**
202
+ * 将`descriptor`从此节点中移除
203
+ * @param descriptor 要移除的描述器
204
+ */
206
205
  remove(...descriptor: Descriptor[]) {
207
206
  for (const sub of descriptor) {
208
207
  for (let i = 0; i < this.children.length; i++) {
@@ -219,6 +218,8 @@ class Descriptor extends ObjectPool {
219
218
  /**
220
219
  * 追踪描述器的祖先节点
221
220
  * @param all 追踪全部的祖先节点,默认为true
221
+ * @param depth 追踪深度,默认为Infinity
222
+ * @returns 祖先节点信息
222
223
  */
223
224
  traceAncestor<T extends Descriptor = Descriptor>(all = true, depth = Infinity) {
224
225
  let parent = privateState.getParent<T>(this.uuid)
@@ -242,7 +243,11 @@ class Descriptor extends ObjectPool {
242
243
  return ancestor
243
244
  }
244
245
 
245
- /**将`uuid`节点插入到`target`节点中。*/
246
+ /**
247
+ * 将`uuid`节点插入到`target`节点中
248
+ * @param uuid 要插入的节点UUID
249
+ * @param target 目标节点UUID
250
+ */
246
251
  insert(uuid: string, target: string) {
247
252
  const insertNode = Descriptor.get<Descriptor>(uuid)
248
253
  if (insertNode === null) return console.warn(language.get('descriptor.insert.uuid.notFound', { uuid }))
@@ -255,7 +260,12 @@ class Descriptor extends ObjectPool {
255
260
  targetNode.add(insertNode)
256
261
  }
257
262
 
258
- /**将`uuid`节点插入到`target`节点前。 */
263
+ /**
264
+ * 将`uuid`节点插入到`target`节点前
265
+ * @param uuid 要插入的节点UUID
266
+ * @param target 目标节点UUID
267
+ * @param offset 偏移量,默认为0
268
+ */
259
269
  insertBefore(uuid: string, target: string, offset = 0) {
260
270
  const insertNode = Descriptor.get<Descriptor>(uuid)
261
271
  if (insertNode === null) return console.warn(language.get('descriptor.insert.uuid.notFound', { uuid }))
@@ -284,12 +294,19 @@ class Descriptor extends ObjectPool {
284
294
  observer.trigger('add', { self: sub.uuid, puid: targetParent.uuid })
285
295
  }
286
296
  }
287
- /**将`uuid`节点插入到`target`节点后。 */
297
+ /**
298
+ * 将`uuid`节点插入到`target`节点后
299
+ * @param uuid 要插入的节点UUID
300
+ * @param target 目标节点UUID
301
+ */
288
302
  insertAfter(uuid: string, target: string) {
289
303
  return this.insertBefore(uuid, target, 1)
290
304
  }
291
305
 
292
- /**获取当前节点的邻接节点 */
306
+ /**
307
+ * 获取当前节点的邻接节点
308
+ * @returns 邻接节点信息
309
+ */
293
310
  getAdjacent() {
294
311
  const adjacent: DescriptorAdjacency = {
295
312
  parent: '',
@@ -312,6 +329,7 @@ class Descriptor extends ObjectPool {
312
329
  *
313
330
  * 注意,该方法返回的描述器与原描述器uuid不相同
314
331
  * @param recursive 是否遍历克隆子节点,默认为true
332
+ * @returns 克隆后的描述器
315
333
  */
316
334
  clone<T extends Descriptor = Descriptor>(recursive = true) {
317
335
  const uuid = Generate.uuid()
@@ -320,16 +338,18 @@ class Descriptor extends ObjectPool {
320
338
  for (const sub of this.children) self.add(sub.clone(recursive))
321
339
  return self
322
340
  }
323
- /**从同类型的对象上同步属性,不同步`uuid`、`children`和`type` */
341
+ /**
342
+ * 从同类型的对象上同步属性,不同步`uuid`、`children`和`type`
343
+ * @param descriptor 要同步的描述器
344
+ * @param ignore 要忽略的属性数组
345
+ */
324
346
  syncWith(descriptor: DeserializationObject<Descriptor>, ignore: string[] = []) {
325
- if (this.type !== descriptor.type) return console.warn(language.get('descriptor.syncWith.fail'))
347
+ if (this.type !== descriptor.type) return console.warn(language.get('descriptor.syncWith.failed'))
326
348
 
327
- const source = Descriptor.create({ ...descriptor, type: this.type, uuid: undefined, children: undefined }) as DeserializationObject
349
+ const source = Descriptor.create({ ...this,...descriptor, type: this.type, uuid: undefined, children: undefined }) as DeserializationObject
328
350
  delete source['uuid']
329
351
  delete source['type']
330
352
  delete source['children']
331
- delete source['prefab']
332
- delete source['generatedBy']
333
353
 
334
354
  for (const key of ignore) delete source[key]
335
355
 
@@ -339,6 +359,7 @@ class Descriptor extends ObjectPool {
339
359
  * 遍历所有节点
340
360
  * @param cb 遍历每个节点执行的回调
341
361
  * @param terminated 遍历是否可以被终止,默认false
362
+ * @param defaultParent 默认父节点
342
363
  */
343
364
  traverse<T extends Descriptor = Descriptor, D extends T | null = T | null>(cb: (sub: T, parent: D) => true | void, terminated = false, defaultParent: D = null as D) {
344
365
  if (terminated) {
@@ -347,20 +368,34 @@ class Descriptor extends ObjectPool {
347
368
  Traverser.pre(this as unknown as T, cb, defaultParent)
348
369
  }
349
370
  }
350
- /**将子描述器全部移除 */
371
+ /**
372
+ * 将子描述器全部移除
373
+ */
351
374
  clear() {
352
375
  this.remove(...this.children)
353
376
  }
354
377
 
355
- /**将对象序列化成json字符 */
378
+ /**
379
+ * 将对象序列化成json字符
380
+ * @returns 序列化后的json字符串
381
+ */
356
382
  serialization() {
357
383
  return Descriptor.serialization(this)
358
384
  }
359
385
 
386
+ /**
387
+ * 比较两个描述器是否相等
388
+ * @param descriptor 要比较的描述器
389
+ * @param ignore 要忽略的属性数组
390
+ * @returns 是否相等
391
+ */
360
392
  equals(descriptor: DeserializationObject<Descriptor>, ignore: string[] = ['children']) {
361
393
  return ObjectUtils.equals(this, descriptor, ignore)
362
394
  }
363
395
 
396
+ /**
397
+ * 设置需要更新
398
+ */
364
399
  setNeedsUpdate() {
365
400
  observer.trigger('update', { self: this.uuid })
366
401
  }
@@ -370,19 +405,4 @@ Descriptor.register(Descriptor)
370
405
  listenInstance('free', (uuid: string) => {
371
406
  observer.trigger('free', { self: uuid })
372
407
  privateState.refGraph.delete(uuid)
373
- const prefab = privateState.instanceToPrefabCollection.get(uuid)
374
- if (prefab === undefined) return
375
- privateState.instanceToPrefabCollection.delete(uuid)
376
- const instanceCollection = privateState.prefabToInstanceCollection.get(uuid)
377
- if (instanceCollection === undefined) return
378
- ArrayUtils.remove(instanceCollection, uuid)
379
- })
380
-
381
- export {
382
- Descriptor,
383
- DescriptorAdjacency,
384
- DescriptorAncestor,
385
- DescriptorPrefab,
386
- VariabConfig,
387
- DescriptorVariables,
388
- }
408
+ })
@@ -0,0 +1,29 @@
1
+ import { DeserializationObject } from "../Serialization"
2
+ import { HtmlAttributes, HtmlAttributesSchema } from "../schema"
3
+ import { Descriptor } from "./Descriptor"
4
+ import { HTML } from "./HTML"
5
+
6
+ /**
7
+ * Div类
8
+ * 表示一个HTML div元素,用于在二维场景中显示内容
9
+ *
10
+ * 作用:作为二维界面的基本容器元素,支持文本内容和背景图片的显示
11
+ * 继承自HTML类,拥有HTML元素的所有样式和布局属性
12
+ */
13
+ export class Div extends Descriptor implements HTML {
14
+ /**类类型标识 */
15
+ static type = 'Div'
16
+
17
+ readonly isHTML = true
18
+
19
+ visible: boolean
20
+
21
+ htmlAttributes: HtmlAttributes
22
+
23
+ constructor(descriptor: Partial<DeserializationObject<Div>> = {}) {
24
+ super(descriptor)
25
+ this.visible = descriptor.visible ?? true
26
+ this.htmlAttributes = HtmlAttributesSchema.parse(descriptor.htmlAttributes)
27
+ }
28
+ }
29
+ Descriptor.register(Div)