| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "third_party/skia/include/effects/SkGradientShader.h" | |
| 10 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
| 11 #include "ui/gfx/animation/animation_delegate.h" | 10 #include "ui/views/controls/touch/touch_hud_drawer.h" |
| 12 #include "ui/gfx/animation/linear_animation.h" | |
| 13 #include "ui/gfx/canvas.h" | |
| 14 #include "ui/gfx/geometry/size.h" | |
| 15 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 16 | 12 |
| 17 namespace ash { | 13 namespace ash { |
| 18 | 14 |
| 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 | |
| 129 TouchHudProjection::TouchHudProjection(aura::Window* initial_root) | 15 TouchHudProjection::TouchHudProjection(aura::Window* initial_root) |
| 130 : TouchObserverHUD(initial_root) {} | 16 : TouchObserverHUD(initial_root) { |
| 17 touch_hud_drawer_ = views::TouchHudDrawer::Get(); |
| 18 } |
| 131 | 19 |
| 132 TouchHudProjection::~TouchHudProjection() {} | 20 TouchHudProjection::~TouchHudProjection() {} |
| 133 | 21 |
| 134 void TouchHudProjection::Clear() { | 22 void TouchHudProjection::Clear() { |
| 135 for (std::map<int, TouchPointView*>::iterator iter = points_.begin(); | 23 touch_hud_drawer_->Clear(); |
| 136 iter != points_.end(); iter++) | |
| 137 iter->second->Remove(); | |
| 138 points_.clear(); | |
| 139 } | 24 } |
| 140 | 25 |
| 141 void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) { | 26 void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) { |
| 142 if (event->type() == ui::ET_TOUCH_PRESSED) { | 27 touch_hud_drawer_->HandleTouchEvent(event, widget()); |
| 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 } | |
| 163 } | 28 } |
| 164 | 29 |
| 165 void TouchHudProjection::SetHudForRootWindowController( | 30 void TouchHudProjection::SetHudForRootWindowController( |
| 166 RootWindowController* controller) { | 31 RootWindowController* controller) { |
| 167 controller->set_touch_hud_projection(this); | 32 controller->set_touch_hud_projection(this); |
| 168 } | 33 } |
| 169 | 34 |
| 170 void TouchHudProjection::UnsetHudForRootWindowController( | 35 void TouchHudProjection::UnsetHudForRootWindowController( |
| 171 RootWindowController* controller) { | 36 RootWindowController* controller) { |
| 172 controller->set_touch_hud_projection(NULL); | 37 controller->set_touch_hud_projection(NULL); |
| 173 } | 38 } |
| 174 | 39 |
| 175 } // namespace ash | 40 } // namespace ash |
| OLD | NEW |