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 MoveForwardToTime(base::Time::Now()); | |
| 19 | |
| 18 LaserPoint new_point; | 20 LaserPoint new_point; |
| 19 new_point.location = point; | 21 new_point.location = point; |
| 20 new_point.creation_time = base::Time::Now(); | 22 new_point.age = 0.0; |
|
jdufault
2016/10/17 19:14:13
Remove since the default age is 0.0
sammiequon
2016/10/17 23:39:49
Done.
| |
| 21 points_.push_back(new_point); | 23 points_.push_back(new_point); |
| 22 ClearOldPoints(); | 24 } |
| 25 | |
| 26 void LaserPointerPoints::MoveForwardToTime(const base::Time& latest_time) { | |
| 27 if (!points_.empty()) { | |
| 28 DCHECK(!collection_latest_time_.is_null()); | |
| 29 | |
| 30 // Increase the age of points based on how much time has elapsed. | |
| 31 base::TimeDelta delta = latest_time - collection_latest_time_; | |
| 32 double lifespan_change = | |
| 33 delta.InMillisecondsF() / life_duration_.InMillisecondsF(); | |
| 34 for (LaserPoint& point : points_) | |
| 35 point.age += lifespan_change; | |
| 36 | |
| 37 // Remove points that are too old (points age older than 1.0). | |
| 38 auto first_alive_point = | |
| 39 std::find_if(points_.begin(), points_.end(), | |
| 40 [](const LaserPoint& p) { return p.age < 1.0; }); | |
| 41 points_.erase(points_.begin(), first_alive_point); | |
| 42 } | |
| 43 collection_latest_time_ = latest_time; | |
| 23 } | 44 } |
| 24 | 45 |
| 25 void LaserPointerPoints::Clear() { | 46 void LaserPointerPoints::Clear() { |
| 26 points_.clear(); | 47 points_.clear(); |
| 27 } | 48 } |
| 28 | 49 |
| 29 gfx::Rect LaserPointerPoints::GetBoundingBox() { | 50 gfx::Rect LaserPointerPoints::GetBoundingBox() { |
| 30 if (IsEmpty()) | 51 if (IsEmpty()) |
| 31 return gfx::Rect(); | 52 return gfx::Rect(); |
| 32 | 53 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 54 } | 75 } |
| 55 | 76 |
| 56 int LaserPointerPoints::GetNumberOfPoints() const { | 77 int LaserPointerPoints::GetNumberOfPoints() const { |
| 57 return points_.size(); | 78 return points_.size(); |
| 58 } | 79 } |
| 59 | 80 |
| 60 const std::deque<LaserPointerPoints::LaserPoint>& | 81 const std::deque<LaserPointerPoints::LaserPoint>& |
| 61 LaserPointerPoints::laser_points() { | 82 LaserPointerPoints::laser_points() { |
| 62 return points_; | 83 return points_; |
| 63 } | 84 } |
| 64 | |
| 65 void LaserPointerPoints::ClearOldPoints() { | |
| 66 DCHECK(!IsEmpty()); | |
| 67 auto first_alive_point = | |
| 68 std::find_if(points_.begin(), points_.end(), [this](LaserPoint& p) { | |
| 69 return GetNewest().creation_time - p.creation_time < life_duration_; | |
| 70 }); | |
| 71 points_.erase(points_.begin(), first_alive_point); | |
| 72 } | |
| 73 | |
| 74 } // namespace ash | 85 } // namespace ash |
| OLD | NEW |