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

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

Issue 1180703002: Fixes matrix transformations in sprites (Closed) Base URL: git@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
OLDNEW
1 part of sprites; 1 part of sprites;
2 2
3 // TODO: Actually draw images 3 // TODO: Actually draw images
4 4
5 class SpriteNode extends TransformNode { 5 class Sprite extends NodeWithSize {
6 6
7 Image _image; 7 Image _image;
8 bool constrainProportions = false; 8 bool constrainProportions = false;
9 double _opacity = 1.0; 9 double _opacity = 1.0;
10 Color colorOverlay; 10 Color colorOverlay;
11 TransferMode transferMode; 11 TransferMode transferMode;
12 12
13 SpriteNode() { 13 Sprite() {
14 this.pivot = new Vector2(0.5, 0.5);
15 } 14 }
16 15
17 SpriteNode.withImage(Image image) : super() { 16 Sprite.withImage(Image image) {
18 this.pivot = new Vector2(0.5, 0.5); 17 this.pivot = new Point(0.5, 0.5);
18 this.size = new Size(image.width.toDouble(), image.height.toDouble());
19 _image = image; 19 _image = image;
20 } 20 }
21 21
22 double get opacity => _opacity; 22 double get opacity => _opacity;
23 23
24 void set opacity(double opacity) { 24 void set opacity(double opacity) {
25 assert(opacity >= 0.0 && opacity <= 1.0); 25 assert(opacity >= 0.0 && opacity <= 1.0);
26 _opacity = opacity; 26 _opacity = opacity;
27 } 27 }
28 28
29 void paint(PictureRecorder canvas) { 29 void paint(PictureRecorder canvas) {
30 canvas.save();
31
32 // Account for pivot point
33 applyTransformForPivot(canvas);
30 34
31 if (_image != null && _image.width > 0 && _image.height > 0) { 35 if (_image != null && _image.width > 0 && _image.height > 0) {
32 canvas.save();
33 36
34 double scaleX = _width/_image.width; 37 double scaleX = size.width/_image.width;
35 double scaleY = _height/_image.height; 38 double scaleY = size.height/_image.height;
36 39
37 if (constrainProportions) { 40 if (constrainProportions) {
38 // Constrain proportions, using the smallest scale and by centering the image 41 // Constrain proportions, using the smallest scale and by centering the image
39 if (scaleX < scaleY) { 42 if (scaleX < scaleY) {
40 canvas.translate(0.0, (_height - scaleX * _image.height)/2.0); 43 canvas.translate(0.0, (size.height - scaleX * _image.height)/2.0);
41 scaleY = scaleX; 44 scaleY = scaleX;
42 } 45 }
43 else { 46 else {
44 canvas.translate((_width - scaleY * _image.width)/2.0, 0.0); 47 canvas.translate((size.width - scaleY * _image.width)/2.0, 0.0);
45 scaleX = scaleY; 48 scaleX = scaleY;
46 } 49 }
47 } 50 }
48 51
49 canvas.scale(scaleX, scaleY); 52 canvas.scale(scaleX, scaleY);
50 53
51 // Setup paint object for opacity and transfer mode 54 // Setup paint object for opacity and transfer mode
52 Paint paint = new Paint(); 55 Paint paint = new Paint();
53 paint.setARGB((255.0*_opacity).toInt(), 255, 255, 255); 56 paint.setARGB((255.0*_opacity).toInt(), 255, 255, 255);
54 if (colorOverlay != null) { 57 if (colorOverlay != null) {
55 paint.setColorFilter(new ColorFilter.Mode(colorOverlay, TransferMode.src ATopMode)); 58 paint.setColorFilter(new ColorFilter.Mode(colorOverlay, TransferMode.src ATopMode));
56 } 59 }
57 if (transferMode != null) { 60 if (transferMode != null) {
58 paint.setTransferMode(transferMode); 61 paint.setTransferMode(transferMode);
59 } 62 }
60 63
61 canvas.drawImage(_image, 0.0, 0.0, paint); 64 canvas.drawImage(_image, 0.0, 0.0, paint);
62 canvas.restore();
63 } 65 }
64 else { 66 else {
65 // Paint a red square for missing texture 67 // Paint a red square for missing texture
66 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, this.width, this.height), 68 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height),
67 new Paint()..setARGB(255, 255, 0, 0)); 69 new Paint()..setARGB(255, 255, 0, 0));
68 } 70 }
71 canvas.restore();
69 } 72 }
70
71 } 73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698