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 "ui/gfx/geometry/cubic_bezier.h" |
| 8 |
| 9 namespace ash { |
| 10 |
| 11 LaserPointerPoints::LaserPointerPoints(base::TimeDelta life_duration) { |
| 12 life_duration_ = life_duration; |
| 13 } |
| 14 |
| 15 LaserPointerPoints::LaserPointerPoints(const LaserPointerPoints& old) { |
| 16 bounds_ = old.bounds_; |
| 17 points_ = old.points_; |
| 18 life_duration_ = old.life_duration_; |
| 19 } |
| 20 |
| 21 LaserPointerPoints::~LaserPointerPoints() {} |
| 22 |
| 23 void LaserPointerPoints::AddPoint(const gfx::Point& point) { |
| 24 // Creates and adds the new point. Updates the collection and bounding box |
| 25 // accordingly. |
| 26 base::Time pointTime = base::Time::Now(); |
| 27 LaserPointerPoint newPoint; |
| 28 newPoint.location_ = point; |
| 29 newPoint.creationTime_ = pointTime; |
| 30 |
| 31 if (points_.empty()) |
| 32 bounds_.SetRect(point.x(), point.y(), 0, 0); |
| 33 else { |
| 34 std::vector<LaserPointerPoint> generatedPoints; |
| 35 // GenerateCurvePoints(newPoint, generatedPoints); |
| 36 for (size_t j = 0; j < generatedPoints.size(); j++) { |
| 37 points_.push_back(generatedPoints[j]); |
| 38 } |
| 39 CalculateBounds(newPoint.location_); |
| 40 } |
| 41 points_.push_back(newPoint); |
| 42 ClearOldPoints(); |
| 43 } |
| 44 |
| 45 void LaserPointerPoints::Clear() { |
| 46 points_.clear(); |
| 47 } |
| 48 |
| 49 gfx::Rect LaserPointerPoints::GetBoundingBox() const { |
| 50 return bounds_; |
| 51 } |
| 52 |
| 53 LaserPointerPoints::LaserPointerPoint LaserPointerPoints::GetOldest() const { |
| 54 if (!points_.empty()) |
| 55 return points_.front(); |
| 56 return LaserPointerPoint(); |
| 57 } |
| 58 |
| 59 LaserPointerPoints::LaserPointerPoint LaserPointerPoints::GetMostRecent() |
| 60 const { |
| 61 if (!points_.empty()) |
| 62 return points_.back(); |
| 63 return LaserPointerPoint(); |
| 64 } |
| 65 |
| 66 bool LaserPointerPoints::IsEmpty() const { |
| 67 return points_.empty(); |
| 68 } |
| 69 |
| 70 int LaserPointerPoints::GetNumberOfPoints() const { |
| 71 return points_.size(); |
| 72 } |
| 73 |
| 74 std::deque<LaserPointerPoints::LaserPointerPoint>::const_iterator |
| 75 LaserPointerPoints::PointsStart() const { |
| 76 return points_.cbegin(); |
| 77 } |
| 78 |
| 79 std::deque<LaserPointerPoints::LaserPointerPoint>::const_iterator |
| 80 LaserPointerPoints::PointsEnd() const { |
| 81 return points_.cend(); |
| 82 } |
| 83 |
| 84 void LaserPointerPoints::CalculateBounds(const gfx::Point& point) { |
| 85 gfx::Rect oldBounds; |
| 86 oldBounds.SetRect(bounds_.x(), bounds_.y(), bounds_.width(), |
| 87 bounds_.height()); |
| 88 // This will do nothing if the point is within the current bounds. |
| 89 bool xChanged = false; |
| 90 bool yChanged = false; |
| 91 if (point.x() <= oldBounds.x()) { |
| 92 bounds_.set_x(point.x()); |
| 93 xChanged = true; |
| 94 } |
| 95 if (point.y() <= oldBounds.y()) { |
| 96 bounds_.set_y(point.y()); |
| 97 yChanged = true; |
| 98 } |
| 99 if (point.x() >= oldBounds.right()) { |
| 100 bounds_.set_width(std::abs(point.x() - bounds_.x())); |
| 101 } else if (xChanged) |
| 102 bounds_.set_width(std::abs(oldBounds.right() - bounds_.x())); |
| 103 if (point.y() >= oldBounds.bottom()) { |
| 104 bounds_.set_height(std::abs(point.y() - bounds_.y())); |
| 105 } else if (yChanged) |
| 106 bounds_.set_height(std::abs(oldBounds.bottom() - bounds_.y())); |
| 107 } |
| 108 |
| 109 void LaserPointerPoints::ClearOldPoints() { |
| 110 if (!points_.empty()) { |
| 111 bool needRecalculateBounds = false; |
| 112 LaserPointerPoint newest = points_.back(); |
| 113 for (auto it = points_.begin(); it != points_.end(); it++) { |
| 114 if (newest.creationTime_ - it->creationTime_ > life_duration_) { |
| 115 if (!needRecalculateBounds || it->location_.x() <= bounds_.x() || |
| 116 it->location_.x() >= bounds_.right() || |
| 117 it->location_.y() <= bounds_.y() || |
| 118 it->location_.y() >= bounds_.bottom()) { |
| 119 // If the point was on of the outermost points we have to recalculate |
| 120 // the bounds. |
| 121 needRecalculateBounds = true; |
| 122 } |
| 123 points_.erase(it); |
| 124 } else { |
| 125 // Since the points are sorted by time we can end the loop early once |
| 126 // the condition fails. |
| 127 break; |
| 128 } |
| 129 } |
| 130 if (needRecalculateBounds) |
| 131 RecalculateBounds(); |
| 132 } |
| 133 } |
| 134 |
| 135 float LaserPointerPoints::DistanceMousePoints(const LaserPointerPoint& point1, |
| 136 const LaserPointerPoint& point2) { |
| 137 return sqrt((point2.location_.y() - point1.location_.y()) * |
| 138 (point2.location_.y() - point1.location_.y()) + |
| 139 (point2.location_.x() - point1.location_.x()) * |
| 140 (point2.location_.x() - point1.location_.x())); |
| 141 } |
| 142 |
| 143 void LaserPointerPoints::GenerateCurvePoints( |
| 144 const LaserPointerPoint& newPoint, |
| 145 std::vector<LaserPointerPoint>& generatedPoints) { |
| 146 if (points_.empty()) |
| 147 return; |
| 148 |
| 149 LaserPointerPoint lastPoint = points_.back(); |
| 150 gfx::CubicBezier bezier = gfx::CubicBezier(0.25, 0.0, 0.75, 1.0); |
| 151 float distance = DistanceMousePoints(newPoint, lastPoint); |
| 152 |
| 153 // Generate some equidistant points using the bezier. |
| 154 for (float j = 1; j <= distance - 1; j += 3) { |
| 155 float normalized = 1 - ((distance - j) / distance); |
| 156 base::Time generatedTime = |
| 157 lastPoint.creationTime_ + |
| 158 base::TimeDelta::FromMillisecondsD( |
| 159 double{normalized * |
| 160 float{(newPoint.creationTime_ - lastPoint.creationTime_) |
| 161 .InMillisecondsF()}}); |
| 162 LaserPointerPoint generatedPoint; |
| 163 float currentX = |
| 164 lastPoint.location_.x() + |
| 165 normalized * (newPoint.location_.x() - lastPoint.location_.x()); |
| 166 float currentY = lastPoint.location_.y() + |
| 167 float{bezier.Solve(normalized)} * |
| 168 (newPoint.location_.y() - lastPoint.location_.y()); |
| 169 generatedPoint.creationTime_ = generatedTime; |
| 170 generatedPoint.location_ = gfx::Point(currentX, currentY); |
| 171 generatedPoints.push_back(generatedPoint); |
| 172 } |
| 173 } |
| 174 |
| 175 void LaserPointerPoints::RecalculateBounds() { |
| 176 if (points_.empty()) |
| 177 bounds_.SetRect(0, 0, 0, 0); |
| 178 auto it = points_.begin(); |
| 179 bounds_.SetRect(it->location_.x(), it->location_.y(), 0, 0); |
| 180 for (; it != points_.end(); it++) { |
| 181 CalculateBounds(it->location_); |
| 182 } |
| 183 } |
| 184 } // namespace ash |
OLD | NEW |