Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "content/browser/android/edge_effect_l.h" | |
| 6 | |
| 7 #include "cc/layers/ui_resource_layer.h" | |
| 8 #include "ui/base/android/system_ui_resource_manager.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // Time it will take the effect to fully recede in ms | |
| 15 const int kRecedeTime = 1000; | |
|
aelias_OOO_until_Jul13
2014/08/14 22:54:19
Rename to kRecedeTimeMs
jdduke (slow)
2014/08/15 19:15:06
Done.
| |
| 16 | |
| 17 // Time it will take before a pulled glow begins receding in ms | |
| 18 const int kPullTime = 167; | |
|
aelias_OOO_until_Jul13
2014/08/14 22:54:19
Rename to kPullTimeMs
jdduke (slow)
2014/08/15 19:15:06
Done.
| |
| 19 | |
| 20 const float kMaxAlpha = 1.f; | |
| 21 | |
| 22 const float kPullGlowBegin = 0.f; | |
| 23 | |
| 24 // Min/max velocity that will be absorbed | |
| 25 const float kMinVelocity = 100.f; | |
| 26 const float kMaxVelocity = 10000.f; | |
| 27 | |
| 28 const float kEpsilon = 0.001f; | |
| 29 | |
| 30 const float kSin = 0.5f; // sin(PI / 6) | |
| 31 const float kCos = 0.866f; // cos(PI / 6); | |
| 32 | |
| 33 // How much dragging should effect the height of the glow image. | |
| 34 // Number determined by user testing. | |
| 35 const float kPullDistanceAlphaGlowFactor = 1.1f; | |
| 36 | |
| 37 const int kVelocityGlowFactor = 12; | |
| 38 | |
| 39 const ui::SystemUIResourceManager::ResourceType kResourceType = | |
| 40 ui::SystemUIResourceManager::OVERSCROLL_GLOW_L; | |
| 41 | |
| 42 template <typename T> | |
| 43 T Lerp(T a, T b, T t) { | |
| 44 return a + (b - a) * t; | |
| 45 } | |
| 46 | |
| 47 template <typename T> | |
| 48 T Clamp(T value, T low, T high) { | |
| 49 return value < low ? low : (value > high ? high : value); | |
| 50 } | |
| 51 | |
| 52 template <typename T> | |
| 53 T Damp(T input, T factor) { | |
| 54 T result; | |
| 55 if (factor == 1) { | |
| 56 result = 1 - (1 - input) * (1 - input); | |
| 57 } else { | |
| 58 result = 1 - std::pow(1 - input, 2 * factor); | |
| 59 } | |
| 60 return result; | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 EdgeEffectL::EdgeEffectL(ui::SystemUIResourceManager* resource_manager) | |
| 66 : resource_manager_(resource_manager), | |
| 67 glow_(cc::UIResourceLayer::Create()), | |
| 68 glow_alpha_(0), | |
| 69 glow_scale_y_(0), | |
| 70 glow_alpha_start_(0), | |
| 71 glow_alpha_finish_(0), | |
| 72 glow_scale_y_start_(0), | |
| 73 glow_scale_y_finish_(0), | |
| 74 displacement_(0.5f), | |
| 75 target_displacement_(0.5f), | |
| 76 state_(STATE_IDLE), | |
| 77 pull_distance_(0) { | |
| 78 // Prevent the provided layers from drawing until the effect is activated. | |
| 79 glow_->SetIsDrawable(false); | |
| 80 } | |
| 81 | |
| 82 EdgeEffectL::~EdgeEffectL() { | |
| 83 glow_->RemoveFromParent(); | |
| 84 } | |
| 85 | |
| 86 bool EdgeEffectL::IsFinished() const { | |
| 87 return state_ == STATE_IDLE; | |
| 88 } | |
| 89 | |
| 90 void EdgeEffectL::Finish() { | |
| 91 glow_->SetIsDrawable(false); | |
| 92 pull_distance_ = 0; | |
| 93 state_ = STATE_IDLE; | |
| 94 } | |
| 95 | |
| 96 void EdgeEffectL::Pull(base::TimeTicks current_time, float delta_distance) { | |
| 97 Pull(current_time, delta_distance, 0.5f); | |
| 98 } | |
| 99 | |
| 100 void EdgeEffectL::Pull(base::TimeTicks current_time, | |
| 101 float delta_distance, | |
| 102 float displacement) { | |
| 103 target_displacement_ = displacement; | |
| 104 if (state_ == STATE_PULL_DECAY && current_time - start_time_ < duration_) { | |
| 105 return; | |
| 106 } | |
| 107 if (state_ != STATE_PULL) { | |
| 108 glow_scale_y_ = std::max(kPullGlowBegin, glow_scale_y_); | |
| 109 } | |
| 110 state_ = STATE_PULL; | |
| 111 | |
| 112 start_time_ = current_time; | |
| 113 duration_ = base::TimeDelta::FromMilliseconds(kPullTime); | |
| 114 | |
| 115 float abs_delta_distance = std::abs(delta_distance); | |
| 116 pull_distance_ += delta_distance; | |
| 117 | |
| 118 glow_alpha_ = glow_alpha_start_ = std::min( | |
| 119 kMaxAlpha, | |
| 120 glow_alpha_ + (abs_delta_distance * kPullDistanceAlphaGlowFactor)); | |
| 121 | |
| 122 if (pull_distance_ == 0) { | |
| 123 glow_scale_y_ = glow_scale_y_start_ = 0; | |
| 124 } else { | |
| 125 float scale = 1.f - | |
| 126 1.f / std::sqrt(std::abs(pull_distance_) * bounds_.height()) - | |
| 127 0.3f; | |
| 128 glow_scale_y_ = glow_scale_y_start_ = std::max(0.f, scale) / 0.7f; | |
| 129 } | |
| 130 | |
| 131 glow_alpha_finish_ = glow_alpha_; | |
| 132 glow_scale_y_finish_ = glow_scale_y_; | |
| 133 } | |
| 134 | |
| 135 void EdgeEffectL::Release(base::TimeTicks current_time) { | |
| 136 pull_distance_ = 0; | |
| 137 | |
| 138 if (state_ != STATE_PULL && state_ != STATE_PULL_DECAY) | |
| 139 return; | |
| 140 | |
| 141 state_ = STATE_RECEDE; | |
| 142 glow_alpha_start_ = glow_alpha_; | |
| 143 glow_scale_y_start_ = glow_scale_y_; | |
| 144 | |
| 145 glow_alpha_finish_ = 0.f; | |
| 146 glow_scale_y_finish_ = 0.f; | |
| 147 | |
| 148 start_time_ = current_time; | |
| 149 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTime); | |
| 150 } | |
| 151 | |
| 152 void EdgeEffectL::Absorb(base::TimeTicks current_time, float velocity) { | |
| 153 state_ = STATE_ABSORB; | |
| 154 | |
| 155 velocity = Clamp(std::abs(velocity), kMinVelocity, kMaxVelocity); | |
| 156 | |
| 157 start_time_ = current_time; | |
| 158 // This should never be less than 1 millisecond. | |
| 159 duration_ = base::TimeDelta::FromMilliseconds(0.15f + (velocity * 0.02f)); | |
| 160 | |
| 161 // The glow depends more on the velocity, and therefore starts out | |
| 162 // nearly invisible. | |
| 163 glow_alpha_start_ = 0.3f; | |
| 164 glow_scale_y_start_ = std::max(glow_scale_y_, 0.f); | |
| 165 | |
| 166 // Growth for the size of the glow should be quadratic to properly respond | |
| 167 // to a user's scrolling speed. The faster the scrolling speed, the more | |
| 168 // intense the effect should be for both the size and the saturation. | |
| 169 glow_scale_y_finish_ = | |
| 170 std::min(0.025f + (velocity * (velocity / 100) * 0.00015f) / 2.f, 1.f); | |
| 171 // Alpha should change for the glow as well as size. | |
| 172 glow_alpha_finish_ = Clamp( | |
| 173 glow_alpha_start_, velocity * kVelocityGlowFactor * .00001f, kMaxAlpha); | |
| 174 target_displacement_ = 0.5; | |
| 175 } | |
| 176 | |
| 177 bool EdgeEffectL::Update(base::TimeTicks current_time) { | |
| 178 if (IsFinished()) | |
| 179 return false; | |
| 180 | |
| 181 const double dt = (current_time - start_time_).InMilliseconds(); | |
| 182 const double t = std::min(dt / duration_.InMilliseconds(), 1.); | |
| 183 const float interp = static_cast<float>(Damp(t, 1.)); | |
| 184 | |
| 185 glow_alpha_ = Lerp(glow_alpha_start_, glow_alpha_finish_, interp); | |
| 186 glow_scale_y_ = Lerp(glow_scale_y_start_, glow_scale_y_finish_, interp); | |
| 187 displacement_ = (displacement_ + target_displacement_) / 2.f; | |
| 188 | |
| 189 if (t >= 1.f - kEpsilon) { | |
| 190 switch (state_) { | |
| 191 case STATE_ABSORB: | |
| 192 state_ = STATE_RECEDE; | |
| 193 start_time_ = current_time; | |
| 194 duration_ = base::TimeDelta::FromMilliseconds(kRecedeTime); | |
| 195 | |
| 196 glow_alpha_start_ = glow_alpha_; | |
| 197 glow_scale_y_start_ = glow_scale_y_; | |
| 198 | |
| 199 glow_alpha_finish_ = 0.f; | |
| 200 glow_scale_y_finish_ = 0.f; | |
| 201 break; | |
| 202 case STATE_PULL: | |
| 203 // Hold in this state until explicitly released. | |
| 204 break; | |
| 205 case STATE_PULL_DECAY: | |
| 206 state_ = STATE_RECEDE; | |
| 207 break; | |
| 208 case STATE_RECEDE: | |
| 209 Finish(); | |
| 210 break; | |
| 211 default: | |
| 212 break; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 bool one_last_frame = false; | |
| 217 if (state_ == STATE_RECEDE && glow_scale_y_ <= 0) { | |
| 218 Finish(); | |
| 219 one_last_frame = true; | |
| 220 } | |
| 221 | |
| 222 return !IsFinished() || one_last_frame; | |
| 223 } | |
| 224 | |
| 225 void EdgeEffectL::ApplyToLayers(const gfx::SizeF& size, | |
| 226 const gfx::Transform& transform) { | |
| 227 if (IsFinished()) | |
| 228 return; | |
| 229 | |
| 230 // An empty window size, while meaningless, is also relatively harmless, and | |
| 231 // will simply prevent any drawing of the layers. | |
| 232 if (size.IsEmpty()) { | |
| 233 glow_->SetIsDrawable(false); | |
| 234 return; | |
| 235 } | |
| 236 | |
| 237 const float r = size.width() * 0.75f / kSin; | |
| 238 const float y = kCos * r; | |
| 239 const float h = r - y; | |
| 240 bounds_ = gfx::Size(size.width(), (int)std::min(size.height(), h)); | |
| 241 gfx::Size image_bounds(r, std::min(1.f, glow_scale_y_) * bounds_.height()); | |
| 242 | |
| 243 glow_->SetIsDrawable(true); | |
| 244 glow_->SetUIResourceId(resource_manager_->GetUIResourceId(kResourceType)); | |
| 245 glow_->SetTransformOrigin(gfx::Point3F(bounds_.width() * 0.5f, 0, 0)); | |
| 246 glow_->SetBounds(image_bounds); | |
| 247 glow_->SetContentsOpaque(false); | |
| 248 glow_->SetOpacity(Clamp(glow_alpha_, 0.f, 1.f)); | |
| 249 | |
| 250 const float displacement = Clamp(displacement_, 0.f, 1.f) - 0.5f; | |
| 251 const float displacement_offset_x = bounds_.width() * displacement * 0.5f; | |
| 252 const float image_offset_x = (bounds_.width() - image_bounds.width()) * 0.5f; | |
| 253 gfx::Transform offset_transform; | |
| 254 offset_transform.Translate(image_offset_x - displacement_offset_x, 0); | |
| 255 offset_transform.ConcatTransform(transform); | |
| 256 glow_->SetTransform(offset_transform); | |
| 257 } | |
| 258 | |
| 259 void EdgeEffectL::SetParent(cc::Layer* parent) { | |
| 260 if (glow_->parent() != parent) | |
| 261 parent->AddChild(glow_); | |
| 262 glow_->SetUIResourceId(resource_manager_->GetUIResourceId(kResourceType)); | |
| 263 } | |
| 264 | |
| 265 // static | |
| 266 void EdgeEffectL::PreloadResources( | |
| 267 ui::SystemUIResourceManager* resource_manager) { | |
| 268 DCHECK(resource_manager); | |
| 269 resource_manager->PreloadResource(kResourceType); | |
| 270 } | |
| 271 | |
| 272 } // namespace content | |
| OLD | NEW |