OLD | NEW |
| (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 } | |
OLD | NEW |