Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: ash/touch_hud/touch_hud_renderer.cc

Issue 2145223002: Revert of ash: Refactor touch hud drawing to ash/touch_hud. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/touch_hud/touch_hud_renderer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/events/event.h"
9 #include "ui/gfx/animation/animation_delegate.h"
10 #include "ui/gfx/animation/linear_animation.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/geometry/size.h"
13 #include "ui/views/view.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_observer.h"
16
17 namespace ash {
18
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 |Destroy()|
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, kPointRadius + 1)) {
36 SetPaintToLayer(true);
37 layer()->SetFillsBoundsOpaquely(false);
38
39 SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2));
40
41 stroke_paint_.setStyle(SkPaint::kStroke_Style);
42 stroke_paint_.setColor(kProjectionStrokeColor);
43
44 gradient_colors_[0] = kProjectionFillColor;
45 gradient_colors_[1] = kProjectionStrokeColor;
46
47 gradient_pos_[0] = SkFloatToScalar(0.9f);
48 gradient_pos_[1] = SkFloatToScalar(1.0f);
49
50 parent_widget->GetContentsView()->AddChildView(this);
51
52 parent_widget->AddObserver(this);
53 }
54
55 void UpdateTouch(const ui::LocatedEvent& touch) {
56 if (touch.type() == ui::ET_TOUCH_RELEASED ||
57 touch.type() == ui::ET_TOUCH_CANCELLED ||
58 touch.type() == ui::ET_POINTER_UP ||
59 touch.type() == ui::ET_POINTER_CANCELLED) {
60 fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs,
61 kFadeoutFrameRate, 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 Destroy() { delete this; }
71
72 private:
73 ~TouchPointView() override {
74 GetWidget()->RemoveObserver(this);
75 parent()->RemoveChildView(this);
76 }
77
78 // Overridden from views::View.
79 void OnPaint(gfx::Canvas* canvas) override {
80 int alpha = kProjectionAlpha;
81 if (fadeout_)
82 alpha = static_cast<int>(fadeout_->CurrentValueBetween(alpha, 0));
83 fill_paint_.setAlpha(alpha);
84 stroke_paint_.setAlpha(alpha);
85 fill_paint_.setShader(SkGradientShader::MakeRadial(
86 gradient_center_, SkIntToScalar(kPointRadius), gradient_colors_,
87 gradient_pos_, arraysize(gradient_colors_),
88 SkShader::kMirror_TileMode));
89 canvas->DrawCircle(circle_center_, SkIntToScalar(kPointRadius),
90 fill_paint_);
91 canvas->DrawCircle(circle_center_, SkIntToScalar(kPointRadius),
92 stroke_paint_);
93 }
94
95 // Overridden from gfx::AnimationDelegate.
96 void AnimationEnded(const gfx::Animation* animation) override {
97 DCHECK_EQ(fadeout_.get(), animation);
98 delete this;
99 }
100
101 void AnimationProgressed(const gfx::Animation* animation) override {
102 DCHECK_EQ(fadeout_.get(), animation);
103 SchedulePaint();
104 }
105
106 void AnimationCanceled(const gfx::Animation* animation) override {
107 AnimationEnded(animation);
108 }
109
110 // Overridden from views::WidgetObserver.
111 void OnWidgetDestroying(views::Widget* widget) override {
112 if (fadeout_)
113 fadeout_->Stop();
114 else
115 Destroy();
116 }
117
118 const gfx::Point circle_center_;
119 const SkPoint gradient_center_;
120
121 SkPaint fill_paint_;
122 SkPaint stroke_paint_;
123 SkColor gradient_colors_[2];
124 SkScalar gradient_pos_[2];
125
126 std::unique_ptr<gfx::Animation> fadeout_;
127
128 DISALLOW_COPY_AND_ASSIGN(TouchPointView);
129 };
130
131 TouchHudRenderer::TouchHudRenderer(views::Widget* parent_widget)
132 : parent_widget_(parent_widget) {}
133
134 TouchHudRenderer::~TouchHudRenderer() {}
135
136 void TouchHudRenderer::Clear() {
137 for (std::map<int, TouchPointView*>::iterator iter = points_.begin();
138 iter != points_.end(); iter++)
139 iter->second->Destroy();
140 points_.clear();
141 }
142
143 void TouchHudRenderer::HandleTouchEvent(const ui::LocatedEvent& event) {
144 int id = 0;
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_);
156 point->UpdateTouch(event);
157 std::pair<std::map<int, TouchPointView*>::iterator, bool> result =
158 points_.insert(std::make_pair(id, point));
159 // If a |TouchPointView| is already mapped to the touch id, destroy it and
160 // replace it with the new one.
161 if (!result.second) {
162 result.first->second->Destroy();
163 result.first->second = point;
164 }
165 } else {
166 std::map<int, TouchPointView*>::iterator iter = points_.find(id);
167 if (iter != points_.end()) {
168 iter->second->UpdateTouch(event);
169 if (event.type() == ui::ET_TOUCH_RELEASED ||
170 event.type() == ui::ET_TOUCH_CANCELLED ||
171 event.type() == ui::ET_POINTER_UP ||
172 event.type() == ui::ET_POINTER_CANCELLED)
173 points_.erase(iter);
174 }
175 }
176 }
177
178 } // namespace ash
OLDNEW
« no previous file with comments | « ash/touch_hud/touch_hud_renderer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698