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

Side by Side Diff: sky/examples/game/lib/sprite_node.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
(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 double _opacity = 1.0;
10 Color colorOverlay;
11 TransferMode transferMode;
12
13 SpriteNode() {
14 this.pivot = new Vector2(0.5, 0.5);
15 }
16
17 SpriteNode.withImage(Image image) : super() {
18 this.pivot = new Vector2(0.5, 0.5);
19 _image = image;
20 }
21
22 double get opacity => _opacity;
23
24 void set opacity(double opacity) {
25 assert(opacity >= 0.0 && opacity <= 1.0);
26 _opacity = opacity;
27 }
28
29 void paint(PictureRecorder canvas) {
30
31 if (_image != null && _image.width > 0 && _image.height > 0) {
32 canvas.save();
33
34 double scaleX = _width/_image.width;
35 double scaleY = _height/_image.height;
36
37 if (constrainProportions) {
38 // Constrain proportions, using the smallest scale and by centering the image
39 if (scaleX < scaleY) {
40 canvas.translate(0.0, (_height - scaleX * _image.height)/2.0);
41 scaleY = scaleX;
42 }
43 else {
44 canvas.translate((_width - scaleY * _image.width)/2.0, 0.0);
45 scaleX = scaleY;
46 }
47 }
48
49 canvas.scale(scaleX, scaleY);
50
51 // Setup paint object for opacity and transfer mode
52 Paint paint = new Paint();
53 paint.setARGB((255.0*_opacity).toInt(), 255, 255, 255);
54 if (colorOverlay != null) {
55 paint.setColorFilter(new ColorFilter.Mode(colorOverlay, TransferMode.src ATopMode));
56 }
57 if (transferMode != null) {
58 paint.setTransferMode(transferMode);
59 }
60
61 canvas.drawImage(_image, 0.0, 0.0, paint);
62 canvas.restore();
63 }
64 else {
65 // Paint a red square for missing texture
66 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, this.width, this.height),
67 new Paint()..setARGB(255, 255, 0, 0));
68 }
69 }
70
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698