| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/android/vr_shell/vr_controller.h" | 5 #include "chrome/browser/android/vr_shell/vr_controller.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list; | 189 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list; |
| 190 std::unique_ptr<WebGestureEvent> gesture(new WebGestureEvent()); | 190 std::unique_ptr<WebGestureEvent> gesture(new WebGestureEvent()); |
| 191 | 191 |
| 192 if (controller_state_->GetConnectionState() != gvr::kControllerConnected) { | 192 if (controller_state_->GetConnectionState() != gvr::kControllerConnected) { |
| 193 gesture_list.push_back(std::move(gesture)); | 193 gesture_list.push_back(std::move(gesture)); |
| 194 return gesture_list; | 194 return gesture_list; |
| 195 } | 195 } |
| 196 UpdateTouchInfo(); | 196 UpdateTouchInfo(); |
| 197 UpdateGestureFromTouchInfo(gesture.get()); | 197 UpdateGestureFromTouchInfo(gesture.get()); |
| 198 | 198 |
| 199 if (gesture->type == WebInputEvent::Undefined && | 199 if (gesture->type() == WebInputEvent::Undefined && |
| 200 ButtonUpHappened(gvr::kControllerButtonClick)) { | 200 ButtonUpHappened(gvr::kControllerButtonClick)) { |
| 201 gesture->type = WebInputEvent::GestureTapDown; | 201 gesture->setType(WebInputEvent::GestureTapDown); |
| 202 gesture->x = 0; | 202 gesture->x = 0; |
| 203 gesture->y = 0; | 203 gesture->y = 0; |
| 204 } | 204 } |
| 205 gesture->sourceDevice = blink::WebGestureDeviceTouchpad; | 205 gesture->sourceDevice = blink::WebGestureDeviceTouchpad; |
| 206 gesture_list.push_back(std::move(gesture)); | 206 gesture_list.push_back(std::move(gesture)); |
| 207 | 207 |
| 208 if (gesture_list.back()->type == WebInputEvent::GestureScrollEnd) { | 208 if (gesture_list.back()->type() == WebInputEvent::GestureScrollEnd) { |
| 209 if (!ButtonDownHappened(gvr::kControllerButtonClick)) { | 209 if (!ButtonDownHappened(gvr::kControllerButtonClick)) { |
| 210 std::unique_ptr<WebGestureEvent> fling(new WebGestureEvent()); | 210 std::unique_ptr<WebGestureEvent> fling(new WebGestureEvent( |
| 211 fling->timeStampSeconds = gesture_list.back()->timeStampSeconds; | 211 WebInputEvent::GestureFlingStart, WebInputEvent::NoModifiers, |
| 212 gesture_list.back()->timeStampSeconds())); |
| 212 fling->sourceDevice = blink::WebGestureDeviceTouchpad; | 213 fling->sourceDevice = blink::WebGestureDeviceTouchpad; |
| 213 fling->type = WebInputEvent::GestureFlingStart; | |
| 214 if (IsHorizontalGesture()) { | 214 if (IsHorizontalGesture()) { |
| 215 fling->data.flingStart.velocityX = | 215 fling->data.flingStart.velocityX = |
| 216 overall_velocity_.x * kDisplacementScaleFactor; | 216 overall_velocity_.x * kDisplacementScaleFactor; |
| 217 } else { | 217 } else { |
| 218 fling->data.flingStart.velocityY = | 218 fling->data.flingStart.velocityY = |
| 219 overall_velocity_.y * kDisplacementScaleFactor; | 219 overall_velocity_.y * kDisplacementScaleFactor; |
| 220 } | 220 } |
| 221 gesture_list.push_back(std::move(fling)); | 221 gesture_list.push_back(std::move(fling)); |
| 222 } | 222 } |
| 223 Reset(); | 223 Reset(); |
| 224 } | 224 } |
| 225 | 225 |
| 226 return gesture_list; | 226 return gesture_list; |
| 227 } | 227 } |
| 228 | 228 |
| 229 void VrController::UpdateGestureFromTouchInfo(WebGestureEvent* gesture) { | 229 void VrController::UpdateGestureFromTouchInfo(WebGestureEvent* gesture) { |
| 230 gesture->timeStampSeconds = | 230 gesture->setTimeStampSeconds( |
| 231 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | 231 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF()); |
| 232 switch (state_) { | 232 switch (state_) { |
| 233 // User has not put finger on touch pad. | 233 // User has not put finger on touch pad. |
| 234 case WAITING: | 234 case WAITING: |
| 235 HandleWaitingState(gesture); | 235 HandleWaitingState(gesture); |
| 236 break; | 236 break; |
| 237 // User has not started a gesture (by moving out of slop). | 237 // User has not started a gesture (by moving out of slop). |
| 238 case TOUCHING: | 238 case TOUCHING: |
| 239 HandleDetectingState(gesture); | 239 HandleDetectingState(gesture); |
| 240 break; | 240 break; |
| 241 // User is scrolling on touchpad | 241 // User is scrolling on touchpad |
| 242 case SCROLLING: | 242 case SCROLLING: |
| 243 HandleScrollingState(gesture); | 243 HandleScrollingState(gesture); |
| 244 break; | 244 break; |
| 245 default: | 245 default: |
| 246 LOG(ERROR) << "Wrong gesture detector state: " << state_; | 246 LOG(ERROR) << "Wrong gesture detector state: " << state_; |
| 247 break; | 247 break; |
| 248 } | 248 } |
| 249 } | 249 } |
| 250 | 250 |
| 251 void VrController::HandleWaitingState(WebGestureEvent* gesture) { | 251 void VrController::HandleWaitingState(WebGestureEvent* gesture) { |
| 252 // User puts finger on touch pad (or when the touch down for current gesture | 252 // User puts finger on touch pad (or when the touch down for current gesture |
| 253 // is missed, initiate gesture from current touch point). | 253 // is missed, initiate gesture from current touch point). |
| 254 if (touch_info_->touch_down || touch_info_->is_touching) { | 254 if (touch_info_->touch_down || touch_info_->is_touching) { |
| 255 // update initial touchpoint | 255 // update initial touchpoint |
| 256 *init_touch_point_ = touch_info_->touch_point; | 256 *init_touch_point_ = touch_info_->touch_point; |
| 257 // update current touchpoint | 257 // update current touchpoint |
| 258 *cur_touch_point_ = touch_info_->touch_point; | 258 *cur_touch_point_ = touch_info_->touch_point; |
| 259 state_ = TOUCHING; | 259 state_ = TOUCHING; |
| 260 | 260 |
| 261 gesture->type = WebInputEvent::GestureFlingCancel; | 261 gesture->setType(WebInputEvent::GestureFlingCancel); |
| 262 gesture->data.flingCancel.preventBoosting = false; | 262 gesture->data.flingCancel.preventBoosting = false; |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 | 265 |
| 266 void VrController::HandleDetectingState(WebGestureEvent* gesture) { | 266 void VrController::HandleDetectingState(WebGestureEvent* gesture) { |
| 267 // User lifts up finger from touch pad. | 267 // User lifts up finger from touch pad. |
| 268 if (touch_info_->touch_up || !(touch_info_->is_touching)) { | 268 if (touch_info_->touch_up || !(touch_info_->is_touching)) { |
| 269 Reset(); | 269 Reset(); |
| 270 return; | 270 return; |
| 271 } | 271 } |
| 272 | 272 |
| 273 // Touch position is changed, the touch point moves outside of slop, | 273 // Touch position is changed, the touch point moves outside of slop, |
| 274 // and the Controller's button is not down. | 274 // and the Controller's button is not down. |
| 275 if (UpdateCurrentTouchpoint() && touch_info_->is_touching && | 275 if (UpdateCurrentTouchpoint() && touch_info_->is_touching && |
| 276 !InSlop(touch_info_->touch_point.position) && | 276 !InSlop(touch_info_->touch_point.position) && |
| 277 !ButtonDownHappened(gvr::kControllerButtonClick)) { | 277 !ButtonDownHappened(gvr::kControllerButtonClick)) { |
| 278 state_ = SCROLLING; | 278 state_ = SCROLLING; |
| 279 gesture->type = WebInputEvent::GestureScrollBegin; | 279 gesture->setType(WebInputEvent::GestureScrollBegin); |
| 280 UpdateGesture(gesture); | 280 UpdateGesture(gesture); |
| 281 gesture->data.scrollBegin.deltaXHint = | 281 gesture->data.scrollBegin.deltaXHint = |
| 282 displacement_.x * kDisplacementScaleFactor; | 282 displacement_.x * kDisplacementScaleFactor; |
| 283 gesture->data.scrollBegin.deltaYHint = | 283 gesture->data.scrollBegin.deltaYHint = |
| 284 displacement_.y * kDisplacementScaleFactor; | 284 displacement_.y * kDisplacementScaleFactor; |
| 285 } | 285 } |
| 286 } | 286 } |
| 287 | 287 |
| 288 void VrController::HandleScrollingState(WebGestureEvent* gesture) { | 288 void VrController::HandleScrollingState(WebGestureEvent* gesture) { |
| 289 // Update current touch point. | 289 // Update current touch point. |
| 290 bool touch_position_changed = UpdateCurrentTouchpoint(); | 290 bool touch_position_changed = UpdateCurrentTouchpoint(); |
| 291 if (touch_info_->touch_up || !(touch_info_->is_touching) || | 291 if (touch_info_->touch_up || !(touch_info_->is_touching) || |
| 292 ButtonDownHappened(gvr::kControllerButtonClick)) { | 292 ButtonDownHappened(gvr::kControllerButtonClick)) { |
| 293 // Gesture ends. | 293 // Gesture ends. |
| 294 gesture->type = WebInputEvent::GestureScrollEnd; | 294 gesture->setType(WebInputEvent::GestureScrollEnd); |
| 295 UpdateGesture(gesture); | 295 UpdateGesture(gesture); |
| 296 } else if (touch_position_changed) { | 296 } else if (touch_position_changed) { |
| 297 // User continues scrolling and there is a change in touch position. | 297 // User continues scrolling and there is a change in touch position. |
| 298 gesture->type = WebInputEvent::GestureScrollUpdate; | 298 gesture->setType(WebInputEvent::GestureScrollUpdate); |
| 299 UpdateGesture(gesture); | 299 UpdateGesture(gesture); |
| 300 if (IsHorizontalGesture()) { | 300 if (IsHorizontalGesture()) { |
| 301 gesture->data.scrollUpdate.deltaX = | 301 gesture->data.scrollUpdate.deltaX = |
| 302 displacement_.x * kDisplacementScaleFactor; | 302 displacement_.x * kDisplacementScaleFactor; |
| 303 } else { | 303 } else { |
| 304 gesture->data.scrollUpdate.deltaY = | 304 gesture->data.scrollUpdate.deltaY = |
| 305 displacement_.y * kDisplacementScaleFactor; | 305 displacement_.y * kDisplacementScaleFactor; |
| 306 } | 306 } |
| 307 } | 307 } |
| 308 } | 308 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration); | 366 gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration); |
| 367 | 367 |
| 368 float weight = duration / (kRC + duration); | 368 float weight = duration / (kRC + duration); |
| 369 | 369 |
| 370 overall_velocity_ = | 370 overall_velocity_ = |
| 371 Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight), | 371 Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight), |
| 372 Vector::ScalarMult(velocity, weight)); | 372 Vector::ScalarMult(velocity, weight)); |
| 373 } | 373 } |
| 374 | 374 |
| 375 } // namespace vr_shell | 375 } // namespace vr_shell |
| OLD | NEW |