Chromium Code Reviews| 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 | |
| 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 } | |
| OLD | NEW |