1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/vr/elements/resizer.h"
6 
7 #include "base/numerics/math_constants.h"
8 #include "base/numerics/ranges.h"
9 #include "chrome/browser/vr/pose_util.h"
10 #include "chrome/browser/vr/ui_scene_constants.h"
11 #include "ui/gfx/animation/tween.h"
12 #include "ui/gfx/geometry/angle_conversions.h"
13 #include "ui/gfx/geometry/quaternion.h"
14 
15 namespace vr {
16 
17 namespace {
18 
19 // Fraction here is in reference to "time fraction" terminology in web
20 // animations.
21 constexpr float kDefaultFraction = 0.5f;
22 
23 static_assert(0.5f * (kMinResizerScale + kMaxResizerScale) == 1.0f,
24               "1.0 must be the midpoint of the min and max scale");
25 
26 }  // namespace
27 
Resizer()28 Resizer::Resizer() : t_(kDefaultFraction), initial_t_(kDefaultFraction) {
29   set_bounds_contain_children(true);
30 }
31 
32 Resizer::~Resizer() = default;
33 
LocalTransform() const34 gfx::Transform Resizer::LocalTransform() const {
35   return transform_;
36 }
37 
GetTargetLocalTransform() const38 gfx::Transform Resizer::GetTargetLocalTransform() const {
39   return transform_;
40 }
41 
SetTouchingTouchpad(bool touching)42 void Resizer::SetTouchingTouchpad(bool touching) {
43   if (enabled_ && touching) {
44     initial_t_ = t_;
45     initial_touchpad_position_ = touchpad_position_;
46   }
47 }
48 
SetEnabled(bool enabled)49 void Resizer::SetEnabled(bool enabled) {
50   enabled_ = enabled;
51   if (enabled) {
52     initial_t_ = t_;
53     initial_touchpad_position_ = touchpad_position_;
54   }
55 }
56 
Reset()57 void Resizer::Reset() {
58   transform_.MakeIdentity();
59   set_world_space_transform_dirty();
60   t_ = initial_t_ = kDefaultFraction;
61 }
62 
UpdateTransform(const gfx::Transform & head_pose)63 void Resizer::UpdateTransform(const gfx::Transform& head_pose) {
64   float delta = touchpad_position_.y() - initial_touchpad_position_.y();
65   t_ = base::ClampToRange(initial_t_ + delta, 0.0f, 1.0f);
66   float scale =
67       gfx::Tween::FloatValueBetween(t_, kMinResizerScale, kMaxResizerScale);
68   transform_.MakeIdentity();
69   transform_.Scale(scale, scale);
70   set_world_space_transform_dirty();
71 }
72 
OnBeginFrame(const gfx::Transform & head_pose)73 bool Resizer::OnBeginFrame(const gfx::Transform& head_pose) {
74   if (enabled_) {
75     UpdateTransform(head_pose);
76     return true;
77   }
78   return false;
79 }
80 
81 #ifndef NDEBUG
DumpGeometry(std::ostringstream * os) const82 void Resizer::DumpGeometry(std::ostringstream* os) const {
83   gfx::Transform t = LocalTransform();
84   gfx::Vector3dF right = {1, 0, 0};
85   t.TransformVector(&right);
86   *os << "s(" << right.x() << ") ";
87 }
88 #endif
89 
ShouldUpdateWorldSpaceTransform(bool parent_transform_changed) const90 bool Resizer::ShouldUpdateWorldSpaceTransform(
91     bool parent_transform_changed) const {
92   return true;
93 }
94 
95 }  // namespace vr
96