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

Side by Side Diff: sky/examples/game/lib/spritesheet.dart

Issue 1218593002: Move sky/examples to sky/sdk/lib/example, and code changes to support that change. Fixes T277. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 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 unified diff | Download patch
OLDNEW
(Empty)
1 part of sprites;
2
3 /// A sprite sheet packs a number of smaller images into a single large image.
4 ///
5 /// The placement of the smaller images are defined by a json file. The larger i mage and json file is typically created
6 /// by a tool such as TexturePacker. The [SpriteSheet] class will take a referen ce to a larger image and a json string.
7 /// From the image and the string the [SpriteSheet] creates a number of [Texture ] objects. The names of the frames in
8 /// the sprite sheet definition are used to reference the different textures.
9 class SpriteSheet {
10
11 Image _image;
12 Map<String, Texture> _textures = new Map();
13
14 /// Creates a new sprite sheet from an [_image] and a sprite sheet [jsonDefini tion].
15 ///
16 /// var mySpriteSheet = new SpriteSheet(myImage, jsonString);
17 SpriteSheet(this._image, String jsonDefinition) {
18 assert(_image != null);
19 assert(jsonDefinition != null);
20
21 JsonDecoder decoder = new JsonDecoder();
22 Map file = decoder.convert(jsonDefinition);
23 assert(file != null);
24
25 List frames = file["frames"];
26
27 for (Map frameInfo in frames) {
28 String fileName = frameInfo["filename"];
29 Rect frame = _readJsonRect(frameInfo["frame"]);
30 bool rotated = frameInfo["rotated"];
31 bool trimmed = frameInfo["trimmed"];
32 Rect spriteSourceSize = _readJsonRect(frameInfo["spriteSourceSize"]);
33 Size sourceSize = _readJsonSize(frameInfo["sourceSize"]);
34 Point pivot = _readJsonPoint(frameInfo["pivot"]);
35
36 var texture = new Texture._fromSpriteFrame(_image, fileName, sourceSize, r otated, trimmed, frame,
37 spriteSourceSize, pivot);
38 _textures[fileName] = texture;
39 }
40 }
41
42 Rect _readJsonRect(Map data) {
43 num x = data["x"];
44 num y = data["y"];
45 num w = data["w"];
46 num h = data["h"];
47
48 return new Rect.fromLTRB(x.toDouble(), y.toDouble(), (x + w).toDouble(), (y + h).toDouble());
49 }
50
51 Size _readJsonSize(Map data) {
52 num w = data["w"];
53 num h = data["h"];
54
55 return new Size(w.toDouble(), h.toDouble());
56 }
57
58 Point _readJsonPoint(Map data) {
59 num x = data["x"];
60 num y = data["y"];
61
62 return new Point(x.toDouble(), y.toDouble());
63 }
64
65 /// The image used by the sprite sheet.
66 ///
67 /// var spriteSheetImage = mySpriteSheet.image;
68 Image get image => _image;
69
70 /// Returns a texture by its name.
71 ///
72 /// var myTexture = mySpriteSheet["example.png"];
73 Texture operator [](String fileName) => _textures[fileName];
74 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698