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

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

Issue 1161023006: Adds basic support for images in game example (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « sky/examples/game/lib/game_world.dart ('k') | sky/examples/game/lib/sprites.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 part of sprites;
2
3 // TODO: Actually draw images
4
5 class SpriteNode extends TransformNode {
6
7 Image _image;
8 bool constrainProportions = false;
9
10 SpriteNode() : super() {
abarth-chromium 2015/06/01 21:58:55 The call to super() is implied. You don't need to
11 this.pivot = new Vector2(0.5, 0.5);
12 }
13
14 SpriteNode.withImage(Image image) : super() {
15 this.pivot = new Vector2(0.5, 0.5);
16 _image = image;
17 }
18
19 void paint(PictureRecorder canvas) {
20
21 if (_image != null && _image.width > 0 && _image.height > 0) {
22 canvas.save();
23
24 double scaleX = _width/_image.width;
25 double scaleY = _height/_image.height;
26
27 if (constrainProportions) {
28 // Constrain proportions, using the smallest scale and by centering the image
29 if (scaleX < scaleY) {
30 canvas.translate(0.0, (_height - scaleX * _image.height)/2.0);
31 scaleY = scaleX;
32 }
33 else {
34 canvas.translate((_width - scaleY * _image.width)/2.0, 0.0);
35 scaleX = scaleY;
36 }
37 }
38
39 canvas.scale(scaleX, scaleY);
40 canvas.drawImage(_image, 0.0, 0.0, new Paint()..setARGB(255, 255, 255, 255 ));
41 canvas.restore();
42 }
43 else {
44 // Paint a red square for missing texture
45 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, this.width, this.height),
46 new Paint()..setARGB(255, 255, 0, 0));
47 }
48 }
49
50 }
OLDNEW
« no previous file with comments | « sky/examples/game/lib/game_world.dart ('k') | sky/examples/game/lib/sprites.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698