touch_hud_projection.cc revision f2477e01787aa58f445919b809d89e252beef54f
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
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 "third_party/skia/include/effects/SkGradientShader.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/size.h"
15#include "ui/views/widget/widget.h"
16
17namespace ash {
18namespace internal {
19
20const int kPointRadius = 20;
21const SkColor kProjectionFillColor = SkColorSetRGB(0xF5, 0xF5, 0xDC);
22const SkColor kProjectionStrokeColor = SK_ColorGRAY;
23const int kProjectionAlpha = 0xB0;
24const int kFadeoutDurationInMs = 250;
25const int kFadeoutFrameRate = 60;
26
27// TouchPointView draws a single touch point. This object manages its own
28// lifetime and deletes itself upon fade-out completion or whenever |Remove()|
29// is explicitly called.
30class TouchPointView : public views::View,
31                       public gfx::AnimationDelegate,
32                       public views::WidgetObserver {
33 public:
34  explicit TouchPointView(views::Widget* parent_widget)
35      : circle_center_(kPointRadius + 1, kPointRadius + 1),
36        gradient_center_(SkPoint::Make(kPointRadius + 1,
37                                       kPointRadius + 1)) {
38    SetPaintToLayer(true);
39    SetFillsBoundsOpaquely(false);
40
41    SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2));
42
43    stroke_paint_.setStyle(SkPaint::kStroke_Style);
44    stroke_paint_.setColor(kProjectionStrokeColor);
45
46    gradient_colors_[0] = kProjectionFillColor;
47    gradient_colors_[1] = kProjectionStrokeColor;
48
49    gradient_pos_[0] = SkFloatToScalar(0.9f);
50    gradient_pos_[1] = SkFloatToScalar(1.0f);
51
52    parent_widget->GetContentsView()->AddChildView(this);
53
54    parent_widget->AddObserver(this);
55  }
56
57  void UpdateTouch(const ui::TouchEvent& touch) {
58    if (touch.type() == ui::ET_TOUCH_RELEASED ||
59        touch.type() == ui::ET_TOUCH_CANCELLED) {
60      fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs,
61                                             kFadeoutFrameRate,
62                                             this));
63      fadeout_->Start();
64    } else {
65      SetX(parent()->GetMirroredXInView(touch.root_location().x()) -
66               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 gfx::AnimationDelegate.
105  virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE {
106    DCHECK_EQ(fadeout_.get(), animation);
107    delete this;
108  }
109
110  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE {
111    DCHECK_EQ(fadeout_.get(), animation);
112    SchedulePaint();
113  }
114
115  virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE {
116    AnimationEnded(animation);
117  }
118
119  // Overridden from views::WidgetObserver.
120  virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE {
121    if (fadeout_)
122      fadeout_->Stop();
123    else
124      Remove();
125  }
126
127  const gfx::Point circle_center_;
128  const SkPoint gradient_center_;
129
130  SkPaint fill_paint_;
131  SkPaint stroke_paint_;
132  SkColor gradient_colors_[2];
133  SkScalar gradient_pos_[2];
134
135  scoped_ptr<gfx::Animation> fadeout_;
136
137  DISALLOW_COPY_AND_ASSIGN(TouchPointView);
138};
139
140TouchHudProjection::TouchHudProjection(aura::Window* initial_root)
141    : TouchObserverHUD(initial_root) {
142}
143
144TouchHudProjection::~TouchHudProjection() {
145}
146
147void TouchHudProjection::Clear() {
148  for (std::map<int, TouchPointView*>::iterator iter = points_.begin();
149      iter != points_.end(); iter++)
150    iter->second->Remove();
151  points_.clear();
152}
153
154void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) {
155  if (event->type() == ui::ET_TOUCH_PRESSED) {
156    TouchPointView* point = new TouchPointView(widget());
157    point->UpdateTouch(*event);
158    std::pair<std::map<int, TouchPointView*>::iterator, bool> result =
159        points_.insert(std::make_pair(event->touch_id(), point));
160    // If a |TouchPointView| is already mapped to the touch id, remove it and
161    // replace it with the new one.
162    if (!result.second) {
163      result.first->second->Remove();
164      result.first->second = point;
165    }
166  } else {
167    std::map<int, TouchPointView*>::iterator iter =
168        points_.find(event->touch_id());
169    if (iter != points_.end()) {
170      iter->second->UpdateTouch(*event);
171      if (event->type() == ui::ET_TOUCH_RELEASED ||
172          event->type() == ui::ET_TOUCH_CANCELLED)
173        points_.erase(iter);
174    }
175  }
176}
177
178void TouchHudProjection::SetHudForRootWindowController(
179    RootWindowController* controller) {
180  controller->set_touch_hud_projection(this);
181}
182
183void TouchHudProjection::UnsetHudForRootWindowController(
184    RootWindowController* controller) {
185  controller->set_touch_hud_projection(NULL);
186}
187
188}  // namespace internal
189}  // namespace ash
190