canvasengine 2.0.0-beta.37 → 2.0.0-beta.39
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/dist/{DebugRenderer-CTWPthRt.js → DebugRenderer-Rrw9FlTd.js} +2 -2
- package/dist/{DebugRenderer-CTWPthRt.js.map → DebugRenderer-Rrw9FlTd.js.map} +1 -1
- package/dist/components/Button.d.ts +50 -3
- package/dist/components/Button.d.ts.map +1 -1
- package/dist/components/Canvas.d.ts.map +1 -1
- package/dist/components/Joystick.d.ts +36 -0
- package/dist/components/Joystick.d.ts.map +1 -0
- package/dist/components/Sprite.d.ts +2 -0
- package/dist/components/Sprite.d.ts.map +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/types/Spritesheet.d.ts +0 -118
- package/dist/components/types/Spritesheet.d.ts.map +1 -1
- package/dist/directives/Controls.d.ts +16 -7
- package/dist/directives/Controls.d.ts.map +1 -1
- package/dist/directives/GamepadControls.d.ts +3 -1
- package/dist/directives/GamepadControls.d.ts.map +1 -1
- package/dist/directives/JoystickControls.d.ts +172 -0
- package/dist/directives/JoystickControls.d.ts.map +1 -0
- package/dist/directives/index.d.ts +1 -0
- package/dist/directives/index.d.ts.map +1 -1
- package/dist/engine/reactive.d.ts.map +1 -1
- package/dist/engine/signal.d.ts.map +1 -1
- package/dist/{index-BqwprEPH.js → index-BQ99FClW.js} +6057 -5433
- package/dist/index-BQ99FClW.js.map +1 -0
- package/dist/index.global.js +7 -7
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +59 -57
- package/dist/utils/GlobalAssetLoader.d.ts +141 -0
- package/dist/utils/GlobalAssetLoader.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/components/Button.ts +168 -41
- package/src/components/Canvas.ts +3 -0
- package/src/components/Joystick.ts +361 -0
- package/src/components/Sprite.ts +82 -16
- package/src/components/index.ts +2 -1
- package/src/components/types/Spritesheet.ts +0 -118
- package/src/directives/Controls.ts +42 -8
- package/src/directives/GamepadControls.ts +40 -18
- package/src/directives/JoystickControls.ts +396 -0
- package/src/directives/index.ts +1 -0
- package/src/engine/reactive.ts +362 -242
- package/src/engine/signal.ts +8 -2
- package/src/utils/GlobalAssetLoader.ts +257 -0
- package/dist/index-BqwprEPH.js.map +0 -1
package/src/engine/signal.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Observable,
|
|
3
|
-
|
|
3
|
+
Subscription
|
|
4
4
|
} from "rxjs";
|
|
5
5
|
import type { Element } from "./reactive";
|
|
6
6
|
import { Tick } from "../directives/Scheduler";
|
|
@@ -55,7 +55,7 @@ export function tick(fn: (tickValue: Tick, element: Element) => void) {
|
|
|
55
55
|
let subscription: Subscription | undefined
|
|
56
56
|
if (context.tick) {
|
|
57
57
|
subscription = context.tick.observable.subscribe(({ value }) => {
|
|
58
|
-
|
|
58
|
+
fn(value, el)
|
|
59
59
|
})
|
|
60
60
|
}
|
|
61
61
|
return () => {
|
|
@@ -142,6 +142,12 @@ export function h<C extends ComponentFunction<any>>(
|
|
|
142
142
|
...((component as any).effectMounts ?? [])
|
|
143
143
|
];
|
|
144
144
|
|
|
145
|
+
// Copy dependencies prop to the returned element so it can be used for delayed mounting
|
|
146
|
+
if (props?.dependencies) {
|
|
147
|
+
component.props = component.props || {};
|
|
148
|
+
component.props.dependencies = props.dependencies;
|
|
149
|
+
}
|
|
150
|
+
|
|
145
151
|
// call mount hook for root component
|
|
146
152
|
if (component instanceof Promise) {
|
|
147
153
|
component.then((component) => {
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global Asset Loader
|
|
3
|
+
*
|
|
4
|
+
* Tracks the loading progress of all assets (images, spritesheets, etc.) across all sprites in a component tree.
|
|
5
|
+
* This allows components to know when all assets are loaded, useful for displaying loaders or progress bars.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const loader = new GlobalAssetLoader();
|
|
10
|
+
*
|
|
11
|
+
* loader.onProgress((progress) => {
|
|
12
|
+
* console.log(`Loading: ${(progress * 100).toFixed(0)}%`);
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* loader.onComplete(() => {
|
|
16
|
+
* console.log('All assets loaded!');
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Register assets as they start loading
|
|
20
|
+
* const assetId = loader.registerAsset('path/to/image.png');
|
|
21
|
+
*
|
|
22
|
+
* // Update progress
|
|
23
|
+
* loader.updateProgress(assetId, 0.5);
|
|
24
|
+
*
|
|
25
|
+
* // Mark as complete
|
|
26
|
+
* loader.completeAsset(assetId);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class GlobalAssetLoader {
|
|
30
|
+
private assets: Map<string, { progress: number; completed: boolean }> = new Map();
|
|
31
|
+
private onProgressCallbacks: Set<(progress: number) => void> = new Set();
|
|
32
|
+
private onCompleteCallbacks: Set<() => void> = new Set();
|
|
33
|
+
private assetCounter: number = 0;
|
|
34
|
+
private isComplete: boolean = false;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Registers a new asset to track
|
|
38
|
+
*
|
|
39
|
+
* @param assetPath - The path or identifier of the asset being loaded
|
|
40
|
+
* @returns A unique ID for this asset that should be used for progress updates
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const assetId = loader.registerAsset('path/to/image.png');
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
registerAsset(assetPath: string): string {
|
|
48
|
+
const assetId = `asset_${this.assetCounter++}_${assetPath}`;
|
|
49
|
+
this.assets.set(assetId, { progress: 0, completed: false });
|
|
50
|
+
this.isComplete = false;
|
|
51
|
+
this.updateGlobalProgress();
|
|
52
|
+
return assetId;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Updates the progress of a specific asset
|
|
57
|
+
*
|
|
58
|
+
* @param assetId - The ID returned by registerAsset
|
|
59
|
+
* @param progress - Progress value between 0 and 1
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* loader.updateProgress(assetId, 0.5); // 50% loaded
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
updateProgress(assetId: string, progress: number): void {
|
|
67
|
+
const asset = this.assets.get(assetId);
|
|
68
|
+
if (!asset) {
|
|
69
|
+
console.warn(`Asset ${assetId} not found in tracker`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
asset.progress = Math.max(0, Math.min(1, progress));
|
|
74
|
+
this.updateGlobalProgress();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Marks an asset as completely loaded
|
|
79
|
+
*
|
|
80
|
+
* @param assetId - The ID returned by registerAsset
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* loader.completeAsset(assetId);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
completeAsset(assetId: string): void {
|
|
88
|
+
const asset = this.assets.get(assetId);
|
|
89
|
+
if (!asset) {
|
|
90
|
+
console.warn(`Asset ${assetId} not found in tracker`);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
asset.progress = 1;
|
|
95
|
+
asset.completed = true;
|
|
96
|
+
this.updateGlobalProgress();
|
|
97
|
+
this.checkCompletion();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Removes an asset from tracking (useful for cleanup)
|
|
102
|
+
*
|
|
103
|
+
* @param assetId - The ID returned by registerAsset
|
|
104
|
+
*/
|
|
105
|
+
removeAsset(assetId: string): void {
|
|
106
|
+
this.assets.delete(assetId);
|
|
107
|
+
this.updateGlobalProgress();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Registers a callback that will be called whenever the global progress changes
|
|
112
|
+
*
|
|
113
|
+
* @param callback - Function that receives the global progress (0-1)
|
|
114
|
+
* @returns A function to unregister the callback
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* const unsubscribe = loader.onProgress((progress) => {
|
|
119
|
+
* console.log(`Loading: ${(progress * 100).toFixed(0)}%`);
|
|
120
|
+
* });
|
|
121
|
+
*
|
|
122
|
+
* // Later, to unsubscribe:
|
|
123
|
+
* unsubscribe();
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
onProgress(callback: (progress: number) => void): () => void {
|
|
127
|
+
this.onProgressCallbacks.add(callback);
|
|
128
|
+
|
|
129
|
+
// Immediately call with current progress (wrap in try-catch for safety)
|
|
130
|
+
try {
|
|
131
|
+
callback(this.getGlobalProgress());
|
|
132
|
+
} catch (error) {
|
|
133
|
+
console.error('Error in onProgress callback:', error);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return () => {
|
|
137
|
+
this.onProgressCallbacks.delete(callback);
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Registers a callback that will be called when all assets are loaded
|
|
143
|
+
*
|
|
144
|
+
* @param callback - Function to call when all assets are complete
|
|
145
|
+
* @returns A function to unregister the callback
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const unsubscribe = loader.onComplete(() => {
|
|
150
|
+
* console.log('All assets loaded!');
|
|
151
|
+
* });
|
|
152
|
+
*
|
|
153
|
+
* // Later, to unsubscribe:
|
|
154
|
+
* unsubscribe();
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
onComplete(callback: () => void): () => void {
|
|
158
|
+
this.onCompleteCallbacks.add(callback);
|
|
159
|
+
|
|
160
|
+
// If already complete, call immediately
|
|
161
|
+
if (this.isComplete) {
|
|
162
|
+
callback();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return () => {
|
|
166
|
+
this.onCompleteCallbacks.delete(callback);
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Gets the current global progress (0-1)
|
|
172
|
+
*
|
|
173
|
+
* @returns Progress value between 0 and 1
|
|
174
|
+
*/
|
|
175
|
+
getGlobalProgress(): number {
|
|
176
|
+
if (this.assets.size === 0) {
|
|
177
|
+
return 1; // No assets means everything is "loaded"
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let totalProgress = 0;
|
|
181
|
+
for (const asset of this.assets.values()) {
|
|
182
|
+
totalProgress += asset.progress;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return totalProgress / this.assets.size;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Gets the number of assets currently being tracked
|
|
190
|
+
*
|
|
191
|
+
* @returns Number of registered assets
|
|
192
|
+
*/
|
|
193
|
+
getAssetCount(): number {
|
|
194
|
+
return this.assets.size;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Gets the number of completed assets
|
|
199
|
+
*
|
|
200
|
+
* @returns Number of completed assets
|
|
201
|
+
*/
|
|
202
|
+
getCompletedCount(): number {
|
|
203
|
+
let count = 0;
|
|
204
|
+
for (const asset of this.assets.values()) {
|
|
205
|
+
if (asset.completed) count++;
|
|
206
|
+
}
|
|
207
|
+
return count;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Checks if all assets are loaded and triggers onComplete callbacks
|
|
212
|
+
*/
|
|
213
|
+
private checkCompletion(): void {
|
|
214
|
+
if (this.isComplete) return;
|
|
215
|
+
|
|
216
|
+
const allCompleted = Array.from(this.assets.values()).every(asset => asset.completed);
|
|
217
|
+
|
|
218
|
+
if (allCompleted && this.assets.size > 0) {
|
|
219
|
+
this.isComplete = true;
|
|
220
|
+
this.onCompleteCallbacks.forEach(callback => {
|
|
221
|
+
try {
|
|
222
|
+
callback();
|
|
223
|
+
} catch (error) {
|
|
224
|
+
console.error('Error in onComplete callback:', error);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Updates global progress and notifies all progress callbacks
|
|
232
|
+
*/
|
|
233
|
+
private updateGlobalProgress(): void {
|
|
234
|
+
const progress = this.getGlobalProgress();
|
|
235
|
+
// Create a copy of callbacks to avoid issues if callbacks modify the set
|
|
236
|
+
const callbacks = Array.from(this.onProgressCallbacks);
|
|
237
|
+
callbacks.forEach(callback => {
|
|
238
|
+
try {
|
|
239
|
+
callback(progress);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.error('Error in onProgress callback:', error);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Resets the loader, clearing all assets and callbacks
|
|
248
|
+
*/
|
|
249
|
+
reset(): void {
|
|
250
|
+
this.assets.clear();
|
|
251
|
+
this.onProgressCallbacks.clear();
|
|
252
|
+
this.onCompleteCallbacks.clear();
|
|
253
|
+
this.assetCounter = 0;
|
|
254
|
+
this.isComplete = false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|