Chromium Code Reviews| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 if (GetRootWindow() != new_root_window) { | 78 if (GetRootWindow() != new_root_window) { |
| 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 bool fading_away) { |
| 90 // Do not add the point but advance the time if the view is in process of | |
| 91 // fading away. | |
| 92 if (fading_away) | |
| 93 laser_points_.MoveForwardToCurrentTime(); | |
| 94 else | |
| 95 laser_points_.AddPoint(new_point); | |
| 90 | 96 |
| 91 // The bounding box should be relative to the screen. | 97 // The bounding box should be relative to the screen. |
| 92 gfx::Point screen_offset = | 98 gfx::Point screen_offset = |
| 93 widget_->GetNativeView()->GetRootWindow()->GetBoundsInScreen().origin(); | 99 widget_->GetNativeView()->GetRootWindow()->GetBoundsInScreen().origin(); |
| 94 | 100 |
| 95 // Expand the bounding box so that it includes the radius of the points on the | 101 // Expand the bounding box so that it includes the radius of the points on the |
| 96 // edges. | 102 // edges. |
| 97 gfx::Rect bounding_box; | 103 gfx::Rect bounding_box; |
| 98 bounding_box = laser_points_.GetBoundingBox(); | 104 bounding_box = laser_points_.GetBoundingBox(); |
| 99 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius); | 105 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius); |
| 100 bounding_box.Offset(screen_offset.x(), screen_offset.y()); | 106 bounding_box.Offset(screen_offset.x(), screen_offset.y()); |
| 101 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2)); | 107 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2)); |
| 102 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2)); | 108 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2)); |
| 103 widget_->SetBounds(bounding_box); | 109 widget_->SetBounds(bounding_box); |
| 104 SchedulePaint(); | 110 SchedulePaint(); |
| 105 } | 111 } |
| 106 | 112 |
| 107 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { | 113 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { |
| 108 if (laser_points_.IsEmpty()) | 114 if (laser_points_.IsEmpty()) |
| 109 return; | 115 return; |
| 110 | 116 |
| 111 SkPaint paint; | 117 SkPaint paint; |
| 112 paint.setStyle(SkPaint::kStroke_Style); | 118 paint.setStyle(SkPaint::kStroke_Style); |
| 113 paint.setAntiAlias(true); | 119 paint.setAntiAlias(true); |
| 114 paint.setStrokeJoin(SkPaint::kBevel_Join); | 120 paint.setStrokeJoin(SkPaint::kBevel_Join); |
| 115 | 121 |
| 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. | 122 // Compute the offset of the current widget. |
| 122 gfx::Point widget_offset = | 123 gfx::Point widget_offset = |
| 123 widget_->GetNativeView()->GetBoundsInRootWindow().origin(); | 124 widget_->GetNativeView()->GetBoundsInRootWindow().origin(); |
| 125 | |
| 126 gfx::Vector2d previous_point_vector = | |
|
jdufault
2016/10/04 22:04:13
FYI, this variable is very confusing (it's not cle
sammiequon
2016/10/05 17:42:23
Done.
| |
| 127 laser_points_.GetOldest().location - widget_offset; | |
| 128 gfx::Point previous_point = | |
| 129 gfx::Point(previous_point_vector.x(), previous_point_vector.y()); | |
| 130 gfx::Point current_point; | |
|
jdufault
2016/10/04 22:04:13
Declare this on line 144 since it is not used outs
sammiequon
2016/10/05 17:42:23
It's used on line 165.
| |
| 131 | |
| 124 int num_points_ = laser_points_.GetNumberOfPoints(); | 132 int num_points_ = laser_points_.GetNumberOfPoints(); |
| 125 int point_count = 0; | 133 int point_count = 0; |
| 134 int current_opacity = 0; | |
| 126 for (const LaserPointerPoints::LaserPoint& point : | 135 for (const LaserPointerPoints::LaserPoint& point : |
| 127 laser_points_.laser_points()) { | 136 laser_points_.laser_points()) { |
| 128 // 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. | |
| 130 double relative_time = 1.0; | |
| 131 if (oldest != newest) { | |
| 132 relative_time = 1.0 - ((point.creation_time - oldest).InMillisecondsF() / | |
| 133 (newest - oldest).InMillisecondsF()); | |
| 134 } | |
| 135 | |
| 136 // Set the radius and opacity based on the distance. | 137 // Set the radius and opacity based on the distance. |
| 137 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, | 138 double current_radius = |
| 138 relative_time); | 139 LinearInterpolate(kPointInitialRadius, kPointFinalRadius, point.age); |
| 140 current_opacity = int{LinearInterpolate( | |
| 141 double{kPointInitialOpacity}, double{kPointFinalOpacity}, point.age)}; | |
| 139 | 142 |
| 140 gfx::Vector2d center = point.location - widget_offset; | 143 gfx::Vector2d center = point.location - widget_offset; |
| 141 current_point = gfx::Point(center.x(), center.y()); | 144 current_point = gfx::Point(center.x(), center.y()); |
| 142 | 145 |
| 143 // If we draw laser_points_ that are within a stroke width of each other, | 146 // 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 | 147 // the result will be very jagged, unless we are on the last point, then we |
| 145 // draw regardless. | 148 // draw regardless. |
| 146 point_count++; | 149 point_count++; |
| 147 float distance_threshold = float{radius * 2}; | 150 float distance_threshold = float{current_radius * 2}; |
| 148 if (DistanceBetweenPoints(previous_point, current_point) <= | 151 if (DistanceBetweenPoints(previous_point, current_point) <= |
| 149 distance_threshold && | 152 distance_threshold && |
| 150 point_count != num_points_) { | 153 point_count != num_points_) { |
| 151 continue; | 154 continue; |
| 152 } | 155 } |
| 153 | 156 |
| 154 int opacity = | 157 paint.setColor(SkColorSetA(kPointColor, current_opacity)); |
| 155 int{LinearInterpolate(double{kPointInitialOpacity}, | 158 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); | 159 canvas->DrawLine(previous_point, current_point, paint); |
| 160 previous_point = current_point; | 160 previous_point = current_point; |
| 161 } | 161 } |
| 162 // Draw the last point as a circle. | 162 // Draw the last point as a circle. |
| 163 paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity)); | 163 paint.setColor(SkColorSetA(kPointColor, current_opacity)); |
| 164 paint.setStyle(SkPaint::kFill_Style); | 164 paint.setStyle(SkPaint::kFill_Style); |
| 165 canvas->DrawCircle(current_point, kPointInitialRadius, paint); | 165 canvas->DrawCircle(current_point, kPointInitialRadius, paint); |
| 166 } | 166 } |
| 167 } // namespace ash | 167 } // namespace ash |
| OLD | NEW |