OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/touch/touch_hud_projection.h" | 5 #include "ash/touch/touch_hud_projection.h" |
6 | 6 |
7 #include "ash/root_window_controller.h" | 7 #include "ash/root_window_controller.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/touch_hud/touch_hud_renderer.h" | 9 #include "third_party/skia/include/effects/SkGradientShader.h" |
10 #include "base/memory/ptr_util.h" | |
11 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
| 11 #include "ui/gfx/animation/animation_delegate.h" |
| 12 #include "ui/gfx/animation/linear_animation.h" |
| 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/geometry/size.h" |
12 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
13 | 16 |
14 namespace ash { | 17 namespace ash { |
15 | 18 |
| 19 const int kPointRadius = 20; |
| 20 const SkColor kProjectionFillColor = SkColorSetRGB(0xF5, 0xF5, 0xDC); |
| 21 const SkColor kProjectionStrokeColor = SK_ColorGRAY; |
| 22 const int kProjectionAlpha = 0xB0; |
| 23 const int kFadeoutDurationInMs = 250; |
| 24 const int kFadeoutFrameRate = 60; |
| 25 |
| 26 // TouchPointView draws a single touch point. This object manages its own |
| 27 // lifetime and deletes itself upon fade-out completion or whenever |Remove()| |
| 28 // is explicitly called. |
| 29 class TouchPointView : public views::View, |
| 30 public gfx::AnimationDelegate, |
| 31 public views::WidgetObserver { |
| 32 public: |
| 33 explicit TouchPointView(views::Widget* parent_widget) |
| 34 : circle_center_(kPointRadius + 1, kPointRadius + 1), |
| 35 gradient_center_(SkPoint::Make(kPointRadius + 1, kPointRadius + 1)) { |
| 36 SetPaintToLayer(true); |
| 37 layer()->SetFillsBoundsOpaquely(false); |
| 38 |
| 39 SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2)); |
| 40 |
| 41 stroke_paint_.setStyle(SkPaint::kStroke_Style); |
| 42 stroke_paint_.setColor(kProjectionStrokeColor); |
| 43 |
| 44 gradient_colors_[0] = kProjectionFillColor; |
| 45 gradient_colors_[1] = kProjectionStrokeColor; |
| 46 |
| 47 gradient_pos_[0] = SkFloatToScalar(0.9f); |
| 48 gradient_pos_[1] = SkFloatToScalar(1.0f); |
| 49 |
| 50 parent_widget->GetContentsView()->AddChildView(this); |
| 51 |
| 52 parent_widget->AddObserver(this); |
| 53 } |
| 54 |
| 55 void UpdateTouch(const ui::TouchEvent& touch) { |
| 56 if (touch.type() == ui::ET_TOUCH_RELEASED || |
| 57 touch.type() == ui::ET_TOUCH_CANCELLED) { |
| 58 fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs, |
| 59 kFadeoutFrameRate, this)); |
| 60 fadeout_->Start(); |
| 61 } else { |
| 62 SetX(parent()->GetMirroredXInView(touch.root_location().x()) - |
| 63 kPointRadius - 1); |
| 64 SetY(touch.root_location().y() - kPointRadius - 1); |
| 65 } |
| 66 } |
| 67 |
| 68 void Remove() { delete this; } |
| 69 |
| 70 private: |
| 71 ~TouchPointView() override { |
| 72 GetWidget()->RemoveObserver(this); |
| 73 parent()->RemoveChildView(this); |
| 74 } |
| 75 |
| 76 // Overridden from views::View. |
| 77 void OnPaint(gfx::Canvas* canvas) override { |
| 78 int alpha = kProjectionAlpha; |
| 79 if (fadeout_) |
| 80 alpha = static_cast<int>(fadeout_->CurrentValueBetween(alpha, 0)); |
| 81 fill_paint_.setAlpha(alpha); |
| 82 stroke_paint_.setAlpha(alpha); |
| 83 fill_paint_.setShader(SkGradientShader::MakeRadial( |
| 84 gradient_center_, SkIntToScalar(kPointRadius), gradient_colors_, |
| 85 gradient_pos_, arraysize(gradient_colors_), |
| 86 SkShader::kMirror_TileMode)); |
| 87 canvas->DrawCircle(circle_center_, SkIntToScalar(kPointRadius), |
| 88 fill_paint_); |
| 89 canvas->DrawCircle(circle_center_, SkIntToScalar(kPointRadius), |
| 90 stroke_paint_); |
| 91 } |
| 92 |
| 93 // Overridden from gfx::AnimationDelegate. |
| 94 void AnimationEnded(const gfx::Animation* animation) override { |
| 95 DCHECK_EQ(fadeout_.get(), animation); |
| 96 delete this; |
| 97 } |
| 98 |
| 99 void AnimationProgressed(const gfx::Animation* animation) override { |
| 100 DCHECK_EQ(fadeout_.get(), animation); |
| 101 SchedulePaint(); |
| 102 } |
| 103 |
| 104 void AnimationCanceled(const gfx::Animation* animation) override { |
| 105 AnimationEnded(animation); |
| 106 } |
| 107 |
| 108 // Overridden from views::WidgetObserver. |
| 109 void OnWidgetDestroying(views::Widget* widget) override { |
| 110 if (fadeout_) |
| 111 fadeout_->Stop(); |
| 112 else |
| 113 Remove(); |
| 114 } |
| 115 |
| 116 const gfx::Point circle_center_; |
| 117 const SkPoint gradient_center_; |
| 118 |
| 119 SkPaint fill_paint_; |
| 120 SkPaint stroke_paint_; |
| 121 SkColor gradient_colors_[2]; |
| 122 SkScalar gradient_pos_[2]; |
| 123 |
| 124 std::unique_ptr<gfx::Animation> fadeout_; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(TouchPointView); |
| 127 }; |
| 128 |
16 TouchHudProjection::TouchHudProjection(aura::Window* initial_root) | 129 TouchHudProjection::TouchHudProjection(aura::Window* initial_root) |
17 : TouchObserverHUD(initial_root), | 130 : TouchObserverHUD(initial_root) {} |
18 touch_hud_renderer_(new TouchHudRenderer(widget())) {} | |
19 | 131 |
20 TouchHudProjection::~TouchHudProjection() {} | 132 TouchHudProjection::~TouchHudProjection() {} |
21 | 133 |
22 void TouchHudProjection::Clear() { | 134 void TouchHudProjection::Clear() { |
23 touch_hud_renderer_->Clear(); | 135 for (std::map<int, TouchPointView*>::iterator iter = points_.begin(); |
| 136 iter != points_.end(); iter++) |
| 137 iter->second->Remove(); |
| 138 points_.clear(); |
24 } | 139 } |
25 | 140 |
26 void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) { | 141 void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) { |
27 touch_hud_renderer_->HandleTouchEvent(*event); | 142 if (event->type() == ui::ET_TOUCH_PRESSED) { |
| 143 TouchPointView* point = new TouchPointView(widget()); |
| 144 point->UpdateTouch(*event); |
| 145 std::pair<std::map<int, TouchPointView*>::iterator, bool> result = |
| 146 points_.insert(std::make_pair(event->touch_id(), point)); |
| 147 // If a |TouchPointView| is already mapped to the touch id, remove it and |
| 148 // replace it with the new one. |
| 149 if (!result.second) { |
| 150 result.first->second->Remove(); |
| 151 result.first->second = point; |
| 152 } |
| 153 } else { |
| 154 std::map<int, TouchPointView*>::iterator iter = |
| 155 points_.find(event->touch_id()); |
| 156 if (iter != points_.end()) { |
| 157 iter->second->UpdateTouch(*event); |
| 158 if (event->type() == ui::ET_TOUCH_RELEASED || |
| 159 event->type() == ui::ET_TOUCH_CANCELLED) |
| 160 points_.erase(iter); |
| 161 } |
| 162 } |
28 } | 163 } |
29 | 164 |
30 void TouchHudProjection::SetHudForRootWindowController( | 165 void TouchHudProjection::SetHudForRootWindowController( |
31 RootWindowController* controller) { | 166 RootWindowController* controller) { |
32 controller->set_touch_hud_projection(this); | 167 controller->set_touch_hud_projection(this); |
33 } | 168 } |
34 | 169 |
35 void TouchHudProjection::UnsetHudForRootWindowController( | 170 void TouchHudProjection::UnsetHudForRootWindowController( |
36 RootWindowController* controller) { | 171 RootWindowController* controller) { |
37 controller->set_touch_hud_projection(NULL); | 172 controller->set_touch_hud_projection(NULL); |
38 } | 173 } |
39 | 174 |
40 } // namespace ash | 175 } // namespace ash |
OLD | NEW |