layer.dart 1.39 KB
Newer Older
1
part of flutter_sprites;
Viktor Lidholt's avatar
Viktor Lidholt committed
2

3 4 5 6 7 8
/// A [Node] that provides an intermediate rendering surface in the sprite
/// rendering tree. A [Layer] can be used to change the opacity, color, or to
/// apply an effect to a set of nodes. All nodes that are children to the
/// [Layer] will be rendered into the surface. If the area that is needed for
/// the children to be drawn is know, the [layerRect] property should be set as
/// this can enhance performance.
Viktor Lidholt's avatar
Viktor Lidholt committed
9
class Layer extends Node with SpritePaint {
10 11 12 13 14 15

  /// The area that the children of the [Layer] will occupy. This value is
  /// treated as a hint to the rendering system and may in some cases be
  /// ignored. If the area isn't known, the layerRect can be set to [null].
  ///
  ///     myLayer.layerRect = new Rect.fromLTRB(0.0, 0.0, 200.0, 100.0);
16 17
  Rect layerRect;

18 19 20 21
  /// Creates a new layer. The layerRect can optionally be passed as an argument
  /// if it is known.
  ///
  ///     var myLayer = new Layer();
Hixie's avatar
Hixie committed
22
  Layer([this.layerRect = null]);
23

Viktor Lidholt's avatar
Viktor Lidholt committed
24
  Paint _cachedPaint = new Paint()
25
    ..filterQuality = FilterQuality.low
Viktor Lidholt's avatar
Viktor Lidholt committed
26 27
    ..isAntiAlias = false;

28
  @override
Adam Barth's avatar
Adam Barth committed
29
  void _prePaint(Canvas canvas, Matrix4 matrix) {
Viktor Lidholt's avatar
Viktor Lidholt committed
30 31 32
    super._prePaint(canvas, matrix);

    _updatePaint(_cachedPaint);
33
    canvas.saveLayer(layerRect, _cachedPaint);
Viktor Lidholt's avatar
Viktor Lidholt committed
34 35
  }

36
  @override
Adam Barth's avatar
Adam Barth committed
37
  void _postPaint(Canvas canvas, Matrix4 totalMatrix) {
Viktor Lidholt's avatar
Viktor Lidholt committed
38 39 40 41
    canvas.restore();
    super._postPaint(canvas, totalMatrix);
  }
}