OLD | NEW |
---|---|
1 part of sprites; | 1 part of sprites; |
2 | 2 |
3 /// A Sprite is a [Node] that renders a bitmap image to the screen. | 3 /// A Sprite is a [Node] that renders a bitmap image to the screen. |
4 class Sprite extends NodeWithSize { | 4 class Sprite extends NodeWithSize { |
5 | 5 |
6 /// The image that the sprite will render to screen. | 6 /// The texture that the sprite will render to screen. |
7 /// | 7 /// |
8 /// If the image is null, the sprite will be rendered as a red square | 8 /// If the texture is null, the sprite will be rendered as a red square |
9 /// marking the bounds of the sprite. | 9 /// marking the bounds of the sprite. |
10 /// | 10 /// |
11 /// mySprite.image = myImage; | 11 /// mySprite.texture = myTexture; |
12 Image image; | 12 Texture texture; |
13 | 13 |
14 /// If true, constrains the proportions of the image by scaling it down, if it s proportions doesn't match the [size]. | 14 /// If true, constrains the proportions of the image by scaling it down, if it s proportions doesn't match the [size]. |
15 /// | 15 /// |
16 /// mySprite.constrainProportions = true; | 16 /// mySprite.constrainProportions = true; |
17 bool constrainProportions = false; | 17 bool constrainProportions = false; |
18 double _opacity = 1.0; | 18 double _opacity = 1.0; |
19 | 19 |
20 /// The color to draw on top of the sprite, null if no color overlay is used. | 20 /// The color to draw on top of the sprite, null if no color overlay is used. |
21 /// | 21 /// |
22 /// // Color the sprite red | 22 /// // Color the sprite red |
23 /// mySprite.colorOverlay = new Color(0x77ff0000); | 23 /// mySprite.colorOverlay = new Color(0x77ff0000); |
24 Color colorOverlay; | 24 Color colorOverlay; |
25 | 25 |
26 /// The transfer mode used when drawing the sprite to screen. | 26 /// The transfer mode used when drawing the sprite to screen. |
27 /// | 27 /// |
28 /// // Add the colors of the sprite with the colors of the background | 28 /// // Add the colors of the sprite with the colors of the background |
29 /// mySprite.transferMode = TransferMode.plusMode; | 29 /// mySprite.transferMode = TransferMode.plusMode; |
30 TransferMode transferMode; | 30 TransferMode transferMode; |
31 | 31 |
32 /// Creates a new sprite from the provided [image]. | 32 /// Creates a new sprite from the provided [texture]. |
33 /// | 33 /// |
34 /// var mySprite = new Sprite(myImage); | 34 /// var mySprite = new Sprite(myTexture) |
35 Sprite([Image this.image]) { | 35 Sprite([this.texture]) { |
36 pivot = new Point(0.5, 0.5); | 36 if (texture != null) { |
37 if (image != null) { | 37 size = texture.size; |
38 size = new Size(image.width.toDouble(), image.height.toDouble()); | 38 pivot = texture._pivot; |
39 } | |
40 else { | |
jackson
2015/06/23 21:56:54
I would move this onto the previous line
| |
41 pivot = new Point(0.5, 0.5); | |
39 } | 42 } |
40 } | 43 } |
41 | 44 |
45 /// Creates a new sprite from the provided [image]. | |
46 /// | |
47 /// var mySprite = new Sprite.fromImage(myImage); | |
48 Sprite.fromImage(Image image) { | |
49 assert(image != null); | |
50 | |
51 texture = new Texture(image); | |
52 size = texture.size; | |
53 | |
54 pivot = new Point(0.5, 0.5); | |
55 } | |
56 | |
42 /// The opacity of the sprite in the range 0.0 to 1.0. | 57 /// The opacity of the sprite in the range 0.0 to 1.0. |
43 /// | 58 /// |
44 /// mySprite.opacity = 0.5; | 59 /// mySprite.opacity = 0.5; |
45 double get opacity => _opacity; | 60 double get opacity => _opacity; |
46 | 61 |
47 void set opacity(double opacity) { | 62 void set opacity(double opacity) { |
48 assert(opacity != null); | 63 assert(opacity != null); |
49 assert(opacity >= 0.0 && opacity <= 1.0); | 64 assert(opacity >= 0.0 && opacity <= 1.0); |
50 _opacity = opacity; | 65 _opacity = opacity; |
51 } | 66 } |
52 | 67 |
53 void paint(PictureRecorder canvas) { | 68 void paint(PictureRecorder canvas) { |
54 canvas.save(); | 69 canvas.save(); |
55 | 70 |
56 // Account for pivot point | 71 // Account for pivot point |
57 applyTransformForPivot(canvas); | 72 applyTransformForPivot(canvas); |
58 | 73 |
59 if (image != null && image.width > 0 && image.height > 0) { | 74 if (texture != null) { |
75 double w = texture.size.width; | |
76 double h = texture.size.height; | |
77 | |
78 if (w <= 0 || h <= 0) return; | |
jackson
2015/06/23 21:56:54
I would move return into the next line
| |
60 | 79 |
61 double scaleX = size.width/image.width; | 80 double scaleX = size.width/w; |
jackson
2015/06/23 21:56:54
I would put a space around binary operators like /
| |
62 double scaleY = size.height/image.height; | 81 double scaleY = size.height/h; |
jackson
2015/06/23 21:56:54
ditto
| |
63 | 82 |
64 if (constrainProportions) { | 83 if (constrainProportions) { |
65 // Constrain proportions, using the smallest scale and by centering the image | 84 // Constrain proportions, using the smallest scale and by centering the image |
66 if (scaleX < scaleY) { | 85 if (scaleX < scaleY) { |
67 canvas.translate(0.0, (size.height - scaleX * image.height)/2.0); | 86 canvas.translate(0.0, (size.height - scaleX * h)/2.0); |
jackson
2015/06/23 21:56:54
ditto
| |
68 scaleY = scaleX; | 87 scaleY = scaleX; |
69 } | 88 } |
70 else { | 89 else { |
71 canvas.translate((size.width - scaleY * image.width)/2.0, 0.0); | 90 canvas.translate((size.width - scaleY * w)/2.0, 0.0); |
jackson
2015/06/23 21:56:54
ditto
| |
72 scaleX = scaleY; | 91 scaleX = scaleY; |
73 } | 92 } |
74 } | 93 } |
75 | 94 |
76 canvas.scale(scaleX, scaleY); | 95 canvas.scale(scaleX, scaleY); |
77 | 96 |
78 // Setup paint object for opacity and transfer mode | 97 // Setup paint object for opacity and transfer mode |
79 Paint paint = new Paint(); | 98 Paint paint = new Paint(); |
80 paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255); | 99 paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255); |
81 if (colorOverlay != null) { | 100 if (colorOverlay != null) { |
82 paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.src ATop)); | 101 paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.src ATop)); |
83 } | 102 } |
84 if (transferMode != null) { | 103 if (transferMode != null) { |
85 paint.setTransferMode(transferMode); | 104 paint.setTransferMode(transferMode); |
86 } | 105 } |
87 | 106 |
88 canvas.drawImage(image, 0.0, 0.0, paint); | 107 // Do actual drawing of the sprite |
108 canvas.drawImageRect(texture.image, texture._frame, texture._spriteSourceS ize /*new Rect.fromLTRB(0.0, 0.0, w, h)*/, paint); | |
jackson
2015/06/23 21:56:54
Did you mean to leave this comment in here? We don
| |
89 } | 109 } |
90 else { | 110 else { |
91 // Paint a red square for missing texture | 111 // Paint a red square for missing texture |
92 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height), | 112 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height), |
93 new Paint()..color = const Color.fromARGB(255, 255, 0, 0)); | 113 new Paint()..color = const Color.fromARGB(255, 255, 0, 0)); |
94 } | 114 } |
95 canvas.restore(); | 115 canvas.restore(); |
96 } | 116 } |
97 } | 117 } |
OLD | NEW |