Commit e6ccc971 authored by mthiesse's avatar mthiesse Committed by Commit bot

Implement controller handling in vr_shell.cc

BUG=641413

Review-Url: https://codereview.chromium.org/2378903005
Cr-Commit-Position: refs/heads/master@{#422783}
parent 67aa2c21
...@@ -14,7 +14,6 @@ namespace vr_shell { ...@@ -14,7 +14,6 @@ namespace vr_shell {
typedef struct { typedef struct {
gvr_vec3f delta; gvr_vec3f delta;
int type;
} GestureAngularMove; } GestureAngularMove;
typedef struct { typedef struct {
......
...@@ -43,9 +43,11 @@ void VrInputManager::SendGesture(VrGesture gesture) { ...@@ -43,9 +43,11 @@ void VrInputManager::SendGesture(VrGesture gesture) {
} else if (gesture.type == WebInputEvent::GestureTap) { } else if (gesture.type == WebInputEvent::GestureTap) {
SendClickEvent(event_time_milliseconds, gesture.details.buttons.pos.x, SendClickEvent(event_time_milliseconds, gesture.details.buttons.pos.x,
gesture.details.buttons.pos.y); gesture.details.buttons.pos.y);
} else if (gesture.type == WebInputEvent::MouseMove) { } else if (gesture.type == WebInputEvent::MouseMove ||
SendMouseMoveEvent(event_time_milliseconds, gesture.details.move.delta.x, gesture.type == WebInputEvent::MouseEnter ||
gesture.details.move.delta.y, gesture.details.move.type); gesture.type == WebInputEvent::MouseLeave) {
SendMouseEvent(event_time_milliseconds, gesture.details.move.delta.x,
gesture.details.move.delta.y, gesture.type);
} }
} }
...@@ -119,13 +121,13 @@ void VrInputManager::SendScrollEvent(int64_t time_ms, ...@@ -119,13 +121,13 @@ void VrInputManager::SendScrollEvent(int64_t time_ms,
} }
} }
void VrInputManager::SendMouseMoveEvent(int64_t time_ms, void VrInputManager::SendMouseEvent(int64_t time_ms,
float x, float x,
float y, float y,
int type) { WebInputEvent::Type type) {
WebMouseEvent result; WebMouseEvent result;
result.type = WebInputEvent::MouseMove; result.type = type;
result.pointerType = blink::WebPointerProperties::PointerType::Mouse; result.pointerType = blink::WebPointerProperties::PointerType::Mouse;
result.x = x / dpi_scale_; result.x = x / dpi_scale_;
result.y = y / dpi_scale_; result.y = y / dpi_scale_;
...@@ -134,12 +136,6 @@ void VrInputManager::SendMouseMoveEvent(int64_t time_ms, ...@@ -134,12 +136,6 @@ void VrInputManager::SendMouseMoveEvent(int64_t time_ms,
result.timeStampSeconds = time_ms / 1000.0; result.timeStampSeconds = time_ms / 1000.0;
result.clickCount = 1; result.clickCount = 1;
result.modifiers = 0; result.modifiers = 0;
if (type == 1) {
result.type = WebInputEvent::MouseEnter;
} else if (type == 2) {
result.type = WebInputEvent::MouseLeave;
}
result.button = WebMouseEvent::Button::NoButton; result.button = WebMouseEvent::Button::NoButton;
ForwardMouseEvent(result); ForwardMouseEvent(result);
......
...@@ -30,7 +30,8 @@ class VrInputManager ...@@ -30,7 +30,8 @@ class VrInputManager
float dy, float dy,
int type); int type);
void SendClickEvent(int64_t time_ms, float x, float y); void SendClickEvent(int64_t time_ms, float x, float y);
void SendMouseMoveEvent(int64_t time_ms, float x, float y, int type); void SendMouseEvent(int64_t time_ms, float x, float y,
WebInputEvent::Type type);
void ScrollBegin(int64_t time_ms, void ScrollBegin(int64_t time_ms,
float x, float x,
......
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
#include "chrome/browser/android/vr_shell/ui_scene.h" #include "chrome/browser/android/vr_shell/ui_scene.h"
#include "chrome/browser/android/vr_shell/vr_compositor.h" #include "chrome/browser/android/vr_shell/vr_compositor.h"
#include "chrome/browser/android/vr_shell/vr_controller.h"
#include "chrome/browser/android/vr_shell/vr_gl_util.h" #include "chrome/browser/android/vr_shell/vr_gl_util.h"
#include "chrome/browser/android/vr_shell/vr_input_manager.h"
#include "chrome/browser/android/vr_shell/vr_math.h" #include "chrome/browser/android/vr_shell/vr_math.h"
#include "chrome/browser/android/vr_shell/vr_shell_delegate.h" #include "chrome/browser/android/vr_shell/vr_shell_delegate.h"
#include "chrome/browser/android/vr_shell/vr_shell_renderer.h" #include "chrome/browser/android/vr_shell/vr_shell_renderer.h"
...@@ -198,6 +200,10 @@ void VrShell::GvrInit(JNIEnv* env, ...@@ -198,6 +200,10 @@ void VrShell::GvrInit(JNIEnv* env,
if (delegate_) if (delegate_)
delegate_->OnVrShellReady(this); delegate_->OnVrShellReady(this);
controller_.reset(
new VrController(reinterpret_cast<gvr_context*>(native_gvr_api)));
content_input_manager_ = new VrInputManager(content_cvc_->GetWebContents());
ui_input_manager_ = new VrInputManager(ui_cvc_->GetWebContents());
} }
void VrShell::InitializeGl(JNIEnv* env, void VrShell::InitializeGl(JNIEnv* env,
...@@ -224,17 +230,30 @@ void VrShell::InitializeGl(JNIEnv* env, ...@@ -224,17 +230,30 @@ void VrShell::InitializeGl(JNIEnv* env,
} }
void VrShell::UpdateController(const gvr::Vec3f& forward_vector) { void VrShell::UpdateController(const gvr::Vec3f& forward_vector) {
if (!controller_active_) { controller_->UpdateState();
std::unique_ptr<VrGesture> gesture = controller_->DetectGesture();
// TODO(asimjour) for now, scroll is sent to the main content.
if (gesture->type == WebInputEvent::GestureScrollBegin ||
gesture->type == WebInputEvent::GestureScrollUpdate ||
gesture->type == WebInputEvent::GestureScrollEnd) {
content_input_manager_->ProcessUpdatedGesture(*gesture.get());
}
WebInputEvent::Type original_type = gesture->type;
if (!controller_->IsConnected()) {
// No controller detected, set up a gaze cursor that tracks the // No controller detected, set up a gaze cursor that tracks the
// forward direction. // forward direction.
controller_quat_ = GetRotationFromZAxis(forward_vector); controller_quat_ = GetRotationFromZAxis(forward_vector);
} else {
controller_quat_ = controller_->Orientation();
} }
gvr::Mat4f mat = QuatToMatrix(controller_quat_); gvr::Mat4f mat = QuatToMatrix(controller_quat_);
gvr::Vec3f forward = MatrixVectorMul(mat, kNeutralPose); gvr::Vec3f forward = MatrixVectorMul(mat, kNeutralPose);
gvr::Vec3f origin = kHandPosition; gvr::Vec3f origin = kHandPosition;
target_element_ = nullptr;; target_element_ = nullptr;
float distance = scene_.GetUiElementById(kBrowserUiElementId) float distance = scene_.GetUiElementById(kBrowserUiElementId)
->GetRayDistance(origin, forward); ->GetRayDistance(origin, forward);
...@@ -254,9 +273,16 @@ void VrShell::UpdateController(const gvr::Vec3f& forward_vector) { ...@@ -254,9 +273,16 @@ void VrShell::UpdateController(const gvr::Vec3f& forward_vector) {
target_point_ = GetRayPoint(origin, forward, distance); target_point_ = GetRayPoint(origin, forward, distance);
// Determine which UI element (if any) the cursor is pointing to. // Determine which UI element (if any) the cursor is pointing to.
float closest_element = std::numeric_limits<float>::infinity(); float closest_element_distance = std::numeric_limits<float>::infinity();
int pixel_x = 0;
int pixel_y = 0;
VrInputManager* input_target = nullptr;
for (std::size_t i = 0; i < scene_.GetUiElements().size(); ++i) { for (std::size_t i = 0; i < scene_.GetUiElements().size(); ++i) {
const ContentRectangle& plane = *scene_.GetUiElements()[i].get(); const ContentRectangle& plane = *scene_.GetUiElements()[i].get();
if (!plane.visible) {
continue;
}
float distance_to_plane = plane.GetRayDistance(origin, forward); float distance_to_plane = plane.GetRayDistance(origin, forward);
gvr::Vec3f plane_intersection_point = gvr::Vec3f plane_intersection_point =
GetRayPoint(origin, forward, distance_to_plane); GetRayPoint(origin, forward, distance_to_plane);
...@@ -265,15 +291,47 @@ void VrShell::UpdateController(const gvr::Vec3f& forward_vector) { ...@@ -265,15 +291,47 @@ void VrShell::UpdateController(const gvr::Vec3f& forward_vector) {
MatrixVectorMul(plane.transform.from_world, plane_intersection_point); MatrixVectorMul(plane.transform.from_world, plane_intersection_point);
float x = rect_2d_point.x + 0.5f; float x = rect_2d_point.x + 0.5f;
float y = 0.5f - rect_2d_point.y; float y = 0.5f - rect_2d_point.y;
if (distance_to_plane > 0 && distance_to_plane < closest_element) { if (distance_to_plane > 0 && distance_to_plane < closest_element_distance) {
bool is_inside = x >= 0.0f && x < 1.0f && y >= 0.0f && y < 1.0f; bool is_inside = x >= 0.0f && x < 1.0f && y >= 0.0f && y < 1.0f;
if (is_inside) { if (is_inside) {
closest_element = distance_to_plane; closest_element_distance = distance_to_plane;
pixel_x =
static_cast<int>(plane.copy_rect.width * x + plane.copy_rect.x);
pixel_y =
static_cast<int>(plane.copy_rect.height * y + plane.copy_rect.y);
target_point_ = plane_intersection_point; target_point_ = plane_intersection_point;
target_element_ = &plane; target_element_ = &plane;
input_target = (plane.id == kBrowserUiElementId)
? content_input_manager_.get() : ui_input_manager_.get();
} }
} }
} }
bool new_target = input_target != current_input_target_;
if (new_target && current_input_target_ != nullptr) {
// Send a move event indicating that the pointer moved off of an element.
gesture->type = WebInputEvent::MouseLeave;
gesture->details.move.delta.x = 0;
gesture->details.move.delta.y = 0;
current_input_target_->ProcessUpdatedGesture(*gesture.get());
}
current_input_target_ = input_target;
if (current_input_target_ == nullptr) {
return;
}
gesture->type = new_target ? WebInputEvent::MouseEnter
: WebInputEvent::MouseMove;
gesture->details.move.delta.x = pixel_x;
gesture->details.move.delta.y = pixel_y;
current_input_target_->ProcessUpdatedGesture(*gesture.get());
if (original_type == WebInputEvent::GestureTap) {
gesture->type = WebInputEvent::GestureTap;
gesture->details.buttons.pos.x = pixel_x;
gesture->details.buttons.pos.y = pixel_y;
current_input_target_->ProcessUpdatedGesture(*gesture.get());
}
} }
void ApplyNeckModel(gvr::Mat4f& mat_forward) { void ApplyNeckModel(gvr::Mat4f& mat_forward) {
...@@ -586,6 +644,7 @@ void VrShell::DrawWebVrEye(const gvr::Mat4f& view_matrix, ...@@ -586,6 +644,7 @@ void VrShell::DrawWebVrEye(const gvr::Mat4f& view_matrix,
void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) { void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) {
if (gvr_api_ == nullptr) if (gvr_api_ == nullptr)
return; return;
controller_->OnPause();
gvr_api_->PauseTracking(); gvr_api_->PauseTracking();
} }
...@@ -595,6 +654,7 @@ void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { ...@@ -595,6 +654,7 @@ void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) {
gvr_api_->RefreshViewerProfile(); gvr_api_->RefreshViewerProfile();
gvr_api_->ResumeTracking(); gvr_api_->ResumeTracking();
controller_->OnResume();
} }
base::WeakPtr<VrShell> VrShell::GetWeakPtr() { base::WeakPtr<VrShell> VrShell::GetWeakPtr() {
......
...@@ -30,8 +30,12 @@ class WindowAndroid; ...@@ -30,8 +30,12 @@ class WindowAndroid;
namespace vr_shell { namespace vr_shell {
class VrCompositor; class VrCompositor;
class VrController;
class VrInputManager;
class VrShellDelegate; class VrShellDelegate;
class VrShellRenderer; class VrShellRenderer;
struct VrGesture;
class VrShell : public device::GvrDelegate { class VrShell : public device::GvrDelegate {
public: public:
...@@ -146,10 +150,10 @@ class VrShell : public device::GvrDelegate { ...@@ -146,10 +150,10 @@ class VrShell : public device::GvrDelegate {
base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_; base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_;
gvr::Quatf controller_quat_; gvr::Quatf controller_quat_;
bool controller_active_ = false;
gvr::Vec3f target_point_; gvr::Vec3f target_point_;
const ContentRectangle* target_element_ = nullptr; const ContentRectangle* target_element_ = nullptr;
VrInputManager* current_input_target_ = nullptr;
int ui_tex_width_ = 0; int ui_tex_width_ = 0;
int ui_tex_height_ = 0; int ui_tex_height_ = 0;
...@@ -157,6 +161,10 @@ class VrShell : public device::GvrDelegate { ...@@ -157,6 +161,10 @@ class VrShell : public device::GvrDelegate {
bool webvr_secure_origin_ = false; bool webvr_secure_origin_ = false;
int64_t webvr_warning_end_nanos_ = 0; int64_t webvr_warning_end_nanos_ = 0;
std::unique_ptr<VrController> controller_;
scoped_refptr<VrInputManager> content_input_manager_;
scoped_refptr<VrInputManager> ui_input_manager_;
base::WeakPtrFactory<VrShell> weak_ptr_factory_; base::WeakPtrFactory<VrShell> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(VrShell); DISALLOW_COPY_AND_ASSIGN(VrShell);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment