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 "third_party/skia/include/core/SkRect.h" |
| 16 #include "ui/compositor/layer.h" |
| 17 #include "ui/gfx/canvas.h" |
| 18 #include "ui/gfx/transform.h" |
| 19 #include "ui/wm/core/coordinate_conversion.h" |
| 20 |
| 21 namespace ash { |
| 22 namespace { |
| 23 |
| 24 const double kMouselaserUiStartRadius = 4; |
| 25 const double kMouselaserUiEndRadius = 0.25; |
| 26 const int kMouselaserUiStartOpacityValue = 200; |
| 27 const int kMouselaserUiEndOpacityValue = 0; |
| 28 |
| 29 views::Widget* CreateLaserPointerWidget() { |
| 30 views::Widget* widget = new views::Widget; |
| 31 views::Widget::InitParams params; |
| 32 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| 33 params.name = "LaserOverlay"; |
| 34 params.accept_events = false; |
| 35 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| 36 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 37 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 38 WmShell::Get() |
| 39 ->GetRootWindowForNewWindows() |
| 40 ->GetRootWindowController() |
| 41 ->ConfigureWidgetInitParamsForContainer( |
| 42 widget, kShellWindowId_OverlayContainer, ¶ms); |
| 43 |
| 44 widget->Init(params); |
| 45 widget->SetOpacity(1.f); |
| 46 return widget; |
| 47 } |
| 48 |
| 49 float DistanceBetweenPoints(const gfx::Point& point1, |
| 50 const gfx::Point& point2) { |
| 51 float deltaX = point1.x() - point2.x(); |
| 52 float deltaY = point1.y() - point2.y(); |
| 53 return deltaX * deltaX + deltaY * deltaY; |
| 54 } |
| 55 |
| 56 void PaintLaser(gfx::Canvas* canvas, |
| 57 const LaserPointerPoints& points, |
| 58 const gfx::Rect& widget_bounds) { |
| 59 SkPaint paint; |
| 60 paint.setStyle(SkPaint::kStroke_Style); |
| 61 paint.setAntiAlias(true); |
| 62 paint.setStrokeJoin(SkPaint::kBevel_Join); |
| 63 SkColor paintColor; |
| 64 |
| 65 if (!points.IsEmpty()) { |
| 66 base::Time oldest = points.GetOldest().creationTime_; |
| 67 base::Time most_recent = points.GetMostRecent().creationTime_; |
| 68 gfx::Point last_point, current_point; |
| 69 int point_count = 0; |
| 70 int num_points = points.GetNumberOfPoints(); |
| 71 for (auto it = points.PointsStart(); it != points.PointsEnd(); |
| 72 it++, point_count++) { |
| 73 float normalized; |
| 74 // Only one point, the regular normalization will return undefined for |
| 75 // this case. |
| 76 if (oldest == most_recent) |
| 77 normalized = 1.0; |
| 78 else { // Normalize the distance. |
| 79 normalized = float{(it->creationTime_ - oldest).InMillisecondsF()} / |
| 80 float{(most_recent - oldest).InMillisecondsF()}; |
| 81 normalized = 1.0 - normalized; |
| 82 } |
| 83 |
| 84 float radius = |
| 85 float{kMouselaserUiStartRadius} - |
| 86 normalized * float{kMouselaserUiStartRadius - kMouselaserUiEndRadius}; |
| 87 int opacity = kMouselaserUiStartOpacityValue - |
| 88 int{normalized * float{kMouselaserUiStartOpacityValue - |
| 89 kMouselaserUiEndOpacityValue}}; |
| 90 gfx::Vector2d center = it->location_ - widget_bounds.origin(); |
| 91 current_point = gfx::Point(center.x(), center.y()); |
| 92 paintColor = SkColorSetARGB(opacity, 255, 0, 0); |
| 93 paint.setColor(paintColor); |
| 94 paint.setStrokeWidth(radius * 2); |
| 95 if (point_count == 0) { |
| 96 last_point = current_point; |
| 97 } else if (DistanceBetweenPoints(last_point, current_point) > |
| 98 radius * 4 * radius) { |
| 99 canvas->DrawLine(last_point, current_point, paint); |
| 100 last_point = current_point; |
| 101 } else if (point_count == num_points - 1) { |
| 102 canvas->DrawLine(last_point, current_point, paint); |
| 103 } |
| 104 } |
| 105 paintColor = SkColorSetARGB(kMouselaserUiStartOpacityValue, 255, 0, 0); |
| 106 paint.setColor(paintColor); |
| 107 paint.setStyle(SkPaint::kFill_Style); |
| 108 canvas->DrawCircle(current_point, kMouselaserUiStartRadius, paint); |
| 109 } |
| 110 } |
| 111 } // namespace |
| 112 |
| 113 //////////////////////////////////////////////////////////////////////////////// |
| 114 |
| 115 // LaserPointerView |
| 116 LaserPointerView::LaserPointerView() |
| 117 : views::View(), laser_points_(base::TimeDelta()) { |
| 118 gfx::Rect newBoundingBox; |
| 119 newBoundingBox.SetRect(0, 0, 1388, 768); |
| 120 widget_.reset(CreateLaserPointerWidget()); |
| 121 widget_->SetBounds(newBoundingBox); |
| 122 widget_->Show(); |
| 123 widget_->SetContentsView(this); |
| 124 } |
| 125 |
| 126 LaserPointerView::~LaserPointerView() { |
| 127 Stop(); |
| 128 } |
| 129 |
| 130 void LaserPointerView::Stop() { |
| 131 laser_points_.Clear(); |
| 132 SetNewPoints(laser_points_); |
| 133 } |
| 134 |
| 135 void LaserPointerView::SetNewPoints(const LaserPointerPoints& laser_points) { |
| 136 laser_points_ = laser_points; |
| 137 gfx::Rect oldBoundingBox, newBoundingBox; |
| 138 oldBoundingBox = laser_points.GetBoundingBox(); |
| 139 newBoundingBox.SetRect( |
| 140 oldBoundingBox.x() - kMouselaserUiStartRadius, |
| 141 oldBoundingBox.y() - kMouselaserUiStartRadius, |
| 142 oldBoundingBox.width() + 2 * kMouselaserUiStartRadius, |
| 143 oldBoundingBox.height() + 2 * kMouselaserUiStartRadius); |
| 144 widget_->SetBounds(newBoundingBox); |
| 145 SchedulePaint(); |
| 146 } |
| 147 |
| 148 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { |
| 149 canvas->Save(); |
| 150 PaintLaser(canvas, laser_points_, widget_->GetWindowBoundsInScreen()); |
| 151 canvas->Restore(); |
| 152 } |
| 153 } // namespace ash |
OLD | NEW |