| 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_view.h" | 5 #include "ash/laser/laser_pointer_view.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "ash/common/shell_window_ids.h" | 9 #include "ash/common/shell_window_ids.h" |
| 10 #include "ash/laser/laser_pointer_points.h" | 10 #include "ash/laser/laser_pointer_points.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 // TODO(sammiequon): Investigate if we should stop (which removes all | 79 // TODO(sammiequon): Investigate if we should stop (which removes all |
| 80 // points) or keep the old points. See http://crbug.com/647793. | 80 // points) or keep the old points. See http://crbug.com/647793. |
| 81 Stop(); | 81 Stop(); |
| 82 views::Widget::ReparentNativeView( | 82 views::Widget::ReparentNativeView( |
| 83 widget_->GetNativeView(), | 83 widget_->GetNativeView(), |
| 84 Shell::GetContainer(new_root_window, kShellWindowId_OverlayContainer)); | 84 Shell::GetContainer(new_root_window, kShellWindowId_OverlayContainer)); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { | 88 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { |
| 89 laser_points_.AddPoint(new_point); | 89 // Do not add the point but advance the time if the view is in process of |
| 90 // fading away. |
| 91 if (is_fading_away_) |
| 92 laser_points_.MoveForwardInTime(); |
| 93 else |
| 94 laser_points_.AddPoint(new_point); |
| 90 | 95 |
| 91 // The bounding box should be relative to the screen. | 96 // The bounding box should be relative to the screen. |
| 92 gfx::Point screen_offset = | 97 gfx::Point screen_offset = |
| 93 widget_->GetNativeView()->GetRootWindow()->GetBoundsInScreen().origin(); | 98 widget_->GetNativeView()->GetRootWindow()->GetBoundsInScreen().origin(); |
| 94 | 99 |
| 95 // Expand the bounding box so that it includes the radius of the points on the | 100 // Expand the bounding box so that it includes the radius of the points on the |
| 96 // edges. | 101 // edges. |
| 97 gfx::Rect bounding_box; | 102 gfx::Rect bounding_box; |
| 98 bounding_box = laser_points_.GetBoundingBox(); | 103 bounding_box = laser_points_.GetBoundingBox(); |
| 99 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius); | 104 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius); |
| 100 bounding_box.Offset(screen_offset.x(), screen_offset.y()); | 105 bounding_box.Offset(screen_offset.x(), screen_offset.y()); |
| 101 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2)); | 106 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2)); |
| 102 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2)); | 107 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2)); |
| 103 widget_->SetBounds(bounding_box); | 108 widget_->SetBounds(bounding_box); |
| 104 SchedulePaint(); | 109 SchedulePaint(); |
| 105 } | 110 } |
| 106 | 111 |
| 112 void LaserPointerView::SetIsFadingAway(bool is_fading) { |
| 113 is_fading_away_ = is_fading; |
| 114 } |
| 115 |
| 116 bool LaserPointerView::GetIsFadingAway() const { |
| 117 return is_fading_away_; |
| 118 } |
| 119 |
| 107 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { | 120 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { |
| 108 if (laser_points_.IsEmpty()) | 121 if (laser_points_.IsEmpty()) |
| 109 return; | 122 return; |
| 110 | 123 |
| 111 SkPaint paint; | 124 SkPaint paint; |
| 112 paint.setStyle(SkPaint::kStroke_Style); | 125 paint.setStyle(SkPaint::kStroke_Style); |
| 113 paint.setAntiAlias(true); | 126 paint.setAntiAlias(true); |
| 114 paint.setStrokeJoin(SkPaint::kBevel_Join); | 127 paint.setStrokeJoin(SkPaint::kBevel_Join); |
| 115 | 128 |
| 116 base::Time oldest = laser_points_.GetOldest().creation_time; | |
| 117 base::Time newest = laser_points_.GetNewest().creation_time; | |
| 118 gfx::Point previous_point = laser_points_.GetOldest().location; | |
| 119 gfx::Point current_point; | |
| 120 | |
| 121 // Compute the offset of the current widget. | 129 // Compute the offset of the current widget. |
| 122 gfx::Point widget_offset = | 130 gfx::Point widget_offset = |
| 123 widget_->GetNativeView()->GetBoundsInRootWindow().origin(); | 131 widget_->GetNativeView()->GetBoundsInRootWindow().origin(); |
| 132 |
| 133 base::Time oldest = laser_points_.GetCollectionEarliestTime(); |
| 134 base::Time newest = laser_points_.GetCollectionLatestTime(); |
| 135 gfx::Vector2d previous_point_vector = |
| 136 laser_points_.GetOldest().location - widget_offset; |
| 137 gfx::Point previous_point = |
| 138 gfx::Point(previous_point_vector.x(), previous_point_vector.y()); |
| 139 gfx::Point current_point; |
| 140 |
| 124 int num_points_ = laser_points_.GetNumberOfPoints(); | 141 int num_points_ = laser_points_.GetNumberOfPoints(); |
| 125 int point_count = 0; | 142 int point_count = 0; |
| 143 int current_opacity = 0; |
| 126 for (const LaserPointerPoints::LaserPoint& point : | 144 for (const LaserPointerPoints::LaserPoint& point : |
| 127 laser_points_.laser_points()) { | 145 laser_points_.laser_points()) { |
| 128 // relative_time is a value between [0,1] where 0 means the point is about | 146 // relative_time is a value between [0,1] where 0 means the point is about |
| 129 // to be removed and 1 means that the point was just added. | 147 // to be removed and 1 means that the point was just added. |
| 130 double relative_time = 1.0; | 148 double relative_time = 0.0; |
| 131 if (oldest != newest) { | 149 if (oldest != newest) { |
| 132 relative_time = 1.0 - ((point.creation_time - oldest).InMillisecondsF() / | 150 relative_time = 1.0 - ((point.creation_time - oldest).InMillisecondsF() / |
| 133 (newest - oldest).InMillisecondsF()); | 151 (newest - oldest).InMillisecondsF()); |
| 134 } | 152 } |
| 135 | 153 |
| 136 // Set the radius and opacity based on the distance. | 154 // Set the radius and opacity based on the distance. |
| 137 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, | 155 double current_radius = LinearInterpolate(kPointInitialRadius, |
| 138 relative_time); | 156 kPointFinalRadius, relative_time); |
| 157 current_opacity = |
| 158 int{LinearInterpolate(double{kPointInitialOpacity}, |
| 159 double{kPointFinalOpacity}, relative_time)}; |
| 139 | 160 |
| 140 gfx::Vector2d center = point.location - widget_offset; | 161 gfx::Vector2d center = point.location - widget_offset; |
| 141 current_point = gfx::Point(center.x(), center.y()); | 162 current_point = gfx::Point(center.x(), center.y()); |
| 142 | 163 |
| 143 // If we draw laser_points_ that are within a stroke width of each other, | 164 // If we draw laser_points_ that are within a stroke width of each other, |
| 144 // the result will be very jagged, unless we are on the last point, then we | 165 // the result will be very jagged, unless we are on the last point, then we |
| 145 // draw regardless. | 166 // draw regardless. |
| 146 point_count++; | 167 point_count++; |
| 147 float distance_threshold = float{radius * 2}; | 168 float distance_threshold = float{current_radius * 2}; |
| 148 if (DistanceBetweenPoints(previous_point, current_point) <= | 169 if (DistanceBetweenPoints(previous_point, current_point) <= |
| 149 distance_threshold && | 170 distance_threshold && |
| 150 point_count != num_points_) { | 171 point_count != num_points_) { |
| 151 continue; | 172 continue; |
| 152 } | 173 } |
| 153 | 174 |
| 154 int opacity = | 175 paint.setColor(SkColorSetA(kPointColor, current_opacity)); |
| 155 int{LinearInterpolate(double{kPointInitialOpacity}, | 176 paint.setStrokeWidth(current_radius * 2); |
| 156 double{kPointFinalOpacity}, relative_time)}; | |
| 157 paint.setColor(SkColorSetA(kPointColor, opacity)); | |
| 158 paint.setStrokeWidth(radius * 2); | |
| 159 canvas->DrawLine(previous_point, current_point, paint); | 177 canvas->DrawLine(previous_point, current_point, paint); |
| 160 previous_point = current_point; | 178 previous_point = current_point; |
| 161 } | 179 } |
| 162 // Draw the last point as a circle. | 180 // Draw the last point as a circle. |
| 163 paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity)); | 181 paint.setColor(SkColorSetA(kPointColor, current_opacity)); |
| 164 paint.setStyle(SkPaint::kFill_Style); | 182 paint.setStyle(SkPaint::kFill_Style); |
| 165 canvas->DrawCircle(current_point, kPointInitialRadius, paint); | 183 canvas->DrawCircle(current_point, kPointInitialRadius, paint); |
| 166 } | 184 } |
| 167 } // namespace ash | 185 } // namespace ash |
| OLD | NEW |