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 "ui/base/android/system_ui_resource_manager.h"
14 // Time it will take the effect to fully recede in ms
15 const int kRecedeTimeMs
= 600;
17 // Time it will take before a pulled glow begins receding in ms
18 const int kPullTimeMs
= 167;
20 // Time it will take for a pulled glow to decay to partial strength before
22 const int kPullDecayTimeMs
= 2000;
24 const float kMaxAlpha
= 0.5f
;
26 const float kPullGlowBegin
= 0.f
;
28 // Min/max velocity that will be absorbed
29 const float kMinVelocity
= 100.f
;
30 const float kMaxVelocity
= 10000.f
;
32 const float kEpsilon
= 0.001f
;
34 const float kSin
= 0.5f
; // sin(PI / 6)
35 const float kCos
= 0.866f
; // cos(PI / 6);
37 // How much dragging should effect the height of the glow image.
38 // Number determined by user testing.
39 const float kPullDistanceAlphaGlowFactor
= 0.8f
;
41 const int kVelocityGlowFactor
= 6;
43 const ui::SystemUIResourceManager::ResourceType kResourceType
=
44 ui::SystemUIResourceManager::OVERSCROLL_GLOW_L
;
47 T
Lerp(T a
, T b
, T t
) {
48 return a
+ (b
- a
) * t
;
52 T
Clamp(T value
, T low
, T high
) {
53 return value
< low
? low
: (value
> high
? high
: value
);
57 T
Damp(T input
, T factor
) {
60 result
= 1 - (1 - input
) * (1 - input
);
62 result
= 1 - std::pow(1 - input
, 2 * factor
);
69 EdgeEffectL::EdgeEffectL(ui::SystemUIResourceManager
* resource_manager
)
70 : resource_manager_(resource_manager
),
71 glow_(cc::UIResourceLayer::Create()),
75 glow_alpha_finish_(0),
76 glow_scale_y_start_(0),
77 glow_scale_y_finish_(0),
79 target_displacement_(0.5f
),
82 // Prevent the provided layers from drawing until the effect is activated.
83 glow_
->SetIsDrawable(false);
86 EdgeEffectL::~EdgeEffectL() {
87 glow_
->RemoveFromParent();
90 bool EdgeEffectL::IsFinished() const {
91 return state_
== STATE_IDLE
;
94 void EdgeEffectL::Finish() {
95 glow_
->SetIsDrawable(false);
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_
) {
107 if (state_
!= STATE_PULL
) {
108 glow_scale_y_
= std::max(kPullGlowBegin
, glow_scale_y_
);
112 start_time_
= current_time
;
113 duration_
= base::TimeDelta::FromMilliseconds(kPullTimeMs
);
115 float abs_delta_distance
= std::abs(delta_distance
);
116 pull_distance_
+= delta_distance
;
118 glow_alpha_
= glow_alpha_start_
= std::min(
120 glow_alpha_
+ (abs_delta_distance
* kPullDistanceAlphaGlowFactor
));
122 if (pull_distance_
== 0) {
123 glow_scale_y_
= glow_scale_y_start_
= 0;
126 1.f
/ std::sqrt(std::abs(pull_distance_
) * bounds_
.height()) -
128 glow_scale_y_
= glow_scale_y_start_
= std::max(0.f
, scale
) / 0.7f
;
131 glow_alpha_finish_
= glow_alpha_
;
132 glow_scale_y_finish_
= glow_scale_y_
;
135 void EdgeEffectL::Release(base::TimeTicks current_time
) {
138 if (state_
!= STATE_PULL
&& state_
!= STATE_PULL_DECAY
)
141 state_
= STATE_RECEDE
;
142 glow_alpha_start_
= glow_alpha_
;
143 glow_scale_y_start_
= glow_scale_y_
;
145 glow_alpha_finish_
= 0.f
;
146 glow_scale_y_finish_
= 0.f
;
148 start_time_
= current_time
;
149 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
152 void EdgeEffectL::Absorb(base::TimeTicks current_time
, float velocity
) {
153 state_
= STATE_ABSORB
;
155 velocity
= Clamp(std::abs(velocity
), kMinVelocity
, kMaxVelocity
);
157 start_time_
= current_time
;
158 // This should never be less than 1 millisecond.
159 duration_
= base::TimeDelta::FromMilliseconds(0.15f
+ (velocity
* 0.02f
));
161 // The glow depends more on the velocity, and therefore starts out
163 glow_alpha_start_
= 0.3f
;
164 glow_scale_y_start_
= std::max(glow_scale_y_
, 0.f
);
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;
177 bool EdgeEffectL::Update(base::TimeTicks current_time
) {
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.));
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
;
189 if (t
>= 1.f
- kEpsilon
) {
192 state_
= STATE_RECEDE
;
193 start_time_
= current_time
;
194 duration_
= base::TimeDelta::FromMilliseconds(kRecedeTimeMs
);
196 glow_alpha_start_
= glow_alpha_
;
197 glow_scale_y_start_
= glow_scale_y_
;
199 glow_alpha_finish_
= 0.f
;
200 glow_scale_y_finish_
= 0.f
;
203 state_
= STATE_PULL_DECAY
;
204 start_time_
= current_time
;
205 duration_
= base::TimeDelta::FromMilliseconds(kPullDecayTimeMs
);
207 glow_alpha_start_
= glow_alpha_
;
208 glow_scale_y_start_
= glow_scale_y_
;
210 // After pull, the glow should fade to nothing.
211 glow_alpha_finish_
= 0.f
;
212 glow_scale_y_finish_
= 0.f
;
214 case STATE_PULL_DECAY
:
215 state_
= STATE_RECEDE
;
225 bool one_last_frame
= false;
226 if (state_
== STATE_RECEDE
&& glow_scale_y_
<= 0) {
228 one_last_frame
= true;
231 return !IsFinished() || one_last_frame
;
234 void EdgeEffectL::ApplyToLayers(const gfx::SizeF
& size
,
235 const gfx::Transform
& transform
) {
239 // An empty window size, while meaningless, is also relatively harmless, and
240 // will simply prevent any drawing of the layers.
241 if (size
.IsEmpty()) {
242 glow_
->SetIsDrawable(false);
246 const float r
= size
.width() * 0.75f
/ kSin
;
247 const float y
= kCos
* r
;
248 const float h
= r
- y
;
249 const float o_r
= size
.height() * 0.75f
/ kSin
;
250 const float o_y
= kCos
* o_r
;
251 const float o_h
= o_r
- o_y
;
252 const float base_glow_scale
= h
> 0.f
? std::min(o_h
/ h
, 1.f
) : 1.f
;
253 bounds_
= gfx::Size(size
.width(), (int)std::min(size
.height(), h
));
254 gfx::Size
image_bounds(
255 r
, std::min(1.f
, glow_scale_y_
) * base_glow_scale
* bounds_
.height());
257 glow_
->SetIsDrawable(true);
258 glow_
->SetUIResourceId(resource_manager_
->GetUIResourceId(kResourceType
));
259 glow_
->SetTransformOrigin(gfx::Point3F(bounds_
.width() * 0.5f
, 0, 0));
260 glow_
->SetBounds(image_bounds
);
261 glow_
->SetContentsOpaque(false);
262 glow_
->SetOpacity(Clamp(glow_alpha_
, 0.f
, 1.f
));
264 const float displacement
= Clamp(displacement_
, 0.f
, 1.f
) - 0.5f
;
265 const float displacement_offset_x
= bounds_
.width() * displacement
* 0.5f
;
266 const float image_offset_x
= (bounds_
.width() - image_bounds
.width()) * 0.5f
;
267 gfx::Transform offset_transform
;
268 offset_transform
.Translate(image_offset_x
- displacement_offset_x
, 0);
269 offset_transform
.ConcatTransform(transform
);
270 glow_
->SetTransform(offset_transform
);
273 void EdgeEffectL::SetParent(cc::Layer
* parent
) {
274 if (glow_
->parent() != parent
)
275 parent
->AddChild(glow_
);
276 glow_
->SetUIResourceId(resource_manager_
->GetUIResourceId(kResourceType
));
280 void EdgeEffectL::PreloadResources(
281 ui::SystemUIResourceManager
* resource_manager
) {
282 DCHECK(resource_manager
);
283 resource_manager
->PreloadResource(kResourceType
);
286 } // namespace content