flutter/examples/game/lib/sprite.dart
Viktor Lidholt 6c7a619f99 Updates game for new AppView API
Deletes old test game

Demo game is now playable

Updates demo game with steering and changes in sprites

Fixes smaller bugs in sprites

Refactor class names in game demo

Strips Box2D from game

Fixes ordering in game node

Adds frameRate property to SpriteBox and improves update methods.

Fixes node to box transformations for hit tests

Fixes minor code issues

R=abarth@chromium.org

Review URL: https://codereview.chromium.org/1179333002.
2015-06-15 09:11:16 -07:00

74 lines
1.9 KiB
Dart

part of sprites;
// TODO: Actually draw images
class Sprite extends NodeWithSize {
Image _image;
bool constrainProportions = false;
double _opacity = 1.0;
Color colorOverlay;
TransferMode transferMode;
Sprite() {
}
Sprite.withImage(Image image) {
pivot = new Point(0.5, 0.5);
size = new Size(image.width.toDouble(), image.height.toDouble());
_image = image;
}
double get opacity => _opacity;
void set opacity(double opacity) {
assert(opacity >= 0.0 && opacity <= 1.0);
_opacity = opacity;
}
void paint(PictureRecorder canvas) {
canvas.save();
// Account for pivot point
applyTransformForPivot(canvas);
if (_image != null && _image.width > 0 && _image.height > 0) {
double scaleX = size.width/_image.width;
double scaleY = size.height/_image.height;
if (constrainProportions) {
// Constrain proportions, using the smallest scale and by centering the image
if (scaleX < scaleY) {
canvas.translate(0.0, (size.height - scaleX * _image.height)/2.0);
scaleY = scaleX;
}
else {
canvas.translate((size.width - scaleY * _image.width)/2.0, 0.0);
scaleX = scaleY;
}
}
canvas.scale(scaleX, scaleY);
// Setup paint object for opacity and transfer mode
Paint paint = new Paint();
paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255);
if (colorOverlay != null) {
paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.srcATopMode));
}
if (transferMode != null) {
paint.setTransferMode(transferMode);
}
canvas.drawImage(_image, 0.0, 0.0, paint);
}
else {
// Paint a red square for missing texture
canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height),
new Paint()..color = const Color.fromARGB(255, 255, 0, 0));
}
canvas.restore();
}
}