| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" | 5 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 11 #include "chrome/browser/android/vr_shell/animation.h" | 12 #include "cc/animation/animation_player.h" |
| 12 #include "chrome/browser/android/vr_shell/easing.h" | |
| 13 | 13 |
| 14 namespace vr_shell { | 14 namespace vr_shell { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 bool GetRayPlaneDistance(const gfx::Point3F& ray_origin, | 18 bool GetRayPlaneDistance(const gfx::Point3F& ray_origin, |
| 19 const gfx::Vector3dF& ray_vector, | 19 const gfx::Vector3dF& ray_vector, |
| 20 const gfx::Point3F& plane_origin, | 20 const gfx::Point3F& plane_origin, |
| 21 const gfx::Vector3dF& plane_normal, | 21 const gfx::Vector3dF& plane_normal, |
| 22 float* distance) { | 22 float* distance) { |
| 23 float denom = gfx::DotProduct(ray_vector, plane_normal); | 23 float denom = gfx::DotProduct(ray_vector, plane_normal); |
| 24 if (denom == 0) { | 24 if (denom == 0) { |
| 25 return false; | 25 return false; |
| 26 } | 26 } |
| 27 gfx::Vector3dF rel = ray_origin - plane_origin; | 27 gfx::Vector3dF rel = ray_origin - plane_origin; |
| 28 *distance = -gfx::DotProduct(plane_normal, rel) / denom; | 28 *distance = -gfx::DotProduct(plane_normal, rel) / denom; |
| 29 return true; | 29 return true; |
| 30 } | 30 } |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 gfx::Point3F WorldRectangle::GetCenter() const { | 34 UiElement::UiElement(int id) |
| 35 gfx::Point3F center; | 35 : id_(id), animation_player_(cc::AnimationPlayer::Create(id)) { |
| 36 transform_.TransformPoint(¢er); | 36 animation_player_->AddObserver(this); |
| 37 return center; | |
| 38 } | 37 } |
| 39 | 38 |
| 40 gfx::PointF WorldRectangle::GetUnitRectangleCoordinates( | 39 UiElement::~UiElement() { |
| 41 const gfx::Point3F& world_point) const { | 40 animation_player_->RemoveObserver(this); |
| 42 // TODO(acondor): Simplify the math in this function. | |
| 43 gfx::Point3F origin(0, 0, 0); | |
| 44 gfx::Vector3dF x_axis(1, 0, 0); | |
| 45 gfx::Vector3dF y_axis(0, 1, 0); | |
| 46 transform_.TransformPoint(&origin); | |
| 47 transform_.TransformVector(&x_axis); | |
| 48 transform_.TransformVector(&y_axis); | |
| 49 gfx::Vector3dF origin_to_world = world_point - origin; | |
| 50 float x = gfx::DotProduct(origin_to_world, x_axis) / | |
| 51 gfx::DotProduct(x_axis, x_axis); | |
| 52 float y = gfx::DotProduct(origin_to_world, y_axis) / | |
| 53 gfx::DotProduct(y_axis, y_axis); | |
| 54 return gfx::PointF(x, y); | |
| 55 } | 41 } |
| 56 | 42 |
| 57 gfx::Vector3dF WorldRectangle::GetNormal() const { | |
| 58 gfx::Vector3dF x_axis(1, 0, 0); | |
| 59 gfx::Vector3dF y_axis(0, 1, 0); | |
| 60 transform_.TransformVector(&x_axis); | |
| 61 transform_.TransformVector(&y_axis); | |
| 62 gfx::Vector3dF normal = CrossProduct(y_axis, x_axis); | |
| 63 normal.GetNormalized(&normal); | |
| 64 return normal; | |
| 65 } | |
| 66 | |
| 67 bool WorldRectangle::GetRayDistance(const gfx::Point3F& ray_origin, | |
| 68 const gfx::Vector3dF& ray_vector, | |
| 69 float* distance) const { | |
| 70 return GetRayPlaneDistance(ray_origin, ray_vector, GetCenter(), GetNormal(), | |
| 71 distance); | |
| 72 } | |
| 73 | |
| 74 UiElement::UiElement() : rotation_(gfx::Vector3dF(1, 0, 0), 0) {} | |
| 75 | |
| 76 UiElement::~UiElement() = default; | |
| 77 | |
| 78 void UiElement::Render(UiElementRenderer* renderer, | 43 void UiElement::Render(UiElementRenderer* renderer, |
| 79 gfx::Transform view_proj_matrix) const { | 44 gfx::Transform view_proj_matrix) const { |
| 80 NOTREACHED(); | 45 NOTREACHED(); |
| 81 } | 46 } |
| 82 | 47 |
| 83 void UiElement::Initialize() {} | 48 void UiElement::Initialize() {} |
| 84 | 49 |
| 85 void UiElement::OnHoverEnter(const gfx::PointF& position) {} | 50 void UiElement::OnHoverEnter(const gfx::PointF& position) {} |
| 86 | 51 |
| 87 void UiElement::OnHoverLeave() {} | 52 void UiElement::OnHoverLeave() {} |
| 88 | 53 |
| 89 void UiElement::OnMove(const gfx::PointF& position) {} | 54 void UiElement::OnMove(const gfx::PointF& position) {} |
| 90 | 55 |
| 91 void UiElement::OnButtonDown(const gfx::PointF& position) {} | 56 void UiElement::OnButtonDown(const gfx::PointF& position) {} |
| 92 | 57 |
| 93 void UiElement::OnButtonUp(const gfx::PointF& position) {} | 58 void UiElement::OnButtonUp(const gfx::PointF& position) {} |
| 94 | 59 |
| 95 void UiElement::OnBeginFrame(const base::TimeTicks& begin_frame_time) {} | 60 void UiElement::OnBeginFrame(const base::TimeTicks& begin_frame_time) {} |
| 96 | 61 |
| 97 void UiElement::Animate(const base::TimeTicks& time) { | 62 void UiElement::Animate(const base::TimeTicks& time) { |
| 98 for (auto& it : animations_) { | 63 animation_player_->Tick(time); |
| 99 Animation& animation = *it; | 64 animation_player_->UpdateState(true, nullptr); |
| 100 if (time < animation.start) | |
| 101 continue; | |
| 102 | |
| 103 // If |from| is not specified, start at the current values. | |
| 104 if (animation.from.size() == 0) { | |
| 105 switch (animation.property) { | |
| 106 case Animation::SIZE: | |
| 107 animation.from.push_back(size_.x()); | |
| 108 animation.from.push_back(size_.y()); | |
| 109 break; | |
| 110 case Animation::SCALE: | |
| 111 animation.from.push_back(scale_.x()); | |
| 112 animation.from.push_back(scale_.y()); | |
| 113 animation.from.push_back(scale_.z()); | |
| 114 break; | |
| 115 case Animation::ROTATION: | |
| 116 animation.from.push_back(rotation_.x()); | |
| 117 animation.from.push_back(rotation_.y()); | |
| 118 animation.from.push_back(rotation_.z()); | |
| 119 animation.from.push_back(rotation_.w()); | |
| 120 break; | |
| 121 case Animation::TRANSLATION: | |
| 122 animation.from.push_back(translation_.x()); | |
| 123 animation.from.push_back(translation_.y()); | |
| 124 animation.from.push_back(translation_.z()); | |
| 125 break; | |
| 126 case Animation::OPACITY: | |
| 127 animation.from.push_back(opacity_); | |
| 128 break; | |
| 129 } | |
| 130 } | |
| 131 CHECK_EQ(animation.from.size(), animation.to.size()); | |
| 132 | |
| 133 std::vector<float> values(animation.from.size()); | |
| 134 for (std::size_t i = 0; i < animation.from.size(); ++i) { | |
| 135 if (animation.to[i] == animation.from[i] || | |
| 136 time >= (animation.start + animation.duration)) { | |
| 137 values[i] = animation.to[i]; | |
| 138 continue; | |
| 139 } | |
| 140 double value = animation.easing->CalculateValue( | |
| 141 (time - animation.start).InMillisecondsF() / | |
| 142 animation.duration.InMillisecondsF()); | |
| 143 values[i] = | |
| 144 animation.from[i] + (value * (animation.to[i] - animation.from[i])); | |
| 145 } | |
| 146 switch (animation.property) { | |
| 147 case Animation::SIZE: | |
| 148 CHECK_EQ(animation.from.size(), 2u); | |
| 149 size_.set_x(values[0]); | |
| 150 size_.set_y(values[1]); | |
| 151 break; | |
| 152 case Animation::SCALE: | |
| 153 CHECK_EQ(animation.from.size(), 3u); | |
| 154 scale_ = {values[0], values[1], values[2]}; | |
| 155 break; | |
| 156 case Animation::ROTATION: | |
| 157 CHECK_EQ(animation.from.size(), 4u); | |
| 158 rotation_.set_x(values[0]); | |
| 159 rotation_.set_y(values[1]); | |
| 160 rotation_.set_z(values[2]); | |
| 161 rotation_.set_w(values[3]); | |
| 162 break; | |
| 163 case Animation::TRANSLATION: | |
| 164 CHECK_EQ(animation.from.size(), 3u); | |
| 165 translation_ = {values[0], values[1], values[2]}; | |
| 166 break; | |
| 167 case Animation::OPACITY: | |
| 168 CHECK_EQ(animation.from.size(), 1u); | |
| 169 opacity_ = values[0]; | |
| 170 break; | |
| 171 } | |
| 172 } | |
| 173 for (auto it = animations_.begin(); it != animations_.end();) { | |
| 174 const Animation& animation = **it; | |
| 175 if (time >= (animation.start + animation.duration)) { | |
| 176 it = animations_.erase(it); | |
| 177 } else { | |
| 178 ++it; | |
| 179 } | |
| 180 } | |
| 181 } | 65 } |
| 182 | 66 |
| 183 bool UiElement::IsVisible() const { | 67 bool UiElement::IsVisible() const { |
| 184 return visible_ && computed_opacity_ > 0.0f; | 68 return visible_ && computed_opacity_ > 0.0f; |
| 185 } | 69 } |
| 186 | 70 |
| 187 bool UiElement::IsHitTestable() const { | 71 bool UiElement::IsHitTestable() const { |
| 188 return IsVisible() && hit_testable_; | 72 return IsVisible() && hit_testable_; |
| 189 } | 73 } |
| 190 | 74 |
| 191 void UiElement::SetEnabled(bool enabled) { | 75 void UiElement::SetEnabled(bool enabled) { |
| 192 visible_ = enabled; | 76 visible_ = enabled; |
| 193 } | 77 } |
| 194 | 78 |
| 195 bool UiElement::HitTest(const gfx::PointF& point) const { | 79 bool UiElement::HitTest(const gfx::PointF& point) const { |
| 196 return point.x() >= 0.0f && point.x() <= 1.0f && point.y() >= 0.0f && | 80 return point.x() >= 0.0f && point.x() <= 1.0f && point.y() >= 0.0f && |
| 197 point.y() <= 1.0f; | 81 point.y() <= 1.0f; |
| 198 } | 82 } |
| 199 | 83 |
| 200 void UiElement::SetMode(ColorScheme::Mode mode) { | 84 void UiElement::SetMode(ColorScheme::Mode mode) { |
| 201 if (mode_ == mode) | 85 if (mode_ == mode) |
| 202 return; | 86 return; |
| 203 mode_ = mode; | 87 mode_ = mode; |
| 204 OnSetMode(); | 88 OnSetMode(); |
| 205 } | 89 } |
| 206 | 90 |
| 91 gfx::Point3F UiElement::GetCenter() const { |
| 92 gfx::Point3F center; |
| 93 screen_space_transform_.TransformPoint(¢er); |
| 94 return center; |
| 95 } |
| 96 |
| 97 gfx::PointF UiElement::GetUnitRectangleCoordinates( |
| 98 const gfx::Point3F& world_point) const { |
| 99 // TODO(acondor): Simplify the math in this function. |
| 100 gfx::Point3F origin(0, 0, 0); |
| 101 gfx::Vector3dF x_axis(1, 0, 0); |
| 102 gfx::Vector3dF y_axis(0, 1, 0); |
| 103 screen_space_transform_.TransformPoint(&origin); |
| 104 screen_space_transform_.TransformVector(&x_axis); |
| 105 screen_space_transform_.TransformVector(&y_axis); |
| 106 gfx::Vector3dF origin_to_world = world_point - origin; |
| 107 float x = gfx::DotProduct(origin_to_world, x_axis) / |
| 108 gfx::DotProduct(x_axis, x_axis); |
| 109 float y = gfx::DotProduct(origin_to_world, y_axis) / |
| 110 gfx::DotProduct(y_axis, y_axis); |
| 111 return gfx::PointF(x, y); |
| 112 } |
| 113 |
| 114 gfx::Vector3dF UiElement::GetNormal() const { |
| 115 gfx::Vector3dF x_axis(1, 0, 0); |
| 116 gfx::Vector3dF y_axis(0, 1, 0); |
| 117 screen_space_transform_.TransformVector(&x_axis); |
| 118 screen_space_transform_.TransformVector(&y_axis); |
| 119 gfx::Vector3dF normal = CrossProduct(y_axis, x_axis); |
| 120 normal.GetNormalized(&normal); |
| 121 return normal; |
| 122 } |
| 123 |
| 124 bool UiElement::GetRayDistance(const gfx::Point3F& ray_origin, |
| 125 const gfx::Vector3dF& ray_vector, |
| 126 float* distance) const { |
| 127 return GetRayPlaneDistance(ray_origin, ray_vector, GetCenter(), GetNormal(), |
| 128 distance); |
| 129 } |
| 130 |
| 207 void UiElement::OnSetMode() {} | 131 void UiElement::OnSetMode() {} |
| 208 | 132 |
| 133 void UiElement::NotifyClientOpacityAnimated(float opacity, |
| 134 bool notify_active_elements, |
| 135 bool notify_pending_elements) { |
| 136 opacity_ = opacity; |
| 137 } |
| 138 |
| 139 void UiElement::NotifyClientTransformOperationsAnimated( |
| 140 const cc::TransformOperations& operations, |
| 141 bool notify_active_elements, |
| 142 bool notify_pending_elements) { |
| 143 transform_operations_ = operations; |
| 144 } |
| 145 |
| 146 void UiElement::NotifyClientBoundsAnimated(const gfx::SizeF& size, |
| 147 bool notify_active_elements, |
| 148 bool notify_pending_elements) { |
| 149 set_size({size.width(), size.height(), 1}); |
| 150 } |
| 151 |
| 209 } // namespace vr_shell | 152 } // namespace vr_shell |
| OLD | NEW |