f083169868990226c8528bd48c2890334959f48f
[platform/framework/web/crosswalk.git] / src / ui / events / gesture_detection / gesture_event_data_packet.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/gesture_event_data_packet.h"
6
7 #include "base/logging.h"
8 #include "ui/events/gesture_detection/motion_event.h"
9
10 namespace ui {
11 namespace {
12
13 GestureEventDataPacket::GestureSource ToGestureSource(
14     const ui::MotionEvent& event) {
15   switch (event.GetAction()) {
16     case ui::MotionEvent::ACTION_DOWN:
17       return GestureEventDataPacket::TOUCH_SEQUENCE_START;
18     case ui::MotionEvent::ACTION_UP:
19       return GestureEventDataPacket::TOUCH_SEQUENCE_END;
20     case ui::MotionEvent::ACTION_MOVE:
21       return GestureEventDataPacket::TOUCH_MOVE;
22     case ui::MotionEvent::ACTION_CANCEL:
23       return GestureEventDataPacket::TOUCH_SEQUENCE_CANCEL;
24     case ui::MotionEvent::ACTION_POINTER_DOWN:
25       return GestureEventDataPacket::TOUCH_START;
26     case ui::MotionEvent::ACTION_POINTER_UP:
27       return GestureEventDataPacket::TOUCH_END;
28   };
29   NOTREACHED() << "Invalid ui::MotionEvent action: " << event.GetAction();
30   return GestureEventDataPacket::INVALID;
31 }
32
33 }  // namespace
34
35 GestureEventDataPacket::GestureEventDataPacket()
36     : gesture_count_(0), gesture_source_(UNDEFINED) {
37 }
38
39 GestureEventDataPacket::GestureEventDataPacket(
40     base::TimeTicks timestamp,
41     GestureSource source,
42     const gfx::PointF& touch_location,
43     const gfx::PointF& raw_touch_location)
44     : timestamp_(timestamp),
45       gesture_count_(0),
46       touch_location_(touch_location),
47       raw_touch_location_(raw_touch_location),
48       gesture_source_(source) {
49   DCHECK_NE(gesture_source_, UNDEFINED);
50 }
51
52 GestureEventDataPacket::GestureEventDataPacket(
53     const GestureEventDataPacket& other)
54     : timestamp_(other.timestamp_),
55       gesture_count_(other.gesture_count_),
56       touch_location_(other.touch_location_),
57       raw_touch_location_(other.raw_touch_location_),
58       gesture_source_(other.gesture_source_) {
59   std::copy(other.gestures_, other.gestures_ + other.gesture_count_, gestures_);
60 }
61
62 GestureEventDataPacket::~GestureEventDataPacket() {
63 }
64
65 GestureEventDataPacket& GestureEventDataPacket::operator=(
66     const GestureEventDataPacket& other) {
67   timestamp_ = other.timestamp_;
68   gesture_count_ = other.gesture_count_;
69   gesture_source_ = other.gesture_source_;
70   touch_location_ = other.touch_location_;
71   raw_touch_location_ = other.raw_touch_location_;
72   std::copy(other.gestures_, other.gestures_ + other.gesture_count_, gestures_);
73   return *this;
74 }
75
76 void GestureEventDataPacket::Push(const GestureEventData& gesture) {
77   DCHECK_NE(ET_UNKNOWN, gesture.type());
78   CHECK_LT(gesture_count_, static_cast<size_t>(kMaxGesturesPerTouch));
79   gestures_[gesture_count_++] = gesture;
80 }
81
82 GestureEventDataPacket GestureEventDataPacket::FromTouch(
83     const ui::MotionEvent& touch) {
84   return GestureEventDataPacket(touch.GetEventTime(),
85                                 ToGestureSource(touch),
86                                 gfx::PointF(touch.GetX(), touch.GetY()),
87                                 gfx::PointF(touch.GetRawX(), touch.GetRawY()));
88 }
89
90 GestureEventDataPacket GestureEventDataPacket::FromTouchTimeout(
91     const GestureEventData& gesture) {
92   GestureEventDataPacket packet(gesture.time,
93                                 TOUCH_TIMEOUT,
94                                 gfx::PointF(gesture.x, gesture.y),
95                                 gfx::PointF(gesture.raw_x, gesture.raw_y));
96   packet.Push(gesture);
97   return packet;
98 }
99
100 }  // namespace ui