transformationMatrix property Null safety

Matrix transformationMatrix
override

The transformation matrix of this display object relative to this display object's parent.

The transformation matrix is calculated according the following properties of this display object: x, y, pivotX, pivotY, rotation, scaleX, scaleY, skewX and skewY

Implementation

@override
Matrix get transformationMatrix {
  // _transformationMatrix.identity();
  // _transformationMatrix.translate(-_pivotX, -_pivotY);
  // _transformationMatrix.scale(_scaleX, _scaleY);
  // _transformationMatrix.rotate(_rotation);
  // _transformationMatrix.translate(_x, _y);

  if (_transformationMatrixRefresh) {
    _transformationMatrixRefresh = false;

    final matrix = _transformationMatrix;
    final rotation = _rotation;
    var scaleX = _scaleX;
    var scaleY = _scaleY;
    final skewX = _skewX;
    final skewY = _skewY;

    // Having a scale of 0 is bad, the matrix.det gets zero which causes
    // infinite values on a inverted matrix. It also causes a bug on
    // Firefox in some Linux distributions:
    // https://bugzilla.mozilla.org/show_bug.cgi?id=661452

    if (scaleX > -0.0001 && scaleX < 0.0001) {
      scaleX = (scaleX >= 0) ? 0.0001 : -0.0001;
    }
    if (scaleY > -0.0001 && scaleY < 0.0001) {
      scaleY = (scaleY >= 0) ? 0.0001 : -0.0001;
    }

    if (skewX != 0.0 || skewY != 0.0) {
      final ma = scaleX * cos(skewY + rotation);
      final mb = scaleX * sin(skewY + rotation);
      final mc = -scaleY * sin(skewX + rotation);
      final md = scaleY * cos(skewX + rotation);
      final mx = _x - _pivotX * ma - _pivotY * mc;
      final my = _y - _pivotX * mb - _pivotY * md;
      matrix.setTo(ma, mb, mc, md, mx, my);
    } else if (rotation != 0.0) {
      final cr = cos(rotation);
      final sr = sin(rotation);
      final ma = scaleX * cr;
      final mb = scaleX * sr;
      final mc = -scaleY * sr;
      final md = scaleY * cr;
      final mx = _x - _pivotX * ma - _pivotY * mc;
      final my = _y - _pivotX * mb - _pivotY * md;
      matrix.setTo(ma, mb, mc, md, mx, my);
    } else {
      final mx = _x - _pivotX * scaleX;
      final my = _y - _pivotY * scaleY;
      matrix.setTo(scaleX, 0.0, 0.0, scaleY, mx, my);
    }
  }

  return _transformationMatrix;
}