Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: chrome/browser/android/vr_shell/vr_controller.cc

Issue 2762003002: Refactor GVR controller gamepad API integration (Closed)
Patch Set: Fix comment, include gvr_types only for data provider Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 void VrController::OnResume() { 91 void VrController::OnResume() {
92 if (controller_api_) 92 if (controller_api_)
93 controller_api_->Resume(); 93 controller_api_->Resume();
94 } 94 }
95 95
96 void VrController::OnPause() { 96 void VrController::OnPause() {
97 if (controller_api_) 97 if (controller_api_)
98 controller_api_->Pause(); 98 controller_api_->Pause();
99 } 99 }
100 100
101 device::GvrGamepadData VrController::GetGamepadData() {
102 device::GvrGamepadData pad;
103
104 pad.timestamp = controller_state_->GetLastOrientationTimestamp();
105 pad.touch_pos = controller_state_->GetTouchPos();
106 pad.orientation = controller_state_->GetOrientation();
107 pad.accel = controller_state_->GetAccel();
108 pad.gyro = controller_state_->GetGyro();
109 pad.is_touching = controller_state_->IsTouching();
110 pad.controller_button_pressed =
111 controller_state_->GetButtonState(GVR_CONTROLLER_BUTTON_CLICK);
112 pad.right_handed = handedness_ == GVR_CONTROLLER_RIGHT_HANDED;
113
114 return pad;
115 }
116
101 bool VrController::IsTouching() { 117 bool VrController::IsTouching() {
102 return controller_state_->IsTouching(); 118 return controller_state_->IsTouching();
103 } 119 }
104 120
105 float VrController::TouchPosX() { 121 float VrController::TouchPosX() {
106 return controller_state_->GetTouchPos().x; 122 return controller_state_->GetTouchPos().x;
107 } 123 }
108 124
109 float VrController::TouchPosY() { 125 float VrController::TouchPosY() {
110 return controller_state_->GetTouchPos().y; 126 return controller_state_->GetTouchPos().y;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 } 190 }
175 last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp(); 191 last_touch_timestamp_ = controller_state_->GetLastTouchTimestamp();
176 last_timestamp_nanos_ = 192 last_timestamp_nanos_ =
177 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; 193 gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos;
178 } 194 }
179 195
180 void VrController::Initialize(gvr_context* gvr_context) { 196 void VrController::Initialize(gvr_context* gvr_context) {
181 CHECK(gvr_context != nullptr) << "invalid gvr_context"; 197 CHECK(gvr_context != nullptr) << "invalid gvr_context";
182 controller_api_.reset(new gvr::ControllerApi); 198 controller_api_.reset(new gvr::ControllerApi);
183 controller_state_.reset(new gvr::ControllerState); 199 controller_state_.reset(new gvr::ControllerState);
200
184 int32_t options = gvr::ControllerApi::DefaultOptions(); 201 int32_t options = gvr::ControllerApi::DefaultOptions();
185 202
186 // Enable non-default options, if you need them: 203 // Enable non-default options - WebVR needs gyro, and since VrShell
187 // options |= GVR_CONTROLLER_ENABLE_GYRO; 204 // implements GvrGamepadDataProvider we need this always.
205 options |= GVR_CONTROLLER_ENABLE_GYRO;
206
188 CHECK(controller_api_->Init(options, gvr_context)); 207 CHECK(controller_api_->Init(options, gvr_context));
208
209 std::unique_ptr<gvr::GvrApi> gvr = gvr::GvrApi::WrapNonOwned(gvr_context);
210 // TODO(bajones): Monitor changes to the controller handedness.
211 handedness_ = gvr->GetUserPrefs().GetControllerHandedness();
212
189 controller_api_->Resume(); 213 controller_api_->Resume();
190 } 214 }
191 215
192 std::vector<std::unique_ptr<WebGestureEvent>> VrController::DetectGestures() { 216 std::vector<std::unique_ptr<WebGestureEvent>> VrController::DetectGestures() {
193 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list; 217 std::vector<std::unique_ptr<WebGestureEvent>> gesture_list;
194 std::unique_ptr<WebGestureEvent> gesture(new WebGestureEvent()); 218 std::unique_ptr<WebGestureEvent> gesture(new WebGestureEvent());
195 219
196 if (controller_state_->GetConnectionState() != gvr::kControllerConnected) { 220 if (controller_state_->GetConnectionState() != gvr::kControllerConnected) {
197 gesture_list.push_back(std::move(gesture)); 221 gesture_list.push_back(std::move(gesture));
198 return gesture_list; 222 return gesture_list;
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration); 410 gvr::Vec2f velocity = Vector::ScalarMult(displacement, 1 / duration);
387 411
388 float weight = duration / (kRC + duration); 412 float weight = duration / (kRC + duration);
389 413
390 overall_velocity_ = 414 overall_velocity_ =
391 Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight), 415 Vector::Add(Vector::ScalarMult(overall_velocity_, 1 - weight),
392 Vector::ScalarMult(velocity, weight)); 416 Vector::ScalarMult(velocity, weight));
393 } 417 }
394 418
395 } // namespace vr_shell 419 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698