OLD | NEW |
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 SpriteNode extends TransformNode { |
6 | 6 |
7 Image _image; | 7 Image _image; |
8 bool constrainProportions = false; | 8 bool constrainProportions = false; |
| 9 double _opacity = 1.0; |
| 10 Color colorOverlay; |
| 11 TransferMode transferMode; |
9 | 12 |
10 SpriteNode() { | 13 SpriteNode() { |
11 this.pivot = new Vector2(0.5, 0.5); | 14 this.pivot = new Vector2(0.5, 0.5); |
12 } | 15 } |
13 | 16 |
14 SpriteNode.withImage(Image image) : super() { | 17 SpriteNode.withImage(Image image) : super() { |
15 this.pivot = new Vector2(0.5, 0.5); | 18 this.pivot = new Vector2(0.5, 0.5); |
16 _image = image; | 19 _image = image; |
17 } | 20 } |
18 | 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 |
19 void paint(PictureRecorder canvas) { | 29 void paint(PictureRecorder canvas) { |
20 | 30 |
21 if (_image != null && _image.width > 0 && _image.height > 0) { | 31 if (_image != null && _image.width > 0 && _image.height > 0) { |
22 canvas.save(); | 32 canvas.save(); |
23 | 33 |
24 double scaleX = _width/_image.width; | 34 double scaleX = _width/_image.width; |
25 double scaleY = _height/_image.height; | 35 double scaleY = _height/_image.height; |
26 | 36 |
27 if (constrainProportions) { | 37 if (constrainProportions) { |
28 // Constrain proportions, using the smallest scale and by centering the
image | 38 // Constrain proportions, using the smallest scale and by centering the
image |
29 if (scaleX < scaleY) { | 39 if (scaleX < scaleY) { |
30 canvas.translate(0.0, (_height - scaleX * _image.height)/2.0); | 40 canvas.translate(0.0, (_height - scaleX * _image.height)/2.0); |
31 scaleY = scaleX; | 41 scaleY = scaleX; |
32 } | 42 } |
33 else { | 43 else { |
34 canvas.translate((_width - scaleY * _image.width)/2.0, 0.0); | 44 canvas.translate((_width - scaleY * _image.width)/2.0, 0.0); |
35 scaleX = scaleY; | 45 scaleX = scaleY; |
36 } | 46 } |
37 } | 47 } |
38 | 48 |
39 canvas.scale(scaleX, scaleY); | 49 canvas.scale(scaleX, scaleY); |
40 canvas.drawImage(_image, 0.0, 0.0, new Paint()..setARGB(255, 255, 255, 255
)); | 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(colorOverlay, TransferMode.srcATopM
ode)); |
| 56 } |
| 57 if (transferMode != null) { |
| 58 paint.setTransferMode(transferMode); |
| 59 } |
| 60 |
| 61 canvas.drawImage(_image, 0.0, 0.0, paint); |
41 canvas.restore(); | 62 canvas.restore(); |
42 } | 63 } |
43 else { | 64 else { |
44 // Paint a red square for missing texture | 65 // Paint a red square for missing texture |
45 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, this.width, this.height), | 66 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, this.width, this.height), |
46 new Paint()..setARGB(255, 255, 0, 0)); | 67 new Paint()..setARGB(255, 255, 0, 0)); |
47 } | 68 } |
48 } | 69 } |
49 | 70 |
50 } | 71 } |
OLD | NEW |