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 kPointInitialRadius = 5; | |
25 const double kPointFinalRadius = 0.25; | |
26 const double kPointInitialOpacity = 200.0; | |
27 const double kPointFinalOpacity = 0.0; | |
jdufault
2016/08/19 01:02:09
Conceptually I think these are integral values, so
sammiequon
2016/08/19 20:28:47
Done.
| |
28 const SkColor kPointColor = SkColorSetRGB(255, 0, 0); | |
29 | |
30 views::Widget* CreateLaserPointerWidget() { | |
31 views::Widget* widget = new views::Widget; | |
32 views::Widget::InitParams params; | |
33 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; | |
34 params.name = "LaserOverlay"; | |
35 params.accept_events = false; | |
36 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | |
37 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
38 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
39 WmShell::Get() | |
40 ->GetRootWindowForNewWindows() | |
41 ->GetRootWindowController() | |
42 ->ConfigureWidgetInitParamsForContainer( | |
43 widget, kShellWindowId_OverlayContainer, ¶ms); | |
44 | |
45 widget->Init(params); | |
46 return widget; | |
47 } | |
48 | |
49 float DistanceBetweenPoints(const gfx::Point& point1, | |
50 const gfx::Point& point2) { | |
51 return (point1 - point2).Length(); | |
52 } | |
53 | |
54 double LinearInterpolate(double initialValue, | |
jdufault
2016/08/19 01:02:09
initial, final, progress
sammiequon
2016/08/19 20:28:47
Final is a keyword and i thought it will be weird
| |
55 double finalValue, | |
56 double progress) { | |
57 return initialValue + (finalValue - initialValue) * progress; | |
58 } | |
59 | |
60 void PaintLaser(gfx::Canvas* canvas, | |
61 const LaserPointerPoints& points, | |
62 const gfx::Rect& widget_bounds) {} | |
63 | |
64 } // namespace | |
65 | |
66 //////////////////////////////////////////////////////////////////////////////// | |
67 | |
68 // LaserPointerView | |
69 LaserPointerView::LaserPointerView(base::TimeDelta life_duration) | |
70 : views::View(), laser_points_(life_duration) { | |
jdufault
2016/08/19 01:02:09
The default ctor call shouldn't be needed.
sammiequon
2016/08/19 20:28:47
Done.
| |
71 widget_.reset(CreateLaserPointerWidget()); | |
72 widget_->Show(); | |
73 widget_->SetContentsView(this); | |
74 set_owned_by_client(); | |
75 } | |
76 | |
77 LaserPointerView::~LaserPointerView() {} | |
78 | |
79 void LaserPointerView::Stop() { | |
80 // Clear the points so that there is nothing to be rendered. | |
81 laser_points_.Clear(); | |
82 SchedulePaint(); | |
83 } | |
84 | |
85 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { | |
86 laser_points_.AddPoint(new_point); | |
87 // Set the bounding box of the points to be able to enclose a point of max | |
88 // radius located on an edge. | |
89 gfx::Rect bounding_box; | |
90 bounding_box = laser_points_.GetBoundingBox(); | |
91 bounding_box.SetRect(bounding_box.x() - kPointInitialRadius, | |
92 bounding_box.y() - kPointInitialRadius, | |
93 bounding_box.width() + (kPointInitialRadius * 2), | |
94 bounding_box.height() + (kPointInitialRadius * 2)); | |
95 widget_->SetBounds(bounding_box); | |
96 SchedulePaint(); | |
97 } | |
98 | |
99 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { | |
100 canvas->Save(); | |
101 if (!laser_points_.IsEmpty()) { | |
jdufault
2016/08/19 01:02:09
Can we move the if (laser_points_.IsEmpty()) call
sammiequon
2016/08/19 20:28:47
Done.
| |
102 SkPaint paint; | |
103 paint.setStyle(SkPaint::kStroke_Style); | |
104 paint.setAntiAlias(true); | |
105 paint.setStrokeJoin(SkPaint::kBevel_Join); | |
106 SkColor paint_color; | |
jdufault
2016/08/19 01:02:09
I would inline paint_color computations and remove
sammiequon
2016/08/19 20:28:47
Done.
| |
107 | |
108 base::Time oldest = laser_points_.GetOldest().creation_time; | |
109 base::Time most_recent = laser_points_.GetMostRecent().creation_time; | |
jdufault
2016/08/19 01:02:09
newest?
sammiequon
2016/08/19 20:28:47
Done.
| |
110 gfx::Point last_point, current_point; | |
111 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); | |
112 int point_count = 0; | |
113 int num_laser_points_ = laser_points_.GetNumberOfPoints(); | |
114 for (auto it = laser_points_.PointsStart(); it != laser_points_.PointsEnd(); | |
115 it++, point_count++) { | |
116 // Normalized is a value between [0,1] where 0 means the point is about to | |
117 // be removed and 1 means that the point was just added. | |
118 double relative_time = 1.0; | |
119 if (oldest != most_recent) { | |
120 relative_time = 1.0 - ((it->creation_time - oldest).InMillisecondsF() / | |
121 (most_recent - oldest).InMillisecondsF()); | |
122 } | |
123 | |
124 // Set the radius and opacity based on the distance. | |
125 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, | |
126 relative_time); | |
127 int opacity = int{LinearInterpolate(kPointInitialOpacity, | |
128 kPointFinalOpacity, relative_time)}; | |
129 | |
130 gfx::Vector2d center = it->location - widget_bounds.origin(); | |
131 current_point = gfx::Point(center.x(), center.y()); | |
132 | |
133 paint_color = SkColorSetA(kPointColor, opacity); | |
134 paint.setColor(paint_color); | |
135 paint.setStrokeWidth(radius * 2); | |
136 | |
137 // If we draw laser_points_ that are within a stroke width of each other, | |
138 // the result will be very jagged. | |
139 float distance_threshold = float{radius * 2}; | |
140 if (point_count == 0) { | |
141 last_point = current_point; | |
142 } else if (DistanceBetweenPoints(last_point, current_point) > | |
143 distance_threshold) { | |
144 canvas->DrawLine(last_point, current_point, paint); | |
145 last_point = current_point; | |
146 } else if (point_count == num_laser_points_ - 1) { | |
147 canvas->DrawLine(last_point, current_point, paint); | |
148 } | |
149 } | |
150 paint_color = SkColorSetA(kPointColor, int{kPointInitialOpacity}); | |
151 paint.setColor(paint_color); | |
152 paint.setStyle(SkPaint::kFill_Style); | |
153 canvas->DrawCircle(current_point, kPointInitialRadius, paint); | |
154 } | |
155 canvas->Restore(); | |
156 } | |
157 } // namespace ash | |
OLD | NEW |