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

Unified Diff: sky/examples/game/lib/sprite_node.dart

Issue 1161023006: Adds basic support for images in game example (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/examples/game/lib/game_world.dart ('k') | sky/examples/game/lib/sprites.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/examples/game/lib/sprite_node.dart
diff --git a/sky/examples/game/lib/sprite_node.dart b/sky/examples/game/lib/sprite_node.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2f802ca22867fbf7fcf213f9de318d2c5701649a
--- /dev/null
+++ b/sky/examples/game/lib/sprite_node.dart
@@ -0,0 +1,50 @@
+part of sprites;
+
+// TODO: Actually draw images
+
+class SpriteNode extends TransformNode {
+
+ Image _image;
+ bool constrainProportions = false;
+
+ SpriteNode() : super() {
abarth-chromium 2015/06/01 21:58:55 The call to super() is implied. You don't need to
+ this.pivot = new Vector2(0.5, 0.5);
+ }
+
+ SpriteNode.withImage(Image image) : super() {
+ this.pivot = new Vector2(0.5, 0.5);
+ _image = image;
+ }
+
+ void paint(PictureRecorder canvas) {
+
+ if (_image != null && _image.width > 0 && _image.height > 0) {
+ canvas.save();
+
+ double scaleX = _width/_image.width;
+ double scaleY = _height/_image.height;
+
+ if (constrainProportions) {
+ // Constrain proportions, using the smallest scale and by centering the image
+ if (scaleX < scaleY) {
+ canvas.translate(0.0, (_height - scaleX * _image.height)/2.0);
+ scaleY = scaleX;
+ }
+ else {
+ canvas.translate((_width - scaleY * _image.width)/2.0, 0.0);
+ scaleX = scaleY;
+ }
+ }
+
+ canvas.scale(scaleX, scaleY);
+ canvas.drawImage(_image, 0.0, 0.0, new Paint()..setARGB(255, 255, 255, 255));
+ canvas.restore();
+ }
+ else {
+ // Paint a red square for missing texture
+ canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, this.width, this.height),
+ new Paint()..setARGB(255, 255, 0, 0));
+ }
+ }
+
+}
« no previous file with comments | « sky/examples/game/lib/game_world.dart ('k') | sky/examples/game/lib/sprites.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698