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/gesture_detection/motion_event_generic.h"
7 #include "base/logging.h"
8 #include "ui/events/base_event_utils.h"
12 PointerProperties::PointerProperties()
13 : PointerProperties(0, 0, 0) {
16 PointerProperties::PointerProperties(float x
, float y
, float touch_major
)
18 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN
),
24 touch_major(touch_major
),
30 PointerProperties::PointerProperties(const MotionEvent
& event
,
32 : id(event
.GetPointerId(pointer_index
)),
33 tool_type(event
.GetToolType(pointer_index
)),
34 x(event
.GetX(pointer_index
)),
35 y(event
.GetY(pointer_index
)),
36 raw_x(event
.GetRawX(pointer_index
)),
37 raw_y(event
.GetRawY(pointer_index
)),
38 pressure(event
.GetPressure(pointer_index
)),
39 touch_major(event
.GetTouchMajor(pointer_index
)),
40 touch_minor(event
.GetTouchMinor(pointer_index
)),
41 orientation(event
.GetOrientation(pointer_index
)),
45 MotionEventGeneric::MotionEventGeneric(Action action
,
46 base::TimeTicks event_time
,
47 const PointerProperties
& pointer
)
49 event_time_(event_time
),
50 unique_event_id_(ui::GetNextTouchEventId()),
57 MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric
& other
)
58 : action_(other
.action_
),
59 event_time_(other
.event_time_
),
60 unique_event_id_(other
.unique_event_id_
),
61 action_index_(other
.action_index_
),
62 button_state_(other
.button_state_
),
64 pointers_(other
.pointers_
) {
65 const size_t history_size
= other
.GetHistorySize();
66 for (size_t h
= 0; h
< history_size
; ++h
)
67 PushHistoricalEvent(other
.historical_events_
[h
]->Clone());
70 MotionEventGeneric::~MotionEventGeneric() {
73 uint32
MotionEventGeneric::GetUniqueEventId() const {
74 return unique_event_id_
;
77 MotionEvent::Action
MotionEventGeneric::GetAction() const {
81 int MotionEventGeneric::GetActionIndex() const {
82 DCHECK(action_
== ACTION_POINTER_DOWN
|| action_
== ACTION_POINTER_UP
);
83 DCHECK_GE(action_index_
, 0);
84 DCHECK_LT(action_index_
, static_cast<int>(pointers_
->size()));
88 size_t MotionEventGeneric::GetPointerCount() const {
89 return pointers_
->size();
92 int MotionEventGeneric::GetPointerId(size_t pointer_index
) const {
93 DCHECK_LT(pointer_index
, pointers_
->size());
94 return pointers_
[pointer_index
].id
;
97 float MotionEventGeneric::GetX(size_t pointer_index
) const {
98 DCHECK_LT(pointer_index
, pointers_
->size());
99 return pointers_
[pointer_index
].x
;
102 float MotionEventGeneric::GetY(size_t pointer_index
) const {
103 DCHECK_LT(pointer_index
, pointers_
->size());
104 return pointers_
[pointer_index
].y
;
107 float MotionEventGeneric::GetRawX(size_t pointer_index
) const {
108 DCHECK_LT(pointer_index
, pointers_
->size());
109 return pointers_
[pointer_index
].raw_x
;
112 float MotionEventGeneric::GetRawY(size_t pointer_index
) const {
113 DCHECK_LT(pointer_index
, pointers_
->size());
114 return pointers_
[pointer_index
].raw_y
;
117 float MotionEventGeneric::GetTouchMajor(size_t pointer_index
) const {
118 DCHECK_LT(pointer_index
, pointers_
->size());
119 return pointers_
[pointer_index
].touch_major
;
122 float MotionEventGeneric::GetTouchMinor(size_t pointer_index
) const {
123 DCHECK_LT(pointer_index
, pointers_
->size());
124 return pointers_
[pointer_index
].touch_minor
;
127 float MotionEventGeneric::GetOrientation(size_t pointer_index
) const {
128 DCHECK_LT(pointer_index
, pointers_
->size());
129 return pointers_
[pointer_index
].orientation
;
132 float MotionEventGeneric::GetPressure(size_t pointer_index
) const {
133 DCHECK_LT(pointer_index
, pointers_
->size());
134 return pointers_
[pointer_index
].pressure
;
137 MotionEvent::ToolType
MotionEventGeneric::GetToolType(
138 size_t pointer_index
) const {
139 DCHECK_LT(pointer_index
, pointers_
->size());
140 return pointers_
[pointer_index
].tool_type
;
143 int MotionEventGeneric::GetButtonState() const {
144 return button_state_
;
147 int MotionEventGeneric::GetFlags() const {
151 base::TimeTicks
MotionEventGeneric::GetEventTime() const {
155 size_t MotionEventGeneric::GetHistorySize() const {
156 return historical_events_
.size();
159 base::TimeTicks
MotionEventGeneric::GetHistoricalEventTime(
160 size_t historical_index
) const {
161 DCHECK_LT(historical_index
, historical_events_
.size());
162 return historical_events_
[historical_index
]->GetEventTime();
165 float MotionEventGeneric::GetHistoricalTouchMajor(
166 size_t pointer_index
,
167 size_t historical_index
) const {
168 DCHECK_LT(historical_index
, historical_events_
.size());
169 return historical_events_
[historical_index
]->GetTouchMajor(pointer_index
);
172 float MotionEventGeneric::GetHistoricalX(size_t pointer_index
,
173 size_t historical_index
) const {
174 DCHECK_LT(historical_index
, historical_events_
.size());
175 return historical_events_
[historical_index
]->GetX(pointer_index
);
178 float MotionEventGeneric::GetHistoricalY(size_t pointer_index
,
179 size_t historical_index
) const {
180 DCHECK_LT(historical_index
, historical_events_
.size());
181 return historical_events_
[historical_index
]->GetY(pointer_index
);
185 scoped_ptr
<MotionEventGeneric
> MotionEventGeneric::CloneEvent(
186 const MotionEvent
& event
) {
187 bool with_history
= true;
188 return make_scoped_ptr(new MotionEventGeneric(event
, with_history
));
192 scoped_ptr
<MotionEventGeneric
> MotionEventGeneric::CancelEvent(
193 const MotionEvent
& event
) {
194 bool with_history
= false;
195 scoped_ptr
<MotionEventGeneric
> cancel_event(
196 new MotionEventGeneric(event
, with_history
));
197 cancel_event
->set_action(ACTION_CANCEL
);
198 cancel_event
->set_unique_event_id(ui::GetNextTouchEventId());
199 return cancel_event
.Pass();
202 size_t MotionEventGeneric::PushPointer(const PointerProperties
& pointer
) {
203 DCHECK_EQ(0U, GetHistorySize());
204 pointers_
->push_back(pointer
);
205 return pointers_
->size() - 1;
208 void MotionEventGeneric::RemovePointerAt(size_t index
) {
209 DCHECK_LT(index
, pointers_
->size());
210 pointers_
->erase(pointers_
->begin() + index
);
213 void MotionEventGeneric::PushHistoricalEvent(scoped_ptr
<MotionEvent
> event
) {
215 DCHECK_EQ(event
->GetAction(), ACTION_MOVE
);
216 DCHECK_EQ(event
->GetPointerCount(), GetPointerCount());
217 DCHECK_EQ(event
->GetAction(), GetAction());
218 DCHECK_LE(event
->GetEventTime().ToInternalValue(),
219 GetEventTime().ToInternalValue());
220 historical_events_
.push_back(event
.Pass());
223 MotionEventGeneric::MotionEventGeneric()
224 : action_(ACTION_CANCEL
),
225 unique_event_id_(ui::GetNextTouchEventId()),
230 MotionEventGeneric::MotionEventGeneric(const MotionEvent
& event
,
232 : action_(event
.GetAction()),
233 event_time_(event
.GetEventTime()),
234 unique_event_id_(event
.GetUniqueEventId()),
236 (action_
== ACTION_POINTER_UP
|| action_
== ACTION_POINTER_DOWN
)
237 ? event
.GetActionIndex()
239 button_state_(event
.GetButtonState()),
240 flags_(event
.GetFlags()) {
241 const size_t pointer_count
= event
.GetPointerCount();
242 for (size_t i
= 0; i
< pointer_count
; ++i
)
243 PushPointer(PointerProperties(event
, i
));
248 const size_t history_size
= event
.GetHistorySize();
249 for (size_t h
= 0; h
< history_size
; ++h
) {
250 scoped_ptr
<MotionEventGeneric
> historical_event(new MotionEventGeneric());
251 historical_event
->set_action(ACTION_MOVE
);
252 historical_event
->set_event_time(event
.GetHistoricalEventTime(h
));
253 for (size_t i
= 0; i
< pointer_count
; ++i
) {
254 historical_event
->PushPointer(
255 PointerProperties(event
.GetHistoricalX(i
, h
),
256 event
.GetHistoricalY(i
, h
),
257 event
.GetHistoricalTouchMajor(i
, h
)));
259 PushHistoricalEvent(historical_event
.Pass());
263 MotionEventGeneric
& MotionEventGeneric::operator=(
264 const MotionEventGeneric
& other
) {
265 action_
= other
.action_
;
266 event_time_
= other
.event_time_
;
267 unique_event_id_
= other
.unique_event_id_
;
268 action_index_
= other
.action_index_
;
269 button_state_
= other
.button_state_
;
270 flags_
= other
.flags_
;
271 pointers_
= other
.pointers_
;
272 const size_t history_size
= other
.GetHistorySize();
273 for (size_t h
= 0; h
< history_size
; ++h
)
274 PushHistoricalEvent(other
.historical_events_
[h
]->Clone());
278 void MotionEventGeneric::PopPointer() {
279 DCHECK_GT(pointers_
->size(), 0U);
280 pointers_
->pop_back();