From f752534f7c166e3dcf4e453764165674771b1500 Mon Sep 17 00:00:00 2001 From: Sam Gondelman Date: Thu, 18 Jun 2015 09:52:36 -0700 Subject: [PATCH] actionEvent signal, captureActions, began work on hmdControls.js --- examples/hmdControls.js | 87 +++++++++++++++++++ interface/src/Application.cpp | 22 ++--- .../ControllerScriptingInterface.cpp | 1 + .../scripting/ControllerScriptingInterface.h | 7 ++ interface/src/ui/UserInputMapper.cpp | 8 +- .../AbstractControllerScriptingInterface.h | 5 ++ 6 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 examples/hmdControls.js diff --git a/examples/hmdControls.js b/examples/hmdControls.js new file mode 100644 index 0000000000..3e73decaec --- /dev/null +++ b/examples/hmdControls.js @@ -0,0 +1,87 @@ +// +// hmdControls.js +// examples +// +// Created by Sam Gondelman on 6/17/15 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var MOVE_DISTANCE = 0.5; +var PITCH_INCREMENT = Math.PI / 8; +var YAW_INCREMENT = Math.PI / 8; +var BOOM_SPEED = 0.5; +var THRESHOLD = 0.2; + +var hmdControls = (function () { + + function onActionEvent(action, state) { + if (state < THRESHOLD) { + return; + } + switch (action) { + case 0: // backward + var direction = Quat.getFront(Camera.getOrientation()); + direction = Vec3.multiply(-1, direction); + direction = Vec3.multiply(Vec3.normalize(direction), MOVE_DISTANCE); + MyAvatar.position = Vec3.sum(MyAvatar.position, direction); + break; + case 1: // forward + var direction = Quat.getFront(Camera.getOrientation()); + direction = Vec3.multiply(Vec3.normalize(direction), MOVE_DISTANCE); + MyAvatar.position = Vec3.sum(MyAvatar.position, direction); + break; + case 2: // left + var direction = Quat.getRight(Camera.getOrientation()); + direction = Vec3.multiply(-1, direction); + direction = Vec3.multiply(Vec3.normalize(direction), MOVE_DISTANCE); + MyAvatar.position = Vec3.sum(MyAvatar.position, direction); + break; + case 3: // right + var direction = Quat.getRight(Camera.getOrientation()); + direction = Vec3.multiply(Vec3.normalize(direction), MOVE_DISTANCE); + MyAvatar.position = Vec3.sum(MyAvatar.position, direction); + break; + case 4: // down + var direction = Quat.getUp(Camera.getOrientation()); + direction = Vec3.multiply(-1, direction); + direction = Vec3.multiply(Vec3.normalize(direction), MOVE_DISTANCE); + MyAvatar.position = Vec3.sum(MyAvatar.position, direction); + break; + case 5: // up + var direction = Quat.getUp(Camera.getOrientation()); + direction = Vec3.multiply(Vec3.normalize(direction), MOVE_DISTANCE); + MyAvatar.position = Vec3.sum(MyAvatar.position, direction); + break; + case 6: // yaw left + MyAvatar.bodyYaw = MyAvatar.bodyYaw + YAW_INCREMENT; + break; + case 7: // yaw right + MyAvatar.bodyYaw = MyAvatar.bodyYaw - YAW_INCREMENT; + break; + case 8: // pitch down + MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch - PITCH_INCREMENT)); + break; + case 9: // pitch up + MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch + PITCH_INCREMENT)); + break; + default: + break; + } + } + + function setUp() { + Controller.captureActionEvents(); + + Controller.actionEvent.connect(onActionEvent); + } + + function tearDown() { + Controller.releaseActionEvents(); + } + + setUp(); + Script.scriptEnding.connect(tearDown); +}()); \ No newline at end of file diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 651e343087..ba6c86cafb 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2505,16 +2505,18 @@ void Application::update(float deltaTime) { // Transfer the user inputs to the driveKeys _myAvatar->clearDriveKeys(); - _myAvatar->setDriveKeys(FWD, _userInputMapper.getActionState(UserInputMapper::LONGITUDINAL_FORWARD)); - _myAvatar->setDriveKeys(BACK, _userInputMapper.getActionState(UserInputMapper::LONGITUDINAL_BACKWARD)); - _myAvatar->setDriveKeys(UP, _userInputMapper.getActionState(UserInputMapper::VERTICAL_UP)); - _myAvatar->setDriveKeys(DOWN, _userInputMapper.getActionState(UserInputMapper::VERTICAL_DOWN)); - _myAvatar->setDriveKeys(LEFT, _userInputMapper.getActionState(UserInputMapper::LATERAL_LEFT)); - _myAvatar->setDriveKeys(RIGHT, _userInputMapper.getActionState(UserInputMapper::LATERAL_RIGHT)); - _myAvatar->setDriveKeys(ROT_UP, _userInputMapper.getActionState(UserInputMapper::PITCH_UP)); - _myAvatar->setDriveKeys(ROT_DOWN, _userInputMapper.getActionState(UserInputMapper::PITCH_DOWN)); - _myAvatar->setDriveKeys(ROT_LEFT, _userInputMapper.getActionState(UserInputMapper::YAW_LEFT)); - _myAvatar->setDriveKeys(ROT_RIGHT, _userInputMapper.getActionState(UserInputMapper::YAW_RIGHT)); + if (!_controllerScriptingInterface.areActionsCaptured()) { + _myAvatar->setDriveKeys(FWD, _userInputMapper.getActionState(UserInputMapper::LONGITUDINAL_FORWARD)); + _myAvatar->setDriveKeys(BACK, _userInputMapper.getActionState(UserInputMapper::LONGITUDINAL_BACKWARD)); + _myAvatar->setDriveKeys(UP, _userInputMapper.getActionState(UserInputMapper::VERTICAL_UP)); + _myAvatar->setDriveKeys(DOWN, _userInputMapper.getActionState(UserInputMapper::VERTICAL_DOWN)); + _myAvatar->setDriveKeys(LEFT, _userInputMapper.getActionState(UserInputMapper::LATERAL_LEFT)); + _myAvatar->setDriveKeys(RIGHT, _userInputMapper.getActionState(UserInputMapper::LATERAL_RIGHT)); + _myAvatar->setDriveKeys(ROT_UP, _userInputMapper.getActionState(UserInputMapper::PITCH_UP)); + _myAvatar->setDriveKeys(ROT_DOWN, _userInputMapper.getActionState(UserInputMapper::PITCH_DOWN)); + _myAvatar->setDriveKeys(ROT_LEFT, _userInputMapper.getActionState(UserInputMapper::YAW_LEFT)); + _myAvatar->setDriveKeys(ROT_RIGHT, _userInputMapper.getActionState(UserInputMapper::YAW_RIGHT)); + } _myAvatar->setDriveKeys(BOOM_IN, _userInputMapper.getActionState(UserInputMapper::BOOM_IN)); _myAvatar->setDriveKeys(BOOM_OUT, _userInputMapper.getActionState(UserInputMapper::BOOM_OUT)); diff --git a/interface/src/scripting/ControllerScriptingInterface.cpp b/interface/src/scripting/ControllerScriptingInterface.cpp index 5bee51c9b9..0d9cf38705 100644 --- a/interface/src/scripting/ControllerScriptingInterface.cpp +++ b/interface/src/scripting/ControllerScriptingInterface.cpp @@ -28,6 +28,7 @@ ControllerScriptingInterface::ControllerScriptingInterface() : { } + static int actionMetaTypeId = qRegisterMetaType(); static int inputChannelMetaTypeId = qRegisterMetaType(); static int inputMetaTypeId = qRegisterMetaType(); diff --git a/interface/src/scripting/ControllerScriptingInterface.h b/interface/src/scripting/ControllerScriptingInterface.h index 3a54826195..44b59beded 100644 --- a/interface/src/scripting/ControllerScriptingInterface.h +++ b/interface/src/scripting/ControllerScriptingInterface.h @@ -72,12 +72,15 @@ public: void emitTouchUpdateEvent(const TouchEvent& event) { emit touchUpdateEvent(event); } void emitWheelEvent(QWheelEvent* event) { emit wheelEvent(*event); } + + void emitActionEvents(); bool isKeyCaptured(QKeyEvent* event) const; bool isKeyCaptured(const KeyEvent& event) const; bool isMouseCaptured() const { return _mouseCaptured; } bool isTouchCaptured() const { return _touchCaptured; } bool isWheelCaptured() const { return _wheelCaptured; } + bool areActionsCaptured() const { return _actionsCaptured; } bool isJoystickCaptured(int joystickIndex) const; void updateInputControllers(); @@ -122,6 +125,9 @@ public slots: virtual void captureWheelEvents() { _wheelCaptured = true; } virtual void releaseWheelEvents() { _wheelCaptured = false; } + + virtual void captureActionEvents() { _actionsCaptured = true; } + virtual void releaseActionEvents() { _actionsCaptured = false; } virtual void captureJoystick(int joystickIndex); virtual void releaseJoystick(int joystickIndex); @@ -142,6 +148,7 @@ private: bool _mouseCaptured; bool _touchCaptured; bool _wheelCaptured; + bool _actionsCaptured; QMultiMap _capturedKeys; QSet _capturedJoysticks; diff --git a/interface/src/ui/UserInputMapper.cpp b/interface/src/ui/UserInputMapper.cpp index 127aead761..985353e31e 100755 --- a/interface/src/ui/UserInputMapper.cpp +++ b/interface/src/ui/UserInputMapper.cpp @@ -8,9 +8,12 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "UserInputMapper.h" #include +#include "Application.h" + +#include "UserInputMapper.h" + // UserInputMapper Class @@ -207,6 +210,9 @@ void UserInputMapper::update(float deltaTime) { // Scale all the channel step with the scale for (auto i = 0; i < NUM_ACTIONS; i++) { _actionStates[i] *= _actionScales[i]; + if (_actionStates[i] > 0) { + emit Application::getInstance()->getControllerScriptingInterface()->actionEvent(i, _actionStates[i]); + } } } diff --git a/libraries/script-engine/src/AbstractControllerScriptingInterface.h b/libraries/script-engine/src/AbstractControllerScriptingInterface.h index 43076039a9..52c18bc8cf 100644 --- a/libraries/script-engine/src/AbstractControllerScriptingInterface.h +++ b/libraries/script-engine/src/AbstractControllerScriptingInterface.h @@ -83,6 +83,9 @@ public slots: virtual void captureWheelEvents() = 0; virtual void releaseWheelEvents() = 0; + + virtual void captureActionEvents() = 0; + virtual void releaseActionEvents() = 0; virtual void captureJoystick(int joystickIndex) = 0; virtual void releaseJoystick(int joystickIndex) = 0; @@ -111,6 +114,8 @@ signals: void touchUpdateEvent(const TouchEvent& event); void wheelEvent(const WheelEvent& event); + + void actionEvent(int action, float state); };