mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Merge pull request #547 from Hixie/matrix-math
Avoid using transforms when simple offsets will do.
This commit is contained in:
commit
94c9b232af
@ -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';
|
||||
|
40
packages/flutter/lib/src/painting/transforms.dart
Normal file
40
packages/flutter/lib/src/painting/transforms.dart
Normal file
@ -0,0 +1,40 @@
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
@ -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) {
|
||||
|
42
packages/unit/test/painting/transform_utilities_test.dart
Normal file
42
packages/unit/test/painting/transform_utilities_test.dart
Normal file
@ -0,0 +1,42 @@
|
||||
// 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)));
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user