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
)[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
) {
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_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 {
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_
,
148 cached_action_index_
,
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
) {
162 int index_to_delete
= static_cast<int>(GetIndexFromId(event
.touch_id()));
164 active_touches_
[index_to_delete
] = active_touches_
[pointer_count_
];
167 MotionEventAura::PointData::PointData()
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
)
187 active_touches_
[pointer_count_
] = GetPointDataFromTouchEvent(touch
);
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
;
204 cached_action_
= ACTION_POINTER_DOWN
;
205 cached_action_index_
=
206 static_cast<int>(GetIndexFromId(touch
.touch_id()));
209 case ET_TOUCH_RELEASED
:
210 if (pointer_count_
== 1) {
211 cached_action_
= ACTION_UP
;
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_
));
219 case ET_TOUCH_CANCELLED
:
220 cached_action_
= ACTION_CANCEL
;
223 cached_action_
= ACTION_MOVE
;
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
)