Commit 94c9b232 authored by Ian Hickson's avatar Ian Hickson

Merge pull request #547 from Hixie/matrix-math

Avoid using transforms when simple offsets will do.
parents 355fe033 ebf032b8
......@@ -18,3 +18,4 @@ export 'src/painting/colors.dart';
export 'src/painting/shadows.dart';
export 'src/painting/text_painter.dart';
export 'src/painting/text_style.dart';
export 'src/painting/transforms.dart';
// 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.
import 'dart:typed_data';
import 'package:vector_math/vector_math_64.dart';
import 'basic_types.dart';
class MatrixUtils {
MatrixUtils._();
/// If the given transform is nothing but a 2D translation, then returns that
/// translation as an Offset.
///
/// Otherwise, returns null.
static Offset getAsTranslation(Matrix4 transform) {
Float64List values = transform.storage;
// values are stored in column-major order
if (values[0] == 1.0 &&
values[1] == 0.0 &&
values[2] == 0.0 &&
values[3] == 0.0 &&
values[4] == 0.0 &&
values[5] == 1.0 &&
values[6] == 0.0 &&
values[7] == 0.0 &&
values[8] == 0.0 &&
values[9] == 0.0 &&
values[10] == 1.0 &&
values[11] == 0.0 &&
values[14] == 0.0 &&
values[15] == 1.0) {
return new Offset(values[12], values[13]);
}
return null;
}
}
\ No newline at end of file
......@@ -865,8 +865,14 @@ class RenderTransform extends RenderProxyBox {
}
void paint(PaintingContext context, Offset offset) {
if (child != null)
context.pushTransform(needsCompositing, offset, _effectiveTransform, super.paint);
if (child != null) {
Matrix4 transform = _effectiveTransform;
Offset childOffset = MatrixUtils.getAsTranslation(transform);
if (childOffset == null)
context.pushTransform(needsCompositing, offset, transform, super.paint);
else
super.paint(context, offset + childOffset);
}
}
void applyPaintTransform(Matrix4 transform) {
......
// 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.
import 'package:flutter/painting.dart';
import 'package:test/test.dart';
import 'package:vector_math/vector_math_64.dart';
void main() {
test("MatrixUtils.getAsTranslation()", () {
Matrix4 test;
test = new Matrix4.identity();
expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
test = new Matrix4.zero();
expect(MatrixUtils.getAsTranslation(test), isNull);
test = new Matrix4.rotationX(1.0);
expect(MatrixUtils.getAsTranslation(test), isNull);
test = new Matrix4.rotationZ(1.0);
expect(MatrixUtils.getAsTranslation(test), isNull);
test = new Matrix4.translationValues(1.0, 2.0, 0.0);
expect(MatrixUtils.getAsTranslation(test), equals(const Offset(1.0, 2.0)));
test = new Matrix4.translationValues(1.0, 2.0, 3.0);
expect(MatrixUtils.getAsTranslation(test), isNull);
test = new Matrix4.identity();
expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
test.rotateX(2.0);
expect(MatrixUtils.getAsTranslation(test), isNull);
test = new Matrix4.identity();
expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
test.scale(2.0);
expect(MatrixUtils.getAsTranslation(test), isNull);
test = new Matrix4.identity();
expect(MatrixUtils.getAsTranslation(test), equals(Offset.zero));
test.translate(2.0, -2.0);
expect(MatrixUtils.getAsTranslation(test), equals(const Offset(2.0, -2.0)));
test.translate(4.0, 8.0);
expect(MatrixUtils.getAsTranslation(test), equals(const Offset(6.0, 6.0)));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment