Chromium Code Reviews| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/android/vr_shell/elbow_model.h" | 14 #include "chrome/browser/android/vr_shell/elbow_model.h" |
| 15 #include "device/vr/vr_math.h" | 15 #include "device/vr/vr_math.h" |
| 16 #include "third_party/WebKit/public/platform/WebGestureEvent.h" | |
| 17 #include "third_party/WebKit/public/platform/WebInputEvent.h" | |
| 16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr.h" | 18 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr.h" |
| 17 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr_controller.h" | 19 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr_controller.h" |
| 18 | 20 |
| 19 namespace vr_shell { | 21 namespace vr_shell { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 constexpr float kDisplacementScaleFactor = 300.0f; | 25 constexpr float kDisplacementScaleFactor = 300.0f; |
| 24 | 26 |
| 25 // A slop represents a small rectangular region around the first touch point of | 27 // A slop represents a small rectangular region around the first touch point of |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 if (extrapolated_touch_ == kMaxNumOfExtrapolations) { | 239 if (extrapolated_touch_ == kMaxNumOfExtrapolations) { |
| 238 overall_velocity_ = {0, 0}; | 240 overall_velocity_ = {0, 0}; |
| 239 } | 241 } |
| 240 extrapolated_touch_ = 0; | 242 extrapolated_touch_ = 0; |
| 241 } | 243 } |
| 242 last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp(); | 244 last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp(); |
| 243 last_timestamp_nanos_ = | 245 last_timestamp_nanos_ = |
| 244 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; | 246 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; |
| 245 } | 247 } |
| 246 | 248 |
| 247 std::vector<std::unique_ptr<WebGestureEvent>> VrController::DetectGestures() { | 249 std::unique_ptr<GestureList> VrController::DetectGestures() { |
| 248 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list; | 250 std::unique_ptr<GestureList> gesture_list = base::MakeUnique<GestureList>(); |
| 249 std::unique_ptr<WebGestureEvent> gesture(new WebGestureEvent()); | 251 std::unique_ptr<blink::WebGestureEvent> gesture(new blink::WebGestureEvent()); |
| 250 | 252 |
| 251 if (controller_state_->GetConnectionState() != gvr::kControllerConnected) { | 253 if (controller_state_->GetConnectionState() != gvr::kControllerConnected) { |
| 252 gesture_list.push_back(std::move(gesture)); | 254 gesture_list->push_back(std::move(gesture)); |
| 253 return gesture_list; | 255 return gesture_list; |
| 254 } | 256 } |
| 255 | 257 |
| 256 touch_position_changed_ = UpdateCurrentTouchpoint(); | 258 touch_position_changed_ = UpdateCurrentTouchpoint(); |
| 257 UpdateTouchInfo(); | 259 UpdateTouchInfo(); |
| 258 if (touch_position_changed_) | 260 if (touch_position_changed_) |
| 259 UpdateOverallVelocity(); | 261 UpdateOverallVelocity(); |
| 260 | 262 |
| 261 UpdateGestureFromTouchInfo(gesture.get()); | 263 UpdateGestureFromTouchInfo(gesture.get()); |
| 264 gesture->source_device = blink::kWebGestureDeviceTouchpad; | |
| 265 gesture_list->push_back(std::move(gesture)); | |
| 262 | 266 |
| 263 if (gesture->GetType() == WebInputEvent::kUndefined && | 267 if (gesture_list->back()->GetType() == |
| 264 ButtonUpHappened(gvr::kControllerButtonClick)) { | 268 blink::WebInputEvent::kGestureScrollEnd) { |
| 265 gesture->SetType(WebInputEvent::kGestureTapDown); | |
| 266 gesture->x = 0; | |
| 267 gesture->y = 0; | |
| 268 } | |
| 269 gesture->source_device = blink::kWebGestureDeviceTouchpad; | |
| 270 gesture_list.push_back(std::move(gesture)); | |
| 271 | |
| 272 if (gesture_list.back()->GetType() == WebInputEvent::kGestureScrollEnd) { | |
| 273 if (!ButtonDownHappened(gvr::kControllerButtonClick) && | 269 if (!ButtonDownHappened(gvr::kControllerButtonClick) && |
| 274 (last_velocity_.x() != 0.0 || last_velocity_.y() != 0.0)) { | 270 (last_velocity_.x() != 0.0 || last_velocity_.y() != 0.0)) { |
| 275 std::unique_ptr<WebGestureEvent> fling(new WebGestureEvent( | 271 std::unique_ptr<blink::WebGestureEvent> fling( |
| 276 WebInputEvent::kGestureFlingStart, WebInputEvent::kNoModifiers, | 272 new blink::WebGestureEvent(blink::WebInputEvent::kGestureFlingStart, |
| 277 gesture_list.back()->TimeStampSeconds())); | 273 blink::WebInputEvent::kNoModifiers, |
| 274 gesture_list->back()->TimeStampSeconds())); | |
| 278 fling->source_device = blink::kWebGestureDeviceTouchpad; | 275 fling->source_device = blink::kWebGestureDeviceTouchpad; |
| 279 if (IsHorizontalGesture()) { | 276 if (IsHorizontalGesture()) { |
| 280 fling->data.fling_start.velocity_x = | 277 fling->data.fling_start.velocity_x = |
| 281 last_velocity_.x() * kDisplacementScaleFactor; | 278 last_velocity_.x() * kDisplacementScaleFactor; |
| 282 } else { | 279 } else { |
| 283 fling->data.fling_start.velocity_y = | 280 fling->data.fling_start.velocity_y = |
| 284 last_velocity_.y() * kDisplacementScaleFactor; | 281 last_velocity_.y() * kDisplacementScaleFactor; |
| 285 } | 282 } |
| 286 gesture_list.push_back(std::move(fling)); | 283 gesture_list->push_back(std::move(fling)); |
| 287 } | 284 } |
| 288 Reset(); | 285 Reset(); |
| 289 } | 286 } |
| 290 | 287 |
| 291 return gesture_list; | 288 return gesture_list; |
| 292 } | 289 } |
| 293 | 290 |
| 294 void VrController::UpdateGestureFromTouchInfo(WebGestureEvent* gesture) { | 291 void VrController::UpdateGestureFromTouchInfo(blink::WebGestureEvent* gesture) { |
| 295 gesture->SetTimeStampSeconds( | 292 gesture->SetTimeStampSeconds( |
| 296 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF()); | 293 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF()); |
| 297 switch (state_) { | 294 switch (state_) { |
| 298 // User has not put finger on touch pad. | 295 // User has not put finger on touch pad. |
| 299 case WAITING: | 296 case WAITING: |
| 300 HandleWaitingState(gesture); | 297 HandleWaitingState(gesture); |
| 301 break; | 298 break; |
| 302 // User has not started a gesture (by moving out of slop). | 299 // User has not started a gesture (by moving out of slop). |
| 303 case TOUCHING: | 300 case TOUCHING: |
| 304 HandleDetectingState(gesture); | 301 HandleDetectingState(gesture); |
| 305 break; | 302 break; |
| 306 // User is scrolling on touchpad | 303 // User is scrolling on touchpad |
| 307 case SCROLLING: | 304 case SCROLLING: |
| 308 HandleScrollingState(gesture); | 305 HandleScrollingState(gesture); |
| 309 break; | 306 break; |
| 310 default: | 307 default: |
| 311 NOTREACHED(); | 308 NOTREACHED(); |
| 312 break; | 309 break; |
| 313 } | 310 } |
| 314 } | 311 } |
| 315 | 312 |
| 316 void VrController::HandleWaitingState(WebGestureEvent* gesture) { | 313 void VrController::HandleWaitingState(blink::WebGestureEvent* gesture) { |
| 317 // User puts finger on touch pad (or when the touch down for current gesture | 314 // User puts finger on touch pad (or when the touch down for current gesture |
| 318 // is missed, initiate gesture from current touch point). | 315 // is missed, initiate gesture from current touch point). |
| 319 if (touch_info_->touch_down || touch_info_->is_touching) { | 316 if (touch_info_->touch_down || touch_info_->is_touching) { |
| 320 // update initial touchpoint | 317 // update initial touchpoint |
| 321 *init_touch_point_ = touch_info_->touch_point; | 318 *init_touch_point_ = touch_info_->touch_point; |
| 322 // update current touchpoint | 319 // update current touchpoint |
| 323 *cur_touch_point_ = touch_info_->touch_point; | 320 *cur_touch_point_ = touch_info_->touch_point; |
| 324 state_ = TOUCHING; | 321 state_ = TOUCHING; |
| 325 | 322 |
| 326 gesture->SetType(WebInputEvent::kGestureFlingCancel); | 323 gesture->SetType(blink::WebInputEvent::kGestureFlingCancel); |
| 327 gesture->data.fling_cancel.prevent_boosting = false; | 324 gesture->data.fling_cancel.prevent_boosting = false; |
| 328 } | 325 } |
| 329 } | 326 } |
| 330 | 327 |
| 331 void VrController::HandleDetectingState(WebGestureEvent* gesture) { | 328 void VrController::HandleDetectingState(blink::WebGestureEvent* gesture) { |
| 332 // User lifts up finger from touch pad. | 329 // User lifts up finger from touch pad. |
| 333 if (touch_info_->touch_up || !(touch_info_->is_touching)) { | 330 if (touch_info_->touch_up || !(touch_info_->is_touching)) { |
| 334 Reset(); | 331 Reset(); |
| 335 return; | 332 return; |
| 336 } | 333 } |
| 337 | 334 |
| 338 // Touch position is changed, the touch point moves outside of slop, | 335 // Touch position is changed, the touch point moves outside of slop, |
| 339 // and the Controller's button is not down. | 336 // and the Controller's button is not down. |
| 340 if (touch_position_changed_ && touch_info_->is_touching && | 337 if (touch_position_changed_ && touch_info_->is_touching && |
| 341 !InSlop(touch_info_->touch_point.position) && | 338 !InSlop(touch_info_->touch_point.position) && |
| 342 !ButtonDownHappened(gvr::kControllerButtonClick)) { | 339 !ButtonDownHappened(gvr::kControllerButtonClick) && |
| 340 !ButtonState(gvr::kControllerButtonClick)) { | |
|
asimjour1
2017/05/11 12:31:12
Do we need both !ButtonDownHappened and !ButtonSta
mthiesse
2017/05/11 14:12:00
Done.
| |
| 343 state_ = SCROLLING; | 341 state_ = SCROLLING; |
| 344 gesture->SetType(WebInputEvent::kGestureScrollBegin); | 342 gesture->SetType(blink::WebInputEvent::kGestureScrollBegin); |
| 345 UpdateGestureParameters(); | 343 UpdateGestureParameters(); |
| 346 gesture->data.scroll_begin.delta_x_hint = | 344 gesture->data.scroll_begin.delta_x_hint = |
| 347 displacement_.x() * kDisplacementScaleFactor; | 345 displacement_.x() * kDisplacementScaleFactor; |
| 348 gesture->data.scroll_begin.delta_y_hint = | 346 gesture->data.scroll_begin.delta_y_hint = |
| 349 displacement_.y() * kDisplacementScaleFactor; | 347 displacement_.y() * kDisplacementScaleFactor; |
| 350 gesture->data.scroll_begin.delta_hint_units = | 348 gesture->data.scroll_begin.delta_hint_units = |
| 351 blink::WebGestureEvent::ScrollUnits::kPrecisePixels; | 349 blink::WebGestureEvent::ScrollUnits::kPrecisePixels; |
| 352 } | 350 } |
| 353 } | 351 } |
| 354 | 352 |
| 355 void VrController::HandleScrollingState(WebGestureEvent* gesture) { | 353 void VrController::HandleScrollingState(blink::WebGestureEvent* gesture) { |
| 356 if (touch_info_->touch_up || !(touch_info_->is_touching) || | 354 if (touch_info_->touch_up || !(touch_info_->is_touching) || |
| 357 ButtonDownHappened(gvr::kControllerButtonClick)) { | 355 ButtonDownHappened(gvr::kControllerButtonClick)) { |
| 358 // Gesture ends. | 356 // Gesture ends. |
| 359 gesture->SetType(WebInputEvent::kGestureScrollEnd); | 357 gesture->SetType(blink::WebInputEvent::kGestureScrollEnd); |
| 360 UpdateGestureParameters(); | 358 UpdateGestureParameters(); |
| 361 } else if (touch_position_changed_) { | 359 } else if (touch_position_changed_) { |
| 362 // User continues scrolling and there is a change in touch position. | 360 // User continues scrolling and there is a change in touch position. |
| 363 gesture->SetType(WebInputEvent::kGestureScrollUpdate); | 361 gesture->SetType(blink::WebInputEvent::kGestureScrollUpdate); |
| 364 UpdateGestureParameters(); | 362 UpdateGestureParameters(); |
| 365 if (IsHorizontalGesture()) { | 363 if (IsHorizontalGesture()) { |
| 366 gesture->data.scroll_update.delta_x = | 364 gesture->data.scroll_update.delta_x = |
| 367 displacement_.x() * kDisplacementScaleFactor; | 365 displacement_.x() * kDisplacementScaleFactor; |
| 368 } else { | 366 } else { |
| 369 gesture->data.scroll_update.delta_y = | 367 gesture->data.scroll_update.delta_y = |
| 370 displacement_.y() * kDisplacementScaleFactor; | 368 displacement_.y() * kDisplacementScaleFactor; |
| 371 } | 369 } |
| 372 last_velocity_ = overall_velocity_; | 370 last_velocity_ = overall_velocity_; |
| 373 } | 371 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 439 | 437 |
| 440 const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration)); | 438 const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration)); |
| 441 | 439 |
| 442 float weight = duration / (kRC + duration); | 440 float weight = duration / (kRC + duration); |
| 443 | 441 |
| 444 overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) + | 442 overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) + |
| 445 ScaleVector2d(velocity, weight); | 443 ScaleVector2d(velocity, weight); |
| 446 } | 444 } |
| 447 | 445 |
| 448 } // namespace vr_shell | 446 } // namespace vr_shell |
| OLD | NEW |