Chromium Code Reviews| Index: ash/common/system/chromeos/palette/tools/laser_pointer_points.cc |
| diff --git a/ash/common/system/chromeos/palette/tools/laser_pointer_points.cc b/ash/common/system/chromeos/palette/tools/laser_pointer_points.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..29d15dd2f5911d4d46f76fe3cdda4fe9eccac210 |
| --- /dev/null |
| +++ b/ash/common/system/chromeos/palette/tools/laser_pointer_points.cc |
| @@ -0,0 +1,83 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/common/system/chromeos/palette/tools/laser_pointer_points.h" |
| + |
| +#include <limits> |
| + |
| +namespace ash { |
| + |
| +LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) |
| + : life_duration_(life_duration) {} |
| + |
| +LaserPointerPoints::~LaserPointerPoints() {} |
| + |
| +void LaserPointerPoints::AddPoint(const gfx::Point& point) { |
| + LaserPoint new_point; |
| + new_point.location = point; |
| + new_point.creation_time = base::Time::Now(); |
| + points_.push_back(new_point); |
| + ClearOldPoints(); |
| +} |
| + |
| +void LaserPointerPoints::Clear() { |
| + points_.clear(); |
| +} |
| + |
| +gfx::Rect LaserPointerPoints::GetBoundingBox() { |
| + gfx::Point min_point(INT_MAX, INT_MAX); |
|
jdufault
2016/08/19 01:02:08
std::numeric_limits<int64_t>::max() (or whatever t
sammiequon
2016/08/19 20:28:46
Done.
|
| + gfx::Point max_point(INT_MIN, INT_MIN); |
| + for (auto it = points_.begin(); it != points_.end(); it++) { |
|
jdufault
2016/08/19 01:02:08
for (const LaserPoint& point : points_)
sammiequon
2016/08/19 20:28:46
Done.
|
| + min_point = gfx::Point(std::min(it->location.x(), min_point.x()), |
| + std::min(it->location.y(), min_point.y())); |
| + max_point = gfx::Point(std::max(it->location.x(), max_point.x()), |
| + std::max(it->location.y(), max_point.y())); |
| + } |
| + return gfx::BoundingRect(min_point, max_point); |
| +} |
| + |
| +LaserPointerPoints::LaserPoint LaserPointerPoints::GetOldest() const { |
| + DCHECK(!IsEmpty()); |
| + return points_.front(); |
| +} |
| + |
| +LaserPointerPoints::LaserPoint LaserPointerPoints::GetMostRecent() const { |
| + DCHECK(!IsEmpty()); |
| + return points_.back(); |
| +} |
| + |
| +bool LaserPointerPoints::IsEmpty() const { |
| + return points_.empty(); |
| +} |
| + |
| +int LaserPointerPoints::GetNumberOfPoints() const { |
| + return points_.size(); |
| +} |
| + |
| +std::deque<LaserPointerPoints::LaserPoint>::const_iterator |
| +LaserPointerPoints::PointsStart() const { |
| + return points_.cbegin(); |
| +} |
| + |
| +std::deque<LaserPointerPoints::LaserPoint>::const_iterator |
| +LaserPointerPoints::PointsEnd() const { |
| + return points_.cend(); |
| +} |
| + |
| +void LaserPointerPoints::ClearOldPoints() { |
|
jdufault
2016/08/19 01:02:08
This only gets called after a point has been added
sammiequon
2016/08/19 20:28:46
Done.
|
| + if (!points_.empty()) { |
| + LaserPoint newest = points_.back(); |
| + auto it = points_.begin(); |
|
jdufault
2016/08/19 01:02:08
What about
auto it = std::find_if(points_.begin
sammiequon
2016/08/19 20:28:46
Done.
|
| + for (; it != points_.end(); ++it) { |
| + if (newest.creation_time - it->creation_time > life_duration_) { |
| + } else { |
| + // Since the points are sorted by time we can end the loop early once |
| + // the condition fails. |
| + break; |
| + } |
| + } |
| + points_.erase(points_.begin(), it); |
| + } |
| +} |
| +} // namespace ash |