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"
12 MotionEventAura::MotionEventAura()
13 : pointer_count_(0), cached_action_index_(-1) {
16 MotionEventAura::MotionEventAura(
18 const base::TimeTicks& last_touch_time,
20 int cached_action_index,
21 const PointData (&active_touches)[GestureSequence::kMaxGesturePoints])
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) {
36 point_data.x = touch.x();
37 point_data.y = touch.y();
38 point_data.touch_id = touch.touch_id();
39 point_data.pressure = touch.force();
40 point_data.source_device_id = touch.source_device_id();
42 // TODO(tdresser): at some point we should start using both radii if they are
43 // available, but for now we use the max.
44 point_data.major_radius = std::max(touch.radius_x(), touch.radius_y());
45 if (!point_data.major_radius)
46 point_data.major_radius = GestureConfiguration::default_radius();
50 void MotionEventAura::OnTouch(const TouchEvent& touch) {
51 switch (touch.type()) {
52 case ET_TOUCH_PRESSED:
55 case ET_TOUCH_RELEASED:
56 case ET_TOUCH_CANCELLED:
57 // Removing these touch points needs to be postponed until after the
58 // MotionEvent has been dispatched. This cleanup occurs in
59 // CleanupRemovedTouchPoints.
70 UpdateCachedAction(touch);
71 last_touch_time_ = touch.time_stamp() + base::TimeTicks();
74 int MotionEventAura::GetId() const {
75 return GetPointerId(0);
78 MotionEvent::Action MotionEventAura::GetAction() const {
79 return cached_action_;
82 int MotionEventAura::GetActionIndex() const {
83 DCHECK(cached_action_ == ACTION_POINTER_DOWN ||
84 cached_action_ == ACTION_POINTER_UP);
85 DCHECK_GE(cached_action_index_, 0);
86 DCHECK_LE(cached_action_index_, static_cast<int>(pointer_count_));
87 return cached_action_index_;
90 size_t MotionEventAura::GetPointerCount() const { return pointer_count_; }
92 int MotionEventAura::GetPointerId(size_t pointer_index) const {
93 DCHECK_LE(pointer_index, pointer_count_);
94 return active_touches_[pointer_index].touch_id;
97 float MotionEventAura::GetX(size_t pointer_index) const {
98 DCHECK_LE(pointer_index, pointer_count_);
99 return active_touches_[pointer_index].x;
102 float MotionEventAura::GetY(size_t pointer_index) const {
103 DCHECK_LE(pointer_index, pointer_count_);
104 return active_touches_[pointer_index].y;
107 float MotionEventAura::GetTouchMajor(size_t pointer_index) const {
108 DCHECK_LE(pointer_index, pointer_count_);
109 return active_touches_[pointer_index].major_radius * 2;
112 float MotionEventAura::GetPressure(size_t pointer_index) const {
113 DCHECK_LE(pointer_index, pointer_count_);
114 return active_touches_[pointer_index].pressure;
117 base::TimeTicks MotionEventAura::GetEventTime() const {
118 return last_touch_time_;
121 size_t MotionEventAura::GetHistorySize() const { return 0; }
123 base::TimeTicks MotionEventAura::GetHistoricalEventTime(
124 size_t historical_index) const {
126 return base::TimeTicks();
129 float MotionEventAura::GetHistoricalTouchMajor(size_t pointer_index,
130 size_t historical_index) const {
135 float MotionEventAura::GetHistoricalX(size_t pointer_index,
136 size_t historical_index) const {
141 float MotionEventAura::GetHistoricalY(size_t pointer_index,
142 size_t historical_index) const {
147 scoped_ptr<MotionEvent> MotionEventAura::Clone() const {
148 return scoped_ptr<MotionEvent>(new MotionEventAura(pointer_count_,
151 cached_action_index_,
154 scoped_ptr<MotionEvent> MotionEventAura::Cancel() const {
155 return scoped_ptr<MotionEvent>(new MotionEventAura(
156 pointer_count_, last_touch_time_, ACTION_CANCEL, -1, active_touches_));
159 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent& event) {
160 if (event.type() != ET_TOUCH_RELEASED &&
161 event.type() != ET_TOUCH_CANCELLED) {
165 int index_to_delete = static_cast<int>(GetIndexFromId(event.touch_id()));
167 active_touches_[index_to_delete] = active_touches_[pointer_count_];
170 MotionEventAura::PointData::PointData()
179 int MotionEventAura::GetSourceDeviceId(size_t pointer_index) const {
180 DCHECK_LE(pointer_index, pointer_count_);
181 return active_touches_[pointer_index].source_device_id;
184 void MotionEventAura::AddTouch(const TouchEvent& touch) {
185 if (pointer_count_ == static_cast<size_t>(GestureSequence::kMaxGesturePoints))
188 active_touches_[pointer_count_] = GetPointDataFromTouchEvent(touch);
193 void MotionEventAura::UpdateTouch(const TouchEvent& touch) {
194 active_touches_[GetIndexFromId(touch.touch_id())] =
195 GetPointDataFromTouchEvent(touch);
198 void MotionEventAura::UpdateCachedAction(const TouchEvent& touch) {
199 DCHECK(pointer_count_);
200 switch (touch.type()) {
201 case ET_TOUCH_PRESSED:
202 if (pointer_count_ == 1) {
203 cached_action_ = ACTION_DOWN;
205 cached_action_ = ACTION_POINTER_DOWN;
206 cached_action_index_ =
207 static_cast<int>(GetIndexFromId(touch.touch_id()));
210 case ET_TOUCH_RELEASED:
211 if (pointer_count_ == 1) {
212 cached_action_ = ACTION_UP;
214 cached_action_ = ACTION_POINTER_UP;
215 cached_action_index_ =
216 static_cast<int>(GetIndexFromId(touch.touch_id()));
217 DCHECK_LE(cached_action_index_, static_cast<int>(pointer_count_));
220 case ET_TOUCH_CANCELLED:
221 cached_action_ = ACTION_CANCEL;
224 cached_action_ = ACTION_MOVE;
232 size_t MotionEventAura::GetIndexFromId(int id) const {
233 for (size_t i = 0; i < pointer_count_; ++i) {
234 if (active_touches_[i].touch_id == id)