Skip to content

Commit

Permalink
Add Oculus Touch controller btn mapping for immersive web.
Browse files Browse the repository at this point in the history
  • Loading branch information
daoshengmu committed Dec 21, 2018
1 parent cdfea41 commit 99a84d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/src/main/cpp/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace crow {
class Pointer;
typedef std::shared_ptr<Pointer> PointerPtr;

static const int kControllerMaxButtonCount = 4;
static const int kControllerMaxButtonCount = 6;
static const int kControllerMaxAxes = 6;

struct Controller {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/cpp/ControllerContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <assert.h>
#include "ControllerContainer.h"
#include "Controller.h"
#include "Pointer.h"
Expand Down Expand Up @@ -258,6 +259,9 @@ ControllerContainer::SetButtonCount(const int32_t aControllerIndex, const uint32

void
ControllerContainer::SetButtonState(const int32_t aControllerIndex, const Button aWhichButton, const int32_t aImmersiveIndex, const bool aPressed, const bool aTouched, const float aImmersiveTrigger) {
assert(kControllerMaxButtonCount > aImmersiveIndex
&& "Button index must < kControllerMaxButtonCount.");

if (!m.Contains(aControllerIndex)) {
return;
}
Expand Down Expand Up @@ -293,6 +297,9 @@ ControllerContainer::SetButtonState(const int32_t aControllerIndex, const Button

void
ControllerContainer::SetAxes(const int32_t aControllerIndex, const float* aData, const uint32_t aLength) {
assert(kControllerMaxAxes >= aLength
&& "Axis length must <= kControllerMaxAxes.");

if (!m.Contains(aControllerIndex)) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/cpp/ControllerDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class ControllerDelegate {
enum Button {
BUTTON_TRIGGER = 1 << 0,
BUTTON_TOUCHPAD = 1 << 1,
BUTTON_APP = 1 << 2,
BUTTON_APP = 1 << 2,
BUTTON_OTHERS = 1 << 3, // Other buttons only for the immersive mode.
};

virtual void CreateController(const int32_t aControllerIndex, const int32_t aModelIndex, const std::string& aImmersiveName) = 0;
Expand Down
39 changes: 29 additions & 10 deletions app/src/oculusvr/cpp/DeviceDelegateOculusVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,21 +709,39 @@ struct DeviceDelegateOculusVR::State {
triggerTouched = (state[i].controllerState.Touches & ovrTouch_IndexTrigger) != 0;
trackpadPressed = (state[i].controllerState.Buttons & ovrButton_Joystick) != 0;
trackpadTouched = (state[i].controllerState.Touches & ovrTouch_Joystick) != 0;
trackpadX = state[i].controllerState.Joystick.x;
trackpadY = state[i].controllerState.Joystick.y;
axes[0] = trackpadX;
axes[1] = -trackpadY; // We did y axis intentionally inverted in FF desktop as well.
controller->SetScrolledDelta(i, trackpadX, trackpadY);

const bool gripPressed = (state[i].controllerState.Buttons & ovrButton_GripTrigger) != 0;
controller->SetButtonState(i, ControllerDelegate::BUTTON_OTHERS, 2, gripPressed, gripPressed,
state[i].controllerState.GripTrigger);
if (state[i].hand == ElbowModel::HandEnum::Left) {
controller->SetButtonState(i, ControllerDelegate::BUTTON_APP, -1, state[i].controllerState.Buttons & ovrButton_Y,
state[i].controllerState.Touches & ovrTouch_Y);
const bool xPressed = (state[i].controllerState.Buttons & ovrButton_X) != 0;
const bool xTouched = (state[i].controllerState.Touches & ovrTouch_X) != 0;
const bool yPressed = (state[i].controllerState.Buttons & ovrButton_Y) != 0;
const bool yTouched = (state[i].controllerState.Touches & ovrTouch_Y) != 0;

controller->SetButtonState(i, ControllerDelegate::BUTTON_APP, -1, yPressed, yTouched);
controller->SetButtonState(i, ControllerDelegate::BUTTON_OTHERS, 3, xPressed, xTouched);
controller->SetButtonState(i, ControllerDelegate::BUTTON_OTHERS, 4, yPressed, yTouched);
} else if (state[i].hand == ElbowModel::HandEnum::Right) {
controller->SetButtonState(i, ControllerDelegate::BUTTON_APP, -1, state[i].controllerState.Buttons & ovrButton_B,
state[i].controllerState.Touches & ovrTouch_B);
const bool aPressed = (state[i].controllerState.Buttons & ovrButton_A) != 0;
const bool aTouched = (state[i].controllerState.Touches & ovrTouch_A) != 0;
const bool bPressed = (state[i].controllerState.Buttons & ovrButton_B) != 0;
const bool bTouched = (state[i].controllerState.Touches & ovrTouch_B) != 0;

controller->SetButtonState(i, ControllerDelegate::BUTTON_APP, -1, bPressed, bTouched);
controller->SetButtonState(i, ControllerDelegate::BUTTON_OTHERS, 3, aPressed, aTouched);
controller->SetButtonState(i, ControllerDelegate::BUTTON_OTHERS, 4, bPressed, bTouched);
} else {
VRB_WARN("Undefined hand type in DeviceDelegateOculusVR.");
}
trackpadX = state[i].controllerState.Joystick.x;
trackpadY = state[i].controllerState.Joystick.y;
axes[0] = trackpadX;
axes[1] = trackpadY;
controller->SetScrolledDelta(i, trackpadX, trackpadY);

const bool thumbRest = (state[i].controllerState.Touches & ovrTouch_ThumbUp) != 0;
controller->SetButtonState(i, ControllerDelegate::BUTTON_OTHERS, 5, thumbRest, thumbRest);
} else {
triggerPressed = (state[i].controllerState.Buttons & ovrButton_A) != 0;
triggerTouched = triggerPressed;
Expand All @@ -748,7 +766,8 @@ struct DeviceDelegateOculusVR::State {
axes[0] = trackpadTouched ? trackpadX * 2.0f - 1.0f : 0.0f;
axes[1] = trackpadTouched ? trackpadY * 2.0f - 1.0f : 0.0f;
}
controller->SetButtonState(i, ControllerDelegate::BUTTON_TRIGGER, 1, triggerPressed, triggerTouched);
controller->SetButtonState(i, ControllerDelegate::BUTTON_TRIGGER, 1, triggerPressed, triggerTouched,
state[i].controllerState.IndexTrigger);
controller->SetButtonState(i, ControllerDelegate::BUTTON_TOUCHPAD, 0, trackpadPressed, trackpadTouched);

controller->SetAxes(i, axes, kNumAxes);
Expand Down

0 comments on commit 99a84d7

Please sign in to comment.