Index: sky/examples/game/lib/image_map.dart |
diff --git a/sky/examples/game/lib/image_map.dart b/sky/examples/game/lib/image_map.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5645bc67bfb6eeedcc0c3523a76c09e3d052a95b |
--- /dev/null |
+++ b/sky/examples/game/lib/image_map.dart |
@@ -0,0 +1,38 @@ |
+part of sprites; |
+ |
+typedef void ImageMapCallback(ImageMap preloader); |
+ |
+class ImageMap { |
+ |
+ Map<String, Image> _images; |
+ |
+ int _totalNumImages = 0; |
+ int _numLoadedImages = 0; |
+ |
+ ImageMapCallback _callback; |
+ |
+ ImageMap(List<String> urls, ImageMapCallback callback) { |
+ _images = new Map(); |
+ _totalNumImages = urls.length; |
+ _callback = callback; |
+ urls.forEach(_addURL); |
+ } |
+ |
+ void _addURL(String url) { |
+ image_cache.load(url, (Image image) { |
+ // Store reference to image |
+ _images[url] = image; |
+ |
+ // Check if all images are loaded |
+ _numLoadedImages++; |
+ if (_numLoadedImages==_totalNumImages) { |
+ // Everything loaded, make callback |
+ _callback(this); |
+ } |
+ }); |
+ } |
+ |
+ Image getImage(String url) => _images[url]; |
+ |
+ Image operator [](String url) => _images[url]; |
+} |