Chromium Code Reviews| Index: ash/laser/laser_pointer_points.cc |
| diff --git a/ash/laser/laser_pointer_points.cc b/ash/laser/laser_pointer_points.cc |
| index 8f2176ed8dbdfe42a5a5d5d2e78c3425e5797c5b..0b4470c98c4bd464dcde22f979b0c9c90332dc14 100644 |
| --- a/ash/laser/laser_pointer_points.cc |
| +++ b/ash/laser/laser_pointer_points.cc |
| @@ -8,6 +8,16 @@ |
| #include <limits> |
| namespace ash { |
| +namespace { |
| + |
| +double NormalizedValue(double initial_value, |
| + double final_value, |
| + double current_value) { |
| + double computed = |
| + (current_value - initial_value) / (final_value - initial_value); |
| + return std::min(1.0, std::max(computed, 0.0)); |
| +} |
| +} |
| LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) |
| : life_duration_(life_duration) {} |
| @@ -15,11 +25,23 @@ LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) |
| LaserPointerPoints::~LaserPointerPoints() {} |
| void LaserPointerPoints::AddPoint(const gfx::Point& point) { |
| + base::Time current_time = base::Time::Now(); |
| LaserPoint new_point; |
| new_point.location = point; |
| - new_point.creation_time = base::Time::Now(); |
| + new_point.creation_time = current_time; |
| + new_point.age = 0.0; |
| points_.push_back(new_point); |
| + MoveForwardToTime(current_time); |
| +} |
| + |
| +void LaserPointerPoints::MoveForwardToCurrentTime() { |
| + MoveForwardToTime(base::Time::Now()); |
| +} |
| + |
| +void LaserPointerPoints::MoveForwardToTime(const base::Time& new_latest_time) { |
| + collection_latest_time_ = new_latest_time; |
| ClearOldPoints(); |
| + UpdateAges(); |
|
jdufault
2016/10/04 22:04:13
What about doing something like this?
auto delt
sammiequon
2016/10/05 17:42:23
Done.
|
| } |
| void LaserPointerPoints::Clear() { |
| @@ -63,12 +85,19 @@ LaserPointerPoints::laser_points() { |
| } |
| void LaserPointerPoints::ClearOldPoints() { |
| - DCHECK(!IsEmpty()); |
| auto first_alive_point = |
| std::find_if(points_.begin(), points_.end(), [this](LaserPoint& p) { |
| - return GetNewest().creation_time - p.creation_time < life_duration_; |
| + return collection_latest_time_ - p.creation_time < life_duration_; |
| }); |
| points_.erase(points_.begin(), first_alive_point); |
| } |
| +void LaserPointerPoints::UpdateAges() { |
| + std::for_each(points_.begin(), points_.end(), [this](LaserPoint& p) { |
| + p.age = 1.0f - NormalizedValue( |
| + (collection_latest_time_ - life_duration_).ToJsTime(), |
| + collection_latest_time_.ToJsTime(), |
| + p.creation_time.ToJsTime()); |
| + }); |
| +} |
| } // namespace ash |