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