Chromium Code Reviews| 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_view.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "ash/common/shell_window_ids.h" | |
| 10 #include "ash/common/wm_root_window_controller.h" | |
| 11 #include "ash/common/wm_shell.h" | |
| 12 #include "ash/common/wm_window.h" | |
| 13 #include "third_party/skia/include/core/SkColor.h" | |
| 14 #include "third_party/skia/include/core/SkPaint.h" | |
| 15 #include "ui/gfx/canvas.h" | |
| 16 | |
| 17 namespace ash { | |
| 18 namespace { | |
| 19 | |
| 20 const double kPointInitialRadius = 5; | |
| 21 const double kPointFinalRadius = 0.25; | |
| 22 const int kPointInitialOpacity = 200; | |
| 23 const int kPointFinalOpacity = 0; | |
| 24 const SkColor kPointColor = SkColorSetRGB(255, 0, 0); | |
| 25 | |
| 26 views::Widget* CreateLaserPointerWidget() { | |
| 27 views::Widget* widget = new views::Widget; | |
| 28 views::Widget::InitParams params; | |
| 29 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; | |
| 30 params.name = "LaserOverlay"; | |
| 31 params.accept_events = false; | |
| 32 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | |
| 33 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 34 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 35 WmShell::Get() | |
| 36 ->GetRootWindowForNewWindows() | |
| 37 ->GetRootWindowController() | |
| 38 ->ConfigureWidgetInitParamsForContainer( | |
| 39 widget, kShellWindowId_OverlayContainer, ¶ms); | |
| 40 | |
| 41 widget->Init(params); | |
| 42 return widget; | |
| 43 } | |
| 44 | |
| 45 float DistanceBetweenPoints(const gfx::Point& point1, | |
| 46 const gfx::Point& point2) { | |
| 47 return (point1 - point2).Length(); | |
| 48 } | |
| 49 | |
| 50 double LinearInterpolate(double initial_value, | |
| 51 double final_value, | |
| 52 double progress) { | |
| 53 return initial_value + (final_value - initial_value) * progress; | |
| 54 } | |
| 55 } // namespace | |
| 56 | |
| 57 //////////////////////////////////////////////////////////////////////////////// | |
| 58 | |
| 59 // LaserPointerView | |
| 60 LaserPointerView::LaserPointerView(base::TimeDelta life_duration) | |
| 61 : laser_points_(life_duration) { | |
| 62 widget_.reset(CreateLaserPointerWidget()); | |
| 63 widget_->Show(); | |
| 64 widget_->SetContentsView(this); | |
| 65 set_owned_by_client(); | |
| 66 } | |
| 67 | |
| 68 LaserPointerView::~LaserPointerView() {} | |
| 69 | |
| 70 void LaserPointerView::Stop() { | |
| 71 laser_points_.Clear(); | |
| 72 SchedulePaint(); | |
| 73 } | |
| 74 | |
| 75 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { | |
| 76 laser_points_.AddPoint(new_point); | |
| 77 // Expand the bounding box so that it includes the radius of the points on the | |
| 78 // edges. | |
| 79 gfx::Rect bounding_box; | |
| 80 bounding_box = laser_points_.GetBoundingBox(); | |
| 81 bounding_box.SetRect(bounding_box.x() - kPointInitialRadius, | |
| 82 bounding_box.y() - kPointInitialRadius, | |
| 83 bounding_box.width() + (kPointInitialRadius * 2), | |
| 84 bounding_box.height() + (kPointInitialRadius * 2)); | |
| 85 widget_->SetBounds(bounding_box); | |
| 86 SchedulePaint(); | |
| 87 } | |
| 88 | |
| 89 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { | |
| 90 if (laser_points_.IsEmpty()) | |
| 91 return; | |
| 92 | |
| 93 canvas->Save(); | |
| 94 SkPaint paint; | |
| 95 paint.setStyle(SkPaint::kStroke_Style); | |
| 96 paint.setAntiAlias(true); | |
| 97 paint.setStrokeJoin(SkPaint::kBevel_Join); | |
| 98 | |
| 99 base::Time oldest = laser_points_.GetOldest().creation_time; | |
| 100 base::Time newest = laser_points_.GetNewest().creation_time; | |
| 101 gfx::Point last_point, current_point; | |
| 102 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); | |
| 103 int point_count = 0; | |
| 104 int num_points_ = laser_points_.GetNumberOfPoints(); | |
| 105 for (auto it = laser_points_.PointsStart(); it != laser_points_.PointsEnd(); | |
| 106 it++, point_count++) { | |
| 107 // Normalized is a value between [0,1] where 0 means the point is about to | |
|
jdufault
2016/08/22 18:37:36
Update comment to reflect the current variable nam
sammiequon
2016/08/23 00:20:33
Done.
| |
| 108 // be removed and 1 means that the point was just added. | |
| 109 double relative_time = 1.0; | |
| 110 if (oldest != newest) { | |
| 111 relative_time = 1.0 - ((it->creation_time - oldest).InMillisecondsF() / | |
| 112 (newest - oldest).InMillisecondsF()); | |
| 113 } | |
| 114 | |
| 115 // Set the radius and opacity based on the distance. | |
| 116 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, | |
| 117 relative_time); | |
| 118 int opacity = | |
| 119 int{LinearInterpolate(double{kPointInitialOpacity}, | |
| 120 double{kPointFinalOpacity}, relative_time)}; | |
| 121 | |
| 122 paint.setColor(SkColorSetA(kPointColor, opacity)); | |
| 123 paint.setStrokeWidth(radius * 2); | |
| 124 | |
| 125 gfx::Vector2d center = it->location - widget_bounds.origin(); | |
| 126 current_point = gfx::Point(center.x(), center.y()); | |
| 127 | |
| 128 // If we draw laser_points_ that are within a stroke width of each other, | |
| 129 // the result will be very jagged. | |
| 130 float distance_threshold = float{radius * 2}; | |
| 131 if (point_count == 0) { | |
| 132 last_point = current_point; | |
| 133 } else if (DistanceBetweenPoints(last_point, current_point) > | |
| 134 distance_threshold) { | |
| 135 canvas->DrawLine(last_point, current_point, paint); | |
| 136 last_point = current_point; | |
| 137 } else if (point_count == num_points_ - 1) { | |
|
jdufault
2016/08/22 18:37:37
Can this else if be combined with the previous one
sammiequon
2016/08/23 00:20:33
Done.
| |
| 138 canvas->DrawLine(last_point, current_point, paint); | |
| 139 } | |
| 140 } | |
| 141 paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity)); | |
| 142 paint.setStyle(SkPaint::kFill_Style); | |
| 143 canvas->DrawCircle(current_point, kPointInitialRadius, paint); | |
| 144 } | |
| 145 } // namespace ash | |
| OLD | NEW |