OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/common/system/chromeos/palette/tools/laser_pointer_points.h" | |
6 | |
7 #include <limits.h> | |
jdufault
2016/08/17 21:41:07
<limits>
sammiequon
2016/08/18 00:52:07
Done.
| |
8 | |
9 namespace ash { | |
10 | |
11 LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) | |
12 : life_duration_(life_duration) {} | |
13 | |
14 LaserPointerPoints::~LaserPointerPoints() {} | |
15 | |
16 void LaserPointerPoints::AddPoint(const gfx::Point& point) { | |
17 // Creates and adds the new point. Updates the collection and bounding box | |
jdufault
2016/08/17 21:41:08
Drop comment, it is already on the declaration.
sammiequon
2016/08/18 00:52:08
Done.
| |
18 // accordingly. | |
19 base::Time pointTime = base::Time::Now(); | |
jdufault
2016/08/17 21:41:08
point_time
jdufault
2016/08/17 21:41:08
inline this variable since it is only used once.
sammiequon
2016/08/18 00:52:07
Done.
sammiequon
2016/08/18 00:52:07
Done.
| |
20 LaserPoint newPoint; | |
jdufault
2016/08/17 21:41:07
new_point
sammiequon
2016/08/18 00:52:08
Done.
| |
21 newPoint.location = point; | |
22 newPoint.creation_time = pointTime; | |
23 | |
24 if (points_.empty()) | |
25 bounds_.SetRect(point.x(), point.y(), 0, 0); | |
26 else | |
27 CalculateBounds(newPoint.location); | |
28 | |
29 points_.push_back(newPoint); | |
30 ClearOldPoints(); | |
31 } | |
32 | |
33 void LaserPointerPoints::Clear() { | |
34 points_.clear(); | |
35 } | |
36 | |
37 gfx::Rect LaserPointerPoints::GetBoundingBox() const { | |
38 return bounds_; | |
39 } | |
40 | |
41 LaserPointerPoints::LaserPoint LaserPointerPoints::GetOldest() const { | |
42 if (!points_.empty()) | |
jdufault
2016/08/17 21:41:08
Invert all of these conditions since the pattern i
sammiequon
2016/08/18 00:52:08
Done.
| |
43 return points_.front(); | |
44 return LaserPoint(); | |
45 } | |
46 | |
47 LaserPointerPoints::LaserPoint LaserPointerPoints::GetMostRecent() const { | |
jdufault
2016/08/17 21:41:08
See GetOldest comment.
sammiequon
2016/08/18 00:52:08
Done.
| |
48 if (!points_.empty()) | |
49 return points_.back(); | |
50 return LaserPoint(); | |
51 } | |
52 | |
53 bool LaserPointerPoints::IsEmpty() const { | |
54 return points_.empty(); | |
55 } | |
56 | |
57 int LaserPointerPoints::GetNumberOfPoints() const { | |
58 return points_.size(); | |
59 } | |
60 | |
61 std::deque<LaserPointerPoints::LaserPoint>::const_iterator | |
62 LaserPointerPoints::PointsStart() const { | |
63 return points_.cbegin(); | |
64 } | |
65 | |
66 std::deque<LaserPointerPoints::LaserPoint>::const_iterator | |
67 LaserPointerPoints::PointsEnd() const { | |
68 return points_.cend(); | |
69 } | |
70 | |
71 void LaserPointerPoints::CalculateBounds(const gfx::Point& point) { | |
72 // This will do nothing if the point is within the current bounds. | |
73 gfx::Point min_point(bounds_.origin()); | |
74 gfx::Point max_point(bounds_.bottom_right()); | |
75 min_point = gfx::Point(std::min(min_point.x(), point.x()), | |
76 std::min(min_point.y(), point.y())); | |
77 max_point = gfx::Point(std::max(max_point.x(), point.x()), | |
78 std::max(max_point.y(), point.y())); | |
79 bounds_ = gfx::BoundingRect(min_point, max_point); | |
80 } | |
81 | |
82 void LaserPointerPoints::ClearOldPoints() { | |
83 if (!points_.empty()) { | |
84 LaserPoint newest = points_.back(); | |
85 bool needs_recalculate_bounds = false; | |
86 auto it = points_.begin(); | |
87 for (; it != points_.end(); ++it) { | |
88 if (newest.creation_time - it->creation_time > life_duration_) { | |
89 if (!needs_recalculate_bounds && !bounds_.Contains(it->location)) | |
90 needs_recalculate_bounds = true; | |
91 } else { | |
92 // Since the points are sorted by time we can end the loop early once | |
93 // the condition fails. | |
94 break; | |
95 } | |
96 } | |
97 points_.erase(points_.begin(), it); | |
jdufault
2016/08/17 21:41:08
Nice, this works well.
sammiequon
2016/08/18 00:52:07
:).
| |
98 | |
99 if (needs_recalculate_bounds) | |
100 RecalculateBounds(); | |
101 } | |
102 } | |
103 | |
104 void LaserPointerPoints::RecalculateBounds() { | |
105 int j = 0; | |
jdufault
2016/08/17 21:41:08
Because the point length is only 40 or so, I don't
sammiequon
2016/08/18 00:52:07
Removed. Doesn't seem to cause too much lag.
| |
106 for (auto it = points_.begin(); it != points_.end(); ++it, ++j) { | |
107 if (j == 0) | |
108 bounds_.SetRect(it->location.x(), it->location.y(), 0, 0); | |
109 else | |
110 CalculateBounds(it->location); | |
111 } | |
112 } | |
113 } // namespace ash | |
OLD | NEW |