1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/events/gesture_detection/motion_event_generic.h"
#include "base/logging.h"
namespace ui {
PointerProperties::PointerProperties()
: PointerProperties(0, 0, 0) {
}
PointerProperties::PointerProperties(float x, float y, float touch_major)
: id(0),
tool_type(MotionEvent::TOOL_TYPE_UNKNOWN),
x(x),
y(y),
raw_x(x),
raw_y(y),
pressure(0),
touch_major(touch_major),
touch_minor(0),
orientation(0),
source_device_id(0) {
}
PointerProperties::PointerProperties(const MotionEvent& event,
size_t pointer_index)
: id(event.GetPointerId(pointer_index)),
tool_type(event.GetToolType(pointer_index)),
x(event.GetX(pointer_index)),
y(event.GetY(pointer_index)),
raw_x(event.GetRawX(pointer_index)),
raw_y(event.GetRawY(pointer_index)),
pressure(event.GetPressure(pointer_index)),
touch_major(event.GetTouchMajor(pointer_index)),
touch_minor(event.GetTouchMinor(pointer_index)),
orientation(event.GetOrientation(pointer_index)),
source_device_id(0) {
}
MotionEventGeneric::MotionEventGeneric(Action action,
base::TimeTicks event_time,
const PointerProperties& pointer)
: action_(action),
event_time_(event_time),
id_(0),
action_index_(0),
button_state_(0),
flags_(0) {
PushPointer(pointer);
}
MotionEventGeneric::MotionEventGeneric(const MotionEventGeneric& other)
: action_(other.action_),
event_time_(other.event_time_),
id_(other.id_),
action_index_(other.action_index_),
button_state_(other.button_state_),
flags_(other.flags_),
pointers_(other.pointers_) {
const size_t history_size = other.GetHistorySize();
for (size_t h = 0; h < history_size; ++h)
PushHistoricalEvent(other.historical_events_[h]->Clone());
}
MotionEventGeneric::~MotionEventGeneric() {
}
int MotionEventGeneric::GetId() const {
return id_;
}
MotionEvent::Action MotionEventGeneric::GetAction() const {
return action_;
}
int MotionEventGeneric::GetActionIndex() const {
DCHECK(action_ == ACTION_POINTER_DOWN || action_ == ACTION_POINTER_UP);
DCHECK_GE(action_index_, 0);
DCHECK_LT(action_index_, static_cast<int>(pointers_->size()));
return action_index_;
}
size_t MotionEventGeneric::GetPointerCount() const {
return pointers_->size();
}
int MotionEventGeneric::GetPointerId(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].id;
}
float MotionEventGeneric::GetX(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].x;
}
float MotionEventGeneric::GetY(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].y;
}
float MotionEventGeneric::GetRawX(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].raw_x;
}
float MotionEventGeneric::GetRawY(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].raw_y;
}
float MotionEventGeneric::GetTouchMajor(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].touch_major;
}
float MotionEventGeneric::GetTouchMinor(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].touch_minor;
}
float MotionEventGeneric::GetOrientation(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].orientation;
}
float MotionEventGeneric::GetPressure(size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].pressure;
}
MotionEvent::ToolType MotionEventGeneric::GetToolType(
size_t pointer_index) const {
DCHECK_LT(pointer_index, pointers_->size());
return pointers_[pointer_index].tool_type;
}
int MotionEventGeneric::GetButtonState() const {
return button_state_;
}
int MotionEventGeneric::GetFlags() const {
return flags_;
}
base::TimeTicks MotionEventGeneric::GetEventTime() const {
return event_time_;
}
size_t MotionEventGeneric::GetHistorySize() const {
return historical_events_.size();
}
base::TimeTicks MotionEventGeneric::GetHistoricalEventTime(
size_t historical_index) const {
DCHECK_LT(historical_index, historical_events_.size());
return historical_events_[historical_index]->GetEventTime();
}
float MotionEventGeneric::GetHistoricalTouchMajor(
size_t pointer_index,
size_t historical_index) const {
DCHECK_LT(historical_index, historical_events_.size());
return historical_events_[historical_index]->GetTouchMajor(pointer_index);
}
float MotionEventGeneric::GetHistoricalX(size_t pointer_index,
size_t historical_index) const {
DCHECK_LT(historical_index, historical_events_.size());
return historical_events_[historical_index]->GetX(pointer_index);
}
float MotionEventGeneric::GetHistoricalY(size_t pointer_index,
size_t historical_index) const {
DCHECK_LT(historical_index, historical_events_.size());
return historical_events_[historical_index]->GetY(pointer_index);
}
// static
scoped_ptr<MotionEventGeneric> MotionEventGeneric::CloneEvent(
const MotionEvent& event) {
bool with_history = true;
return make_scoped_ptr(new MotionEventGeneric(event, with_history));
}
// static
scoped_ptr<MotionEventGeneric> MotionEventGeneric::CancelEvent(
const MotionEvent& event) {
bool with_history = false;
scoped_ptr<MotionEventGeneric> cancel_event(
new MotionEventGeneric(event, with_history));
cancel_event->set_action(ACTION_CANCEL);
return cancel_event.Pass();
}
void MotionEventGeneric::PushPointer(const PointerProperties& pointer) {
DCHECK_EQ(0U, GetHistorySize());
pointers_->push_back(pointer);
}
void MotionEventGeneric::PushHistoricalEvent(scoped_ptr<MotionEvent> event) {
DCHECK(event);
DCHECK_EQ(event->GetAction(), ACTION_MOVE);
DCHECK_EQ(event->GetPointerCount(), GetPointerCount());
DCHECK_EQ(event->GetAction(), GetAction());
DCHECK_LE(event->GetEventTime().ToInternalValue(),
GetEventTime().ToInternalValue());
historical_events_.push_back(event.release());
}
MotionEventGeneric::MotionEventGeneric()
: action_(ACTION_CANCEL), id_(0), action_index_(0), button_state_(0) {
}
MotionEventGeneric::MotionEventGeneric(const MotionEvent& event,
bool with_history)
: action_(event.GetAction()),
event_time_(event.GetEventTime()),
id_(event.GetId()),
action_index_(
(action_ == ACTION_POINTER_UP || action_ == ACTION_POINTER_DOWN)
? event.GetActionIndex()
: 0),
button_state_(event.GetButtonState()),
flags_(event.GetFlags()) {
const size_t pointer_count = event.GetPointerCount();
for (size_t i = 0; i < pointer_count; ++i)
PushPointer(PointerProperties(event, i));
if (!with_history)
return;
const size_t history_size = event.GetHistorySize();
for (size_t h = 0; h < history_size; ++h) {
scoped_ptr<MotionEventGeneric> historical_event(new MotionEventGeneric());
historical_event->set_action(ACTION_MOVE);
historical_event->set_event_time(event.GetHistoricalEventTime(h));
for (size_t i = 0; i < pointer_count; ++i) {
historical_event->PushPointer(
PointerProperties(event.GetHistoricalX(i, h),
event.GetHistoricalY(i, h),
event.GetHistoricalTouchMajor(i, h)));
}
PushHistoricalEvent(historical_event.Pass());
}
}
MotionEventGeneric& MotionEventGeneric::operator=(
const MotionEventGeneric& other) {
action_ = other.action_;
event_time_ = other.event_time_;
id_ = other.id_;
action_index_ = other.action_index_;
button_state_ = other.button_state_;
flags_ = other.flags_;
pointers_ = other.pointers_;
const size_t history_size = other.GetHistorySize();
for (size_t h = 0; h < history_size; ++h)
PushHistoricalEvent(other.historical_events_[h]->Clone());
return *this;
}
void MotionEventGeneric::PopPointer() {
DCHECK_GT(pointers_->size(), 0U);
pointers_->pop_back();
}
} // namespace ui
|