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
.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();
52 void MotionEventAura::OnTouch(const TouchEvent
& touch
) {
53 switch (touch
.type()) {
54 case ET_TOUCH_PRESSED
:
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.
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_LE(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_LE(pointer_index
, pointer_count_
);
96 return active_touches_
[pointer_index
].touch_id
;
99 float MotionEventAura::GetX(size_t pointer_index
) const {
100 DCHECK_LE(pointer_index
, pointer_count_
);
101 return active_touches_
[pointer_index
].x
;
104 float MotionEventAura::GetY(size_t pointer_index
) const {
105 DCHECK_LE(pointer_index
, pointer_count_
);
106 return active_touches_
[pointer_index
].y
;
109 float MotionEventAura::GetRawX(size_t pointer_index
) const {
110 DCHECK_LE(pointer_index
, pointer_count_
);
111 return active_touches_
[pointer_index
].raw_x
;
114 float MotionEventAura::GetRawY(size_t pointer_index
) const {
115 DCHECK_LE(pointer_index
, pointer_count_
);
116 return active_touches_
[pointer_index
].raw_y
;
119 float MotionEventAura::GetTouchMajor(size_t pointer_index
) const {
120 DCHECK_LE(pointer_index
, pointer_count_
);
121 return active_touches_
[pointer_index
].major_radius
* 2;
124 float MotionEventAura::GetPressure(size_t pointer_index
) const {
125 DCHECK_LE(pointer_index
, pointer_count_
);
126 return active_touches_
[pointer_index
].pressure
;
129 base::TimeTicks
MotionEventAura::GetEventTime() const {
130 return last_touch_time_
;
133 size_t MotionEventAura::GetHistorySize() const { return 0; }
135 base::TimeTicks
MotionEventAura::GetHistoricalEventTime(
136 size_t historical_index
) const {
138 return base::TimeTicks();
141 float MotionEventAura::GetHistoricalTouchMajor(size_t pointer_index
,
142 size_t historical_index
) const {
147 float MotionEventAura::GetHistoricalX(size_t pointer_index
,
148 size_t historical_index
) const {
153 float MotionEventAura::GetHistoricalY(size_t pointer_index
,
154 size_t historical_index
) const {
159 MotionEvent::ToolType
MotionEventAura::GetToolType(size_t pointer_index
) const {
161 return MotionEvent::TOOL_TYPE_UNKNOWN
;
164 int MotionEventAura::GetButtonState() const {
169 scoped_ptr
<MotionEvent
> MotionEventAura::Clone() const {
170 return scoped_ptr
<MotionEvent
>(new MotionEventAura(pointer_count_
,
173 cached_action_index_
,
176 scoped_ptr
<MotionEvent
> MotionEventAura::Cancel() const {
177 return scoped_ptr
<MotionEvent
>(new MotionEventAura(
178 pointer_count_
, last_touch_time_
, ACTION_CANCEL
, -1, active_touches_
));
181 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent
& event
) {
182 if (event
.type() != ET_TOUCH_RELEASED
&&
183 event
.type() != ET_TOUCH_CANCELLED
) {
187 int index_to_delete
= static_cast<int>(GetIndexFromId(event
.touch_id()));
189 active_touches_
[index_to_delete
] = active_touches_
[pointer_count_
];
192 MotionEventAura::PointData::PointData()
203 int MotionEventAura::GetSourceDeviceId(size_t pointer_index
) const {
204 DCHECK_LE(pointer_index
, pointer_count_
);
205 return active_touches_
[pointer_index
].source_device_id
;
208 void MotionEventAura::AddTouch(const TouchEvent
& touch
) {
209 if (pointer_count_
== static_cast<size_t>(GestureSequence::kMaxGesturePoints
))
212 active_touches_
[pointer_count_
] = GetPointDataFromTouchEvent(touch
);
217 void MotionEventAura::UpdateTouch(const TouchEvent
& touch
) {
218 active_touches_
[GetIndexFromId(touch
.touch_id())] =
219 GetPointDataFromTouchEvent(touch
);
222 void MotionEventAura::UpdateCachedAction(const TouchEvent
& touch
) {
223 DCHECK(pointer_count_
);
224 switch (touch
.type()) {
225 case ET_TOUCH_PRESSED
:
226 if (pointer_count_
== 1) {
227 cached_action_
= ACTION_DOWN
;
229 cached_action_
= ACTION_POINTER_DOWN
;
230 cached_action_index_
=
231 static_cast<int>(GetIndexFromId(touch
.touch_id()));
234 case ET_TOUCH_RELEASED
:
235 if (pointer_count_
== 1) {
236 cached_action_
= ACTION_UP
;
238 cached_action_
= ACTION_POINTER_UP
;
239 cached_action_index_
=
240 static_cast<int>(GetIndexFromId(touch
.touch_id()));
241 DCHECK_LE(cached_action_index_
, static_cast<int>(pointer_count_
));
244 case ET_TOUCH_CANCELLED
:
245 cached_action_
= ACTION_CANCEL
;
248 cached_action_
= ACTION_MOVE
;
256 size_t MotionEventAura::GetIndexFromId(int id
) const {
257 for (size_t i
= 0; i
< pointer_count_
; ++i
) {
258 if (active_touches_
[i
].touch_id
== id
)