diff options
author | yoshiki@chromium.org <yoshiki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-29 04:54:14 +0000 |
---|---|---|
committer | yoshiki@chromium.org <yoshiki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-29 04:54:14 +0000 |
commit | 9d4db2fd5bde9ea789077e8e7dee55b6403a949f (patch) | |
tree | c66c065af44d0321b68021e4984afd5e0b3a5a74 /ash/rotator | |
parent | 745cf4a60e364e8e7993857f1198f767d96c603f (diff) | |
download | chromium_src-9d4db2fd5bde9ea789077e8e7dee55b6403a949f.zip chromium_src-9d4db2fd5bde9ea789077e8e7dee55b6403a949f.tar.gz chromium_src-9d4db2fd5bde9ea789077e8e7dee55b6403a949f.tar.bz2 |
Move screen rotation code to ash
BUG=none
TEST=none
Review URL: https://chromiumcodereview.appspot.com/10698007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@144875 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash/rotator')
-rw-r--r-- | ash/rotator/screen_rotation.cc | 126 | ||||
-rw-r--r-- | ash/rotator/screen_rotation.h | 65 |
2 files changed, 191 insertions, 0 deletions
diff --git a/ash/rotator/screen_rotation.cc b/ash/rotator/screen_rotation.cc new file mode 100644 index 0000000..c4c5eeb --- /dev/null +++ b/ash/rotator/screen_rotation.cc @@ -0,0 +1,126 @@ +// Copyright (c) 2012 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 "ash/rotator/screen_rotation.h" + +#include "base/debug/trace_event.h" +#include "base/time.h" +#include "ui/compositor/layer_animation_delegate.h" +#include "ui/gfx/interpolated_transform.h" +#include "ui/gfx/rect.h" +#include "ui/gfx/transform.h" + +namespace ash { + +namespace { + +const int k90DegreeTransitionDurationMs = 350; +const int k180DegreeTransitionDurationMs = 550; +const int k360DegreeTransitionDurationMs = 750; + +base::TimeDelta GetTransitionDuration(int degrees) { + if (degrees == 360) + return base::TimeDelta::FromMilliseconds(k360DegreeTransitionDurationMs); + if (degrees == 180) + return base::TimeDelta::FromMilliseconds(k180DegreeTransitionDurationMs); + if (degrees == 0) + return base::TimeDelta::FromMilliseconds(0); + return base::TimeDelta::FromMilliseconds(k90DegreeTransitionDurationMs); +} + +} // namespace + +ScreenRotation::ScreenRotation(int degrees) + : ui::LayerAnimationElement(GetProperties(), + GetTransitionDuration(degrees)), + degrees_(degrees) { +} + +ScreenRotation::~ScreenRotation() { +} + +void ScreenRotation::OnStart(ui::LayerAnimationDelegate* delegate) { + // No rotation required. + if (degrees_ == 0) + return; + + const ui::Transform& current_transform = + delegate->GetTransformForAnimation(); + const gfx::Rect& bounds = delegate->GetBoundsForAnimation(); + + gfx::Point old_pivot; + gfx::Point new_pivot; + + int width = bounds.width(); + int height = bounds.height(); + + switch (degrees_) { + case 90: + new_origin_ = new_pivot = gfx::Point(width, 0); + break; + case -90: + new_origin_ = new_pivot = gfx::Point(0, height); + break; + case 180: + case 360: + new_pivot = old_pivot = gfx::Point(width / 2, height / 2); + new_origin_.SetPoint(width, height); + break; + } + + // Convert points to world space. + current_transform.TransformPoint(old_pivot); + current_transform.TransformPoint(new_pivot); + current_transform.TransformPoint(new_origin_); + + scoped_ptr<ui::InterpolatedTransform> rotation( + new ui::InterpolatedTransformAboutPivot( + old_pivot, + new ui::InterpolatedRotation(0, degrees_))); + + scoped_ptr<ui::InterpolatedTransform> translation( + new ui::InterpolatedTranslation( + gfx::Point(0, 0), + gfx::Point(new_pivot.x() - old_pivot.x(), + new_pivot.y() - old_pivot.y()))); + + float scale_factor = 0.9f; + scoped_ptr<ui::InterpolatedTransform> scale_down( + new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f)); + + scoped_ptr<ui::InterpolatedTransform> scale_up( + new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f)); + + interpolated_transform_.reset( + new ui::InterpolatedConstantTransform(current_transform)); + + scale_up->SetChild(scale_down.release()); + translation->SetChild(scale_up.release()); + rotation->SetChild(translation.release()); + interpolated_transform_->SetChild(rotation.release()); +} + +bool ScreenRotation::OnProgress(double t, + ui::LayerAnimationDelegate* delegate) { + delegate->SetTransformFromAnimation(interpolated_transform_->Interpolate(t)); + return true; +} + +void ScreenRotation::OnGetTarget(TargetValue* target) const { + target->transform = interpolated_transform_->Interpolate(1.0); +} + +void ScreenRotation::OnAbort() { +} + +// static +const ui::LayerAnimationElement::AnimatableProperties& +ScreenRotation::GetProperties() { + static ui::LayerAnimationElement::AnimatableProperties properties; + if (properties.empty()) + properties.insert(ui::LayerAnimationElement::TRANSFORM); + return properties; +} + +} // namespace ash diff --git a/ash/rotator/screen_rotation.h b/ash/rotator/screen_rotation.h new file mode 100644 index 0000000..7f3c20b --- /dev/null +++ b/ash/rotator/screen_rotation.h @@ -0,0 +1,65 @@ +// Copyright (c) 2012 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. + +#ifndef ASH_SCREEN_ROTATION_H_ +#define ASH_SCREEN_ROTATION_H_ +#pragma once + +#include "ash/ash_export.h" +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "ui/base/animation/animation_delegate.h" +#include "ui/compositor/compositor_export.h" +#include "ui/compositor/layer_animation_element.h" +#include "ui/gfx/point.h" + +namespace ui { +class InterpolatedTransform; +class LayerAnimationDelegate; +} + +namespace aura { +class RootWindow; +} + +namespace ash { + +// A screen rotation represents a single transition from one screen orientation +// to another. The intended usage is that a new instance of the class is +// created for every transition. It is possible to update the target orientation +// in the middle of a transition. +class ASH_EXPORT ScreenRotation : public ui::LayerAnimationElement { + public: + // The screen rotation does not own the view or the listener, and these + // objects are required to outlive the Screen rotation object. + ScreenRotation(int degrees); + virtual ~ScreenRotation(); + + private: + // Implementation of ui::LayerAnimationDelegate + virtual void OnStart(ui::LayerAnimationDelegate* delegate) OVERRIDE; + virtual bool OnProgress(double t, + ui::LayerAnimationDelegate* delegate) OVERRIDE; + virtual void OnGetTarget(TargetValue* target) const OVERRIDE; + virtual void OnAbort() OVERRIDE; + + static const ui::LayerAnimationElement::AnimatableProperties& + GetProperties(); + + // Generates the intermediate transformation matrices used during the + // animation. + scoped_ptr<ui::InterpolatedTransform> interpolated_transform_; + + // The number of degrees to rotate. + int degrees_; + + // The target origin + gfx::Point new_origin_; + + DISALLOW_COPY_AND_ASSIGN(ScreenRotation); +}; + +} // namespace ash + +#endif // ASH_SCREEN_ROTATION_H_ |