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