OLD | NEW |
| (Empty) |
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_hud/touch_hud_renderer.h" | |
6 | |
7 #include "third_party/skia/include/effects/SkGradientShader.h" | |
8 #include "ui/compositor/layer.h" | |
9 #include "ui/compositor/layer_owner.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/geometry/size.h" | |
15 #include "ui/views/view.h" | |
16 #include "ui/views/widget/widget.h" | |
17 #include "ui/views/widget/widget_observer.h" | |
18 | |
19 namespace ash { | |
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 |Destroy()| | |
30 // is explicitly called. | |
31 class TouchPointView : public views::View, | |
32 public gfx::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, kPointRadius + 1)) { | |
38 SetPaintToLayer(true); | |
39 layer()->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::LocatedEvent& touch) { | |
58 if (touch.type() == ui::ET_TOUCH_RELEASED || | |
59 touch.type() == ui::ET_TOUCH_CANCELLED || | |
60 touch.type() == ui::ET_POINTER_UP || | |
61 touch.type() == ui::ET_POINTER_CANCELLED) { | |
62 fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs, | |
63 kFadeoutFrameRate, this)); | |
64 fadeout_->Start(); | |
65 } else { | |
66 SetX(parent()->GetMirroredXInView(touch.root_location().x()) - | |
67 kPointRadius - 1); | |
68 SetY(touch.root_location().y() - kPointRadius - 1); | |
69 } | |
70 } | |
71 | |
72 void Destroy() { delete this; } | |
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 Destroy(); | |
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 TouchHudRenderer::TouchHudRenderer(views::Widget* parent_widget) | |
134 : parent_widget_(parent_widget) {} | |
135 | |
136 TouchHudRenderer::~TouchHudRenderer() {} | |
137 | |
138 void TouchHudRenderer::Clear() { | |
139 for (std::map<int, TouchPointView*>::iterator iter = points_.begin(); | |
140 iter != points_.end(); iter++) | |
141 iter->second->Destroy(); | |
142 points_.clear(); | |
143 } | |
144 | |
145 void TouchHudRenderer::HandleTouchEvent(const ui::LocatedEvent& event) { | |
146 int id = 0; | |
147 if (event.IsTouchEvent()) { | |
148 id = event.AsTouchEvent()->touch_id(); | |
149 } else { | |
150 DCHECK(event.IsPointerEvent()); | |
151 DCHECK(event.AsPointerEvent()->pointer_details().pointer_type == | |
152 ui::EventPointerType::POINTER_TYPE_TOUCH); | |
153 id = event.AsPointerEvent()->pointer_id(); | |
154 } | |
155 if (event.type() == ui::ET_TOUCH_PRESSED || | |
156 event.type() == ui::ET_POINTER_DOWN) { | |
157 TouchPointView* point = new TouchPointView(parent_widget_); | |
158 point->UpdateTouch(event); | |
159 std::pair<std::map<int, TouchPointView*>::iterator, bool> result = | |
160 points_.insert(std::make_pair(id, point)); | |
161 // If a |TouchPointView| is already mapped to the touch id, destroy it and | |
162 // replace it with the new one. | |
163 if (!result.second) { | |
164 result.first->second->Destroy(); | |
165 result.first->second = point; | |
166 } | |
167 } else { | |
168 std::map<int, TouchPointView*>::iterator iter = points_.find(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 event.type() == ui::ET_POINTER_UP || | |
174 event.type() == ui::ET_POINTER_CANCELLED) | |
175 points_.erase(iter); | |
176 } | |
177 } | |
178 } | |
179 | |
180 } // namespace ash | |
OLD | NEW |