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

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();
22 23
  Layer([Rect this.layerRect = null]);

Viktor Lidholt's avatar
Viktor Lidholt committed
24 25 26 27 28 29 30 31
  Paint _cachedPaint = new Paint()
    ..setFilterQuality(FilterQuality.low)
    ..isAntiAlias = false;

  void _prePaint(PaintingCanvas canvas, Matrix4 matrix) {
    super._prePaint(canvas, matrix);

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

  void _postPaint(PaintingCanvas canvas, Matrix4 totalMatrix) {
    canvas.restore();
    super._postPaint(canvas, totalMatrix);
  }
}