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_hud/touch_hud_renderer.h" |
6 | 6 |
7 #include "ash/root_window_controller.h" | |
8 #include "ash/shell.h" | |
9 #include "third_party/skia/include/effects/SkGradientShader.h" | 7 #include "third_party/skia/include/effects/SkGradientShader.h" |
10 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
11 #include "ui/gfx/animation/animation_delegate.h" | 9 #include "ui/gfx/animation/animation_delegate.h" |
12 #include "ui/gfx/animation/linear_animation.h" | 10 #include "ui/gfx/animation/linear_animation.h" |
13 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
14 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 13 #include "ui/views/view.h" |
15 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
| 15 #include "ui/views/widget/widget_observer.h" |
16 | 16 |
17 namespace ash { | 17 namespace ash { |
18 | 18 |
19 const int kPointRadius = 20; | 19 const int kPointRadius = 20; |
20 const SkColor kProjectionFillColor = SkColorSetRGB(0xF5, 0xF5, 0xDC); | 20 const SkColor kProjectionFillColor = SkColorSetRGB(0xF5, 0xF5, 0xDC); |
21 const SkColor kProjectionStrokeColor = SK_ColorGRAY; | 21 const SkColor kProjectionStrokeColor = SK_ColorGRAY; |
22 const int kProjectionAlpha = 0xB0; | 22 const int kProjectionAlpha = 0xB0; |
23 const int kFadeoutDurationInMs = 250; | 23 const int kFadeoutDurationInMs = 250; |
24 const int kFadeoutFrameRate = 60; | 24 const int kFadeoutFrameRate = 60; |
25 | 25 |
26 // TouchPointView draws a single touch point. This object manages its own | 26 // TouchPointView draws a single touch point. This object manages its own |
27 // lifetime and deletes itself upon fade-out completion or whenever |Remove()| | 27 // lifetime and deletes itself upon fade-out completion or whenever |Destroy()| |
28 // is explicitly called. | 28 // is explicitly called. |
29 class TouchPointView : public views::View, | 29 class TouchPointView : public views::View, |
30 public gfx::AnimationDelegate, | 30 public gfx::AnimationDelegate, |
31 public views::WidgetObserver { | 31 public views::WidgetObserver { |
32 public: | 32 public: |
33 explicit TouchPointView(views::Widget* parent_widget) | 33 explicit TouchPointView(views::Widget* parent_widget) |
34 : circle_center_(kPointRadius + 1, kPointRadius + 1), | 34 : circle_center_(kPointRadius + 1, kPointRadius + 1), |
35 gradient_center_(SkPoint::Make(kPointRadius + 1, kPointRadius + 1)) { | 35 gradient_center_(SkPoint::Make(kPointRadius + 1, kPointRadius + 1)) { |
36 SetPaintToLayer(true); | 36 SetPaintToLayer(true); |
37 layer()->SetFillsBoundsOpaquely(false); | 37 layer()->SetFillsBoundsOpaquely(false); |
38 | 38 |
39 SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2)); | 39 SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2)); |
40 | 40 |
41 stroke_paint_.setStyle(SkPaint::kStroke_Style); | 41 stroke_paint_.setStyle(SkPaint::kStroke_Style); |
42 stroke_paint_.setColor(kProjectionStrokeColor); | 42 stroke_paint_.setColor(kProjectionStrokeColor); |
43 | 43 |
44 gradient_colors_[0] = kProjectionFillColor; | 44 gradient_colors_[0] = kProjectionFillColor; |
45 gradient_colors_[1] = kProjectionStrokeColor; | 45 gradient_colors_[1] = kProjectionStrokeColor; |
46 | 46 |
47 gradient_pos_[0] = SkFloatToScalar(0.9f); | 47 gradient_pos_[0] = SkFloatToScalar(0.9f); |
48 gradient_pos_[1] = SkFloatToScalar(1.0f); | 48 gradient_pos_[1] = SkFloatToScalar(1.0f); |
49 | 49 |
50 parent_widget->GetContentsView()->AddChildView(this); | 50 parent_widget->GetContentsView()->AddChildView(this); |
51 | 51 |
52 parent_widget->AddObserver(this); | 52 parent_widget->AddObserver(this); |
53 } | 53 } |
54 | 54 |
55 void UpdateTouch(const ui::TouchEvent& touch) { | 55 void UpdateTouch(const ui::LocatedEvent& touch) { |
56 if (touch.type() == ui::ET_TOUCH_RELEASED || | 56 if (touch.type() == ui::ET_TOUCH_RELEASED || |
57 touch.type() == ui::ET_TOUCH_CANCELLED) { | 57 touch.type() == ui::ET_TOUCH_CANCELLED || |
| 58 touch.type() == ui::ET_POINTER_UP || |
| 59 touch.type() == ui::ET_POINTER_CANCELLED) { |
58 fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs, | 60 fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs, |
59 kFadeoutFrameRate, this)); | 61 kFadeoutFrameRate, this)); |
60 fadeout_->Start(); | 62 fadeout_->Start(); |
61 } else { | 63 } else { |
62 SetX(parent()->GetMirroredXInView(touch.root_location().x()) - | 64 SetX(parent()->GetMirroredXInView(touch.root_location().x()) - |
63 kPointRadius - 1); | 65 kPointRadius - 1); |
64 SetY(touch.root_location().y() - kPointRadius - 1); | 66 SetY(touch.root_location().y() - kPointRadius - 1); |
65 } | 67 } |
66 } | 68 } |
67 | 69 |
68 void Remove() { delete this; } | 70 void Destroy() { delete this; } |
69 | 71 |
70 private: | 72 private: |
71 ~TouchPointView() override { | 73 ~TouchPointView() override { |
72 GetWidget()->RemoveObserver(this); | 74 GetWidget()->RemoveObserver(this); |
73 parent()->RemoveChildView(this); | 75 parent()->RemoveChildView(this); |
74 } | 76 } |
75 | 77 |
76 // Overridden from views::View. | 78 // Overridden from views::View. |
77 void OnPaint(gfx::Canvas* canvas) override { | 79 void OnPaint(gfx::Canvas* canvas) override { |
78 int alpha = kProjectionAlpha; | 80 int alpha = kProjectionAlpha; |
(...skipping 24 matching lines...) Expand all Loading... |
103 | 105 |
104 void AnimationCanceled(const gfx::Animation* animation) override { | 106 void AnimationCanceled(const gfx::Animation* animation) override { |
105 AnimationEnded(animation); | 107 AnimationEnded(animation); |
106 } | 108 } |
107 | 109 |
108 // Overridden from views::WidgetObserver. | 110 // Overridden from views::WidgetObserver. |
109 void OnWidgetDestroying(views::Widget* widget) override { | 111 void OnWidgetDestroying(views::Widget* widget) override { |
110 if (fadeout_) | 112 if (fadeout_) |
111 fadeout_->Stop(); | 113 fadeout_->Stop(); |
112 else | 114 else |
113 Remove(); | 115 Destroy(); |
114 } | 116 } |
115 | 117 |
116 const gfx::Point circle_center_; | 118 const gfx::Point circle_center_; |
117 const SkPoint gradient_center_; | 119 const SkPoint gradient_center_; |
118 | 120 |
119 SkPaint fill_paint_; | 121 SkPaint fill_paint_; |
120 SkPaint stroke_paint_; | 122 SkPaint stroke_paint_; |
121 SkColor gradient_colors_[2]; | 123 SkColor gradient_colors_[2]; |
122 SkScalar gradient_pos_[2]; | 124 SkScalar gradient_pos_[2]; |
123 | 125 |
124 std::unique_ptr<gfx::Animation> fadeout_; | 126 std::unique_ptr<gfx::Animation> fadeout_; |
125 | 127 |
126 DISALLOW_COPY_AND_ASSIGN(TouchPointView); | 128 DISALLOW_COPY_AND_ASSIGN(TouchPointView); |
127 }; | 129 }; |
128 | 130 |
129 TouchHudProjection::TouchHudProjection(aura::Window* initial_root) | 131 TouchHudRenderer::TouchHudRenderer(views::Widget* parent_widget) |
130 : TouchObserverHUD(initial_root) {} | 132 : parent_widget_(parent_widget) {} |
131 | 133 |
132 TouchHudProjection::~TouchHudProjection() {} | 134 TouchHudRenderer::~TouchHudRenderer() {} |
133 | 135 |
134 void TouchHudProjection::Clear() { | 136 void TouchHudRenderer::Clear() { |
135 for (std::map<int, TouchPointView*>::iterator iter = points_.begin(); | 137 for (std::map<int, TouchPointView*>::iterator iter = points_.begin(); |
136 iter != points_.end(); iter++) | 138 iter != points_.end(); iter++) |
137 iter->second->Remove(); | 139 iter->second->Destroy(); |
138 points_.clear(); | 140 points_.clear(); |
139 } | 141 } |
140 | 142 |
141 void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) { | 143 void TouchHudRenderer::HandleTouchEvent(const ui::LocatedEvent* event) { |
142 if (event->type() == ui::ET_TOUCH_PRESSED) { | 144 int id = 0; |
143 TouchPointView* point = new TouchPointView(widget()); | 145 if (event->IsTouchEvent()) { |
| 146 id = event->AsTouchEvent()->touch_id(); |
| 147 } else { |
| 148 DCHECK(event->IsPointerEvent()); |
| 149 DCHECK(event->AsPointerEvent()->pointer_details().pointer_type == |
| 150 ui::EventPointerType::POINTER_TYPE_TOUCH); |
| 151 id = event->AsPointerEvent()->pointer_id(); |
| 152 } |
| 153 if (event->type() == ui::ET_TOUCH_PRESSED || |
| 154 event->type() == ui::ET_POINTER_DOWN) { |
| 155 TouchPointView* point = new TouchPointView(parent_widget_); |
144 point->UpdateTouch(*event); | 156 point->UpdateTouch(*event); |
145 std::pair<std::map<int, TouchPointView*>::iterator, bool> result = | 157 std::pair<std::map<int, TouchPointView*>::iterator, bool> result = |
146 points_.insert(std::make_pair(event->touch_id(), point)); | 158 points_.insert(std::make_pair(id, point)); |
147 // If a |TouchPointView| is already mapped to the touch id, remove it and | 159 // If a |TouchPointView| is already mapped to the touch id, destroy it and |
148 // replace it with the new one. | 160 // replace it with the new one. |
149 if (!result.second) { | 161 if (!result.second) { |
150 result.first->second->Remove(); | 162 result.first->second->Destroy(); |
151 result.first->second = point; | 163 result.first->second = point; |
152 } | 164 } |
153 } else { | 165 } else { |
154 std::map<int, TouchPointView*>::iterator iter = | 166 std::map<int, TouchPointView*>::iterator iter = points_.find(id); |
155 points_.find(event->touch_id()); | |
156 if (iter != points_.end()) { | 167 if (iter != points_.end()) { |
157 iter->second->UpdateTouch(*event); | 168 iter->second->UpdateTouch(*event); |
158 if (event->type() == ui::ET_TOUCH_RELEASED || | 169 if (event->type() == ui::ET_TOUCH_RELEASED || |
159 event->type() == ui::ET_TOUCH_CANCELLED) | 170 event->type() == ui::ET_TOUCH_CANCELLED || |
| 171 event->type() == ui::ET_POINTER_UP || |
| 172 event->type() == ui::ET_POINTER_CANCELLED) |
160 points_.erase(iter); | 173 points_.erase(iter); |
161 } | 174 } |
162 } | 175 } |
163 } | 176 } |
164 | 177 |
165 void TouchHudProjection::SetHudForRootWindowController( | |
166 RootWindowController* controller) { | |
167 controller->set_touch_hud_projection(this); | |
168 } | |
169 | |
170 void TouchHudProjection::UnsetHudForRootWindowController( | |
171 RootWindowController* controller) { | |
172 controller->set_touch_hud_projection(NULL); | |
173 } | |
174 | |
175 } // namespace ash | 178 } // namespace ash |
OLD | NEW |