Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/aura/gestures/gesture_sequence.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "ui/aura/event.h" | |
| 10 #include "ui/base/events.h" | |
| 11 | |
| 12 namespace aura { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Get equivalent TouchState from EventType |type|. | |
| 17 GestureSequence::TouchState TouchEventTypeToTouchState(ui::EventType type) { | |
| 18 switch (type) { | |
| 19 case ui::ET_TOUCH_RELEASED: | |
| 20 return GestureSequence::TS_RELEASED; | |
| 21 case ui::ET_TOUCH_PRESSED: | |
| 22 return GestureSequence::TS_PRESSED; | |
| 23 case ui::ET_TOUCH_MOVED: | |
| 24 return GestureSequence::TS_MOVED; | |
| 25 case ui::ET_TOUCH_STATIONARY: | |
| 26 return GestureSequence::TS_STATIONARY; | |
| 27 case ui::ET_TOUCH_CANCELLED: | |
| 28 return GestureSequence::TS_CANCELLED; | |
| 29 default: | |
| 30 VLOG(1) << "Unknown Touch Event type"; | |
| 31 } | |
| 32 return GestureSequence::TS_UNKNOWN; | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 //////////////////////////////////////////////////////////////////////////////// | |
| 38 // GestureSequence Public: | |
| 39 | |
| 40 GestureSequence::GestureSequence() | |
| 41 : state_(GS_NO_GESTURE), | |
| 42 flags_(0) { | |
| 43 } | |
| 44 | |
| 45 GestureSequence::~GestureSequence() { | |
| 46 } | |
| 47 | |
| 48 GestureSequence::Gestures* GestureSequence::ProcessTouchEventForGesture( | |
| 49 const TouchEvent& event, | |
| 50 ui::TouchStatus status) { | |
| 51 if (status != ui::TOUCH_STATUS_UNKNOWN) | |
| 52 return NULL; // The event was consumed by a touch sequence. | |
| 53 | |
| 54 scoped_ptr<Gestures> gestures(new Gestures()); | |
| 55 UpdateValues(event); | |
| 56 GesturePoint& point = GesturePointForEvent(event); | |
| 57 flags_ = event.flags(); | |
| 58 switch (Signature(state_, event.touch_id(), event.type(), false)) { | |
| 59 case GST_NO_GESTURE_FIRST_PRESSED: | |
| 60 TouchDown(event, point, gestures.get()); | |
| 61 break; | |
| 62 case GST_PENDING_SYNTHETIC_CLICK_FIRST_RELEASED: | |
| 63 if (Click(event, point, gestures.get())) | |
| 64 point.UpdateForTap(); | |
| 65 break; | |
| 66 case GST_PENDING_SYNTHETIC_CLICK_FIRST_MOVED: | |
| 67 case GST_PENDING_SYNTHETIC_CLICK_FIRST_STATIONARY: | |
| 68 if (InClickOrScroll(event, point, gestures.get())) | |
| 69 point.UpdateForScroll(); | |
| 70 break; | |
| 71 case GST_PENDING_SYNTHETIC_CLICK_FIRST_CANCELLED: | |
| 72 NoGesture(event, point, gestures.get()); | |
| 73 break; | |
| 74 case GST_SCROLL_FIRST_MOVED: | |
| 75 if (InScroll(event, point, gestures.get())) | |
| 76 point.UpdateForScroll(); | |
| 77 break; | |
| 78 case GST_SCROLL_FIRST_RELEASED: | |
| 79 case GST_SCROLL_FIRST_CANCELLED: | |
| 80 ScrollEnd(event, point, gestures.get()); | |
| 81 break; | |
| 82 } | |
| 83 return gestures.release(); | |
| 84 } | |
| 85 | |
| 86 void GestureSequence::Reset() { | |
| 87 state_ = GS_NO_GESTURE; | |
| 88 point_.Reset(); | |
| 89 } | |
| 90 | |
| 91 //////////////////////////////////////////////////////////////////////////////// | |
| 92 // GestureSequence Private: | |
| 93 | |
| 94 unsigned int GestureSequence::Signature(GestureState gesture_state, | |
| 95 unsigned int touch_id, | |
| 96 ui::EventType type, | |
| 97 bool touch_handled) { | |
| 98 CHECK((touch_id & 0xfff) == touch_id); | |
| 99 TouchState touch_state = TouchEventTypeToTouchState(type); | |
| 100 return 1 + ((touch_state & 0x7) << 1 | (touch_handled ? 1 << 4 : 0) | | |
| 101 ((touch_id & 0xfff) << 5) | (gesture_state << 17)); | |
| 102 } | |
| 103 | |
| 104 GesturePoint& GestureSequence::GesturePointForEvent( | |
| 105 const TouchEvent& event) { | |
| 106 // TODO(sad): | |
|
sadrul
2012/01/30 23:05:02
This will find the appropriate GesturePoint depend
| |
| 107 return point_; | |
| 108 } | |
| 109 | |
| 110 void GestureSequence::AppendTapDownGestureEvent(const GesturePoint& point, | |
| 111 Gestures* gestures) { | |
| 112 gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent( | |
| 113 ui::ET_GESTURE_TAP_DOWN, | |
| 114 point.first_touch_position().x(), | |
| 115 point.first_touch_position().y(), | |
| 116 flags_, | |
| 117 base::Time::FromDoubleT(point.last_touch_time()), | |
| 118 0.f, 0.f))); | |
| 119 } | |
| 120 | |
| 121 void GestureSequence::AppendClickGestureEvent(const GesturePoint& point, | |
| 122 Gestures* gestures) { | |
| 123 gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent( | |
| 124 ui::ET_GESTURE_TAP, | |
| 125 point.first_touch_position().x(), | |
| 126 point.first_touch_position().y(), | |
| 127 flags_, | |
| 128 base::Time::FromDoubleT(point.last_touch_time()), | |
| 129 0.f, 0.f))); | |
| 130 } | |
| 131 | |
| 132 void GestureSequence::AppendDoubleClickGestureEvent(const GesturePoint& point, | |
| 133 Gestures* gestures) { | |
| 134 gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent( | |
| 135 ui::ET_GESTURE_DOUBLE_TAP, | |
| 136 point.first_touch_position().x(), | |
| 137 point.first_touch_position().y(), | |
| 138 flags_, | |
| 139 base::Time::FromDoubleT(point.last_touch_time()), | |
| 140 0.f, 0.f))); | |
| 141 } | |
| 142 | |
| 143 void GestureSequence::AppendScrollGestureBegin(const GesturePoint& point, | |
| 144 Gestures* gestures) { | |
| 145 gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent( | |
| 146 ui::ET_GESTURE_SCROLL_BEGIN, | |
| 147 point.last_touch_position().x(), | |
| 148 point.last_touch_position().y(), | |
| 149 flags_, | |
| 150 base::Time::FromDoubleT(point.last_touch_time()), | |
| 151 0.f, 0.f))); | |
| 152 } | |
| 153 | |
| 154 void GestureSequence::AppendScrollGestureEnd(const GesturePoint& point, | |
| 155 Gestures* gestures, | |
| 156 float x_velocity, | |
| 157 float y_velocity) { | |
| 158 gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent( | |
| 159 ui::ET_GESTURE_SCROLL_END, | |
| 160 point.last_touch_position().x(), | |
| 161 point.last_touch_position().y(), | |
| 162 flags_, | |
| 163 base::Time::FromDoubleT(point.last_touch_time()), | |
| 164 x_velocity, y_velocity))); | |
| 165 } | |
| 166 | |
| 167 void GestureSequence:: AppendScrollGestureUpdate(const GesturePoint& point, | |
| 168 Gestures* gestures) { | |
| 169 gestures->push_back(linked_ptr<GestureEvent>(new GestureEvent( | |
| 170 ui::ET_GESTURE_SCROLL_UPDATE, | |
| 171 point.last_touch_position().x(), | |
| 172 point.last_touch_position().y(), | |
| 173 flags_, | |
| 174 base::Time::FromDoubleT(point.last_touch_time()), | |
| 175 point.x_delta(), point.y_delta()))); | |
| 176 | |
| 177 // first_touch_position_ = event.location(); | |
| 178 } | |
| 179 | |
| 180 void GestureSequence::UpdateValues(const TouchEvent& event) { | |
| 181 point_.UpdateValues(event, state_); | |
| 182 } | |
| 183 | |
| 184 bool GestureSequence::Click(const TouchEvent& event, | |
| 185 const GesturePoint& point, Gestures* gestures) { | |
| 186 if (point.IsInClickTimeWindow() && point.IsInsideManhattanSquare(event)) { | |
| 187 AppendClickGestureEvent(point, gestures); | |
| 188 if (point.IsInSecondClickTimeWindow() && | |
| 189 point.IsSecondClickInsideManhattanSquare(event)) { | |
| 190 AppendDoubleClickGestureEvent(point, gestures); | |
| 191 } | |
| 192 return true; | |
| 193 } | |
| 194 return false; | |
| 195 } | |
| 196 | |
| 197 bool GestureSequence::InClickOrScroll(const TouchEvent& event, | |
| 198 const GesturePoint& point, Gestures* gestures) { | |
| 199 if (point.IsInClickTimeWindow() && point.IsInsideManhattanSquare(event)) { | |
| 200 SetState(GS_PENDING_SYNTHETIC_CLICK); | |
| 201 return false; | |
| 202 } | |
| 203 if (event.type() == ui::ET_TOUCH_MOVED && | |
| 204 !point.IsInsideManhattanSquare(event)) { | |
| 205 AppendScrollGestureBegin(point, gestures); | |
| 206 AppendScrollGestureUpdate(point, gestures); | |
| 207 SetState(GS_SCROLL); | |
| 208 return true; | |
| 209 } | |
| 210 return false; | |
| 211 } | |
| 212 | |
| 213 bool GestureSequence::InScroll(const TouchEvent& event, | |
| 214 const GesturePoint& point, Gestures* gestures) { | |
| 215 AppendScrollGestureUpdate(point, gestures); | |
| 216 return true; | |
| 217 } | |
| 218 | |
| 219 bool GestureSequence::NoGesture(const TouchEvent&, | |
| 220 const GesturePoint& point, Gestures*) { | |
| 221 Reset(); | |
| 222 return false; | |
| 223 } | |
| 224 | |
| 225 bool GestureSequence::TouchDown(const TouchEvent& event, | |
| 226 const GesturePoint& point, Gestures* gestures) { | |
| 227 AppendTapDownGestureEvent(point, gestures); | |
| 228 SetState(GS_PENDING_SYNTHETIC_CLICK); | |
| 229 return false; | |
| 230 } | |
| 231 | |
| 232 bool GestureSequence::ScrollEnd(const TouchEvent& event, | |
| 233 const GesturePoint& point, Gestures* gestures) { | |
| 234 if (point.IsOverMinFlickSpeed() && event.type() != ui::ET_TOUCH_CANCELLED) | |
| 235 AppendScrollGestureEnd(point, gestures, | |
| 236 point.x_velocity(), point.y_velocity()); | |
| 237 else | |
| 238 AppendScrollGestureEnd(point, gestures, 0.f, 0.f); | |
| 239 SetState(GS_NO_GESTURE); | |
| 240 Reset(); | |
| 241 return false; | |
| 242 } | |
| 243 | |
| 244 } // namespace aura | |
| OLD | NEW |