Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: ash/common/system/chromeos/palette/tools/laser_pointer_view.cc

Issue 2239743004: Palette tool laser prototype. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@patch
Patch Set: Fixed patch set 3 errors. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 = 4;
25 const double kPointFinalRadius = 0.25;
26 const double kPointInitialOpacity = 200.0;
27 const double kPointFinalOpacity = 0.0;
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, &params);
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,
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) {
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()) {
102 SkPaint paint;
103 paint.setStyle(SkPaint::kStroke_Style);
104 paint.setAntiAlias(true);
105 paint.setStrokeJoin(SkPaint::kBevel_Join);
106 SkColor paint_color;
107
108 base::Time oldest = laser_points_.GetOldest().creation_time;
109 base::Time most_recent = laser_points_.GetMostRecent().creation_time;
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 // Normalize the distance.
jdufault 2016/08/17 21:41:09 Remove this comment
sammiequon 2016/08/18 00:52:08 Done.
121 relative_time = (it->creation_time - oldest).InMillisecondsF() /
122 (most_recent - oldest).InMillisecondsF();
123 relative_time = 1.0 - relative_time;
jdufault 2016/08/17 21:41:09 Inline this with the computation above.
sammiequon 2016/08/18 00:52:08 Done.
124 }
125
126 // Set the radius and opacity based on the distance.
127 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius,
128 relative_time);
129 int opacity = int{LinearInterpolate(kPointInitialOpacity,
130 kPointFinalOpacity, relative_time)};
131
132 gfx::Vector2d center = it->location - widget_bounds.origin();
133 current_point = gfx::Point(center.x(), center.y());
134
135 paint_color = SkColorSetA(kPointColor, opacity);
jdufault 2016/08/17 21:41:09 merge the opacity computation block and this block
sammiequon 2016/08/18 00:52:08 Done.
136 paint.setColor(paint_color);
137 paint.setStrokeWidth(radius * 2);
138
139 // If we draw laser_points_ that are within a stroke width of each other,
140 // the result will be very jagged.
141 float distance_threshold = float{radius * 2};
142 if (point_count == 0) {
143 last_point = current_point;
144 } else if (DistanceBetweenPoints(last_point, current_point) >
145 distance_threshold) {
146 canvas->DrawLine(last_point, current_point, paint);
147 last_point = current_point;
148 } else if (point_count == num_laser_points_ - 1) {
149 canvas->DrawLine(last_point, current_point, paint);
jdufault 2016/08/17 21:41:09 Do we need this special case?
sammiequon 2016/08/18 00:52:08 Yeah otherwise there will be a tiny but still visi
jdufault 2016/08/19 01:02:08 Does the for loop iterate one more time after this
150 }
151 }
152 paint_color = SkColorSetA(kPointColor, int{kPointInitialOpacity});
jdufault 2016/08/17 21:41:08 Store kPointInitialOpacity as an int to remove the
sammiequon 2016/08/18 00:52:08 I'd have to cast twice during the opacity lerp tho
153 paint.setColor(paint_color);
154 paint.setStyle(SkPaint::kFill_Style);
155 canvas->DrawCircle(current_point, kPointInitialRadius, paint);
156 }
157 canvas->Restore();
158 }
159 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698