Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Unified Diff: sky/examples/game/lib/spritesheet.dart

Issue 1204783003: Adds basic sprite sheet support to sprites (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Adds basic sprite sheet support to sprites (fixed issues) Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sky/examples/game/lib/spritesheet.dart
diff --git a/sky/examples/game/lib/spritesheet.dart b/sky/examples/game/lib/spritesheet.dart
new file mode 100644
index 0000000000000000000000000000000000000000..57d84883fe229d1ae4b8625e8a991685b920fd46
--- /dev/null
+++ b/sky/examples/game/lib/spritesheet.dart
@@ -0,0 +1,59 @@
+part of sprites;
+
+class SpriteSheet {
+
+ Image _image;
+ Map<String, Texture> _textures = new Map();
+
+ SpriteSheet(this._image, String jsonDefinition) {
+ assert(_image != null);
+ assert(jsonDefinition != null);
+
+ JsonDecoder decoder = new JsonDecoder();
+ Map file = decoder.convert(jsonDefinition);
+ assert(file != null);
+
+ List frames = file["frames"];
+
+ for (Map frameInfo in frames) {
+ String fileName = frameInfo["filename"];
+ Rect frame = _readJsonRect(frameInfo["frame"]);
+ bool rotated = frameInfo["rotated"];
+ bool trimmed = frameInfo["trimmed"];
+ Rect spriteSourceSize = _readJsonRect(frameInfo["spriteSourceSize"]);
+ Size sourceSize = _readJsonSize(frameInfo["sourceSize"]);
+ Point pivot = _readJsonPoint(frameInfo["pivot"]);
+
+ var texture = new Texture._fromSpriteFrame(_image, fileName, sourceSize, rotated, trimmed, frame,
+ spriteSourceSize, pivot);
+ _textures[fileName] = texture;
+ }
+ }
+
+ Rect _readJsonRect(Map data) {
+ num x = data["x"];
eseidel 2015/06/23 23:22:31 Do you mean to use num instead of int or double?
+ num y = data["y"];
+ num w = data["w"];
+ num h = data["h"];
+
+ return new Rect.fromLTRB(x.toDouble(), y.toDouble(), (x + w).toDouble(), (y + h).toDouble());
+ }
+
+ Size _readJsonSize(Map data) {
+ num w = data["w"];
+ num h = data["h"];
+
+ return new Size(w.toDouble(), h.toDouble());
+ }
+
+ Point _readJsonPoint(Map data) {
+ num x = data["x"];
+ num y = data["y"];
+
+ return new Point(x.toDouble(), y.toDouble());
+ }
+
+ Image get image => _image;
+
+ Texture operator [](String fileName) => _textures[fileName];
+}

Powered by Google App Engine
This is Rietveld 408576698