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

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

Issue 1218593002: Move sky/examples to sky/sdk/lib/example, and code changes to support that change. Fixes T277. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 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
(Empty)
1 part of sprites;
2
3 /// A Sprite is a [Node] that renders a bitmap image to the screen.
4 class Sprite extends NodeWithSize {
5
6 /// The texture that the sprite will render to screen.
7 ///
8 /// If the texture is null, the sprite will be rendered as a red square
9 /// marking the bounds of the sprite.
10 ///
11 /// mySprite.texture = myTexture;
12 Texture texture;
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;
17 bool constrainProportions = false;
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);
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;
30 TransferMode transferMode;
31
32 /// Creates a new sprite from the provided [texture].
33 ///
34 /// var mySprite = new Sprite(myTexture)
35 Sprite([this.texture]) {
36 if (texture != null) {
37 size = texture.size;
38 pivot = texture.pivot;
39 } else {
40 pivot = new Point(0.5, 0.5);
41 }
42 }
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
56 /// The opacity of the sprite in the range 0.0 to 1.0.
57 ///
58 /// mySprite.opacity = 0.5;
59 double get opacity => _opacity;
60
61 void set opacity(double opacity) {
62 assert(opacity != null);
63 assert(opacity >= 0.0 && opacity <= 1.0);
64 _opacity = opacity;
65 }
66
67 void paint(RenderCanvas canvas) {
68 canvas.save();
69
70 // Account for pivot point
71 applyTransformForPivot(canvas);
72
73 if (texture != null) {
74 double w = texture.size.width;
75 double h = texture.size.height;
76
77 if (w <= 0 || h <= 0) return;
78
79 double scaleX = size.width / w;
80 double scaleY = size.height / h;
81
82 if (constrainProportions) {
83 // Constrain proportions, using the smallest scale and by centering the image
84 if (scaleX < scaleY) {
85 canvas.translate(0.0, (size.height - scaleX * h) / 2.0);
86 scaleY = scaleX;
87 } else {
88 canvas.translate((size.width - scaleY * w) / 2.0, 0.0);
89 scaleX = scaleY;
90 }
91 }
92
93 canvas.scale(scaleX, scaleY);
94
95 // Setup paint object for opacity and transfer mode
96 Paint paint = new Paint();
97 paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255);
98 if (colorOverlay != null) {
99 paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.src ATop));
100 }
101 if (transferMode != null) {
102 paint.setTransferMode(transferMode);
103 }
104
105 // Do actual drawing of the sprite
106 if (texture.rotated) {
107 // Calculate the rotated frame and spriteSourceSize
108 Size originalFrameSize = texture.frame.size;
109 Rect rotatedFrame = new Rect.fromPointAndSize(texture.frame.upperLeft, n ew Size(originalFrameSize.height, originalFrameSize.width));
110 Point rotatedSpriteSourcePoint = new Point(
111 -texture.spriteSourceSize.top - (texture.spriteSourceSize.bottom - t exture.spriteSourceSize.top),
112 texture.spriteSourceSize.left);
113 Rect rotatedSpriteSourceSize = new Rect.fromPointAndSize(rotatedSpriteSo urcePoint, new Size(originalFrameSize.height, originalFrameSize.width));
114
115 // Draw the rotated sprite
116 canvas.rotate(-Math.PI/2.0);
117 canvas.drawImageRect(texture.image, rotatedFrame, rotatedSpriteSourceSiz e, paint);
118 } else {
119 // Draw the sprite
120 canvas.drawImageRect(texture.image, texture.frame, texture.spriteSourceS ize, paint);
121 }
122 } else {
123 // Paint a red square for missing texture
124 canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height),
125 new Paint()..color = const Color.fromARGB(255, 255, 0, 0));
126 }
127 canvas.restore();
128 }
129 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698