Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / events / gesture_detection / motion_event_generic.cc
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.
4
5 #include "ui/events/gesture_detection/motion_event_generic.h"
6
7 #include "base/logging.h"
8
9 namespace ui {
10
11 PointerProperties::PointerProperties()
12     : id(0),
13       tool_type(MotionEvent::TOOL_TYPE_UNKNOWN),
14       x(0),
15       y(0),
16       raw_x(0),
17       raw_y(0),
18       pressure(0),
19       touch_major(0),
20       touch_minor(0),
21       orientation(0) {
22 }
23
24 PointerProperties::PointerProperties(float x, float y, float touch_major)
25     : id(0),
26       tool_type(MotionEvent::TOOL_TYPE_UNKNOWN),
27       x(x),
28       y(y),
29       raw_x(x),
30       raw_y(y),
31       pressure(0),
32       touch_major(touch_major),
33       touch_minor(0),
34       orientation(0) {
35 }
36
37 PointerProperties::PointerProperties(const MotionEvent& event,
38                                      size_t pointer_index)
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)) {
49 }
50
51 MotionEventGeneric::MotionEventGeneric(Action action,
52                                        base::TimeTicks event_time,
53                                        const PointerProperties& pointer)
54     : action_(action),
55       event_time_(event_time),
56       id_(0),
57       action_index_(0),
58       button_state_(0),
59       flags_(0) {
60   PushPointer(pointer);
61 }
62
63 MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric& other)
64     : action_(other.action_),
65       event_time_(other.event_time_),
66       id_(other.id_),
67       action_index_(other.action_index_),
68       button_state_(other.button_state_),
69       flags_(other.flags_),
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());
74 }
75
76 MotionEventGeneric::~MotionEventGeneric() {
77 }
78
79 int MotionEventGeneric::GetId() const {
80   return id_;
81 }
82
83 MotionEvent::Action MotionEventGeneric::GetAction() const {
84   return action_;
85 }
86
87 int MotionEventGeneric::GetActionIndex() const {
88   return action_index_;
89 }
90
91 size_t MotionEventGeneric::GetPointerCount() const {
92   return pointers_->size();
93 }
94
95 int MotionEventGeneric::GetPointerId(size_t pointer_index) const {
96   DCHECK_LT(pointer_index, pointers_->size());
97   return pointers_[pointer_index].id;
98 }
99
100 float MotionEventGeneric::GetX(size_t pointer_index) const {
101   DCHECK_LT(pointer_index, pointers_->size());
102   return pointers_[pointer_index].x;
103 }
104
105 float MotionEventGeneric::GetY(size_t pointer_index) const {
106   DCHECK_LT(pointer_index, pointers_->size());
107   return pointers_[pointer_index].y;
108 }
109
110 float MotionEventGeneric::GetRawX(size_t pointer_index) const {
111   DCHECK_LT(pointer_index, pointers_->size());
112   return pointers_[pointer_index].raw_x;
113 }
114
115 float MotionEventGeneric::GetRawY(size_t pointer_index) const {
116   DCHECK_LT(pointer_index, pointers_->size());
117   return pointers_[pointer_index].raw_y;
118 }
119
120 float MotionEventGeneric::GetTouchMajor(size_t pointer_index) const {
121   DCHECK_LT(pointer_index, pointers_->size());
122   return pointers_[pointer_index].touch_major;
123 }
124
125 float MotionEventGeneric::GetTouchMinor(size_t pointer_index) const {
126   DCHECK_LT(pointer_index, pointers_->size());
127   return pointers_[pointer_index].touch_minor;
128 }
129
130 float MotionEventGeneric::GetOrientation(size_t pointer_index) const {
131   DCHECK_LT(pointer_index, pointers_->size());
132   return pointers_[pointer_index].orientation;
133 }
134
135 float MotionEventGeneric::GetPressure(size_t pointer_index) const {
136   DCHECK_LT(pointer_index, pointers_->size());
137   return pointers_[pointer_index].pressure;
138 }
139
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;
144 }
145
146 int MotionEventGeneric::GetButtonState() const {
147   return button_state_;
148 }
149
150 int MotionEventGeneric::GetFlags() const {
151   return flags_;
152 }
153
154 base::TimeTicks MotionEventGeneric::GetEventTime() const {
155   return event_time_;
156 }
157
158 size_t MotionEventGeneric::GetHistorySize() const {
159   return historical_events_.size();
160 }
161
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();
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();
202 }
203
204 void MotionEventGeneric::PushPointer(const PointerProperties& pointer) {
205   DCHECK_EQ(0U, GetHistorySize());
206   pointers_->push_back(pointer);
207 }
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
271 void MotionEventGeneric::PopPointer() {
272   DCHECK_GT(pointers_->size(), 0U);
273   pointers_->pop_back();
274 }
275
276 }  // namespace ui