| 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/time/time.h" |   12 #include "base/time/time.h" | 
|   13 #include "chrome/browser/android/vr_shell/vr_math.h" |   13 #include "device/vr/vr_math.h" | 
|   14 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
     gvr.h" |   14 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
     gvr.h" | 
|   15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
     gvr_controller.h" |   15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
     gvr_controller.h" | 
|   16  |   16  | 
|   17 namespace vr_shell { |   17 namespace vr_shell { | 
|   18  |   18  | 
|   19 namespace { |   19 namespace { | 
|   20  |   20  | 
|   21 constexpr float kDisplacementScaleFactor = 300.0f; |   21 constexpr float kDisplacementScaleFactor = 300.0f; | 
|   22  |   22  | 
|   23 // A slop represents a small rectangular region around the first touch point of |   23 // A slop represents a small rectangular region around the first touch point of | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|   34 // not equal. Also, minimum time distance needed to call two timestamps |   34 // not equal. Also, minimum time distance needed to call two timestamps | 
|   35 // not equal. |   35 // not equal. | 
|   36 constexpr float kDelta = 1.0e-7f; |   36 constexpr float kDelta = 1.0e-7f; | 
|   37  |   37  | 
|   38 constexpr float kCutoffHz = 10.0f; |   38 constexpr float kCutoffHz = 10.0f; | 
|   39 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); |   39 constexpr float kRC = static_cast<float>(1.0 / (2.0 * M_PI * kCutoffHz)); | 
|   40 constexpr float kNanoSecondsPerSecond = 1.0e9f; |   40 constexpr float kNanoSecondsPerSecond = 1.0e9f; | 
|   41  |   41  | 
|   42 constexpr int kMaxNumOfExtrapolations = 2; |   42 constexpr int kMaxNumOfExtrapolations = 2; | 
|   43  |   43  | 
|   44 static constexpr gvr::Vec3f kControllerPosition = {0.2f, -0.5f, -0.15f}; |   44 static constexpr gfx::Point3F kControllerPosition = {0.2f, -0.5f, -0.15f}; | 
|   45  |   45  | 
|   46 class Vector { |   46 void ClampTouchpadPosition(gfx::Vector2dF* position) { | 
|   47  public: |   47   position->set_x(std::min(std::max(0.0f, position->x()), 1.0f)); | 
|   48   static inline void ClampTouchpadPosition(gvr::Vec2f* position) { |   48   position->set_y(std::min(std::max(0.0f, position->y()), 1.0f)); | 
|   49     position->x = std::min(std::max(0.0f, position->x), 1.0f); |   49 } | 
|   50     position->y = std::min(std::max(0.0f, position->y), 1.0f); |  | 
|   51   } |  | 
|   52  |  | 
|   53   static inline void SetZero(gvr::Vec2f* v) { |  | 
|   54     v->x = 0; |  | 
|   55     v->y = 0; |  | 
|   56   } |  | 
|   57  |  | 
|   58   static inline gvr::Vec2f Subtract(gvr::Vec2f v1, gvr::Vec2f v2) { |  | 
|   59     gvr::Vec2f result; |  | 
|   60     result.x = v1.x - v2.x; |  | 
|   61     result.y = v1.y - v2.y; |  | 
|   62     return result; |  | 
|   63   } |  | 
|   64  |  | 
|   65   static inline gvr::Vec2f Add(gvr::Vec2f v1, gvr::Vec2f v2) { |  | 
|   66     gvr::Vec2f result; |  | 
|   67     result.x = v1.x + v2.x; |  | 
|   68     result.y = v1.y + v2.y; |  | 
|   69     return result; |  | 
|   70   } |  | 
|   71  |  | 
|   72   static inline bool Equal(const gvr::Vec2f v1, const gvr::Vec2f v2) { |  | 
|   73     return (std::abs(v1.x - v2.x) < kDelta) && (std::abs(v1.y - v2.y) < kDelta); |  | 
|   74   } |  | 
|   75  |  | 
|   76   static inline gvr::Vec2f ScalarMult(gvr::Vec2f v, float scalar) { |  | 
|   77     gvr::Vec2f vect_prod; |  | 
|   78     vect_prod.x = v.x * scalar; |  | 
|   79     vect_prod.y = v.y * scalar; |  | 
|   80     return vect_prod; |  | 
|   81   } |  | 
|   82  |  | 
|   83 };  // Vector |  | 
|   84  |   50  | 
|   85 }  // namespace |   51 }  // namespace | 
|   86  |   52  | 
|   87 VrController::VrController(gvr_context* vr_context) { |   53 VrController::VrController(gvr_context* vr_context) { | 
|   88   DVLOG(1) << __FUNCTION__ << "=" << this; |   54   DVLOG(1) << __FUNCTION__ << "=" << this; | 
|   89   Initialize(vr_context); |   55   Initialize(vr_context); | 
|   90   Reset(); |   56   Reset(); | 
|   91 } |   57 } | 
|   92  |   58  | 
|   93 VrController::~VrController() { |   59 VrController::~VrController() { | 
|   94   DVLOG(1) << __FUNCTION__ << "=" << this; |   60   DVLOG(1) << __FUNCTION__ << "=" << this; | 
|   95 } |   61 } | 
|   96  |   62  | 
|   97 void VrController::OnResume() { |   63 void VrController::OnResume() { | 
|   98   if (controller_api_) |   64   if (controller_api_) | 
|   99     controller_api_->Resume(); |   65     controller_api_->Resume(); | 
|  100 } |   66 } | 
|  101  |   67  | 
|  102 void VrController::OnPause() { |   68 void VrController::OnPause() { | 
|  103   if (controller_api_) |   69   if (controller_api_) | 
|  104     controller_api_->Pause(); |   70     controller_api_->Pause(); | 
|  105 } |   71 } | 
|  106  |   72  | 
|  107 device::GvrGamepadData VrController::GetGamepadData() { |   73 device::GvrGamepadData VrController::GetGamepadData() { | 
|  108   device::GvrGamepadData pad; |   74   device::GvrGamepadData pad; | 
|  109  |   75  | 
|  110   pad.timestamp = controller_state_->GetLastOrientationTimestamp(); |   76   pad.timestamp = controller_state_->GetLastOrientationTimestamp(); | 
|  111   pad.touch_pos = controller_state_->GetTouchPos(); |   77   pad.touch_pos.set_x(TouchPosX()); | 
|  112   pad.orientation = controller_state_->GetOrientation(); |   78   pad.touch_pos.set_y(TouchPosY()); | 
 |   79   pad.orientation = Orientation(); | 
|  113  |   80  | 
|  114   // Use orientation to rotate acceleration/gyro into seated space. |   81   // Use orientation to rotate acceleration/gyro into seated space. | 
|  115   gvr::Mat4f pose_mat = QuatToMatrix(pad.orientation); |   82   vr::Matf pose_mat; | 
|  116   pad.accel = MatrixVectorMul(pose_mat, controller_state_->GetAccel()); |   83   vr::QuatToMatrix(pad.orientation, &pose_mat); | 
|  117   pad.gyro = MatrixVectorMul(pose_mat, controller_state_->GetGyro()); |   84   const gvr::Vec3f& accel = controller_state_->GetAccel(); | 
 |   85   const gvr::Vec3f& gyro = controller_state_->GetGyro(); | 
 |   86   pad.accel = | 
 |   87       vr::MatrixVectorMul(pose_mat, gfx::Vector3dF(accel.x, accel.y, accel.z)); | 
 |   88   pad.gyro = | 
 |   89       vr::MatrixVectorMul(pose_mat, gfx::Vector3dF(gyro.x, gyro.y, gyro.z)); | 
|  118  |   90  | 
|  119   pad.is_touching = controller_state_->IsTouching(); |   91   pad.is_touching = controller_state_->IsTouching(); | 
|  120   pad.controller_button_pressed = |   92   pad.controller_button_pressed = | 
|  121       controller_state_->GetButtonState(GVR_CONTROLLER_BUTTON_CLICK); |   93       controller_state_->GetButtonState(GVR_CONTROLLER_BUTTON_CLICK); | 
|  122   pad.right_handed = handedness_ == GVR_CONTROLLER_RIGHT_HANDED; |   94   pad.right_handed = handedness_ == GVR_CONTROLLER_RIGHT_HANDED; | 
|  123  |   95  | 
|  124   return pad; |   96   return pad; | 
|  125 } |   97 } | 
|  126  |   98  | 
|  127 bool VrController::IsTouching() { |   99 bool VrController::IsTouching() { | 
|  128   return controller_state_->IsTouching(); |  100   return controller_state_->IsTouching(); | 
|  129 } |  101 } | 
|  130  |  102  | 
|  131 float VrController::TouchPosX() { |  103 float VrController::TouchPosX() { | 
|  132   return controller_state_->GetTouchPos().x; |  104   return controller_state_->GetTouchPos().x; | 
|  133 } |  105 } | 
|  134  |  106  | 
|  135 float VrController::TouchPosY() { |  107 float VrController::TouchPosY() { | 
|  136   return controller_state_->GetTouchPos().y; |  108   return controller_state_->GetTouchPos().y; | 
|  137 } |  109 } | 
|  138  |  110  | 
|  139 gvr::Quatf VrController::Orientation() const { |  111 vr::Quatf VrController::Orientation() const { | 
|  140   return controller_state_->GetOrientation(); |  112   const gvr::Quatf& orientation = controller_state_->GetOrientation(); | 
 |  113   return *reinterpret_cast<vr::Quatf*>(const_cast<gvr::Quatf*>(&orientation)); | 
|  141 } |  114 } | 
|  142  |  115  | 
|  143 gvr::Mat4f VrController::GetTransform() const { |  116 void VrController::GetTransform(vr::Matf* out) const { | 
|  144   // TODO(acondor): Position and orientation needs to be obtained |  117   // TODO(acondor): Position and orientation needs to be obtained | 
|  145   // from an elbow model. |  118   // from an elbow model. | 
|  146   // Placing the controller in a fixed position for now. |  119   // Placing the controller in a fixed position for now. | 
|  147   gvr::Mat4f mat; |  120   vr::SetIdentityM(out); | 
|  148   SetIdentityM(mat); |  | 
|  149   // Changing rotation point. |  121   // Changing rotation point. | 
|  150   TranslateM(mat, mat, 0, 0, 0.05); |  122   vr::TranslateM(*out, gfx::Vector3dF(0, 0, 0.05), out); | 
|  151   mat = MatrixMul(QuatToMatrix(Orientation()), mat); |  123   vr::Matf quat_to_matrix; | 
|  152   TranslateM(mat, mat, kControllerPosition.x, kControllerPosition.y, |  124   vr::QuatToMatrix(Orientation(), &quat_to_matrix); | 
|  153              kControllerPosition.z - 0.05); |  125   vr::Matf copy = *out; | 
|  154   return mat; |  126   vr::MatrixMul(quat_to_matrix, copy, out); | 
 |  127   gfx::Vector3dF translation(kControllerPosition.x(), kControllerPosition.y(), | 
 |  128                              kControllerPosition.z() - 0.05); | 
 |  129   vr::TranslateM(*out, translation, out); | 
|  155 } |  130 } | 
|  156  |  131  | 
|  157 VrControllerModel::State VrController::GetModelState() const { |  132 VrControllerModel::State VrController::GetModelState() const { | 
|  158   if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) |  133   if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_CLICK)) | 
|  159     return VrControllerModel::TOUCHPAD; |  134     return VrControllerModel::TOUCHPAD; | 
|  160   if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) |  135   if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_APP)) | 
|  161     return VrControllerModel::APP; |  136     return VrControllerModel::APP; | 
|  162   if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) |  137   if (ButtonState(gvr::ControllerButton::GVR_CONTROLLER_BUTTON_HOME)) | 
|  163     return VrControllerModel::SYSTEM; |  138     return VrControllerModel::SYSTEM; | 
|  164   return VrControllerModel::IDLE; |  139   return VrControllerModel::IDLE; | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  199     VLOG(1) << "Controller Connection status: " |  174     VLOG(1) << "Controller Connection status: " | 
|  200             << gvr_controller_connection_state_to_string( |  175             << gvr_controller_connection_state_to_string( | 
|  201                    controller_state_->GetConnectionState()); |  176                    controller_state_->GetConnectionState()); | 
|  202   } |  177   } | 
|  203 } |  178 } | 
|  204  |  179  | 
|  205 void VrController::UpdateTouchInfo() { |  180 void VrController::UpdateTouchInfo() { | 
|  206   CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; |  181   CHECK(touch_info_ != nullptr) << "touch_info_ not initialized properly."; | 
|  207   if (IsTouching() && state_ == SCROLLING && |  182   if (IsTouching() && state_ == SCROLLING && | 
|  208       (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || |  183       (controller_state_->GetLastTouchTimestamp() == last_touch_timestamp_ || | 
|  209        (Vector::Equal(cur_touch_point_->position, |  184        (cur_touch_point_->position == prev_touch_point_->position && | 
|  210                       prev_touch_point_->position) && |  | 
|  211         extrapolated_touch_ < kMaxNumOfExtrapolations))) { |  185         extrapolated_touch_ < kMaxNumOfExtrapolations))) { | 
|  212     extrapolated_touch_++; |  186     extrapolated_touch_++; | 
|  213     touch_position_changed_ = true; |  187     touch_position_changed_ = true; | 
|  214     // Fill the touch_info |  188     // Fill the touch_info | 
|  215     float duration = |  189     float duration = | 
|  216         (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos - |  190         (gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos - | 
|  217          last_timestamp_nanos_) / |  191          last_timestamp_nanos_) / | 
|  218         kNanoSecondsPerSecond; |  192         kNanoSecondsPerSecond; | 
|  219     touch_info_->touch_point.position.x = |  193     touch_info_->touch_point.position.set_x(cur_touch_point_->position.x() + | 
|  220         cur_touch_point_->position.x + overall_velocity_.x * duration; |  194                                             overall_velocity_.x() * duration); | 
|  221     touch_info_->touch_point.position.y = |  195     touch_info_->touch_point.position.set_y(cur_touch_point_->position.y() + | 
|  222         cur_touch_point_->position.y + overall_velocity_.y * duration; |  196                                             overall_velocity_.y() * duration); | 
|  223   } else { |  197   } else { | 
|  224     if (extrapolated_touch_ == kMaxNumOfExtrapolations) { |  198     if (extrapolated_touch_ == kMaxNumOfExtrapolations) { | 
|  225       Vector::SetZero(&overall_velocity_); |  199       overall_velocity_ = {0, 0}; | 
|  226     } |  200     } | 
|  227     extrapolated_touch_ = 0; |  201     extrapolated_touch_ = 0; | 
|  228   } |  202   } | 
|  229   last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp(); |  203   last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp(); | 
|  230   last_timestamp_nanos_ = |  204   last_timestamp_nanos_ = | 
|  231       gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; |  205       gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; | 
|  232 } |  206 } | 
|  233  |  207  | 
|  234 void VrController::Initialize(gvr_context* gvr_context) { |  208 void VrController::Initialize(gvr_context* gvr_context) { | 
|  235   CHECK(gvr_context != nullptr) << "invalid gvr_context"; |  209   CHECK(gvr_context != nullptr) << "invalid gvr_context"; | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  272       ButtonUpHappened(gvr::kControllerButtonClick)) { |  246       ButtonUpHappened(gvr::kControllerButtonClick)) { | 
|  273     gesture->setType(WebInputEvent::GestureTapDown); |  247     gesture->setType(WebInputEvent::GestureTapDown); | 
|  274     gesture->x = 0; |  248     gesture->x = 0; | 
|  275     gesture->y = 0; |  249     gesture->y = 0; | 
|  276   } |  250   } | 
|  277   gesture->sourceDevice = blink::WebGestureDeviceTouchpad; |  251   gesture->sourceDevice = blink::WebGestureDeviceTouchpad; | 
|  278   gesture_list.push_back(std::move(gesture)); |  252   gesture_list.push_back(std::move(gesture)); | 
|  279  |  253  | 
|  280   if (gesture_list.back()->type() == WebInputEvent::GestureScrollEnd) { |  254   if (gesture_list.back()->type() == WebInputEvent::GestureScrollEnd) { | 
|  281     if (!ButtonDownHappened(gvr::kControllerButtonClick) && |  255     if (!ButtonDownHappened(gvr::kControllerButtonClick) && | 
|  282         (last_velocity_.x != 0.0 || last_velocity_.y != 0.0)) { |  256         (last_velocity_.x() != 0.0 || last_velocity_.y() != 0.0)) { | 
|  283       std::unique_ptr<WebGestureEvent> fling(new WebGestureEvent( |  257       std::unique_ptr<WebGestureEvent> fling(new WebGestureEvent( | 
|  284           WebInputEvent::GestureFlingStart, WebInputEvent::NoModifiers, |  258           WebInputEvent::GestureFlingStart, WebInputEvent::NoModifiers, | 
|  285           gesture_list.back()->timeStampSeconds())); |  259           gesture_list.back()->timeStampSeconds())); | 
|  286       fling->sourceDevice = blink::WebGestureDeviceTouchpad; |  260       fling->sourceDevice = blink::WebGestureDeviceTouchpad; | 
|  287       if (IsHorizontalGesture()) { |  261       if (IsHorizontalGesture()) { | 
|  288         fling->data.flingStart.velocityX = |  262         fling->data.flingStart.velocityX = | 
|  289             last_velocity_.x * kDisplacementScaleFactor; |  263             last_velocity_.x() * kDisplacementScaleFactor; | 
|  290       } else { |  264       } else { | 
|  291         fling->data.flingStart.velocityY = |  265         fling->data.flingStart.velocityY = | 
|  292             last_velocity_.y * kDisplacementScaleFactor; |  266             last_velocity_.y() * kDisplacementScaleFactor; | 
|  293       } |  267       } | 
|  294       gesture_list.push_back(std::move(fling)); |  268       gesture_list.push_back(std::move(fling)); | 
|  295     } |  269     } | 
|  296     Reset(); |  270     Reset(); | 
|  297   } |  271   } | 
|  298  |  272  | 
|  299   return gesture_list; |  273   return gesture_list; | 
|  300 } |  274 } | 
|  301  |  275  | 
|  302 void VrController::UpdateGestureFromTouchInfo(WebGestureEvent* gesture) { |  276 void VrController::UpdateGestureFromTouchInfo(WebGestureEvent* gesture) { | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  345  |  319  | 
|  346   // Touch position is changed, the touch point moves outside of slop, |  320   // Touch position is changed, the touch point moves outside of slop, | 
|  347   // and the Controller's button is not down. |  321   // and the Controller's button is not down. | 
|  348   if (touch_position_changed_ && touch_info_->is_touching && |  322   if (touch_position_changed_ && touch_info_->is_touching && | 
|  349       !InSlop(touch_info_->touch_point.position) && |  323       !InSlop(touch_info_->touch_point.position) && | 
|  350       !ButtonDownHappened(gvr::kControllerButtonClick)) { |  324       !ButtonDownHappened(gvr::kControllerButtonClick)) { | 
|  351     state_ = SCROLLING; |  325     state_ = SCROLLING; | 
|  352     gesture->setType(WebInputEvent::GestureScrollBegin); |  326     gesture->setType(WebInputEvent::GestureScrollBegin); | 
|  353     UpdateGestureParameters(); |  327     UpdateGestureParameters(); | 
|  354     gesture->data.scrollBegin.deltaXHint = |  328     gesture->data.scrollBegin.deltaXHint = | 
|  355         displacement_.x * kDisplacementScaleFactor; |  329         displacement_.x() * kDisplacementScaleFactor; | 
|  356     gesture->data.scrollBegin.deltaYHint = |  330     gesture->data.scrollBegin.deltaYHint = | 
|  357         displacement_.y * kDisplacementScaleFactor; |  331         displacement_.y() * kDisplacementScaleFactor; | 
|  358     gesture->data.scrollBegin.deltaHintUnits = |  332     gesture->data.scrollBegin.deltaHintUnits = | 
|  359         blink::WebGestureEvent::ScrollUnits::PrecisePixels; |  333         blink::WebGestureEvent::ScrollUnits::PrecisePixels; | 
|  360   } |  334   } | 
|  361 } |  335 } | 
|  362  |  336  | 
|  363 void VrController::HandleScrollingState(WebGestureEvent* gesture) { |  337 void VrController::HandleScrollingState(WebGestureEvent* gesture) { | 
|  364   if (touch_info_->touch_up || !(touch_info_->is_touching) || |  338   if (touch_info_->touch_up || !(touch_info_->is_touching) || | 
|  365       ButtonDownHappened(gvr::kControllerButtonClick)) { |  339       ButtonDownHappened(gvr::kControllerButtonClick)) { | 
|  366     // Gesture ends. |  340     // Gesture ends. | 
|  367     gesture->setType(WebInputEvent::GestureScrollEnd); |  341     gesture->setType(WebInputEvent::GestureScrollEnd); | 
|  368     UpdateGestureParameters(); |  342     UpdateGestureParameters(); | 
|  369   } else if (touch_position_changed_) { |  343   } else if (touch_position_changed_) { | 
|  370     // User continues scrolling and there is a change in touch position. |  344     // User continues scrolling and there is a change in touch position. | 
|  371     gesture->setType(WebInputEvent::GestureScrollUpdate); |  345     gesture->setType(WebInputEvent::GestureScrollUpdate); | 
|  372     UpdateGestureParameters(); |  346     UpdateGestureParameters(); | 
|  373     if (IsHorizontalGesture()) { |  347     if (IsHorizontalGesture()) { | 
|  374       gesture->data.scrollUpdate.deltaX = |  348       gesture->data.scrollUpdate.deltaX = | 
|  375           displacement_.x * kDisplacementScaleFactor; |  349           displacement_.x() * kDisplacementScaleFactor; | 
|  376     } else { |  350     } else { | 
|  377       gesture->data.scrollUpdate.deltaY = |  351       gesture->data.scrollUpdate.deltaY = | 
|  378           displacement_.y * kDisplacementScaleFactor; |  352           displacement_.y() * kDisplacementScaleFactor; | 
|  379     } |  353     } | 
|  380     last_velocity_ = overall_velocity_; |  354     last_velocity_ = overall_velocity_; | 
|  381   } |  355   } | 
|  382 } |  356 } | 
|  383  |  357  | 
|  384 bool VrController::IsHorizontalGesture() { |  358 bool VrController::IsHorizontalGesture() { | 
|  385   return std::abs(last_velocity_.x) > std::abs(last_velocity_.y); |  359   return std::abs(last_velocity_.x()) > std::abs(last_velocity_.y()); | 
|  386 } |  360 } | 
|  387  |  361  | 
|  388 bool VrController::InSlop(const gvr::Vec2f touch_position) { |  362 bool VrController::InSlop(const gfx::Vector2dF touch_position) { | 
|  389   return (std::abs(touch_position.x - init_touch_point_->position.x) < |  363   return (std::abs(touch_position.x() - init_touch_point_->position.x()) < | 
|  390           kSlopHorizontal) && |  364           kSlopHorizontal) && | 
|  391          (std::abs(touch_position.y - init_touch_point_->position.y) < |  365          (std::abs(touch_position.y() - init_touch_point_->position.y()) < | 
|  392           kSlopVertical); |  366           kSlopVertical); | 
|  393 } |  367 } | 
|  394  |  368  | 
|  395 void VrController::Reset() { |  369 void VrController::Reset() { | 
|  396   // Reset state. |  370   // Reset state. | 
|  397   state_ = WAITING; |  371   state_ = WAITING; | 
|  398  |  372  | 
|  399   // Reset the pointers. |  373   // Reset the pointers. | 
|  400   prev_touch_point_.reset(new TouchPoint); |  374   prev_touch_point_.reset(new TouchPoint); | 
|  401   cur_touch_point_.reset(new TouchPoint); |  375   cur_touch_point_.reset(new TouchPoint); | 
|  402   init_touch_point_.reset(new TouchPoint); |  376   init_touch_point_.reset(new TouchPoint); | 
|  403   touch_info_.reset(new TouchInfo); |  377   touch_info_.reset(new TouchInfo); | 
|  404   Vector::SetZero(&overall_velocity_); |  378   overall_velocity_ = {0, 0}; | 
|  405   Vector::SetZero(&last_velocity_); |  379   last_velocity_ = {0, 0}; | 
|  406 } |  380 } | 
|  407  |  381  | 
|  408 void VrController::UpdateGestureParameters() { |  382 void VrController::UpdateGestureParameters() { | 
|  409   displacement_ = Vector::Subtract(touch_info_->touch_point.position, |  383   displacement_ = | 
|  410                                    prev_touch_point_->position); |  384       touch_info_->touch_point.position - prev_touch_point_->position; | 
|  411 } |  385 } | 
|  412  |  386  | 
|  413 bool VrController::UpdateCurrentTouchpoint() { |  387 bool VrController::UpdateCurrentTouchpoint() { | 
|  414   touch_info_->touch_up = TouchUpHappened(); |  388   touch_info_->touch_up = TouchUpHappened(); | 
|  415   touch_info_->touch_down = TouchDownHappened(); |  389   touch_info_->touch_down = TouchDownHappened(); | 
|  416   touch_info_->is_touching = IsTouching(); |  390   touch_info_->is_touching = IsTouching(); | 
|  417   touch_info_->touch_point.position.x = TouchPosX(); |  391   touch_info_->touch_point.position.set_x(TouchPosX()); | 
|  418   touch_info_->touch_point.position.y = TouchPosY(); |  392   touch_info_->touch_point.position.set_y(TouchPosY()); | 
|  419   Vector::ClampTouchpadPosition(&touch_info_->touch_point.position); |  393   ClampTouchpadPosition(&touch_info_->touch_point.position); | 
|  420   touch_info_->touch_point.timestamp = |  394   touch_info_->touch_point.timestamp = | 
|  421       gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; |  395       gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; | 
|  422  |  396  | 
|  423   if (IsTouching() || TouchUpHappened()) { |  397   if (IsTouching() || TouchUpHappened()) { | 
|  424     // Update the touch point when the touch position has changed. |  398     // Update the touch point when the touch position has changed. | 
|  425     if (!Vector::Equal(cur_touch_point_->position, |  399     if (cur_touch_point_->position != touch_info_->touch_point.position) { | 
|  426                        touch_info_->touch_point.position)) { |  | 
|  427       prev_touch_point_.swap(cur_touch_point_); |  400       prev_touch_point_.swap(cur_touch_point_); | 
|  428       cur_touch_point_.reset(new TouchPoint); |  401       cur_touch_point_.reset(new TouchPoint); | 
|  429       cur_touch_point_->position = touch_info_->touch_point.position; |  402       cur_touch_point_->position = touch_info_->touch_point.position; | 
|  430       cur_touch_point_->timestamp = touch_info_->touch_point.timestamp; |  403       cur_touch_point_->timestamp = touch_info_->touch_point.timestamp; | 
|  431       return true; |  404       return true; | 
|  432     } |  405     } | 
|  433   } |  406   } | 
|  434   return false; |  407   return false; | 
|  435 } |  408 } | 
|  436  |  409  | 
|  437 void VrController::UpdateOverallVelocity() { |  410 void VrController::UpdateOverallVelocity() { | 
|  438   float duration = |  411   float duration = | 
|  439       (touch_info_->touch_point.timestamp - prev_touch_point_->timestamp) / |  412       (touch_info_->touch_point.timestamp - prev_touch_point_->timestamp) / | 
|  440       kNanoSecondsPerSecond; |  413       kNanoSecondsPerSecond; | 
|  441  |  414  | 
|  442   // If the timestamp does not change, do not update velocity. |  415   // If the timestamp does not change, do not update velocity. | 
|  443   if (duration < kDelta) |  416   if (duration < kDelta) | 
|  444     return; |  417     return; | 
|  445  |  418  | 
|  446   gvr::Vec2f displacement = Vector::Subtract(touch_info_->touch_point.position, |  419   const gfx::Vector2dF& displacement = | 
|  447                                              prev_touch_point_->position); |  420       touch_info_->touch_point.position - prev_touch_point_->position; | 
|  448  |  421  | 
|  449   gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration); |  422   const gfx::Vector2dF& velocity = ScaleVector2d(displacement, (1 / duration)); | 
|  450  |  423  | 
|  451   float weight = duration / (kRC + duration); |  424   float weight = duration / (kRC + duration); | 
|  452  |  425  | 
|  453   overall_velocity_ = |  426   overall_velocity_ = ScaleVector2d(overall_velocity_, (1 - weight)) + | 
|  454       Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight), |  427                       ScaleVector2d(velocity, weight); | 
|  455                   Vector::ScalarMult(velocity, weight)); |  | 
|  456 } |  428 } | 
|  457  |  429  | 
|  458 }  // namespace vr_shell |  430 }  // namespace vr_shell | 
| OLD | NEW |