From 184303d3c9a817593052ec64bcc1e04fb8a2a267 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Sun, 18 Oct 2015 20:44:32 -0700 Subject: [PATCH] Using JS mappings for joystick, updating test code --- interface/resources/qml/TestControllers.qml | 57 +++++++++++++++++-- .../src/controllers/UserInputMapper.cpp | 35 +++++++++++- .../src/controllers/UserInputMapper.h | 8 ++- .../src/input-plugins/Joystick.cpp | 2 + 4 files changed, 93 insertions(+), 9 deletions(-) diff --git a/interface/resources/qml/TestControllers.qml b/interface/resources/qml/TestControllers.qml index d8c9cb4343..137a8548ac 100644 --- a/interface/resources/qml/TestControllers.qml +++ b/interface/resources/qml/TestControllers.qml @@ -50,9 +50,38 @@ HifiControls.VrDialog { Row { spacing: 8 Button { - text: "Default Mapping" + text: "Standard Mapping" onClicked: { var mapping = Controller.newMapping("Default"); + mapping.from(standard.LX).to(actions.TranslateX); + mapping.from(standard.LY).to(actions.TranslateZ); + mapping.from(standard.RY).to(actions.Pitch); + mapping.from(standard.RX).to(actions.Yaw); + mapping.from(standard.DU).scale(0.5).to(actions.LONGITUDINAL_FORWARD); + mapping.from(standard.DD).scale(0.5).to(actions.LONGITUDINAL_BACKWARD); + mapping.from(standard.DL).scale(0.5).to(actions.LATERAL_LEFT); + mapping.from(standard.DR).scale(0.5).to(actions.LATERAL_RIGHT); + mapping.from(standard.X).to(actions.VERTICAL_DOWN); + mapping.from(standard.Y).to(actions.VERTICAL_UP); + mapping.from(standard.RT).scale(0.1).to(actions.BOOM_IN); + mapping.from(standard.LT).scale(0.1).to(actions.BOOM_OUT); + mapping.from(standard.B).to(actions.ACTION1); + mapping.from(standard.A).to(actions.ACTION2); + mapping.from(standard.RB).to(actions.SHIFT); + mapping.from(standard.Back).to(actions.TOGGLE_MUTE); + mapping.from(standard.Start).to(actions.CONTEXT_MENU); + Controller.enableMapping("Default"); + enabled = false; + text = "Standard Built" + } + } + + Button { + text: root.xbox ? "XBox Mapping" : "XBox not found" + property bool built: false + enabled: root.xbox && !built + onClicked: { + var mapping = Controller.newMapping(); mapping.from(xbox.A).to(standard.A); mapping.from(xbox.B).to(standard.B); mapping.from(xbox.X).to(standard.X); @@ -73,14 +102,32 @@ HifiControls.VrDialog { mapping.from(xbox.RX).to(standard.RX); mapping.from(xbox.LT).to(standard.LT); mapping.from(xbox.RT).to(standard.RT); - Controller.enableMapping("Default"); - enabled = false; - text = "Built" + mapping.enable(); + built = false; + text = "XBox Built" } } Button { - text: "Build Mapping" + text: root.hydra ? "Hydra Mapping" : "Hydra Not Found" + property bool built: false + enabled: root.hydra && !built + onClicked: { + var mapping = Controller.newMapping(); + mapping.from(hydra.LY).invert().to(standard.LY); + mapping.from(hydra.LX).to(standard.LX); + mapping.from(hydra.RY).invert().to(standard.RY); + mapping.from(hydra.RX).to(standard.RX); + mapping.from(hydra.LT).to(standard.LT); + mapping.from(hydra.RT).to(standard.RT); + mapping.enable(); + built = false; + text = "Hydra Built" + } + } + + Button { + text: "Test Mapping" onClicked: { var mapping = Controller.newMapping(); // Inverting a value diff --git a/libraries/controllers/src/controllers/UserInputMapper.cpp b/libraries/controllers/src/controllers/UserInputMapper.cpp index 2c80eb98a1..672d6a2542 100755 --- a/libraries/controllers/src/controllers/UserInputMapper.cpp +++ b/libraries/controllers/src/controllers/UserInputMapper.cpp @@ -242,6 +242,12 @@ QVector UserInputMapper::getAllInputsForDevice(ui return channels; } +void fixBisectedAxis(float& full, float& negative, float& positive) { + full = full + (negative * -1.0f) + positive; + negative = full >= 0.0f ? 0.0f : full * -1.0f; + positive = full <= 0.0f ? 0.0f : full; +} + void UserInputMapper::update(float deltaTime) { // Reset the axis state for next loop @@ -303,14 +309,25 @@ void UserInputMapper::update(float deltaTime) { } // Scale all the channel step with the scale - static const float EPSILON = 0.01f; for (auto i = 0; i < NUM_ACTIONS; i++) { if (_externalActionStates[i] != 0) { _actionStates[i] += _externalActionStates[i]; _externalActionStates[i] = 0.0f; } - _actionStates[i] *= _actionScales[i]; + } + // merge the bisected and non-bisected axes for now + fixBisectedAxis(_actionStates[TRANSLATE_X], _actionStates[LATERAL_LEFT], _actionStates[LATERAL_RIGHT]); + fixBisectedAxis(_actionStates[TRANSLATE_Y], _actionStates[VERTICAL_DOWN], _actionStates[VERTICAL_UP]); + fixBisectedAxis(_actionStates[TRANSLATE_Z], _actionStates[LONGITUDINAL_FORWARD], _actionStates[LONGITUDINAL_BACKWARD]); + fixBisectedAxis(_actionStates[TRANSLATE_CAMERA_Z], _actionStates[BOOM_IN], _actionStates[BOOM_OUT]); + fixBisectedAxis(_actionStates[ROTATE_Y], _actionStates[YAW_LEFT], _actionStates[YAW_RIGHT]); + fixBisectedAxis(_actionStates[ROTATE_X], _actionStates[PITCH_UP], _actionStates[PITCH_DOWN]); + + + static const float EPSILON = 0.01f; + for (auto i = 0; i < NUM_ACTIONS; i++) { + _actionStates[i] *= _actionScales[i]; // Emit only on change, and emit when moving back to 0 if (fabsf(_actionStates[i] - _lastActionStates[i]) > EPSILON) { _lastActionStates[i] = _actionStates[i]; @@ -420,9 +437,9 @@ void UserInputMapper::createActionNames() { void UserInputMapper::registerStandardDevice() { _standardController = std::make_shared(); _standardController->registerToUserInputMapper(*this); + _standardController->assignDefaultInputMapping(*this); } - static int actionMetaTypeId = qRegisterMetaType(); static int inputMetaTypeId = qRegisterMetaType(); static int inputPairMetaTypeId = qRegisterMetaType(); @@ -481,3 +498,15 @@ void UserInputMapper::registerControllerTypes(QScriptEngine* engine) { qScriptRegisterMetaType(engine, inputToScriptValue, inputFromScriptValue); qScriptRegisterMetaType(engine, inputPairToScriptValue, inputPairFromScriptValue); } + +UserInputMapper::Input UserInputMapper::makeStandardInput(controller::StandardButtonChannel button) { + return Input(STANDARD_DEVICE, button, ChannelType::BUTTON); +} + +UserInputMapper::Input UserInputMapper::makeStandardInput(controller::StandardAxisChannel axis) { + return Input(STANDARD_DEVICE, axis, ChannelType::AXIS); +} + +UserInputMapper::Input UserInputMapper::makeStandardInput(controller::StandardPoseChannel pose) { + return Input(STANDARD_DEVICE, pose, ChannelType::POSE); +} diff --git a/libraries/controllers/src/controllers/UserInputMapper.h b/libraries/controllers/src/controllers/UserInputMapper.h index b8bc0850cf..d463ed0482 100644 --- a/libraries/controllers/src/controllers/UserInputMapper.h +++ b/libraries/controllers/src/controllers/UserInputMapper.h @@ -21,6 +21,7 @@ #include "Pose.h" #include "Input.h" #include "DeviceProxy.h" +#include "StandardControls.h" class StandardController; typedef std::shared_ptr StandardControllerPointer; @@ -84,8 +85,9 @@ public: ROTATE_Z, ROLL = ROTATE_Z, TRANSLATE_CAMERA_Z, + NUM_COMBINED_AXES, - LEFT_HAND, + LEFT_HAND = NUM_COMBINED_AXES, RIGHT_HAND, LEFT_HAND_CLICK, @@ -145,6 +147,10 @@ public: bool addInputChannel(Action action, const Input& input, float scale = 1.0f); bool addInputChannel(Action action, const Input& input, const Input& modifer, float scale = 1.0f); + UserInputMapper::Input makeStandardInput(controller::StandardButtonChannel button); + UserInputMapper::Input makeStandardInput(controller::StandardAxisChannel axis); + UserInputMapper::Input makeStandardInput(controller::StandardPoseChannel pose); + // Under the hood, the input channels are organized in map sorted on the _output // The InputChannel class is just the full values describing the input channel in one object class InputChannel { diff --git a/libraries/input-plugins/src/input-plugins/Joystick.cpp b/libraries/input-plugins/src/input-plugins/Joystick.cpp index 09e81f7346..9d9ac8bc26 100644 --- a/libraries/input-plugins/src/input-plugins/Joystick.cpp +++ b/libraries/input-plugins/src/input-plugins/Joystick.cpp @@ -145,6 +145,7 @@ void Joystick::registerToUserInputMapper(UserInputMapper& mapper) { void Joystick::assignDefaultInputMapping(UserInputMapper& mapper) { +#if 0 #ifdef HAVE_SDL2 const float JOYSTICK_MOVE_SPEED = 1.0f; const float DPAD_MOVE_SPEED = 0.5f; @@ -202,4 +203,5 @@ void Joystick::assignDefaultInputMapping(UserInputMapper& mapper) { mapper.addInputChannel(UserInputMapper::ACTION1, makeInput(controller::B)); mapper.addInputChannel(UserInputMapper::ACTION2, makeInput(controller::A)); #endif +#endif } \ No newline at end of file