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