Chromium Code Reviews| 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 "ash/laser/laser_pointer_points.h" | 5 #include "ash/laser/laser_pointer_points.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 namespace ash { | 10 namespace ash { |
| 11 namespace { | |
| 12 | |
| 13 double NormalizedValue(double initial_value, | |
| 14 double final_value, | |
| 15 double current_value) { | |
| 16 double computed = | |
| 17 (current_value - initial_value) / (final_value - initial_value); | |
| 18 return std::min(1.0, std::max(computed, 0.0)); | |
| 19 } | |
| 20 } | |
| 11 | 21 |
| 12 LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) | 22 LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) |
| 13 : life_duration_(life_duration) {} | 23 : life_duration_(life_duration) {} |
| 14 | 24 |
| 15 LaserPointerPoints::~LaserPointerPoints() {} | 25 LaserPointerPoints::~LaserPointerPoints() {} |
| 16 | 26 |
| 17 void LaserPointerPoints::AddPoint(const gfx::Point& point) { | 27 void LaserPointerPoints::AddPoint(const gfx::Point& point) { |
| 28 base::Time current_time = base::Time::Now(); | |
| 18 LaserPoint new_point; | 29 LaserPoint new_point; |
| 19 new_point.location = point; | 30 new_point.location = point; |
| 20 new_point.creation_time = base::Time::Now(); | 31 new_point.creation_time = current_time; |
| 32 new_point.age = 0.0; | |
| 21 points_.push_back(new_point); | 33 points_.push_back(new_point); |
| 34 MoveForwardToTime(current_time); | |
| 35 } | |
| 36 | |
| 37 void LaserPointerPoints::MoveForwardToCurrentTime() { | |
| 38 MoveForwardToTime(base::Time::Now()); | |
| 39 } | |
| 40 | |
| 41 void LaserPointerPoints::MoveForwardToTime(const base::Time& new_latest_time) { | |
| 42 collection_latest_time_ = new_latest_time; | |
| 22 ClearOldPoints(); | 43 ClearOldPoints(); |
| 44 UpdateAges(); | |
|
jdufault
2016/10/04 22:04:13
What about doing something like this?
auto delt
sammiequon
2016/10/05 17:42:23
Done.
| |
| 23 } | 45 } |
| 24 | 46 |
| 25 void LaserPointerPoints::Clear() { | 47 void LaserPointerPoints::Clear() { |
| 26 points_.clear(); | 48 points_.clear(); |
| 27 } | 49 } |
| 28 | 50 |
| 29 gfx::Rect LaserPointerPoints::GetBoundingBox() { | 51 gfx::Rect LaserPointerPoints::GetBoundingBox() { |
| 30 if (IsEmpty()) | 52 if (IsEmpty()) |
| 31 return gfx::Rect(); | 53 return gfx::Rect(); |
| 32 | 54 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 56 int LaserPointerPoints::GetNumberOfPoints() const { | 78 int LaserPointerPoints::GetNumberOfPoints() const { |
| 57 return points_.size(); | 79 return points_.size(); |
| 58 } | 80 } |
| 59 | 81 |
| 60 const std::deque<LaserPointerPoints::LaserPoint>& | 82 const std::deque<LaserPointerPoints::LaserPoint>& |
| 61 LaserPointerPoints::laser_points() { | 83 LaserPointerPoints::laser_points() { |
| 62 return points_; | 84 return points_; |
| 63 } | 85 } |
| 64 | 86 |
| 65 void LaserPointerPoints::ClearOldPoints() { | 87 void LaserPointerPoints::ClearOldPoints() { |
| 66 DCHECK(!IsEmpty()); | |
| 67 auto first_alive_point = | 88 auto first_alive_point = |
| 68 std::find_if(points_.begin(), points_.end(), [this](LaserPoint& p) { | 89 std::find_if(points_.begin(), points_.end(), [this](LaserPoint& p) { |
| 69 return GetNewest().creation_time - p.creation_time < life_duration_; | 90 return collection_latest_time_ - p.creation_time < life_duration_; |
| 70 }); | 91 }); |
| 71 points_.erase(points_.begin(), first_alive_point); | 92 points_.erase(points_.begin(), first_alive_point); |
| 72 } | 93 } |
| 73 | 94 |
| 95 void LaserPointerPoints::UpdateAges() { | |
| 96 std::for_each(points_.begin(), points_.end(), [this](LaserPoint& p) { | |
| 97 p.age = 1.0f - NormalizedValue( | |
| 98 (collection_latest_time_ - life_duration_).ToJsTime(), | |
| 99 collection_latest_time_.ToJsTime(), | |
| 100 p.creation_time.ToJsTime()); | |
| 101 }); | |
| 102 } | |
| 74 } // namespace ash | 103 } // namespace ash |
| OLD | NEW |