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 | 11 |
| 12 LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) | 12 LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) |
| 13 : life_duration_(life_duration) {} | 13 : life_duration_(life_duration) {} |
| 14 | 14 |
| 15 LaserPointerPoints::~LaserPointerPoints() {} | 15 LaserPointerPoints::~LaserPointerPoints() {} |
| 16 | 16 |
| 17 void LaserPointerPoints::AddPoint(const gfx::Point& point) { | 17 void LaserPointerPoints::AddPoint(const gfx::Point& point) { |
| 18 base::Time current_time = base::Time::Now(); | |
|
jdufault
2016/10/05 18:08:51
Remove this helper variable.
sammiequon
2016/10/05 21:15:22
Done.
| |
| 18 LaserPoint new_point; | 19 LaserPoint new_point; |
| 19 new_point.location = point; | 20 new_point.location = point; |
| 20 new_point.creation_time = base::Time::Now(); | 21 new_point.age = 0.0; |
| 22 MoveForwardToTime(current_time); | |
| 21 points_.push_back(new_point); | 23 points_.push_back(new_point); |
| 24 } | |
| 25 | |
| 26 void LaserPointerPoints::MoveForwardToCurrentTime() { | |
| 27 MoveForwardToTime(base::Time::Now()); | |
| 28 } | |
| 29 | |
| 30 void LaserPointerPoints::MoveForwardToTime(const base::Time& new_latest_time) { | |
| 31 if (collection_latest_time_.is_null()) | |
| 32 collection_latest_time_ = new_latest_time; | |
| 33 | |
| 34 // Update the ages of the points based on the change in new latest time. | |
| 35 auto delta = new_latest_time - collection_latest_time_; | |
|
jdufault
2016/10/05 18:08:51
Specify type
sammiequon
2016/10/05 21:15:22
Done.
| |
| 36 double lifespan_change = | |
| 37 delta.InMillisecondsF() / life_duration_.InMillisecondsF(); | |
| 38 for (auto& point : points_) | |
|
jdufault
2016/10/05 18:08:51
Specify type
sammiequon
2016/10/05 21:15:22
Done.
| |
| 39 point.age += lifespan_change; | |
| 22 ClearOldPoints(); | 40 ClearOldPoints(); |
| 41 collection_latest_time_ = new_latest_time; | |
| 23 } | 42 } |
| 24 | 43 |
| 25 void LaserPointerPoints::Clear() { | 44 void LaserPointerPoints::Clear() { |
| 26 points_.clear(); | 45 points_.clear(); |
| 27 } | 46 } |
| 28 | 47 |
| 29 gfx::Rect LaserPointerPoints::GetBoundingBox() { | 48 gfx::Rect LaserPointerPoints::GetBoundingBox() { |
| 30 if (IsEmpty()) | 49 if (IsEmpty()) |
| 31 return gfx::Rect(); | 50 return gfx::Rect(); |
| 32 | 51 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 56 int LaserPointerPoints::GetNumberOfPoints() const { | 75 int LaserPointerPoints::GetNumberOfPoints() const { |
| 57 return points_.size(); | 76 return points_.size(); |
| 58 } | 77 } |
| 59 | 78 |
| 60 const std::deque<LaserPointerPoints::LaserPoint>& | 79 const std::deque<LaserPointerPoints::LaserPoint>& |
| 61 LaserPointerPoints::laser_points() { | 80 LaserPointerPoints::laser_points() { |
| 62 return points_; | 81 return points_; |
| 63 } | 82 } |
| 64 | 83 |
| 65 void LaserPointerPoints::ClearOldPoints() { | 84 void LaserPointerPoints::ClearOldPoints() { |
| 66 DCHECK(!IsEmpty()); | |
| 67 auto first_alive_point = | 85 auto first_alive_point = |
| 68 std::find_if(points_.begin(), points_.end(), [this](LaserPoint& p) { | 86 std::find_if(points_.begin(), points_.end(), |
| 69 return GetNewest().creation_time - p.creation_time < life_duration_; | 87 [](LaserPoint& p) { return p.age < 1.0; }); |
| 70 }); | |
| 71 points_.erase(points_.begin(), first_alive_point); | 88 points_.erase(points_.begin(), first_alive_point); |
| 72 } | 89 } |
| 73 | |
| 74 } // namespace ash | 90 } // namespace ash |
| OLD | NEW |