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)); |
+ } |
+ } |
+ |
+} |