hzengine-core 0.1.2-dev

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 (103) hide show
  1. package/dist/async/index.js +162 -0
  2. package/dist/async/zeppos_timer.js +58 -0
  3. package/dist/audio/index.js +260 -0
  4. package/dist/config/index.js +57 -0
  5. package/dist/debug/index.js +8 -0
  6. package/dist/index.js +103 -0
  7. package/dist/platform/index.js +1 -0
  8. package/dist/plugins/basic_command/$.js +8 -0
  9. package/dist/plugins/basic_command/audio.js +40 -0
  10. package/dist/plugins/basic_command/basic.js +124 -0
  11. package/dist/plugins/basic_command/character.js +112 -0
  12. package/dist/plugins/basic_command/conditional.js +260 -0
  13. package/dist/plugins/basic_command/config.js +22 -0
  14. package/dist/plugins/basic_command/decorator.js +24 -0
  15. package/dist/plugins/basic_command/eval.js +67 -0
  16. package/dist/plugins/basic_command/img.js +249 -0
  17. package/dist/plugins/basic_command/index.js +22 -0
  18. package/dist/plugins/basic_command/menu.js +140 -0
  19. package/dist/plugins/global_gesture/index.js +25 -0
  20. package/dist/plugins/transform/animation.js +440 -0
  21. package/dist/plugins/transform/commands.js +38 -0
  22. package/dist/plugins/transform/example_profiles.js +32 -0
  23. package/dist/plugins/transform/hz_anime.js +211 -0
  24. package/dist/plugins/transform/index.js +93 -0
  25. package/dist/script/index.js +537 -0
  26. package/dist/script/readscript.js +15 -0
  27. package/dist/script/strtools.js +157 -0
  28. package/dist/storage/decorator.js +260 -0
  29. package/dist/storage/fs.js +96 -0
  30. package/dist/storage/index.js +442 -0
  31. package/dist/system/index.js +144 -0
  32. package/dist/ui/index.js +535 -0
  33. package/dist/utils/path.js +289 -0
  34. package/license.txt +202 -0
  35. package/package.json +26 -0
  36. package/src/async/index.ts +124 -0
  37. package/src/async/zeppos_timer.js +65 -0
  38. package/src/audio/index.ts +224 -0
  39. package/src/config/index.ts +80 -0
  40. package/src/debug/index.ts +11 -0
  41. package/src/index.ts +122 -0
  42. package/src/platform/index.ts +158 -0
  43. package/src/plugins/basic_command/$.ts +11 -0
  44. package/src/plugins/basic_command/audio.ts +53 -0
  45. package/src/plugins/basic_command/basic.ts +145 -0
  46. package/src/plugins/basic_command/character.ts +144 -0
  47. package/src/plugins/basic_command/conditional.ts +349 -0
  48. package/src/plugins/basic_command/config.ts +29 -0
  49. package/src/plugins/basic_command/decorator.ts +29 -0
  50. package/src/plugins/basic_command/eval.ts +88 -0
  51. package/src/plugins/basic_command/img.ts +317 -0
  52. package/src/plugins/basic_command/index.ts +24 -0
  53. package/src/plugins/basic_command/menu.ts +178 -0
  54. package/src/plugins/global_gesture/index.ts +29 -0
  55. package/src/plugins/transform/animation.ts +542 -0
  56. package/src/plugins/transform/commands.ts +53 -0
  57. package/src/plugins/transform/example_profiles.ts +36 -0
  58. package/src/plugins/transform/hz_anime.ts +214 -0
  59. package/src/plugins/transform/index.ts +141 -0
  60. package/src/plugins/transform/readme.md +1 -0
  61. package/src/script/index.ts +623 -0
  62. package/src/script/readscript.ts +17 -0
  63. package/src/script/strtools.ts +159 -0
  64. package/src/storage/decorator.ts +473 -0
  65. package/src/storage/fs.ts +104 -0
  66. package/src/storage/index.ts +541 -0
  67. package/src/system/index.ts +95 -0
  68. package/src/ui/index.ts +699 -0
  69. package/src/utils/path.js +338 -0
  70. package/tsconfig.json +111 -0
  71. package/types/async/index.d.ts +24 -0
  72. package/types/async/zeppos_timer.d.ts +14 -0
  73. package/types/audio/index.d.ts +64 -0
  74. package/types/config/index.d.ts +9 -0
  75. package/types/debug/index.d.ts +6 -0
  76. package/types/index.d.ts +41 -0
  77. package/types/platform/index.d.ts +134 -0
  78. package/types/plugins/basic_command/$.d.ts +2 -0
  79. package/types/plugins/basic_command/audio.d.ts +2 -0
  80. package/types/plugins/basic_command/basic.d.ts +3 -0
  81. package/types/plugins/basic_command/character.d.ts +2 -0
  82. package/types/plugins/basic_command/conditional.d.ts +2 -0
  83. package/types/plugins/basic_command/config.d.ts +2 -0
  84. package/types/plugins/basic_command/decorator.d.ts +2 -0
  85. package/types/plugins/basic_command/eval.d.ts +2 -0
  86. package/types/plugins/basic_command/img.d.ts +2 -0
  87. package/types/plugins/basic_command/index.d.ts +2 -0
  88. package/types/plugins/basic_command/menu.d.ts +2 -0
  89. package/types/plugins/global_gesture/index.d.ts +2 -0
  90. package/types/plugins/transform/animation.d.ts +131 -0
  91. package/types/plugins/transform/commands.d.ts +7 -0
  92. package/types/plugins/transform/example_profiles.d.ts +2 -0
  93. package/types/plugins/transform/hz_anime.d.ts +51 -0
  94. package/types/plugins/transform/index.d.ts +13 -0
  95. package/types/script/index.d.ts +123 -0
  96. package/types/script/readscript.d.ts +2 -0
  97. package/types/script/strtools.d.ts +31 -0
  98. package/types/storage/decorator.d.ts +41 -0
  99. package/types/storage/fs.d.ts +1 -0
  100. package/types/storage/index.d.ts +86 -0
  101. package/types/system/index.d.ts +35 -0
  102. package/types/ui/index.d.ts +167 -0
  103. package/types/utils/path.d.ts +84 -0
@@ -0,0 +1,214 @@
1
+ import { HZEngineCore, Storage, UI } from "../../index.js";
2
+ import {
3
+ Save,
4
+ CustomSave,
5
+ } from "../../storage/decorator.js";
6
+ import { Animation, Profile, Wrapper } from "./animation.js";
7
+ // import { Time } from "@zos/sensor";
8
+
9
+ export class AnimationPlugin {
10
+ constructor(public _core: HZEngineCore) {
11
+ console.log("[AnimationPlugin] init");
12
+
13
+ _core.loadPlugin("animation", () => this)
14
+ _core.on("anime.cb", this._timerCb.bind(this));
15
+ _core.async.addRepeatTask("anime.cb", [], 0); // 此處周期應考慮加個sync update
16
+
17
+ this._lastCbUtc = this._core.platform.getTime()
18
+ }
19
+
20
+ @Save("anime.nid")
21
+ private accessor _nextAnimationId: number = 1; // self-increment
22
+
23
+ @CustomSave(
24
+ "anime.map",
25
+ function (obj): Record<string, AnimationPlugin.AnimationItem.Serialized> {
26
+ let res: Record<string, AnimationPlugin.AnimationItem.Serialized> = {};
27
+ for (let id in obj) {
28
+ if (!obj[id].isSave) continue;
29
+ res[id] = {
30
+ destroyOnEnd: obj[id].destroyOnEnd,
31
+ isSave: obj[id].isSave,
32
+ targetViewId: obj[id].targetViewId,
33
+ serializedInstance: obj[id].instance.serialize(),
34
+ };
35
+ }
36
+ return res;
37
+ },
38
+ function (
39
+ obj: Record<string, AnimationPlugin.AnimationItem.Serialized>
40
+ ): Record<string, AnimationPlugin.AnimationItem> {
41
+ let res: Record<string, AnimationPlugin.AnimationItem> = {};
42
+ for (let id in obj) {
43
+ if (!obj[id].isSave) continue;
44
+ let animation = Animation.unserialize(obj[id].serializedInstance);
45
+ this._linkAnimationCb(animation, Number(id));
46
+ res[id] = {
47
+ destroyOnEnd: obj[id].destroyOnEnd,
48
+ isSave: obj[id].isSave,
49
+ targetViewId: obj[id].targetViewId,
50
+ instance: animation,
51
+ };
52
+ }
53
+ return res;
54
+ }
55
+ )
56
+ private accessor _animationMap: Record<
57
+ string,
58
+ AnimationPlugin.AnimationItem
59
+ > = {};
60
+
61
+ // private _timeSensor = new Time();
62
+
63
+ private _lastCbUtc: number;
64
+
65
+ public applyAnimation<P extends Profile.PropsType>({
66
+ profile,
67
+ targetView,
68
+ options,
69
+ }: {
70
+ profile: Profile<P>;
71
+ targetView: UI.View<Storage.Saveable<unknown>>;
72
+ options?: AnimationPlugin.Options;
73
+ }): number {
74
+ const id = this._nextAnimationId++;
75
+ let animation = new Animation(profile, {
76
+ // @ts-ignore
77
+ initProps: targetView.prop ?? {},
78
+ wrappers: options?.customWrappers,
79
+ });
80
+ if (!targetView.id) throw "targetView.id must be not null";
81
+ this._animationMap[id] = {
82
+ destroyOnEnd: options?.destroyOnEnd ?? false,
83
+ isSave: options?.isSave ?? true,
84
+ instance: animation,
85
+ targetViewId: targetView.id!,
86
+ };
87
+ this._linkAnimationCb(animation, id);
88
+
89
+ animation.goto(0);
90
+
91
+ this._core.on("afterLoadArchive", () => {
92
+ this._lastCbUtc = this._core.platform.getTime();
93
+ });
94
+ return id;
95
+ }
96
+
97
+ public stopAnimation(id: number) {
98
+ if (this._animationMap[id] !== undefined) {
99
+ this._animationMap[id].instance.stop();
100
+ delete this._animationMap[id];
101
+ }
102
+ }
103
+
104
+ private _nextTempAnimationId: number = -1; // self-decrement
105
+
106
+ _tempAnimationMap: Record<string, Animation> = {};
107
+
108
+ public createTempAnimation<P extends Profile.PropsType = unknown>({
109
+ profile,
110
+ onFrame,
111
+ onEnd,
112
+ initProps,
113
+ wrappers,
114
+ }: {
115
+ profile: Profile<P>;
116
+ onFrame?: (props: Partial<P>) => void;
117
+ onEnd?: () => void;
118
+ initProps?: Partial<P>;
119
+ wrappers?: Record<string, Wrapper>;
120
+ }): number {
121
+ let id = this._nextTempAnimationId--;
122
+ let animation = new Animation(profile, {
123
+ initProps,
124
+ wrappers,
125
+ });
126
+ animation.onFrame = onFrame ?? null;
127
+ animation.onEnd = () => {
128
+ if (onEnd) onEnd();
129
+ delete this._tempAnimationMap[id];
130
+ };
131
+ this._tempAnimationMap[id] = animation
132
+ return id;
133
+ }
134
+
135
+ public clearTempAnimation(id: number) {
136
+ if (this._tempAnimationMap[id] !== undefined) {
137
+ this._tempAnimationMap[id].stop();
138
+ delete this._tempAnimationMap[id];
139
+ }
140
+
141
+ }
142
+
143
+ private _timerCb() {
144
+ let utc = this._core.platform.getTime();
145
+ let delta_time = (utc - this._lastCbUtc) / 1000;
146
+ this._lastCbUtc = utc;
147
+ // this._core.debug.log(`delta time: ${delta_time}`);
148
+ for (let id in this._animationMap) {
149
+ this._animationMap[id].instance.step(delta_time);
150
+ }
151
+
152
+ for (let id in this._tempAnimationMap) {
153
+ this._tempAnimationMap[id].step(delta_time);
154
+ }
155
+ }
156
+
157
+ private _frameCb(props: Record<string, any>, id: number) {
158
+ if (this._animationMap[id] !== undefined) {
159
+ let view = this._core.ui.getView(this._animationMap[id].targetViewId);
160
+ if (!view) {
161
+ this._core.debug.log(`view ${id} not found, stop animation`);
162
+ this.stopAnimation(id); // TODO
163
+ return;
164
+ }
165
+ view.commit(props);
166
+ // this._core.debug.log(`update view ${view.name} with ${JSON.stringify(props)}`);
167
+ }
168
+ }
169
+
170
+ private _linkAnimationCb(animation: Animation, id: number) {
171
+ animation.onFrame = (props) => {
172
+ // console.log("onFrame cb");
173
+
174
+ this._frameCb(props, id);
175
+ };
176
+ animation.onEnd = () => this._endCb(id);
177
+ }
178
+
179
+ private _endCb(id: number) {
180
+ if (this._animationMap[id] !== undefined) {
181
+ if (this._animationMap[id].destroyOnEnd) {
182
+ let view = this._core.ui.getView(id);
183
+ if (view) this._core.ui.destroyView(view);
184
+ }
185
+ delete this._animationMap[id];
186
+ console.log("map item", this._animationMap[id]);
187
+
188
+ }
189
+ else console.log("nf map item", this._animationMap[id]);
190
+
191
+ }
192
+ }
193
+
194
+ namespace AnimationPlugin {
195
+ export interface Options {
196
+ destroyOnEnd?: boolean;
197
+ isSave?: boolean;
198
+ customWrappers?: Record<string, Wrapper>;
199
+ }
200
+ export interface AnimationItem {
201
+ destroyOnEnd: boolean;
202
+ isSave: boolean;
203
+ targetViewId: number;
204
+ instance: Animation;
205
+ }
206
+ export namespace AnimationItem {
207
+ export interface Serialized {
208
+ destroyOnEnd: boolean;
209
+ isSave: boolean;
210
+ targetViewId: number;
211
+ serializedInstance: Animation.Serialized;
212
+ }
213
+ }
214
+ }
@@ -0,0 +1,141 @@
1
+ import { HZEngineCore, Storage, UI } from "../../index.js";
2
+ import { AnimationPlugin } from "./hz_anime.js";
3
+ import { Animation, Profile } from "./animation.js";
4
+ // import hmFS from "@zos/fs";
5
+ import { getTransitionMap, registerHzscriptCommands, TransitionData } from "./commands.js";
6
+
7
+ export function registerPlugin(core: HZEngineCore): InstanceType {
8
+ let animationPlugin = new AnimationPlugin(core);
9
+ let profileMap: Record<string, [path: string]> = {};
10
+
11
+ registerHzscriptCommands(core);
12
+
13
+ core.on("afterLoadProject", () => {
14
+ profileMap = core.storage.getSaveableData(
15
+ core.storage.preloadedData,
16
+ false,
17
+ "animation",
18
+ "profileMap"
19
+ ) as Record<string, [path: string]>;
20
+ });
21
+ function getProfile(name: string): Profile | null {
22
+ let profile_item = profileMap[name];
23
+ if (!profile_item) {
24
+ core.debug.log(`Animation profile [${name}] not found`);
25
+ return null;
26
+ }
27
+ let str = core.platform.readFileSync({
28
+ path: profile_item[0],
29
+ options: {
30
+ encoding: "utf8",
31
+ },
32
+ });
33
+ if (typeof str !== "string") {
34
+ core.debug.log(`Animation profile file [${name}] not found`);
35
+ return null;
36
+ }
37
+ try {
38
+ return JSON.parse(str) as Profile;
39
+ } catch (error) {
40
+ core.debug.log(`Animation profile file [${name}] parse error:`, error);
41
+ }
42
+ return null;
43
+ }
44
+ function applyTransform(
45
+ names: string[],
46
+ target: UI.View<Storage.Saveable<unknown>>,
47
+ isSave: boolean = false,
48
+ destroyOnEnd: boolean = false
49
+ ) {
50
+ let profile: Profile | null = null;
51
+ if (names.length === 1) {
52
+ profile = getProfile(names[0]);
53
+ } else if (names.length > 1) {
54
+ let syncs: Profile[] = [];
55
+ for (let i = 0; i < names.length; i++) {
56
+ let profile = getProfile(names[i]);
57
+ if (profile) syncs.push(profile);
58
+ }
59
+ profile = { syncs };
60
+ }
61
+ if (!profile) profile = [];
62
+ animationPlugin.applyAnimation({
63
+ profile,
64
+ targetView: target,
65
+ options: {
66
+ isSave,
67
+ destroyOnEnd,
68
+ },
69
+ });
70
+ }
71
+
72
+ class TransformRouteStrategy implements UI.Router.RouteStrategy {
73
+ constructor(
74
+ public outTransforms: string[] | null,
75
+ public inTransforms: string[] | null
76
+ ) {}
77
+ create(
78
+ viewName: string,
79
+ layer: string,
80
+ prop: Storage.Saveable<unknown>,
81
+ ui: UI,
82
+ isSave: boolean
83
+ ): UI.View<Storage.Saveable<Storage.Saveable<unknown>>> {
84
+ let view = ui.createView(viewName, layer, prop, isSave);
85
+ this.inTransforms && applyTransform(this.inTransforms, view, isSave);
86
+ return view;
87
+ }
88
+ destroy(viewInstance: UI.View<Storage.Saveable<unknown>>, ui: UI): void {
89
+ if (this.outTransforms) {
90
+ applyTransform(
91
+ this.outTransforms,
92
+ viewInstance,
93
+ viewInstance.isSave,
94
+ true
95
+ );
96
+ } else {
97
+ ui.destroyView(viewInstance);
98
+ }
99
+ }
100
+ // update(
101
+ // viewInstance: UI.View<Storage.Saveable<unknown>>,
102
+ // prop: Storage.Saveable<unknown>,
103
+ // ui: UI
104
+ // ): void {
105
+ // ui.updateView(viewInstance, prop);
106
+ // },
107
+ }
108
+
109
+
110
+ return {
111
+ applyTransform,
112
+ getProfile,
113
+ createStrategy(
114
+ outTransforms: string[] | null,
115
+ inTransforms: string[] | null
116
+ ): UI.Router.RouteStrategy {
117
+ return new TransformRouteStrategy(outTransforms, inTransforms);
118
+ },
119
+ animationPlugin,
120
+ getTransition(name: string): TransitionData | null {
121
+ return getTransitionMap(core)![name] ?? null;
122
+ }
123
+ };
124
+ }
125
+ export interface InstanceType {
126
+ applyTransform(
127
+ names: string[],
128
+ target: UI.View<Storage.Saveable<unknown>>,
129
+ isSave?: boolean,
130
+ destroyOnEnd?: boolean
131
+ ): void;
132
+ getProfile(name: string): Profile | null;
133
+ createStrategy(
134
+ outTransforms: string[] | null,
135
+ inTransforms: string[] | null
136
+ ): UI.Router.RouteStrategy;
137
+ animationPlugin: AnimationPlugin;
138
+ getTransition(name: string): TransitionData | null;
139
+ }
140
+
141
+ export { Animation, Profile, AnimationPlugin };
@@ -0,0 +1 @@
1
+ //