Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: ash/common/system/chromeos/palette/tools/laser_pointer_points.cc

Issue 2231533004: Pointer watcher modifications to support laser pointer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
24 void LaserPointerPoints::AddPoint(const gfx::Point& point) {
25 // Creates and adds the new point. Updates the collection and bounding box
26 // accordingly.
27 base::Time pointTime = base::Time::Now();
28 LaserPointerPoint newPoint;
29 newPoint.location_ = point;
30 newPoint.creationTime_ = pointTime;
31
32 if (points_.empty())
33 bounds_.SetRect(point.x(), point.y(), 0, 0);
34 else {
35 std::vector<LaserPointerPoint> generatedPoints;
36 // GenerateCurvePoints(newPoint, generatedPoints);
37 for (size_t j = 0; j < generatedPoints.size(); j++) {
38 points_.push_back(generatedPoints[j]);
39 }
40 CalculateBounds(newPoint.location_);
41 }
42 points_.push_back(newPoint);
43 ClearOldPoints();
44 }
45
46 void LaserPointerPoints::Clear() {
47 points_.clear();
48 }
49
50 gfx::Rect LaserPointerPoints::GetBoundingBox() const {
51 return bounds_;
52 }
53
54 LaserPointerPoints::LaserPointerPoint LaserPointerPoints::GetOldest() const {
55 if (!points_.empty())
56 return points_.front();
57 return LaserPointerPoint();
58 }
59
60 LaserPointerPoints::LaserPointerPoint LaserPointerPoints::GetMostRecent() 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 LaserPointerPo ints::PointsStart() const {
75 return points_.cbegin();
76 }
77
78 std::deque<LaserPointerPoints::LaserPointerPoint>::const_iterator LaserPointerPo ints::PointsEnd() const {
79 return points_.cend();
80 }
81
82 void LaserPointerPoints::CalculateBounds(const gfx::Point& point) {
83 gfx::Rect oldBounds;
84 oldBounds.SetRect(bounds_.x(), bounds_.y(),
85 bounds_.width(), bounds_.height());
86 // This will do nothing if the point is within the current bounds.
87 bool xChanged = false;
88 bool yChanged = false;
89 if (point.x() <= oldBounds.x()) {
90 bounds_.set_x(point.x());
91 xChanged = true;
92 }
93 if (point.y() <= oldBounds.y()) {
94 bounds_.set_y(point.y());
95 yChanged = true;
96 }
97 if (point.x() >= oldBounds.right()) {
98 bounds_.set_width(std::abs(point.x() - bounds_.x()));
99 } else if(xChanged)
100 bounds_.set_width(std::abs(oldBounds.right() - bounds_.x()));
101 if (point.y() >= oldBounds.bottom()) {
102 bounds_.set_height(std::abs(point.y() - bounds_.y()));
103 } else if(yChanged)
104 bounds_.set_height(std::abs(oldBounds.bottom() - bounds_.y()));
105 }
106
107 void LaserPointerPoints::ClearOldPoints() {
108 if (!points_.empty()) {
109 bool needRecalculateBounds = false;
110 LaserPointerPoint newest = points_.back();
111 for (auto it = points_.begin(); it != points_.end(); it++) {
112 if (newest.creationTime_ - it->creationTime_ > life_duration_) {
113 if (!needRecalculateBounds ||
114 it->location_.x() <= bounds_.x() ||
115 it->location_.x() >= bounds_.right() ||
116 it->location_.y() <= bounds_.y() ||
117 it->location_.y() >= bounds_.bottom()) {
118 // If the point was on of the outermost points we have to recalculate
119 // the bounds.
120 needRecalculateBounds = true;
121 }
122 points_.erase(it);
123 } else {
124 // Since the points are sorted by time we can end the loop early once
125 // the condition fails.
126 break;
127 }
128 }
129 if (needRecalculateBounds)
130 RecalculateBounds();
131 }
132 }
133
134 float LaserPointerPoints::DistanceMousePoints(const LaserPointerPoint& point1,
135 const LaserPointerPoint& point2) {
136 return sqrt((point2.location_.y() - point1.location_.y()) *
137 (point2.location_.y() - point1.location_.y()) +
138 (point2.location_.x() - point1.location_.x()) *
139 (point2.location_.x() - point1.location_.x()));
140 }
141
142 void LaserPointerPoints::GenerateCurvePoints(
143 const LaserPointerPoint& newPoint,
144 std::vector<LaserPointerPoint>& generatedPoints) {
145 if (points_.empty())
146 return;
147
148 LaserPointerPoint lastPoint = points_.back();
149 gfx::CubicBezier bezier = gfx::CubicBezier(0.25, 0.0, 0.75, 1.0);
150 float distance = DistanceMousePoints(newPoint, lastPoint);
151
152 // Generate some equidistant points using the bezier.
153 for (float j = 1; j <= distance - 1; j+=3) {
154 float normalized = 1 - ((distance - j) / distance);
155 base::Time generatedTime = lastPoint.creationTime_ +
156 base::TimeDelta::FromMillisecondsD(double{
157 normalized *
158 float{(newPoint.creationTime_ -
159 lastPoint.creationTime_).InMillisecondsF()}
160 });
161 LaserPointerPoint generatedPoint;
162 float currentX = lastPoint.location_.x() +
163 normalized *
164 (newPoint.location_.x() - lastPoint.location_.x());
165 float currentY = lastPoint.location_.y() +
166 float{bezier.Solve(normalized)} *
167 (newPoint.location_.y() - lastPoint.location_.y());
168 generatedPoint.creationTime_ = generatedTime;
169 generatedPoint.location_ = gfx::Point(currentX, currentY);
170 generatedPoints.push_back(generatedPoint);
171 }
172 }
173
174 void LaserPointerPoints::RecalculateBounds() {
175 if (points_.empty())
176 bounds_.SetRect(0, 0, 0, 0);
177 auto it = points_.begin();
178 bounds_.SetRect(it->location_.x(), it->location_.y(), 0, 0);
179 for (; it != points_.end(); it++) {
180 CalculateBounds(it->location_);
181 }
182 }
183 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698