OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/gesture_detection/motion_event_generic.h" | 5 #include "ui/events/gesture_detection/motion_event_generic.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace ui { | 9 namespace ui { |
10 | 10 |
11 PointerProperties::PointerProperties() | 11 PointerProperties::PointerProperties() |
12 : id(0), | 12 : id(0), |
13 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN), | 13 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN), |
14 x(0), | 14 x(0), |
15 y(0), | 15 y(0), |
16 raw_x(0), | 16 raw_x(0), |
17 raw_y(0), | 17 raw_y(0), |
18 pressure(0), | 18 pressure(0), |
19 touch_major(0), | 19 touch_major(0), |
20 touch_minor(0), | 20 touch_minor(0), |
21 orientation(0) { | 21 orientation(0) { |
22 } | 22 } |
23 | 23 |
24 PointerProperties::PointerProperties(float x, float y) | 24 PointerProperties::PointerProperties(float x, float y, float touch_major) |
25 : id(0), | 25 : id(0), |
26 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN), | 26 tool_type(MotionEvent::TOOL_TYPE_UNKNOWN), |
27 x(x), | 27 x(x), |
28 y(y), | 28 y(y), |
29 raw_x(x), | 29 raw_x(x), |
30 raw_y(y), | 30 raw_y(y), |
31 pressure(0), | 31 pressure(0), |
32 touch_major(0), | 32 touch_major(touch_major), |
33 touch_minor(0), | 33 touch_minor(0), |
34 orientation(0) { | 34 orientation(0) { |
35 } | 35 } |
36 | 36 |
37 MotionEventGeneric::MotionEventGeneric() | 37 PointerProperties::PointerProperties(const MotionEvent& event, |
38 : action_(ACTION_CANCEL), | 38 size_t pointer_index) |
39 id_(0), | 39 : id(event.GetPointerId(pointer_index)), |
40 action_index_(0), | 40 tool_type(event.GetToolType(pointer_index)), |
41 button_state_(0), | 41 x(event.GetX(pointer_index)), |
42 flags_(0) { | 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)) { |
43 } | 49 } |
44 | 50 |
45 MotionEventGeneric::MotionEventGeneric(Action action, | 51 MotionEventGeneric::MotionEventGeneric(Action action, |
46 base::TimeTicks event_time, | 52 base::TimeTicks event_time, |
47 const PointerProperties& pointer) | 53 const PointerProperties& pointer) |
48 : action_(action), | 54 : action_(action), |
49 event_time_(event_time), | 55 event_time_(event_time), |
50 id_(0), | 56 id_(0), |
51 action_index_(0), | 57 action_index_(0), |
52 button_state_(0), | 58 button_state_(0), |
53 flags_(0) { | 59 flags_(0) { |
54 PushPointer(pointer); | 60 PushPointer(pointer); |
55 } | 61 } |
56 | 62 |
57 MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric& other) | 63 MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric& other) |
58 : action_(other.action_), | 64 : action_(other.action_), |
59 event_time_(other.event_time_), | 65 event_time_(other.event_time_), |
60 id_(other.id_), | 66 id_(other.id_), |
61 action_index_(other.action_index_), | 67 action_index_(other.action_index_), |
62 button_state_(other.button_state_), | 68 button_state_(other.button_state_), |
63 flags_(other.flags_), | 69 flags_(other.flags_), |
64 pointers_(other.pointers_) { | 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()); |
65 } | 74 } |
66 | 75 |
67 MotionEventGeneric::~MotionEventGeneric() { | 76 MotionEventGeneric::~MotionEventGeneric() { |
68 } | 77 } |
69 | 78 |
70 int MotionEventGeneric::GetId() const { | 79 int MotionEventGeneric::GetId() const { |
71 return id_; | 80 return id_; |
72 } | 81 } |
73 | 82 |
74 MotionEvent::Action MotionEventGeneric::GetAction() const { | 83 MotionEvent::Action MotionEventGeneric::GetAction() const { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } | 148 } |
140 | 149 |
141 int MotionEventGeneric::GetFlags() const { | 150 int MotionEventGeneric::GetFlags() const { |
142 return flags_; | 151 return flags_; |
143 } | 152 } |
144 | 153 |
145 base::TimeTicks MotionEventGeneric::GetEventTime() const { | 154 base::TimeTicks MotionEventGeneric::GetEventTime() const { |
146 return event_time_; | 155 return event_time_; |
147 } | 156 } |
148 | 157 |
149 scoped_ptr<MotionEvent> MotionEventGeneric::Clone() const { | 158 size_t MotionEventGeneric::GetHistorySize() const { |
150 return scoped_ptr<MotionEvent>(new MotionEventGeneric(*this)); | 159 return historical_events_.size(); |
151 } | 160 } |
152 | 161 |
153 scoped_ptr<MotionEvent> MotionEventGeneric::Cancel() const { | 162 base::TimeTicks MotionEventGeneric::GetHistoricalEventTime( |
154 scoped_ptr<MotionEventGeneric> event(new MotionEventGeneric(*this)); | 163 size_t historical_index) const { |
155 event->set_action(ACTION_CANCEL); | 164 DCHECK_LT(historical_index, historical_events_.size()); |
156 return event.Pass(); | 165 return historical_events_[historical_index]->GetEventTime(); |
| 166 } |
| 167 |
| 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); |
| 173 } |
| 174 |
| 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); |
| 179 } |
| 180 |
| 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); |
| 185 } |
| 186 |
| 187 // static |
| 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)); |
| 192 } |
| 193 |
| 194 // static |
| 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(); |
157 } | 202 } |
158 | 203 |
159 void MotionEventGeneric::PushPointer(const PointerProperties& pointer) { | 204 void MotionEventGeneric::PushPointer(const PointerProperties& pointer) { |
| 205 DCHECK_EQ(0U, GetHistorySize()); |
160 pointers_->push_back(pointer); | 206 pointers_->push_back(pointer); |
161 } | 207 } |
162 | 208 |
| 209 void MotionEventGeneric::PushHistoricalEvent(scoped_ptr<MotionEvent> event) { |
| 210 DCHECK(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()); |
| 217 } |
| 218 |
| 219 MotionEventGeneric::MotionEventGeneric() |
| 220 : action_(ACTION_CANCEL), id_(0), action_index_(0), button_state_(0) { |
| 221 } |
| 222 |
| 223 MotionEventGeneric::MotionEventGeneric(const MotionEvent& event, |
| 224 bool with_history) |
| 225 : action_(event.GetAction()), |
| 226 event_time_(event.GetEventTime()), |
| 227 id_(event.GetId()), |
| 228 action_index_( |
| 229 (action_ == ACTION_POINTER_UP || action_ == ACTION_POINTER_DOWN) |
| 230 ? event.GetActionIndex() |
| 231 : 0), |
| 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)); |
| 237 |
| 238 if (!with_history) |
| 239 return; |
| 240 |
| 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))); |
| 251 } |
| 252 PushHistoricalEvent(historical_event.Pass()); |
| 253 } |
| 254 } |
| 255 |
| 256 MotionEventGeneric& MotionEventGeneric::operator=( |
| 257 const MotionEventGeneric& other) { |
| 258 action_ = other.action_; |
| 259 event_time_ = other.event_time_; |
| 260 id_ = other.id_; |
| 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()); |
| 268 return *this; |
| 269 } |
| 270 |
163 void MotionEventGeneric::PopPointer() { | 271 void MotionEventGeneric::PopPointer() { |
164 DCHECK_GT(pointers_->size(), 0U); | 272 DCHECK_GT(pointers_->size(), 0U); |
165 pointers_->pop_back(); | 273 pointers_->pop_back(); |
166 } | 274 } |
167 | 275 |
168 } // namespace ui | 276 } // namespace ui |
OLD | NEW |