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..57825e1bb88750d7c97cff4957117679c6411aff |
--- /dev/null |
+++ b/ash/common/system/chromeos/palette/tools/laser_pointer_view.cc |
@@ -0,0 +1,157 @@ |
+// 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 "third_party/skia/include/core/SkRect.h" |
+#include "ui/compositor/layer.h" |
+#include "ui/gfx/canvas.h" |
+#include "ui/gfx/transform.h" |
+#include "ui/wm/core/coordinate_conversion.h" |
+ |
+namespace ash { |
+namespace { |
+ |
+const double kPointInitialRadius = 5; |
+const double kPointFinalRadius = 0.25; |
+const double kPointInitialOpacity = 200.0; |
+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.
|
+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 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
|
+ double finalValue, |
+ double progress) { |
+ return initialValue + (finalValue - initialValue) * progress; |
+} |
+ |
+void PaintLaser(gfx::Canvas* canvas, |
+ const LaserPointerPoints& points, |
+ const gfx::Rect& widget_bounds) {} |
+ |
+} // namespace |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+// LaserPointerView |
+LaserPointerView::LaserPointerView(base::TimeDelta life_duration) |
+ : 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.
|
+ widget_.reset(CreateLaserPointerWidget()); |
+ widget_->Show(); |
+ widget_->SetContentsView(this); |
+ set_owned_by_client(); |
+} |
+ |
+LaserPointerView::~LaserPointerView() {} |
+ |
+void LaserPointerView::Stop() { |
+ // Clear the points so that there is nothing to be rendered. |
+ laser_points_.Clear(); |
+ SchedulePaint(); |
+} |
+ |
+void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { |
+ laser_points_.AddPoint(new_point); |
+ // Set the bounding box of the points to be able to enclose a point of max |
+ // radius located on an edge. |
+ 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) { |
+ canvas->Save(); |
+ 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.
|
+ SkPaint paint; |
+ paint.setStyle(SkPaint::kStroke_Style); |
+ paint.setAntiAlias(true); |
+ paint.setStrokeJoin(SkPaint::kBevel_Join); |
+ 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.
|
+ |
+ base::Time oldest = laser_points_.GetOldest().creation_time; |
+ 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.
|
+ gfx::Point last_point, current_point; |
+ gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); |
+ int point_count = 0; |
+ int num_laser_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 |
+ // be removed and 1 means that the point was just added. |
+ double relative_time = 1.0; |
+ if (oldest != most_recent) { |
+ relative_time = 1.0 - ((it->creation_time - oldest).InMillisecondsF() / |
+ (most_recent - oldest).InMillisecondsF()); |
+ } |
+ |
+ // Set the radius and opacity based on the distance. |
+ double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, |
+ relative_time); |
+ int opacity = int{LinearInterpolate(kPointInitialOpacity, |
+ kPointFinalOpacity, relative_time)}; |
+ |
+ gfx::Vector2d center = it->location - widget_bounds.origin(); |
+ current_point = gfx::Point(center.x(), center.y()); |
+ |
+ paint_color = SkColorSetA(kPointColor, opacity); |
+ paint.setColor(paint_color); |
+ paint.setStrokeWidth(radius * 2); |
+ |
+ // 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_laser_points_ - 1) { |
+ canvas->DrawLine(last_point, current_point, paint); |
+ } |
+ } |
+ paint_color = SkColorSetA(kPointColor, int{kPointInitialOpacity}); |
+ paint.setColor(paint_color); |
+ paint.setStyle(SkPaint::kFill_Style); |
+ canvas->DrawCircle(current_point, kPointInitialRadius, paint); |
+ } |
+ canvas->Restore(); |
+} |
+} // namespace ash |