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.
5 #include "content/browser/android/edge_effect_l.h"
7 #include "cc/layers/ui_resource_layer.h"
8 #include "content/browser/android/animation_utils.h"
9 #include "ui/android/resources/resource_manager.h"
10 #include "ui/android/resources/system_ui_resource_type.h"
16 // Time it will take the effect to fully recede in ms
17 const int kRecedeTimeMs
= 600;
19 // Time it will take before a pulled glow begins receding in ms
20 const int kPullTimeMs
= 167;
22 // Time it will take for a pulled glow to decay to partial strength before
24 const int kPullDecayTimeMs
= 2000;
26 const float kMaxAlpha
= 0.5f
;
28 const float kPullGlowBegin
= 0.f
;
30 // Min/max velocity that will be absorbed
31 const float kMinVelocity
= 100.f
;
32 const float kMaxVelocity
= 10000.f
;
34 const float kEpsilon
= 0.001f
;
36 const float kSin
= 0.5f
; // sin(PI / 6)
37 const float kCos
= 0.866f
; // cos(PI / 6);
39 // How much dragging should effect the height of the glow image.
40 // Number determined by user testing.
41 const float kPullDistanceAlphaGlowFactor
= 0.8f
;
43 const int kVelocityGlowFactor
= 6;
45 const ui::SystemUIResourceType kResourceId
= ui::OVERSCROLL_GLOW_L
;
49 EdgeEffectL::EdgeEffectL(ui::ResourceManager
* resource_manager
)
50 : resource_manager_(resource_manager
),
51 glow_(cc::UIResourceLayer::Create()),
55 glow_alpha_finish_(0),
56 glow_scale_y_start_(0),
57 glow_scale_y_finish_(0),
59 target_displacement_(0.5f
),
62 // Prevent the provided layers from drawing until the effect is activated.
63 glow_
->SetIsDrawable(false);
66 EdgeEffectL::~EdgeEffectL() {
67 glow_
->RemoveFromParent();
70 bool EdgeEffectL::IsFinished() const {
71 return state_
== STATE_IDLE
;
74 void EdgeEffectL::Finish() {
75 glow_
->SetIsDrawable(false);
80 void EdgeEffectL::Pull(base::TimeTicks current_time
,
83 target_displacement_
= displacement
;
84 if (state_
== STATE_PULL_DECAY
&& current_time
- start_time_
< duration_
) {
87 if (state_
!= STATE_PULL
) {
88 glow_scale_y_
= std::max(kPullGlowBegin
, glow_scale_y_
);
92 start_time_
= current_time
;
93 duration_
= base::TimeDelta::FromMilliseconds(kPullTimeMs
);
95 float abs_delta_distance
= std::abs(delta_distance
);
96 pull_distance_
+= delta_distance
;
98 glow_alpha_
= glow_alpha_start_
= std::min(
100 glow_alpha_
+ (abs_delta_distance
* kPullDistanceAlphaGlowFactor
));
102 if (pull_distance_
== 0) {
103 glow_scale_y_
= glow_scale_y_start_
= 0;
106 1.f
/ std::sqrt(std::abs(pull_distance_
) * bounds_
.height()) -
108 glow_scale_y_
= glow_scale_y_start_
= std::max(0.f
, scale
) / 0.7f
;
111 glow_alpha_finish_
= glow_alpha_
;
112 glow_scale_y_finish_
= glow_scale_y_
;
115 void EdgeEffectL::Release(base::TimeTicks current_time
) {
118 if (state_
!= STATE_PULL
&& state_
!= STATE_PULL_DECAY
)
121 state_
= STATE_RECEDE
;
122 glow_alpha_start_
= glow_alpha_
;
123 glow_scale_y_start_
= glow_scale_y_
;
125 glow_alpha_finish_
= 0.f
;
126 glow_scale_y_finish_
= 0.f
;
128 start_time_
= current_time
;
129 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
132 void EdgeEffectL::Absorb(base::TimeTicks current_time
, float velocity
) {
133 state_
= STATE_ABSORB
;
135 velocity
= Clamp(std::abs(velocity
), kMinVelocity
, kMaxVelocity
);
137 start_time_
= current_time
;
138 // This should never be less than 1 millisecond.
139 duration_
= base::TimeDelta::FromMilliseconds(0.15f
+ (velocity
* 0.02f
));
141 // The glow depends more on the velocity, and therefore starts out
143 glow_alpha_start_
= 0.3f
;
144 glow_scale_y_start_
= std::max(glow_scale_y_
, 0.f
);
146 // Growth for the size of the glow should be quadratic to properly respond
147 // to a user's scrolling speed. The faster the scrolling speed, the more
148 // intense the effect should be for both the size and the saturation.
149 glow_scale_y_finish_
=
150 std::min(0.025f
+ (velocity
* (velocity
/ 100) * 0.00015f
) / 2.f
, 1.f
);
151 // Alpha should change for the glow as well as size.
152 glow_alpha_finish_
= Clamp(
153 glow_alpha_start_
, velocity
* kVelocityGlowFactor
* .00001f
, kMaxAlpha
);
154 target_displacement_
= 0.5;
157 bool EdgeEffectL::Update(base::TimeTicks current_time
) {
161 const double dt
= (current_time
- start_time_
).InMilliseconds();
162 const double t
= std::min(dt
/ duration_
.InMilliseconds(), 1.);
163 const float interp
= static_cast<float>(Damp(t
, 1.));
165 glow_alpha_
= Lerp(glow_alpha_start_
, glow_alpha_finish_
, interp
);
166 glow_scale_y_
= Lerp(glow_scale_y_start_
, glow_scale_y_finish_
, interp
);
167 displacement_
= (displacement_
+ target_displacement_
) / 2.f
;
169 if (t
>= 1.f
- kEpsilon
) {
172 state_
= STATE_RECEDE
;
173 start_time_
= current_time
;
174 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
176 glow_alpha_start_
= glow_alpha_
;
177 glow_scale_y_start_
= glow_scale_y_
;
179 glow_alpha_finish_
= 0.f
;
180 glow_scale_y_finish_
= 0.f
;
183 state_
= STATE_PULL_DECAY
;
184 start_time_
= current_time
;
185 duration_
= base::TimeDelta::FromMilliseconds(kPullDecayTimeMs
);
187 glow_alpha_start_
= glow_alpha_
;
188 glow_scale_y_start_
= glow_scale_y_
;
190 // After pull, the glow should fade to nothing.
191 glow_alpha_finish_
= 0.f
;
192 glow_scale_y_finish_
= 0.f
;
194 case STATE_PULL_DECAY
:
195 state_
= STATE_RECEDE
;
205 bool one_last_frame
= false;
206 if (state_
== STATE_RECEDE
&& glow_scale_y_
<= 0) {
208 one_last_frame
= true;
211 return !IsFinished() || one_last_frame
;
214 void EdgeEffectL::ApplyToLayers(const gfx::SizeF
& size
,
215 const gfx::Transform
& transform
) {
219 // An empty window size, while meaningless, is also relatively harmless, and
220 // will simply prevent any drawing of the layers.
221 if (size
.IsEmpty()) {
222 glow_
->SetIsDrawable(false);
226 const float r
= size
.width() * 0.75f
/ kSin
;
227 const float y
= kCos
* r
;
228 const float h
= r
- y
;
229 const float o_r
= size
.height() * 0.75f
/ kSin
;
230 const float o_y
= kCos
* o_r
;
231 const float o_h
= o_r
- o_y
;
232 const float base_glow_scale
= h
> 0.f
? std::min(o_h
/ h
, 1.f
) : 1.f
;
233 bounds_
= gfx::Size(size
.width(), (int)std::min(size
.height(), h
));
234 gfx::Size
image_bounds(
235 r
, std::min(1.f
, glow_scale_y_
) * base_glow_scale
* bounds_
.height());
237 glow_
->SetIsDrawable(true);
238 glow_
->SetUIResourceId(resource_manager_
->GetUIResourceId(
239 ui::ANDROID_RESOURCE_TYPE_SYSTEM
, kResourceId
));
240 glow_
->SetTransformOrigin(gfx::Point3F(bounds_
.width() * 0.5f
, 0, 0));
241 glow_
->SetBounds(image_bounds
);
242 glow_
->SetContentsOpaque(false);
243 glow_
->SetOpacity(Clamp(glow_alpha_
, 0.f
, 1.f
));
245 const float displacement
= Clamp(displacement_
, 0.f
, 1.f
) - 0.5f
;
246 const float displacement_offset_x
= bounds_
.width() * displacement
* 0.5f
;
247 const float image_offset_x
= (bounds_
.width() - image_bounds
.width()) * 0.5f
;
248 gfx::Transform offset_transform
;
249 offset_transform
.Translate(image_offset_x
- displacement_offset_x
, 0);
250 offset_transform
.ConcatTransform(transform
);
251 glow_
->SetTransform(offset_transform
);
254 void EdgeEffectL::SetParent(cc::Layer
* parent
) {
255 if (glow_
->parent() != parent
)
256 parent
->AddChild(glow_
);
260 void EdgeEffectL::PreloadResources(ui::ResourceManager
* resource_manager
) {
261 DCHECK(resource_manager
);
262 resource_manager
->PreloadResource(ui::ANDROID_RESOURCE_TYPE_SYSTEM
,
266 } // namespace content