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

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 2 errors and removed patch dependency. 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 #include "ash/common/shell_window_ids.h"
jdufault 2016/08/16 19:33:58 newline between <memory> and "ash/..."
sammiequon 2016/08/16 23:18:55 Done.
9 #include "ash/common/wm_root_window_controller.h"
10 #include "ash/common/wm_shell.h"
11 #include "ash/common/wm_window.h"
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "third_party/skia/include/core/SkPaint.h"
14 #include "third_party/skia/include/core/SkRect.h"
15 #include "ui/compositor/layer.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/transform.h"
18 #include "ui/wm/core/coordinate_conversion.h"
19
20 namespace ash {
21 namespace {
22
23 const double kMouselaserUiStartRadius = 4;
jdufault 2016/08/16 19:33:57 What about kPointInitialRadius = 4 kPointFinalRad
jdufault 2016/08/16 19:33:58 Drop Mouse* prefix on all of these.
sammiequon 2016/08/16 23:18:55 Done.
sammiequon 2016/08/16 23:18:55 Done.
24 const double kMouselaserUiEndRadius = 0.25;
25 const int kMouselaserUiStartOpacityValue = 200;
jdufault 2016/08/16 19:33:58 kPointInitialOpacity kPointFinalOpacity
sammiequon 2016/08/16 23:18:56 Done.
26 const int kMouselaserUiEndOpacityValue = 0;
27 const int kMouselaserBlueValue = 0;
jdufault 2016/08/16 19:33:58 kPointColor
sammiequon 2016/08/16 23:18:55 Done.
28 const int kMouselaserGreenValue = 0;
29 const int kMouselaserRedValue = 255;
jdufault 2016/08/16 19:33:57 Declare the RGB values together in an SkColor inst
sammiequon 2016/08/16 23:18:55 Done.
30
31 views::Widget* CreateLaserPointerWidget() {
32 views::Widget* widget = new views::Widget;
33 views::Widget::InitParams params;
34 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
35 params.name = "LaserOverlay";
36 params.accept_events = false;
37 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
38 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
39 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
40 WmShell::Get()
41 ->GetRootWindowForNewWindows()
42 ->GetRootWindowController()
43 ->ConfigureWidgetInitParamsForContainer(
44 widget, kShellWindowId_OverlayContainer, &params);
45
46 widget->Init(params);
47 return widget;
48 }
49
50 float DistanceBetweenPoints(const gfx::Point& point1,
51 const gfx::Point& point2) {
52 gfx::Vector2d vector = point1 - point2;
jdufault 2016/08/16 19:33:57 return (point1 - point2).Length()
sammiequon 2016/08/16 23:18:55 Done.
53 return vector.Length();
54 }
55
56 void PaintLaser(gfx::Canvas* canvas,
57 const LaserPointerPoints& points,
58 const gfx::Rect& widget_bounds) {
59 if (points.IsEmpty())
60 return;
61
62 SkPaint paint;
63 paint.setStyle(SkPaint::kStroke_Style);
64 paint.setAntiAlias(true);
65 paint.setStrokeJoin(SkPaint::kBevel_Join);
66 SkColor paintColor;
67
68 base::Time oldest = points.GetOldest().creation_time;
69 base::Time most_recent = points.GetMostRecent().creation_time;
70 gfx::Point last_point, current_point;
71 int point_count = 0;
72 int num_points = points.GetNumberOfPoints();
73 for (auto it = points.PointsStart(); it != points.PointsEnd();
74 it++, point_count++) {
75 double normalized;
jdufault 2016/08/16 19:33:57 double normalized = 1.0; if (oldest != most_recent
jdufault 2016/08/16 19:33:57 What do you think of the name 'progress' or 'relat
jdufault 2016/08/16 19:33:58 Add a comment along the lines of // Normalized is
sammiequon 2016/08/16 23:18:55 Done.
sammiequon 2016/08/16 23:18:56 Done.
sammiequon 2016/08/16 23:18:56 Done.
76 // Only one point, the regular normalization will return undefined for
77 // this case.
78 if (oldest == most_recent)
79 normalized = 1.0;
80 else { // Normalize the distance.
81 normalized = (it->creation_time - oldest).InMillisecondsF() /
82 (most_recent - oldest).InMillisecondsF();
83 normalized = 1.0 - normalized;
84 }
85
86 // Set the radius and opacity based on the distance.
87 double radius =
88 kMouselaserUiStartRadius -
jdufault 2016/08/16 19:33:57 Add a lerp function and replace all of the instanc
sammiequon 2016/08/16 23:18:55 Done.
89 normalized * (kMouselaserUiStartRadius - kMouselaserUiEndRadius);
90 int opacity = kMouselaserUiStartOpacityValue -
91 int{normalized * (kMouselaserUiStartOpacityValue -
92 kMouselaserUiEndOpacityValue)};
93 gfx::Vector2d center = it->location - widget_bounds.origin();
94 current_point = gfx::Point(center.x(), center.y());
95 paintColor = SkColorSetARGB(opacity, kMouselaserRedValue,
jdufault 2016/08/16 19:33:58 paint_color
sammiequon 2016/08/16 23:18:55 Done.
96 kMouselaserGreenValue, kMouselaserBlueValue);
97 paint.setColor(paintColor);
98 paint.setStrokeWidth(radius * 2);
99
100 // If we draw points that are within a stroke width of each other, the
101 // result will be very jagged.
102 float distance_threshold = float{radius * 2};
jdufault 2016/08/16 19:33:57 Can we use a double to avoid the float cast?
sammiequon 2016/08/16 23:18:55 I think since the points are floats and the time d
103 if (point_count == 0) {
104 last_point = current_point;
105 } else if (DistanceBetweenPoints(last_point, current_point) >
106 distance_threshold) {
107 canvas->DrawLine(last_point, current_point, paint);
108 last_point = current_point;
109 } else if (point_count == num_points - 1) {
110 canvas->DrawLine(last_point, current_point, paint);
111 }
112 }
113 paintColor =
114 SkColorSetARGB(kMouselaserUiStartOpacityValue, kMouselaserRedValue,
115 kMouselaserGreenValue, kMouselaserBlueValue);
116 paint.setColor(paintColor);
117 paint.setStyle(SkPaint::kFill_Style);
118 canvas->DrawCircle(current_point, kMouselaserUiStartRadius, paint);
119 }
120 } // namespace
jdufault 2016/08/16 19:33:58 newline above
sammiequon 2016/08/16 23:18:55 Done.
121
122 ////////////////////////////////////////////////////////////////////////////////
123
124 // LaserPointerView
125 LaserPointerView::LaserPointerView(base::TimeDelta life_duration)
126 : views::View(), laser_points_(life_duration) {
127 widget_.reset(CreateLaserPointerWidget());
128 widget_->Show();
129 widget_->SetContentsView(this);
130 set_owned_by_client();
131 }
132
133 LaserPointerView::~LaserPointerView() {}
134
135 void LaserPointerView::Stop() {
136 // Clear the points so that there is nothing to be rendered.
137 laser_points_.Clear();
138 SchedulePaint();
139 }
140
141 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) {
142 laser_points_.AddPoint(new_point);
143 gfx::Rect old_bounding_box, new_bounding_box;
144 old_bounding_box = laser_points_.GetBoundingBox();
145 new_bounding_box.SetRect(
jdufault 2016/08/16 19:33:58 Can you use only one bounding_box variable, all of
sammiequon 2016/08/16 23:18:55 Done.
146 old_bounding_box.x() - kMouselaserUiStartRadius,
147 old_bounding_box.y() - kMouselaserUiStartRadius,
148 old_bounding_box.width() + 2 * kMouselaserUiStartRadius,
jdufault 2016/08/16 19:33:57 old_bounding_box.width() + (kMouselaserUiStartRadi
sammiequon 2016/08/16 23:18:55 Done.
149 old_bounding_box.height() + 2 * kMouselaserUiStartRadius);
150 widget_->SetBounds(new_bounding_box);
151 SchedulePaint();
152 }
153
154 void LaserPointerView::OnPaint(gfx::Canvas* canvas) {
155 canvas->Save();
156 PaintLaser(canvas, laser_points_, widget_->GetWindowBoundsInScreen());
jdufault 2016/08/16 19:33:58 Inline this method.
sammiequon 2016/08/16 23:18:55 Done.
157 canvas->Restore();
158 }
159 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698