| OLD | NEW |
| 1 part of sprites; | 1 part of sprites; |
| 2 | 2 |
| 3 // TODO: Actually draw images | 3 /// A Sprite is a [Node] that renders a bitmap image to the screen. |
| 4 class Sprite extends NodeWithSize { |
| 4 | 5 |
| 5 class Sprite extends NodeWithSize { | 6 /// The image that the sprite will render to screen. |
| 6 | 7 /// |
| 7 Image _image; | 8 /// If the image is null, the sprite will be rendered as a red square |
| 9 /// marking the bounds of the sprite. |
| 10 /// |
| 11 /// mySprite.image = myImage; |
| 12 Image image; |
| 13 |
| 14 /// If true, constrains the proportions of the image by scaling it down, if it
s proportions doesn't match the [size]. |
| 15 /// |
| 16 /// mySprite.constrainProportions = true; |
| 8 bool constrainProportions = false; | 17 bool constrainProportions = false; |
| 9 double _opacity = 1.0; | 18 double _opacity = 1.0; |
| 19 |
| 20 /// The color to draw on top of the sprite, null if no color overlay is used. |
| 21 /// |
| 22 /// // Color the sprite red |
| 23 /// mySprite.colorOverlay = new Color(0x77ff0000); |
| 10 Color colorOverlay; | 24 Color colorOverlay; |
| 25 |
| 26 /// The transfer mode used when drawing the sprite to screen. |
| 27 /// |
| 28 /// // Add the colors of the sprite with the colors of the background |
| 29 /// mySprite.transferMode = TransferMode.plusMode; |
| 11 TransferMode transferMode; | 30 TransferMode transferMode; |
| 12 | 31 |
| 13 Sprite() { | 32 /// Creates a new sprite from the provided [image]. |
| 14 } | 33 /// |
| 15 | 34 /// var mySprite = new Sprite(myImage); |
| 16 Sprite.withImage(Image image) { | 35 Sprite([Image this.image]) { |
| 17 pivot = new Point(0.5, 0.5); | 36 pivot = new Point(0.5, 0.5); |
| 18 size = new Size(image.width.toDouble(), image.height.toDouble()); | 37 if (image != null) { |
| 19 _image = image; | 38 size = new Size(image.width.toDouble(), image.height.toDouble()); |
| 39 } |
| 20 } | 40 } |
| 21 | 41 |
| 42 /// The opacity of the sprite in the range 0.0 to 1.0. |
| 43 /// |
| 44 /// mySprite.opacity = 0.5; |
| 22 double get opacity => _opacity; | 45 double get opacity => _opacity; |
| 23 | 46 |
| 24 void set opacity(double opacity) { | 47 void set opacity(double opacity) { |
| 25 assert(opacity != null); | 48 assert(opacity != null); |
| 26 assert(opacity >= 0.0 && opacity <= 1.0); | 49 assert(opacity >= 0.0 && opacity <= 1.0); |
| 27 _opacity = opacity; | 50 _opacity = opacity; |
| 28 } | 51 } |
| 29 | 52 |
| 30 void paint(PictureRecorder canvas) { | 53 void paint(PictureRecorder canvas) { |
| 31 canvas.save(); | 54 canvas.save(); |
| 32 | 55 |
| 33 // Account for pivot point | 56 // Account for pivot point |
| 34 applyTransformForPivot(canvas); | 57 applyTransformForPivot(canvas); |
| 35 | 58 |
| 36 if (_image != null && _image.width > 0 && _image.height > 0) { | 59 if (image != null && image.width > 0 && image.height > 0) { |
| 37 | 60 |
| 38 double scaleX = size.width/_image.width; | 61 double scaleX = size.width/image.width; |
| 39 double scaleY = size.height/_image.height; | 62 double scaleY = size.height/image.height; |
| 40 | 63 |
| 41 if (constrainProportions) { | 64 if (constrainProportions) { |
| 42 // Constrain proportions, using the smallest scale and by centering the
image | 65 // Constrain proportions, using the smallest scale and by centering the
image |
| 43 if (scaleX < scaleY) { | 66 if (scaleX < scaleY) { |
| 44 canvas.translate(0.0, (size.height - scaleX * _image.height)/2.0); | 67 canvas.translate(0.0, (size.height - scaleX * image.height)/2.0); |
| 45 scaleY = scaleX; | 68 scaleY = scaleX; |
| 46 } | 69 } |
| 47 else { | 70 else { |
| 48 canvas.translate((size.width - scaleY * _image.width)/2.0, 0.0); | 71 canvas.translate((size.width - scaleY * image.width)/2.0, 0.0); |
| 49 scaleX = scaleY; | 72 scaleX = scaleY; |
| 50 } | 73 } |
| 51 } | 74 } |
| 52 | 75 |
| 53 canvas.scale(scaleX, scaleY); | 76 canvas.scale(scaleX, scaleY); |
| 54 | 77 |
| 55 // Setup paint object for opacity and transfer mode | 78 // Setup paint object for opacity and transfer mode |
| 56 Paint paint = new Paint(); | 79 Paint paint = new Paint(); |
| 57 paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255); | 80 paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255); |
| 58 if (colorOverlay != null) { | 81 if (colorOverlay != null) { |
| 59 paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.src
ATop)); | 82 paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.src
ATop)); |
| 60 } | 83 } |
| 61 if (transferMode != null) { | 84 if (transferMode != null) { |
| 62 paint.setTransferMode(transferMode); | 85 paint.setTransferMode(transferMode); |
| 63 } | 86 } |
| 64 | 87 |
| 65 canvas.drawImage(_image, 0.0, 0.0, paint); | 88 canvas.drawImage(image, 0.0, 0.0, paint); |
| 66 } | 89 } |
| 67 else { | 90 else { |
| 68 // Paint a red square for missing texture | 91 // Paint a red square for missing texture |
| 69 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height), | 92 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height), |
| 70 new Paint()..color = const Color.fromARGB(255, 255, 0, 0)); | 93 new Paint()..color = const Color.fromARGB(255, 255, 0, 0)); |
| 71 } | 94 } |
| 72 canvas.restore(); | 95 canvas.restore(); |
| 73 } | 96 } |
| 74 } | 97 } |
| OLD | NEW |