Index: ash/common/system/chromeos/palette/tools/laser_pointer_view.cc |
diff --git a/ash/common/system/chromeos/palette/tools/laser_pointer_view.cc b/ash/common/system/chromeos/palette/tools/laser_pointer_view.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ed8e94da23f04a39a807eece10e06421d6427a74 |
--- /dev/null |
+++ b/ash/common/system/chromeos/palette/tools/laser_pointer_view.cc |
@@ -0,0 +1,145 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/common/system/chromeos/palette/tools/laser_pointer_view.h" |
+ |
+#include <memory> |
+ |
+#include "ash/common/shell_window_ids.h" |
+#include "ash/common/wm_root_window_controller.h" |
+#include "ash/common/wm_shell.h" |
+#include "ash/common/wm_window.h" |
+#include "third_party/skia/include/core/SkColor.h" |
+#include "third_party/skia/include/core/SkPaint.h" |
+#include "ui/gfx/canvas.h" |
+ |
+namespace ash { |
+namespace { |
+ |
+const double kPointInitialRadius = 5; |
+const double kPointFinalRadius = 0.25; |
+const int kPointInitialOpacity = 200; |
+const int kPointFinalOpacity = 0; |
+const SkColor kPointColor = SkColorSetRGB(255, 0, 0); |
+ |
+views::Widget* CreateLaserPointerWidget() { |
+ views::Widget* widget = new views::Widget; |
+ views::Widget::InitParams params; |
+ params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
+ params.name = "LaserOverlay"; |
+ params.accept_events = false; |
+ params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
+ params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
+ params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
+ WmShell::Get() |
+ ->GetRootWindowForNewWindows() |
+ ->GetRootWindowController() |
+ ->ConfigureWidgetInitParamsForContainer( |
+ widget, kShellWindowId_OverlayContainer, ¶ms); |
+ |
+ widget->Init(params); |
+ return widget; |
+} |
+ |
+float DistanceBetweenPoints(const gfx::Point& point1, |
+ const gfx::Point& point2) { |
+ return (point1 - point2).Length(); |
+} |
+ |
+double LinearInterpolate(double initial_value, |
+ double final_value, |
+ double progress) { |
+ return initial_value + (final_value - initial_value) * progress; |
+} |
+} // namespace |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+// LaserPointerView |
+LaserPointerView::LaserPointerView(base::TimeDelta life_duration) |
+ : laser_points_(life_duration) { |
+ widget_.reset(CreateLaserPointerWidget()); |
+ widget_->Show(); |
+ widget_->SetContentsView(this); |
+ set_owned_by_client(); |
+} |
+ |
+LaserPointerView::~LaserPointerView() {} |
+ |
+void LaserPointerView::Stop() { |
+ laser_points_.Clear(); |
+ SchedulePaint(); |
+} |
+ |
+void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { |
+ laser_points_.AddPoint(new_point); |
+ // Expand the bounding box so that it includes the radius of the points on the |
+ // edges. |
+ gfx::Rect bounding_box; |
+ bounding_box = laser_points_.GetBoundingBox(); |
+ bounding_box.SetRect(bounding_box.x() - kPointInitialRadius, |
+ bounding_box.y() - kPointInitialRadius, |
+ bounding_box.width() + (kPointInitialRadius * 2), |
+ bounding_box.height() + (kPointInitialRadius * 2)); |
+ widget_->SetBounds(bounding_box); |
+ SchedulePaint(); |
+} |
+ |
+void LaserPointerView::OnPaint(gfx::Canvas* canvas) { |
+ if (laser_points_.IsEmpty()) |
+ return; |
+ |
+ canvas->Save(); |
+ SkPaint paint; |
+ paint.setStyle(SkPaint::kStroke_Style); |
+ paint.setAntiAlias(true); |
+ paint.setStrokeJoin(SkPaint::kBevel_Join); |
+ |
+ base::Time oldest = laser_points_.GetOldest().creation_time; |
+ base::Time newest = laser_points_.GetNewest().creation_time; |
+ gfx::Point last_point, current_point; |
+ gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); |
+ int point_count = 0; |
+ int num_points_ = laser_points_.GetNumberOfPoints(); |
+ for (auto it = laser_points_.PointsStart(); it != laser_points_.PointsEnd(); |
+ it++, point_count++) { |
+ // Normalized is a value between [0,1] where 0 means the point is about to |
jdufault
2016/08/22 18:37:36
Update comment to reflect the current variable nam
sammiequon
2016/08/23 00:20:33
Done.
|
+ // be removed and 1 means that the point was just added. |
+ double relative_time = 1.0; |
+ if (oldest != newest) { |
+ relative_time = 1.0 - ((it->creation_time - oldest).InMillisecondsF() / |
+ (newest - oldest).InMillisecondsF()); |
+ } |
+ |
+ // Set the radius and opacity based on the distance. |
+ double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, |
+ relative_time); |
+ int opacity = |
+ int{LinearInterpolate(double{kPointInitialOpacity}, |
+ double{kPointFinalOpacity}, relative_time)}; |
+ |
+ paint.setColor(SkColorSetA(kPointColor, opacity)); |
+ paint.setStrokeWidth(radius * 2); |
+ |
+ gfx::Vector2d center = it->location - widget_bounds.origin(); |
+ current_point = gfx::Point(center.x(), center.y()); |
+ |
+ // If we draw laser_points_ that are within a stroke width of each other, |
+ // the result will be very jagged. |
+ float distance_threshold = float{radius * 2}; |
+ if (point_count == 0) { |
+ last_point = current_point; |
+ } else if (DistanceBetweenPoints(last_point, current_point) > |
+ distance_threshold) { |
+ canvas->DrawLine(last_point, current_point, paint); |
+ last_point = current_point; |
+ } else if (point_count == num_points_ - 1) { |
jdufault
2016/08/22 18:37:37
Can this else if be combined with the previous one
sammiequon
2016/08/23 00:20:33
Done.
|
+ canvas->DrawLine(last_point, current_point, paint); |
+ } |
+ } |
+ paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity)); |
+ paint.setStyle(SkPaint::kFill_Style); |
+ canvas->DrawCircle(current_point, kPointInitialRadius, paint); |
+} |
+} // namespace ash |