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

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

Powered by Google App Engine
This is Rietveld 408576698