Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: sky/examples/game/lib/sprite.dart

Issue 1204783003: Adds basic sprite sheet support to sprites (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Adds basic sprite sheet support to sprites (fixed issues) Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 } else {
40 pivot = new Point(0.5, 0.5);
39 } 41 }
40 } 42 }
41 43
44 /// Creates a new sprite from the provided [image].
45 ///
46 /// var mySprite = new Sprite.fromImage(myImage);
47 Sprite.fromImage(Image image) {
48 assert(image != null);
49
50 texture = new Texture(image);
51 size = texture.size;
52
53 pivot = new Point(0.5, 0.5);
54 }
55
42 /// The opacity of the sprite in the range 0.0 to 1.0. 56 /// The opacity of the sprite in the range 0.0 to 1.0.
43 /// 57 ///
44 /// mySprite.opacity = 0.5; 58 /// mySprite.opacity = 0.5;
45 double get opacity => _opacity; 59 double get opacity => _opacity;
46 60
47 void set opacity(double opacity) { 61 void set opacity(double opacity) {
48 assert(opacity != null); 62 assert(opacity != null);
49 assert(opacity >= 0.0 && opacity <= 1.0); 63 assert(opacity >= 0.0 && opacity <= 1.0);
50 _opacity = opacity; 64 _opacity = opacity;
51 } 65 }
52 66
53 void paint(PictureRecorder canvas) { 67 void paint(PictureRecorder canvas) {
54 canvas.save(); 68 canvas.save();
55 69
56 // Account for pivot point 70 // Account for pivot point
57 applyTransformForPivot(canvas); 71 applyTransformForPivot(canvas);
58 72
59 if (image != null && image.width > 0 && image.height > 0) { 73 if (texture != null) {
74 double w = texture.size.width;
75 double h = texture.size.height;
76
77 if (w <= 0 || h <= 0) return;
60 78
61 double scaleX = size.width/image.width; 79 double scaleX = size.width / w;
62 double scaleY = size.height/image.height; 80 double scaleY = size.height / h;
63 81
64 if (constrainProportions) { 82 if (constrainProportions) {
65 // Constrain proportions, using the smallest scale and by centering the image 83 // Constrain proportions, using the smallest scale and by centering the image
66 if (scaleX < scaleY) { 84 if (scaleX < scaleY) {
67 canvas.translate(0.0, (size.height - scaleX * image.height)/2.0); 85 canvas.translate(0.0, (size.height - scaleX * h) / 2.0);
68 scaleY = scaleX; 86 scaleY = scaleX;
69 } 87 } else {
70 else { 88 canvas.translate((size.width - scaleY * w) / 2.0, 0.0);
71 canvas.translate((size.width - scaleY * image.width)/2.0, 0.0);
72 scaleX = scaleY; 89 scaleX = scaleY;
73 } 90 }
74 } 91 }
75 92
76 canvas.scale(scaleX, scaleY); 93 canvas.scale(scaleX, scaleY);
77 94
78 // Setup paint object for opacity and transfer mode 95 // Setup paint object for opacity and transfer mode
79 Paint paint = new Paint(); 96 Paint paint = new Paint();
80 paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255); 97 paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255);
81 if (colorOverlay != null) { 98 if (colorOverlay != null) {
82 paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.src ATop)); 99 paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.src ATop));
83 } 100 }
84 if (transferMode != null) { 101 if (transferMode != null) {
85 paint.setTransferMode(transferMode); 102 paint.setTransferMode(transferMode);
86 } 103 }
87 104
88 canvas.drawImage(image, 0.0, 0.0, paint); 105 // Do actual drawing of the sprite
89 } 106 canvas.drawImageRect(texture.image, texture.frame, texture.spriteSourceSiz e, paint);
90 else { 107 } else {
91 // Paint a red square for missing texture 108 // Paint a red square for missing texture
92 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height), 109 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height),
93 new Paint()..color = const Color.fromARGB(255, 255, 0, 0)); 110 new Paint()..color = const Color.fromARGB(255, 255, 0, 0));
94 } 111 }
95 canvas.restore(); 112 canvas.restore();
96 } 113 }
97 } 114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698