Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / ui / events / gesture_detection / motion_event_generic.cc
blob475e9c5bc808eb3d40f042fd5a8aff751a4858c5
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"
9 namespace ui {
11 PointerProperties::PointerProperties()
12 : PointerProperties(0, 0, 0) {
15 PointerProperties::PointerProperties(float x, float y, float touch_major)
16 : id(0),
17 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN),
18 x(x),
19 y(y),
20 raw_x(x),
21 raw_y(y),
22 pressure(0),
23 touch_major(touch_major),
24 touch_minor(0),
25 orientation(0),
26 source_device_id(0) {
29 PointerProperties::PointerProperties(const MotionEvent& event,
30 size_t pointer_index)
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)),
41 source_device_id(0) {
44 MotionEventGeneric::MotionEventGeneric(Action action,
45 base::TimeTicks event_time,
46 const PointerProperties& pointer)
47 : action_(action),
48 event_time_(event_time),
49 id_(0),
50 action_index_(0),
51 button_state_(0),
52 flags_(0) {
53 PushPointer(pointer);
56 MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric& other)
57 : action_(other.action_),
58 event_time_(other.event_time_),
59 id_(other.id_),
60 action_index_(other.action_index_),
61 button_state_(other.button_state_),
62 flags_(other.flags_),
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 {
73 return id_;
76 MotionEvent::Action MotionEventGeneric::GetAction() const {
77 return action_;
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()));
84 return action_index_;
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 {
147 return flags_;
150 base::TimeTicks MotionEventGeneric::GetEventTime() const {
151 return event_time_;
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);
183 // static
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));
190 // static
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 size_t MotionEventGeneric::PushPointer(const PointerProperties& pointer) {
201 DCHECK_EQ(0U, GetHistorySize());
202 pointers_->push_back(pointer);
203 return pointers_->size() - 1;
206 void MotionEventGeneric::RemovePointerAt(size_t index) {
207 DCHECK_LT(index, pointers_->size());
208 pointers_->erase(pointers_->begin() + index);
211 void MotionEventGeneric::PushHistoricalEvent(scoped_ptr<MotionEvent> event) {
212 DCHECK(event);
213 DCHECK_EQ(event->GetAction(), ACTION_MOVE);
214 DCHECK_EQ(event->GetPointerCount(), GetPointerCount());
215 DCHECK_EQ(event->GetAction(), GetAction());
216 DCHECK_LE(event->GetEventTime().ToInternalValue(),
217 GetEventTime().ToInternalValue());
218 historical_events_.push_back(event.release());
221 MotionEventGeneric::MotionEventGeneric()
222 : action_(ACTION_CANCEL), id_(0), action_index_(0), button_state_(0) {
225 MotionEventGeneric::MotionEventGeneric(const MotionEvent& event,
226 bool with_history)
227 : action_(event.GetAction()),
228 event_time_(event.GetEventTime()),
229 id_(event.GetId()),
230 action_index_(
231 (action_ == ACTION_POINTER_UP || action_ == ACTION_POINTER_DOWN)
232 ? event.GetActionIndex()
233 : 0),
234 button_state_(event.GetButtonState()),
235 flags_(event.GetFlags()) {
236 const size_t pointer_count = event.GetPointerCount();
237 for (size_t i = 0; i < pointer_count; ++i)
238 PushPointer(PointerProperties(event, i));
240 if (!with_history)
241 return;
243 const size_t history_size = event.GetHistorySize();
244 for (size_t h = 0; h < history_size; ++h) {
245 scoped_ptr<MotionEventGeneric> historical_event(new MotionEventGeneric());
246 historical_event->set_action(ACTION_MOVE);
247 historical_event->set_event_time(event.GetHistoricalEventTime(h));
248 for (size_t i = 0; i < pointer_count; ++i) {
249 historical_event->PushPointer(
250 PointerProperties(event.GetHistoricalX(i, h),
251 event.GetHistoricalY(i, h),
252 event.GetHistoricalTouchMajor(i, h)));
254 PushHistoricalEvent(historical_event.Pass());
258 MotionEventGeneric& MotionEventGeneric::operator=(
259 const MotionEventGeneric& other) {
260 action_ = other.action_;
261 event_time_ = other.event_time_;
262 id_ = other.id_;
263 action_index_ = other.action_index_;
264 button_state_ = other.button_state_;
265 flags_ = other.flags_;
266 pointers_ = other.pointers_;
267 const size_t history_size = other.GetHistorySize();
268 for (size_t h = 0; h < history_size; ++h)
269 PushHistoricalEvent(other.historical_events_[h]->Clone());
270 return *this;
273 void MotionEventGeneric::PopPointer() {
274 DCHECK_GT(pointers_->size(), 0U);
275 pointers_->pop_back();
278 } // namespace ui