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()
13 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN),
24 PointerProperties::PointerProperties(float x, float y, float touch_major)
26 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN),
32 touch_major(touch_major),
37 PointerProperties::PointerProperties(const MotionEvent& event,
39 : id(event.GetPointerId(pointer_index)),
40 tool_type(event.GetToolType(pointer_index)),
41 x(event.GetX(pointer_index)),
42 y(event.GetY(pointer_index)),
43 raw_x(event.GetRawX(pointer_index)),
44 raw_y(event.GetRawY(pointer_index)),
45 pressure(event.GetPressure(pointer_index)),
46 touch_major(event.GetTouchMajor(pointer_index)),
47 touch_minor(event.GetTouchMinor(pointer_index)),
48 orientation(event.GetOrientation(pointer_index)) {
51 MotionEventGeneric::MotionEventGeneric(Action action,
52 base::TimeTicks event_time,
53 const PointerProperties& pointer)
55 event_time_(event_time),
63 MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric& other)
64 : action_(other.action_),
65 event_time_(other.event_time_),
67 action_index_(other.action_index_),
68 button_state_(other.button_state_),
70 pointers_(other.pointers_) {
71 const size_t history_size = other.GetHistorySize();
72 for (size_t h = 0; h < history_size; ++h)
73 PushHistoricalEvent(other.historical_events_[h]->Clone());
76 MotionEventGeneric::~MotionEventGeneric() {
79 int MotionEventGeneric::GetId() const {
83 MotionEvent::Action MotionEventGeneric::GetAction() const {
87 int MotionEventGeneric::GetActionIndex() const {
91 size_t MotionEventGeneric::GetPointerCount() const {
92 return pointers_->size();
95 int MotionEventGeneric::GetPointerId(size_t pointer_index) const {
96 DCHECK_LT(pointer_index, pointers_->size());
97 return pointers_[pointer_index].id;
100 float MotionEventGeneric::GetX(size_t pointer_index) const {
101 DCHECK_LT(pointer_index, pointers_->size());
102 return pointers_[pointer_index].x;
105 float MotionEventGeneric::GetY(size_t pointer_index) const {
106 DCHECK_LT(pointer_index, pointers_->size());
107 return pointers_[pointer_index].y;
110 float MotionEventGeneric::GetRawX(size_t pointer_index) const {
111 DCHECK_LT(pointer_index, pointers_->size());
112 return pointers_[pointer_index].raw_x;
115 float MotionEventGeneric::GetRawY(size_t pointer_index) const {
116 DCHECK_LT(pointer_index, pointers_->size());
117 return pointers_[pointer_index].raw_y;
120 float MotionEventGeneric::GetTouchMajor(size_t pointer_index) const {
121 DCHECK_LT(pointer_index, pointers_->size());
122 return pointers_[pointer_index].touch_major;
125 float MotionEventGeneric::GetTouchMinor(size_t pointer_index) const {
126 DCHECK_LT(pointer_index, pointers_->size());
127 return pointers_[pointer_index].touch_minor;
130 float MotionEventGeneric::GetOrientation(size_t pointer_index) const {
131 DCHECK_LT(pointer_index, pointers_->size());
132 return pointers_[pointer_index].orientation;
135 float MotionEventGeneric::GetPressure(size_t pointer_index) const {
136 DCHECK_LT(pointer_index, pointers_->size());
137 return pointers_[pointer_index].pressure;
140 MotionEvent::ToolType MotionEventGeneric::GetToolType(
141 size_t pointer_index) const {
142 DCHECK_LT(pointer_index, pointers_->size());
143 return pointers_[pointer_index].tool_type;
146 int MotionEventGeneric::GetButtonState() const {
147 return button_state_;
150 int MotionEventGeneric::GetFlags() const {
154 base::TimeTicks MotionEventGeneric::GetEventTime() const {
158 size_t MotionEventGeneric::GetHistorySize() const {
159 return historical_events_.size();
162 base::TimeTicks MotionEventGeneric::GetHistoricalEventTime(
163 size_t historical_index) const {
164 DCHECK_LT(historical_index, historical_events_.size());
165 return historical_events_[historical_index]->GetEventTime();
168 float MotionEventGeneric::GetHistoricalTouchMajor(
169 size_t pointer_index,
170 size_t historical_index) const {
171 DCHECK_LT(historical_index, historical_events_.size());
172 return historical_events_[historical_index]->GetTouchMajor(pointer_index);
175 float MotionEventGeneric::GetHistoricalX(size_t pointer_index,
176 size_t historical_index) const {
177 DCHECK_LT(historical_index, historical_events_.size());
178 return historical_events_[historical_index]->GetX(pointer_index);
181 float MotionEventGeneric::GetHistoricalY(size_t pointer_index,
182 size_t historical_index) const {
183 DCHECK_LT(historical_index, historical_events_.size());
184 return historical_events_[historical_index]->GetY(pointer_index);
188 scoped_ptr<MotionEventGeneric> MotionEventGeneric::CloneEvent(
189 const MotionEvent& event) {
190 bool with_history = true;
191 return make_scoped_ptr(new MotionEventGeneric(event, with_history));
195 scoped_ptr<MotionEventGeneric> MotionEventGeneric::CancelEvent(
196 const MotionEvent& event) {
197 bool with_history = false;
198 scoped_ptr<MotionEventGeneric> cancel_event(
199 new MotionEventGeneric(event, with_history));
200 cancel_event->set_action(ACTION_CANCEL);
201 return cancel_event.Pass();
204 void MotionEventGeneric::PushPointer(const PointerProperties& pointer) {
205 DCHECK_EQ(0U, GetHistorySize());
206 pointers_->push_back(pointer);
209 void MotionEventGeneric::PushHistoricalEvent(scoped_ptr<MotionEvent> event) {
211 DCHECK_EQ(event->GetAction(), ACTION_MOVE);
212 DCHECK_EQ(event->GetPointerCount(), GetPointerCount());
213 DCHECK_EQ(event->GetAction(), GetAction());
214 DCHECK_LE(event->GetEventTime().ToInternalValue(),
215 GetEventTime().ToInternalValue());
216 historical_events_.push_back(event.release());
219 MotionEventGeneric::MotionEventGeneric()
220 : action_(ACTION_CANCEL), id_(0), action_index_(0), button_state_(0) {
223 MotionEventGeneric::MotionEventGeneric(const MotionEvent& event,
225 : action_(event.GetAction()),
226 event_time_(event.GetEventTime()),
229 (action_ == ACTION_POINTER_UP || action_ == ACTION_POINTER_DOWN)
230 ? event.GetActionIndex()
232 button_state_(event.GetButtonState()),
233 flags_(event.GetFlags()) {
234 const size_t pointer_count = event.GetPointerCount();
235 for (size_t i = 0; i < pointer_count; ++i)
236 PushPointer(PointerProperties(event, i));
241 const size_t history_size = event.GetHistorySize();
242 for (size_t h = 0; h < history_size; ++h) {
243 scoped_ptr<MotionEventGeneric> historical_event(new MotionEventGeneric());
244 historical_event->set_action(ACTION_MOVE);
245 historical_event->set_event_time(event.GetHistoricalEventTime(h));
246 for (size_t i = 0; i < pointer_count; ++i) {
247 historical_event->PushPointer(
248 PointerProperties(event.GetHistoricalX(i, h),
249 event.GetHistoricalY(i, h),
250 event.GetHistoricalTouchMajor(i, h)));
252 PushHistoricalEvent(historical_event.Pass());
256 MotionEventGeneric& MotionEventGeneric::operator=(
257 const MotionEventGeneric& other) {
258 action_ = other.action_;
259 event_time_ = other.event_time_;
261 action_index_ = other.action_index_;
262 button_state_ = other.button_state_;
263 flags_ = other.flags_;
264 pointers_ = other.pointers_;
265 const size_t history_size = other.GetHistorySize();
266 for (size_t h = 0; h < history_size; ++h)
267 PushHistoricalEvent(other.historical_events_[h]->Clone());
271 void MotionEventGeneric::PopPointer() {
272 DCHECK_GT(pointers_->size(), 0U);
273 pointers_->pop_back();