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/common/system/chromeos/palette/tools/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/common/wm_root_window_controller.h" | 10 #include "ash/laser/laser_pointer_points.h" |
| 11 #include "ash/common/wm_shell.h" | 11 #include "base/timer/timer.h" |
| 12 #include "ash/common/wm_window.h" | |
| 13 #include "third_party/skia/include/core/SkColor.h" | 12 #include "third_party/skia/include/core/SkColor.h" |
| 14 #include "third_party/skia/include/core/SkPaint.h" | 13 #include "third_party/skia/include/core/SkPaint.h" |
| 14 #include "ui/aura/window.h" | |
| 15 #include "ui/events/event.h" | |
| 15 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 17 #include "ui/views/widget/widget.h" | |
| 16 | 18 |
| 17 namespace ash { | 19 namespace ash { |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 22 // Variables for rendering the laser. Radius in DIP. | |
| 20 const double kPointInitialRadius = 5; | 23 const double kPointInitialRadius = 5; |
| 21 const double kPointFinalRadius = 0.25; | 24 const double kPointFinalRadius = 0.25; |
| 22 const int kPointInitialOpacity = 200; | 25 const int kPointInitialOpacity = 200; |
| 23 const int kPointFinalOpacity = 0; | 26 const int kPointFinalOpacity = 0; |
| 24 const SkColor kPointColor = SkColorSetRGB(255, 0, 0); | 27 const SkColor kPointColor = SkColorSetRGB(255, 0, 0); |
| 25 | 28 |
| 26 float DistanceBetweenPoints(const gfx::Point& point1, | 29 float DistanceBetweenPoints(const gfx::Point& point1, |
| 27 const gfx::Point& point2) { | 30 const gfx::Point& point2) { |
| 28 return (point1 - point2).Length(); | 31 return (point1 - point2).Length(); |
| 29 } | 32 } |
| 30 | 33 |
| 31 double LinearInterpolate(double initial_value, | 34 double LinearInterpolate(double initial_value, |
| 32 double final_value, | 35 double final_value, |
| 33 double progress) { | 36 double progress) { |
| 34 return initial_value + (final_value - initial_value) * progress; | 37 return initial_value + (final_value - initial_value) * progress; |
| 35 } | 38 } |
| 36 | 39 |
| 37 } // namespace | 40 } // namespace |
| 38 | 41 |
| 39 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
| 40 | 43 |
| 41 // LaserPointerView | 44 // LaserPointerView |
| 42 LaserPointerView::LaserPointerView(base::TimeDelta life_duration) | 45 LaserPointerView::LaserPointerView(base::TimeDelta life_duration, |
| 46 aura::Window* root_window) | |
| 43 : laser_points_(life_duration) { | 47 : laser_points_(life_duration) { |
| 44 widget_.reset(new views::Widget); | 48 widget_.reset(new views::Widget); |
| 45 views::Widget::InitParams params; | 49 views::Widget::InitParams params; |
| 46 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; | 50 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| 47 params.name = "LaserOverlay"; | 51 params.name = "Laser Overlay"; |
| 48 params.accept_events = false; | 52 params.accept_events = false; |
| 49 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | 53 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| 50 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 54 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 51 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 55 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 52 WmShell::Get() | 56 params.parent = root_window; |
|
James Cook
2016/09/14 22:53:38
I think this is wrong. I didn't notice before beca
sammiequon
2016/09/16 17:35:26
Done.
| |
| 53 ->GetRootWindowForNewWindows() | 57 |
| 54 ->GetRootWindowController() | |
| 55 ->ConfigureWidgetInitParamsForContainer( | |
| 56 widget_.get(), kShellWindowId_OverlayContainer, ¶ms); | |
| 57 widget_->Init(params); | 58 widget_->Init(params); |
| 58 widget_->Show(); | 59 widget_->Show(); |
| 59 widget_->SetContentsView(this); | 60 widget_->SetContentsView(this); |
| 60 set_owned_by_client(); | 61 set_owned_by_client(); |
| 61 } | 62 } |
| 62 | 63 |
| 63 LaserPointerView::~LaserPointerView() {} | 64 LaserPointerView::~LaserPointerView() {} |
| 64 | 65 |
| 65 void LaserPointerView::Stop() { | 66 void LaserPointerView::Stop() { |
| 66 laser_points_.Clear(); | 67 laser_points_.Clear(); |
| 67 SchedulePaint(); | 68 SchedulePaint(); |
| 68 } | 69 } |
| 69 | 70 |
| 71 aura::Window* LaserPointerView::GetRootWindow() { | |
| 72 return widget_->GetNativeView()->GetRootWindow(); | |
| 73 } | |
| 74 | |
| 70 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { | 75 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { |
| 71 laser_points_.AddPoint(new_point); | 76 laser_points_.AddPoint(new_point); |
| 72 // Expand the bounding box so that it includes the radius of the points on the | 77 // Expand the bounding box so that it includes the radius of the points on the |
| 73 // edges. | 78 // edges. |
| 74 gfx::Rect bounding_box; | 79 gfx::Rect bounding_box; |
| 75 bounding_box = laser_points_.GetBoundingBox(); | 80 bounding_box = laser_points_.GetBoundingBox(); |
| 76 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius); | 81 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius); |
| 77 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2)); | 82 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2)); |
| 78 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2)); | 83 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2)); |
| 79 widget_->SetBounds(bounding_box); | 84 widget_->SetBounds(bounding_box); |
| 80 SchedulePaint(); | 85 SchedulePaint(); |
| 81 } | 86 } |
| 82 | 87 |
| 83 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { | 88 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { |
| 84 if (laser_points_.IsEmpty()) | 89 if (laser_points_.IsEmpty()) |
| 85 return; | 90 return; |
| 86 | 91 |
| 87 SkPaint paint; | 92 SkPaint paint; |
| 88 paint.setStyle(SkPaint::kStroke_Style); | 93 paint.setStyle(SkPaint::kStroke_Style); |
| 89 paint.setAntiAlias(true); | 94 paint.setAntiAlias(true); |
| 90 paint.setStrokeJoin(SkPaint::kBevel_Join); | 95 paint.setStrokeJoin(SkPaint::kBevel_Join); |
| 91 | 96 |
| 92 base::Time oldest = laser_points_.GetOldest().creation_time; | 97 base::Time oldest = laser_points_.GetOldest().creation_time; |
| 93 base::Time newest = laser_points_.GetNewest().creation_time; | 98 base::Time newest = laser_points_.GetNewest().creation_time; |
| 94 gfx::Point previous_point = laser_points_.GetOldest().location; | 99 gfx::Point previous_point = laser_points_.GetOldest().location; |
| 95 gfx::Point current_point; | 100 gfx::Point current_point; |
| 96 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); | 101 |
| 102 // Compute the offset of the current widget. | |
| 103 gfx::Point offset = | |
|
James Cook
2016/09/14 22:53:38
how about |widget_offset| or |widget_origin| ?
sammiequon
2016/09/16 17:35:26
Done.
| |
| 104 widget_->GetNativeView()->GetBoundsInRootWindow().origin(); | |
| 97 int num_points_ = laser_points_.GetNumberOfPoints(); | 105 int num_points_ = laser_points_.GetNumberOfPoints(); |
| 98 int point_count = 0; | 106 int point_count = 0; |
| 99 for (const LaserPointerPoints::LaserPoint& point : | 107 for (const LaserPointerPoints::LaserPoint& point : |
| 100 laser_points_.laser_points()) { | 108 laser_points_.laser_points()) { |
| 101 // relative_time is a value between [0,1] where 0 means the point is about | 109 // relative_time is a value between [0,1] where 0 means the point is about |
| 102 // to be removed and 1 means that the point was just added. | 110 // to be removed and 1 means that the point was just added. |
| 103 double relative_time = 1.0; | 111 double relative_time = 1.0; |
| 104 if (oldest != newest) { | 112 if (oldest != newest) { |
| 105 relative_time = 1.0 - ((point.creation_time - oldest).InMillisecondsF() / | 113 relative_time = 1.0 - ((point.creation_time - oldest).InMillisecondsF() / |
| 106 (newest - oldest).InMillisecondsF()); | 114 (newest - oldest).InMillisecondsF()); |
| 107 } | 115 } |
| 108 | 116 |
| 109 // Set the radius and opacity based on the distance. | 117 // Set the radius and opacity based on the distance. |
| 110 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, | 118 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, |
| 111 relative_time); | 119 relative_time); |
| 112 | 120 |
| 113 gfx::Vector2d center = point.location - widget_bounds.origin(); | 121 gfx::Vector2d center = point.location - offset; |
| 114 current_point = gfx::Point(center.x(), center.y()); | 122 current_point = gfx::Point(center.x(), center.y()); |
| 115 | 123 |
| 116 // If we draw laser_points_ that are within a stroke width of each other, | 124 // If we draw laser_points_ that are within a stroke width of each other, |
| 117 // the result will be very jagged, unless we are on the last point, then we | 125 // the result will be very jagged, unless we are on the last point, then we |
| 118 // draw regardless. | 126 // draw regardless. |
| 119 point_count++; | 127 point_count++; |
| 120 float distance_threshold = float{radius * 2}; | 128 float distance_threshold = float{radius * 2}; |
| 121 if (DistanceBetweenPoints(previous_point, current_point) <= | 129 if (DistanceBetweenPoints(previous_point, current_point) <= |
| 122 distance_threshold && | 130 distance_threshold && |
| 123 point_count != num_points_) { | 131 point_count != num_points_) { |
| 124 continue; | 132 continue; |
| 125 } | 133 } |
| 126 | 134 |
| 127 int opacity = | 135 int opacity = |
| 128 int{LinearInterpolate(double{kPointInitialOpacity}, | 136 int{LinearInterpolate(double{kPointInitialOpacity}, |
| 129 double{kPointFinalOpacity}, relative_time)}; | 137 double{kPointFinalOpacity}, relative_time)}; |
| 130 paint.setColor(SkColorSetA(kPointColor, opacity)); | 138 paint.setColor(SkColorSetA(kPointColor, opacity)); |
| 131 paint.setStrokeWidth(radius * 2); | 139 paint.setStrokeWidth(radius * 2); |
| 132 canvas->DrawLine(previous_point, current_point, paint); | 140 canvas->DrawLine(previous_point, current_point, paint); |
| 133 previous_point = current_point; | 141 previous_point = current_point; |
| 134 } | 142 } |
| 143 // Draw the last point as a circle. | |
| 135 paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity)); | 144 paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity)); |
| 136 paint.setStyle(SkPaint::kFill_Style); | 145 paint.setStyle(SkPaint::kFill_Style); |
| 137 canvas->DrawCircle(current_point, kPointInitialRadius, paint); | 146 canvas->DrawCircle(current_point, kPointInitialRadius, paint); |
| 138 } | 147 } |
| 139 } // namespace ash | 148 } // namespace ash |
| OLD | NEW |