layer.dart 1.55 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
part of flutter_sprites;
Viktor Lidholt's avatar
Viktor Lidholt committed
6

7 8 9 10 11 12
/// 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
13
class Layer extends Node with SpritePaint {
14 15 16 17 18 19

  /// 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);
20 21
  Rect layerRect;

22 23 24 25
  /// 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
26
  Layer([this.layerRect = null]);
27

Viktor Lidholt's avatar
Viktor Lidholt committed
28
  Paint _cachedPaint = new Paint()
29
    ..filterQuality = FilterQuality.low
Viktor Lidholt's avatar
Viktor Lidholt committed
30 31
    ..isAntiAlias = false;

32
  @override
Adam Barth's avatar
Adam Barth committed
33
  void _prePaint(Canvas canvas, Matrix4 matrix) {
Viktor Lidholt's avatar
Viktor Lidholt committed
34 35 36
    super._prePaint(canvas, matrix);

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

40
  @override
Adam Barth's avatar
Adam Barth committed
41
  void _postPaint(Canvas canvas, Matrix4 totalMatrix) {
Viktor Lidholt's avatar
Viktor Lidholt committed
42 43 44 45
    canvas.restore();
    super._postPaint(canvas, totalMatrix);
  }
}