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"
11 PointerProperties::PointerProperties()
12 : PointerProperties(0, 0, 0) {
15 PointerProperties::PointerProperties(float x
, float y
, float touch_major
)
17 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN
),
23 touch_major(touch_major
),
29 PointerProperties::PointerProperties(const MotionEvent
& event
,
31 : id(event
.GetPointerId(pointer_index
)),
32 tool_type(event
.GetToolType(pointer_index
)),
33 x(event
.GetX(pointer_index
)),
34 y(event
.GetY(pointer_index
)),
35 raw_x(event
.GetRawX(pointer_index
)),
36 raw_y(event
.GetRawY(pointer_index
)),
37 pressure(event
.GetPressure(pointer_index
)),
38 touch_major(event
.GetTouchMajor(pointer_index
)),
39 touch_minor(event
.GetTouchMinor(pointer_index
)),
40 orientation(event
.GetOrientation(pointer_index
)),
44 MotionEventGeneric::MotionEventGeneric(Action action
,
45 base::TimeTicks event_time
,
46 const PointerProperties
& pointer
)
48 event_time_(event_time
),
56 MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric
& other
)
57 : action_(other
.action_
),
58 event_time_(other
.event_time_
),
60 action_index_(other
.action_index_
),
61 button_state_(other
.button_state_
),
63 pointers_(other
.pointers_
) {
64 const size_t history_size
= other
.GetHistorySize();
65 for (size_t h
= 0; h
< history_size
; ++h
)
66 PushHistoricalEvent(other
.historical_events_
[h
]->Clone());
69 MotionEventGeneric::~MotionEventGeneric() {
72 int MotionEventGeneric::GetId() const {
76 MotionEvent::Action
MotionEventGeneric::GetAction() const {
80 int MotionEventGeneric::GetActionIndex() const {
81 DCHECK(action_
== ACTION_POINTER_DOWN
|| action_
== ACTION_POINTER_UP
);
82 DCHECK_GE(action_index_
, 0);
83 DCHECK_LT(action_index_
, static_cast<int>(pointers_
->size()));
87 size_t MotionEventGeneric::GetPointerCount() const {
88 return pointers_
->size();
91 int MotionEventGeneric::GetPointerId(size_t pointer_index
) const {
92 DCHECK_LT(pointer_index
, pointers_
->size());
93 return pointers_
[pointer_index
].id
;
96 float MotionEventGeneric::GetX(size_t pointer_index
) const {
97 DCHECK_LT(pointer_index
, pointers_
->size());
98 return pointers_
[pointer_index
].x
;
101 float MotionEventGeneric::GetY(size_t pointer_index
) const {
102 DCHECK_LT(pointer_index
, pointers_
->size());
103 return pointers_
[pointer_index
].y
;
106 float MotionEventGeneric::GetRawX(size_t pointer_index
) const {
107 DCHECK_LT(pointer_index
, pointers_
->size());
108 return pointers_
[pointer_index
].raw_x
;
111 float MotionEventGeneric::GetRawY(size_t pointer_index
) const {
112 DCHECK_LT(pointer_index
, pointers_
->size());
113 return pointers_
[pointer_index
].raw_y
;
116 float MotionEventGeneric::GetTouchMajor(size_t pointer_index
) const {
117 DCHECK_LT(pointer_index
, pointers_
->size());
118 return pointers_
[pointer_index
].touch_major
;
121 float MotionEventGeneric::GetTouchMinor(size_t pointer_index
) const {
122 DCHECK_LT(pointer_index
, pointers_
->size());
123 return pointers_
[pointer_index
].touch_minor
;
126 float MotionEventGeneric::GetOrientation(size_t pointer_index
) const {
127 DCHECK_LT(pointer_index
, pointers_
->size());
128 return pointers_
[pointer_index
].orientation
;
131 float MotionEventGeneric::GetPressure(size_t pointer_index
) const {
132 DCHECK_LT(pointer_index
, pointers_
->size());
133 return pointers_
[pointer_index
].pressure
;
136 MotionEvent::ToolType
MotionEventGeneric::GetToolType(
137 size_t pointer_index
) const {
138 DCHECK_LT(pointer_index
, pointers_
->size());
139 return pointers_
[pointer_index
].tool_type
;
142 int MotionEventGeneric::GetButtonState() const {
143 return button_state_
;
146 int MotionEventGeneric::GetFlags() const {
150 base::TimeTicks
MotionEventGeneric::GetEventTime() const {
154 size_t MotionEventGeneric::GetHistorySize() const {
155 return historical_events_
.size();
158 base::TimeTicks
MotionEventGeneric::GetHistoricalEventTime(
159 size_t historical_index
) const {
160 DCHECK_LT(historical_index
, historical_events_
.size());
161 return historical_events_
[historical_index
]->GetEventTime();
164 float MotionEventGeneric::GetHistoricalTouchMajor(
165 size_t pointer_index
,
166 size_t historical_index
) const {
167 DCHECK_LT(historical_index
, historical_events_
.size());
168 return historical_events_
[historical_index
]->GetTouchMajor(pointer_index
);
171 float MotionEventGeneric::GetHistoricalX(size_t pointer_index
,
172 size_t historical_index
) const {
173 DCHECK_LT(historical_index
, historical_events_
.size());
174 return historical_events_
[historical_index
]->GetX(pointer_index
);
177 float MotionEventGeneric::GetHistoricalY(size_t pointer_index
,
178 size_t historical_index
) const {
179 DCHECK_LT(historical_index
, historical_events_
.size());
180 return historical_events_
[historical_index
]->GetY(pointer_index
);
184 scoped_ptr
<MotionEventGeneric
> MotionEventGeneric::CloneEvent(
185 const MotionEvent
& event
) {
186 bool with_history
= true;
187 return make_scoped_ptr(new MotionEventGeneric(event
, with_history
));
191 scoped_ptr
<MotionEventGeneric
> MotionEventGeneric::CancelEvent(
192 const MotionEvent
& event
) {
193 bool with_history
= false;
194 scoped_ptr
<MotionEventGeneric
> cancel_event(
195 new MotionEventGeneric(event
, with_history
));
196 cancel_event
->set_action(ACTION_CANCEL
);
197 return cancel_event
.Pass();
200 void MotionEventGeneric::PushPointer(const PointerProperties
& pointer
) {
201 DCHECK_EQ(0U, GetHistorySize());
202 pointers_
->push_back(pointer
);
205 void MotionEventGeneric::PushHistoricalEvent(scoped_ptr
<MotionEvent
> event
) {
207 DCHECK_EQ(event
->GetAction(), ACTION_MOVE
);
208 DCHECK_EQ(event
->GetPointerCount(), GetPointerCount());
209 DCHECK_EQ(event
->GetAction(), GetAction());
210 DCHECK_LE(event
->GetEventTime().ToInternalValue(),
211 GetEventTime().ToInternalValue());
212 historical_events_
.push_back(event
.release());
215 MotionEventGeneric::MotionEventGeneric()
216 : action_(ACTION_CANCEL
), id_(0), action_index_(0), button_state_(0) {
219 MotionEventGeneric::MotionEventGeneric(const MotionEvent
& event
,
221 : action_(event
.GetAction()),
222 event_time_(event
.GetEventTime()),
225 (action_
== ACTION_POINTER_UP
|| action_
== ACTION_POINTER_DOWN
)
226 ? event
.GetActionIndex()
228 button_state_(event
.GetButtonState()),
229 flags_(event
.GetFlags()) {
230 const size_t pointer_count
= event
.GetPointerCount();
231 for (size_t i
= 0; i
< pointer_count
; ++i
)
232 PushPointer(PointerProperties(event
, i
));
237 const size_t history_size
= event
.GetHistorySize();
238 for (size_t h
= 0; h
< history_size
; ++h
) {
239 scoped_ptr
<MotionEventGeneric
> historical_event(new MotionEventGeneric());
240 historical_event
->set_action(ACTION_MOVE
);
241 historical_event
->set_event_time(event
.GetHistoricalEventTime(h
));
242 for (size_t i
= 0; i
< pointer_count
; ++i
) {
243 historical_event
->PushPointer(
244 PointerProperties(event
.GetHistoricalX(i
, h
),
245 event
.GetHistoricalY(i
, h
),
246 event
.GetHistoricalTouchMajor(i
, h
)));
248 PushHistoricalEvent(historical_event
.Pass());
252 MotionEventGeneric
& MotionEventGeneric::operator=(
253 const MotionEventGeneric
& other
) {
254 action_
= other
.action_
;
255 event_time_
= other
.event_time_
;
257 action_index_
= other
.action_index_
;
258 button_state_
= other
.button_state_
;
259 flags_
= other
.flags_
;
260 pointers_
= other
.pointers_
;
261 const size_t history_size
= other
.GetHistorySize();
262 for (size_t h
= 0; h
< history_size
; ++h
)
263 PushHistoricalEvent(other
.historical_events_
[h
]->Clone());
267 void MotionEventGeneric::PopPointer() {
268 DCHECK_GT(pointers_
->size(), 0U);
269 pointers_
->pop_back();