Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / events / gestures / motion_event_aura.cc
blobbfe3321bed42f0972e1430cc80ca67c64677c202
1 // Copyright 2014 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.
5 #include "ui/events/gestures/motion_event_aura.h"
7 #include "base/logging.h"
8 #include "ui/events/gestures/gesture_configuration.h"
10 namespace ui {
12 MotionEventAura::MotionEventAura()
13 : pointer_count_(0), cached_action_index_(-1) {
16 MotionEventAura::MotionEventAura(
17 size_t pointer_count,
18 const base::TimeTicks& last_touch_time,
19 Action cached_action,
20 int cached_action_index,
21 const PointData (&active_touches)[MotionEvent::MAX_TOUCH_POINT_COUNT])
22 : pointer_count_(pointer_count),
23 last_touch_time_(last_touch_time),
24 cached_action_(cached_action),
25 cached_action_index_(cached_action_index) {
26 DCHECK(pointer_count_);
27 for (size_t i = 0; i < pointer_count; ++i)
28 active_touches_[i] = active_touches[i];
31 MotionEventAura::~MotionEventAura() {}
33 MotionEventAura::PointData MotionEventAura::GetPointDataFromTouchEvent(
34 const TouchEvent& touch) {
35 PointData point_data;
36 point_data.x = touch.x();
37 point_data.y = touch.y();
38 point_data.raw_x = touch.root_location_f().x();
39 point_data.raw_y = touch.root_location_f().y();
40 point_data.touch_id = touch.touch_id();
41 point_data.pressure = touch.force();
42 point_data.source_device_id = touch.source_device_id();
44 // TODO(tdresser): at some point we should start using both radii if they are
45 // available, but for now we use the max.
46 point_data.major_radius = std::max(touch.radius_x(), touch.radius_y());
47 if (!point_data.major_radius)
48 point_data.major_radius = GestureConfiguration::default_radius();
49 return point_data;
52 void MotionEventAura::OnTouch(const TouchEvent& touch) {
53 switch (touch.type()) {
54 case ET_TOUCH_PRESSED:
55 AddTouch(touch);
56 break;
57 case ET_TOUCH_RELEASED:
58 case ET_TOUCH_CANCELLED:
59 // Removing these touch points needs to be postponed until after the
60 // MotionEvent has been dispatched. This cleanup occurs in
61 // CleanupRemovedTouchPoints.
62 UpdateTouch(touch);
63 break;
64 case ET_TOUCH_MOVED:
65 UpdateTouch(touch);
66 break;
67 default:
68 NOTREACHED();
69 break;
72 UpdateCachedAction(touch);
73 last_touch_time_ = touch.time_stamp() + base::TimeTicks();
76 int MotionEventAura::GetId() const {
77 return GetPointerId(0);
80 MotionEvent::Action MotionEventAura::GetAction() const {
81 return cached_action_;
84 int MotionEventAura::GetActionIndex() const {
85 DCHECK(cached_action_ == ACTION_POINTER_DOWN ||
86 cached_action_ == ACTION_POINTER_UP);
87 DCHECK_GE(cached_action_index_, 0);
88 DCHECK_LT(cached_action_index_, static_cast<int>(pointer_count_));
89 return cached_action_index_;
92 size_t MotionEventAura::GetPointerCount() const { return pointer_count_; }
94 int MotionEventAura::GetPointerId(size_t pointer_index) const {
95 DCHECK_LT(pointer_index, pointer_count_);
96 return active_touches_[pointer_index].touch_id;
99 float MotionEventAura::GetX(size_t pointer_index) const {
100 DCHECK_LT(pointer_index, pointer_count_);
101 return active_touches_[pointer_index].x;
104 float MotionEventAura::GetY(size_t pointer_index) const {
105 DCHECK_LT(pointer_index, pointer_count_);
106 return active_touches_[pointer_index].y;
109 float MotionEventAura::GetRawX(size_t pointer_index) const {
110 DCHECK_LT(pointer_index, pointer_count_);
111 return active_touches_[pointer_index].raw_x;
114 float MotionEventAura::GetRawY(size_t pointer_index) const {
115 DCHECK_LT(pointer_index, pointer_count_);
116 return active_touches_[pointer_index].raw_y;
119 float MotionEventAura::GetTouchMajor(size_t pointer_index) const {
120 DCHECK_LT(pointer_index, pointer_count_);
121 return active_touches_[pointer_index].major_radius * 2;
124 float MotionEventAura::GetPressure(size_t pointer_index) const {
125 DCHECK_LT(pointer_index, pointer_count_);
126 return active_touches_[pointer_index].pressure;
129 MotionEvent::ToolType MotionEventAura::GetToolType(size_t pointer_index) const {
130 // TODO(jdduke): Plumb tool type from the platform, crbug.com/404128.
131 DCHECK_LT(pointer_index, pointer_count_);
132 return MotionEvent::TOOL_TYPE_UNKNOWN;
135 int MotionEventAura::GetButtonState() const {
136 NOTIMPLEMENTED();
137 return 0;
140 base::TimeTicks MotionEventAura::GetEventTime() const {
141 return last_touch_time_;
144 scoped_ptr<MotionEvent> MotionEventAura::Clone() const {
145 return scoped_ptr<MotionEvent>(new MotionEventAura(pointer_count_,
146 last_touch_time_,
147 cached_action_,
148 cached_action_index_,
149 active_touches_));
151 scoped_ptr<MotionEvent> MotionEventAura::Cancel() const {
152 return scoped_ptr<MotionEvent>(new MotionEventAura(
153 pointer_count_, last_touch_time_, ACTION_CANCEL, -1, active_touches_));
156 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent& event) {
157 if (event.type() != ET_TOUCH_RELEASED &&
158 event.type() != ET_TOUCH_CANCELLED) {
159 return;
162 int index_to_delete = static_cast<int>(GetIndexFromId(event.touch_id()));
163 pointer_count_--;
164 active_touches_[index_to_delete] = active_touches_[pointer_count_];
167 MotionEventAura::PointData::PointData()
168 : x(0),
169 y(0),
170 raw_x(0),
171 raw_y(0),
172 touch_id(0),
173 pressure(0),
174 source_device_id(0),
175 major_radius(0) {
178 int MotionEventAura::GetSourceDeviceId(size_t pointer_index) const {
179 DCHECK_LT(pointer_index, pointer_count_);
180 return active_touches_[pointer_index].source_device_id;
183 void MotionEventAura::AddTouch(const TouchEvent& touch) {
184 if (pointer_count_ == MotionEvent::MAX_TOUCH_POINT_COUNT)
185 return;
187 active_touches_[pointer_count_] = GetPointDataFromTouchEvent(touch);
188 pointer_count_++;
192 void MotionEventAura::UpdateTouch(const TouchEvent& touch) {
193 active_touches_[GetIndexFromId(touch.touch_id())] =
194 GetPointDataFromTouchEvent(touch);
197 void MotionEventAura::UpdateCachedAction(const TouchEvent& touch) {
198 DCHECK(pointer_count_);
199 switch (touch.type()) {
200 case ET_TOUCH_PRESSED:
201 if (pointer_count_ == 1) {
202 cached_action_ = ACTION_DOWN;
203 } else {
204 cached_action_ = ACTION_POINTER_DOWN;
205 cached_action_index_ =
206 static_cast<int>(GetIndexFromId(touch.touch_id()));
208 break;
209 case ET_TOUCH_RELEASED:
210 if (pointer_count_ == 1) {
211 cached_action_ = ACTION_UP;
212 } else {
213 cached_action_ = ACTION_POINTER_UP;
214 cached_action_index_ =
215 static_cast<int>(GetIndexFromId(touch.touch_id()));
216 DCHECK_LT(cached_action_index_, static_cast<int>(pointer_count_));
218 break;
219 case ET_TOUCH_CANCELLED:
220 cached_action_ = ACTION_CANCEL;
221 break;
222 case ET_TOUCH_MOVED:
223 cached_action_ = ACTION_MOVE;
224 break;
225 default:
226 NOTREACHED();
227 break;
231 size_t MotionEventAura::GetIndexFromId(int id) const {
232 for (size_t i = 0; i < pointer_count_; ++i) {
233 if (active_touches_[i].touch_id == id)
234 return i;
236 NOTREACHED();
237 return 0;
240 } // namespace ui