From b9b1e7a4fa49c108c40536cee59ce0b2b0a09d86 Mon Sep 17 00:00:00 2001 From: "sky@chromium.org" Date: Tue, 17 May 2011 15:29:51 +0000 Subject: Makes Transform concrete. Fixes bug in coordinate conversion and makes all conversion routines calculate the transform in the same way. Lastly fixes bug in touch_factory.cc that was causing crashes on my machine when running views_unittests. Oh, and adds some tests of conversion methods. BUG=none TEST=none R=ben@chromium.org,sadrul@chromium.org Review URL: http://codereview.chromium.org/7033002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85635 0039d316-1c4b-4281-b951-d872f2087c98 --- ui/gfx/transform.cc | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 ui/gfx/transform.cc (limited to 'ui/gfx/transform.cc') diff --git a/ui/gfx/transform.cc b/ui/gfx/transform.cc new file mode 100644 index 0000000..c84d199 --- /dev/null +++ b/ui/gfx/transform.cc @@ -0,0 +1,103 @@ +// Copyright (c) 2011 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. + +#include "ui/gfx/transform.h" + +#include "ui/gfx/point.h" +#include "ui/gfx/rect.h" +#include "ui/gfx/skia_util.h" + +namespace ui { + +Transform::Transform() { + matrix_.reset(); +} + +Transform::~Transform() {} + +void Transform::SetRotate(float degree) { + matrix_.setRotate(SkFloatToScalar(degree)); +} + +void Transform::SetScaleX(float x) { + matrix_.setScaleX(SkFloatToScalar(x)); +} + +void Transform::SetScaleY(float y) { + matrix_.setScaleY(SkFloatToScalar(y)); +} + +void Transform::SetScale(float x, float y) { + matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +void Transform::SetTranslateX(float x) { + matrix_.setTranslateX(SkFloatToScalar(x)); +} + +void Transform::SetTranslateY(float y) { + matrix_.setTranslateY(SkFloatToScalar(y)); +} + +void Transform::SetTranslate(float x, float y) { + matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +void Transform::ConcatRotate(float degree) { + matrix_.postRotate(SkFloatToScalar(degree)); +} + +void Transform::ConcatScale(float x, float y) { + matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +void Transform::ConcatTranslate(float x, float y) { + // Temporary workaround for bug in Skia. + ui::Transform t; + t.SetTranslate(x, y); + ConcatTransform(t); + // matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +bool Transform::PreconcatTransform(const Transform& transform) { + return matrix_.setConcat(transform.matrix_, matrix_); +} + +bool Transform::ConcatTransform(const Transform& transform) { + return matrix_.setConcat(matrix_, transform.matrix_); +} + +bool Transform::HasChange() const { + return !matrix_.isIdentity(); +} + +bool Transform::TransformPoint(gfx::Point* point) { + SkPoint skp; + matrix_.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); + point->SetPoint(static_cast(skp.fX), static_cast(skp.fY)); + return true; +} + +bool Transform::TransformPointReverse(gfx::Point* point) { + SkMatrix inverse; + // TODO(sad): Try to avoid trying to invert the matrix. + if (matrix_.invert(&inverse)) { + SkPoint skp; + inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); + point->SetPoint(static_cast(skp.fX), static_cast(skp.fY)); + return true; + } + return false; +} + +bool Transform::TransformRect(gfx::Rect* rect) { + SkRect src = gfx::RectToSkRect(*rect); + if (!matrix_.mapRect(&src)) + return false; + gfx::Rect xrect = gfx::SkRectToRect(src); + rect->SetRect(xrect.x(), xrect.y(), xrect.width(), xrect.height()); + return true; +} + +} // namespace ui -- cgit v1.1