mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 05:57:29 +02:00
initial working connection of an xbox gamepad
This commit is contained in:
parent
4733945300
commit
900daf48eb
10 changed files with 183 additions and 32 deletions
24
examples/xbox.js
Normal file
24
examples/xbox.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
//
|
||||||
|
// xbox.js
|
||||||
|
// examples
|
||||||
|
//
|
||||||
|
// Created by Stephen Birarda on September 23, 2014
|
||||||
|
//
|
||||||
|
// Copyright 2014 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
|
||||||
|
//
|
||||||
|
|
||||||
|
gamepad = Controller.joystick();
|
||||||
|
print("THE GAMEPAD NAME is " + gamepad.name);
|
||||||
|
print("THE GAMEPAD HAS " + gamepad.numAxes + " AXES")
|
||||||
|
|
||||||
|
function printValues() {
|
||||||
|
controllerAxes = gamepad.axes;
|
||||||
|
for (i = 0; i < controllerAxes.size; i++) {
|
||||||
|
// print("The value for axis " + i + " is " + controllerAxes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Script.update.connect(printValues);
|
|
@ -2,7 +2,7 @@ set(TARGET_NAME interface)
|
||||||
project(${TARGET_NAME})
|
project(${TARGET_NAME})
|
||||||
|
|
||||||
# set a default root dir for each of our optional externals if it was not passed
|
# set a default root dir for each of our optional externals if it was not passed
|
||||||
set(OPTIONAL_EXTERNALS "Faceplus" "Faceshift" "LibOVR" "PrioVR" "Sixense" "Visage" "LeapMotion" "RtMidi" "Qxmpp")
|
set(OPTIONAL_EXTERNALS "Faceplus" "Faceshift" "LibOVR" "PrioVR" "Sixense" "Visage" "LeapMotion" "RtMidi" "Qxmpp" "SDL")
|
||||||
foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
|
foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
|
||||||
string(TOUPPER ${EXTERNAL} ${EXTERNAL}_UPPERCASE)
|
string(TOUPPER ${EXTERNAL} ${EXTERNAL}_UPPERCASE)
|
||||||
if (NOT ${${EXTERNAL}_UPPERCASE}_ROOT_DIR)
|
if (NOT ${${EXTERNAL}_UPPERCASE}_ROOT_DIR)
|
||||||
|
@ -120,6 +120,10 @@ foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
|
||||||
add_definitions(-DHAVE_${${EXTERNAL}_UPPERCASE})
|
add_definitions(-DHAVE_${${EXTERNAL}_UPPERCASE})
|
||||||
|
|
||||||
# include the library directories (ignoring warnings)
|
# include the library directories (ignoring warnings)
|
||||||
|
if (NOT ${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS)
|
||||||
|
set(${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS ${${${EXTERNAL}_UPPERCASE}_INCLUDE_DIR})
|
||||||
|
endif ()
|
||||||
|
|
||||||
include_directories(SYSTEM ${${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS})
|
include_directories(SYSTEM ${${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS})
|
||||||
|
|
||||||
# perform the system include hack for OS X to ignore warnings
|
# perform the system include hack for OS X to ignore warnings
|
||||||
|
@ -129,6 +133,10 @@ foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
|
||||||
endforeach()
|
endforeach()
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
|
if (NOT ${${EXTERNAL}_UPPERCASE}_LIBRARIES)
|
||||||
|
set(${${EXTERNAL}_UPPERCASE}_LIBRARIES ${${${EXTERNAL}_UPPERCASE}_LIBRARY})
|
||||||
|
endif ()
|
||||||
|
|
||||||
target_link_libraries(${TARGET_NAME} ${${${EXTERNAL}_UPPERCASE}_LIBRARIES})
|
target_link_libraries(${TARGET_NAME} ${${${EXTERNAL}_UPPERCASE}_LIBRARIES})
|
||||||
|
|
||||||
endif ()
|
endif ()
|
||||||
|
|
|
@ -25,12 +25,13 @@ JoystickManager::JoystickManager() {
|
||||||
SDL_Init(SDL_INIT_JOYSTICK);
|
SDL_Init(SDL_INIT_JOYSTICK);
|
||||||
int joystickCount = SDL_NumJoysticks();
|
int joystickCount = SDL_NumJoysticks();
|
||||||
for (int i = 0; i < joystickCount; i++) {
|
for (int i = 0; i < joystickCount; i++) {
|
||||||
SDL_Joystick* joystick = SDL_JoystickOpen(i);
|
SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
|
||||||
if (joystick) {
|
if (sdlJoystick) {
|
||||||
JoystickState state = { SDL_JoystickName(i), QVector<float>(SDL_JoystickNumAxes(joystick)),
|
_sdlJoysticks.append(sdlJoystick);
|
||||||
QVector<bool>(SDL_JoystickNumButtons(joystick)) };
|
|
||||||
_joystickStates.append(state);
|
JoystickInputController controller(SDL_JoystickName(i),
|
||||||
_joysticks.append(joystick);
|
SDL_JoystickNumAxes(sdlJoystick), SDL_JoystickNumButtons(sdlJoystick));
|
||||||
|
_joysticks.append(controller);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -38,8 +39,8 @@ JoystickManager::JoystickManager() {
|
||||||
|
|
||||||
JoystickManager::~JoystickManager() {
|
JoystickManager::~JoystickManager() {
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL
|
||||||
foreach (SDL_Joystick* joystick, _joysticks) {
|
foreach (SDL_Joystick* sdlJoystick, _sdlJoysticks) {
|
||||||
SDL_JoystickClose(joystick);
|
SDL_JoystickClose(sdlJoystick);
|
||||||
}
|
}
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
#endif
|
#endif
|
||||||
|
@ -50,16 +51,16 @@ void JoystickManager::update() {
|
||||||
PerformanceTimer perfTimer("joystick");
|
PerformanceTimer perfTimer("joystick");
|
||||||
SDL_JoystickUpdate();
|
SDL_JoystickUpdate();
|
||||||
|
|
||||||
for (int i = 0; i < _joystickStates.size(); i++) {
|
for (int i = 0; i < _joysticks.size(); i++) {
|
||||||
SDL_Joystick* joystick = _joysticks.at(i);
|
SDL_Joystick* sdlJoystick = _sdlJoysticks.at(i);
|
||||||
JoystickState& state = _joystickStates[i];
|
JoystickInputController& joystick = _joysticks[i];
|
||||||
for (int j = 0; j < state.axes.size(); j++) {
|
for (int j = 0; j < joystick.getNumAxes(); j++) {
|
||||||
float value = glm::round(SDL_JoystickGetAxis(joystick, j) + 0.5f) / numeric_limits<short>::max();
|
float value = glm::round(SDL_JoystickGetAxis(sdlJoystick, j) + 0.5f) / numeric_limits<short>::max();
|
||||||
const float DEAD_ZONE = 0.1f;
|
const float DEAD_ZONE = 0.1f;
|
||||||
state.axes[j] = glm::abs(value) < DEAD_ZONE ? 0.0f : value;
|
joystick.updateAxis(j, glm::abs(value) < DEAD_ZONE ? 0.0f : value);
|
||||||
}
|
}
|
||||||
for (int j = 0; j < state.buttons.size(); j++) {
|
for (int j = 0; j < joystick.getNumButtons(); j++) {
|
||||||
state.buttons[j] = SDL_JoystickGetButton(joystick, j);
|
joystick.updateButton(j, SDL_JoystickGetButton(sdlJoystick, j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -20,6 +20,8 @@
|
||||||
#undef main
|
#undef main
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "JoystickInputController.h"
|
||||||
|
|
||||||
class JoystickState;
|
class JoystickState;
|
||||||
|
|
||||||
/// Handles joystick input through SDL.
|
/// Handles joystick input through SDL.
|
||||||
|
@ -27,27 +29,19 @@ class JoystickManager : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
JoystickManager();
|
JoystickManager();
|
||||||
virtual ~JoystickManager();
|
virtual ~JoystickManager();
|
||||||
|
|
||||||
const QVector<JoystickState>& getJoystickStates() const { return _joystickStates; }
|
QVector<JoystickInputController>& getJoysticks() { return _joysticks; }
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QVector<JoystickState> _joystickStates;
|
QVector<JoystickInputController> _joysticks;
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL
|
||||||
QVector<SDL_Joystick*> _joysticks;
|
QVector<SDL_Joystick*> _sdlJoysticks;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
class JoystickState {
|
|
||||||
public:
|
|
||||||
QString name;
|
|
||||||
QVector<float> axes;
|
|
||||||
QVector<bool> buttons;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // hifi_JoystickManager_h
|
#endif // hifi_JoystickManager_h
|
||||||
|
|
|
@ -61,7 +61,7 @@ static void setPalm(float deltaTime, int index) {
|
||||||
palm->setActive(true);
|
palm->setActive(true);
|
||||||
|
|
||||||
// Read controller buttons and joystick into the hand
|
// Read controller buttons and joystick into the hand
|
||||||
if (!Application::getInstance()->getJoystickManager()->getJoystickStates().isEmpty()) {
|
if (!Application::getInstance()->getJoystickManager()->getJoysticks().isEmpty()) {
|
||||||
const JoystickState& state = Application::getInstance()->getJoystickManager()->getJoystickStates().at(0);
|
const JoystickState& state = Application::getInstance()->getJoystickManager()->getJoystickStates().at(0);
|
||||||
if (state.axes.size() >= 4 && state.buttons.size() >= 4) {
|
if (state.axes.size() >= 4 && state.buttons.size() >= 4) {
|
||||||
if (index == LEFT_HAND_INDEX) {
|
if (index == LEFT_HAND_INDEX) {
|
||||||
|
|
|
@ -297,6 +297,17 @@ AbstractInputController* ControllerScriptingInterface::createInputController(con
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JoystickInputController* ControllerScriptingInterface::joystick() const {
|
||||||
|
// stub to just return the first joystick - assume we only have one connected
|
||||||
|
QVector<JoystickInputController>& activeJoysticks = Application::getInstance()->getJoystickManager()->getJoysticks();
|
||||||
|
|
||||||
|
if (activeJoysticks.size() > 0) {
|
||||||
|
return &activeJoysticks[0];
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ControllerScriptingInterface::releaseInputController(AbstractInputController* input) {
|
void ControllerScriptingInterface::releaseInputController(AbstractInputController* input) {
|
||||||
_inputControllers.erase(input->getKey());
|
_inputControllers.erase(input->getKey());
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,6 +113,7 @@ public slots:
|
||||||
|
|
||||||
/// Factory to create an InputController
|
/// Factory to create an InputController
|
||||||
virtual AbstractInputController* createInputController(const QString& deviceName, const QString& tracker);
|
virtual AbstractInputController* createInputController(const QString& deviceName, const QString& tracker);
|
||||||
|
JoystickInputController* joystick() const;
|
||||||
|
|
||||||
virtual void releaseInputController(AbstractInputController* input);
|
virtual void releaseInputController(AbstractInputController* input);
|
||||||
|
|
||||||
|
|
52
libraries/script-engine/src/JoystickInputController.cpp
Normal file
52
libraries/script-engine/src/JoystickInputController.cpp
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
//
|
||||||
|
// JoystickInputController.cpp
|
||||||
|
// interface/src/devices
|
||||||
|
//
|
||||||
|
// Created by Stephen Birarda on 2014-09-23.
|
||||||
|
// Copyright 2014 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
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <qmetatype.h>
|
||||||
|
|
||||||
|
#include "JoystickInputController.h"
|
||||||
|
|
||||||
|
JoystickInputController::JoystickInputController() :
|
||||||
|
_name(),
|
||||||
|
_axes(0),
|
||||||
|
_buttons(0)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
JoystickInputController::JoystickInputController(const QString& name, int numAxes, int numButtons) :
|
||||||
|
_name(name),
|
||||||
|
_axes(numAxes),
|
||||||
|
_buttons(numButtons)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
JoystickInputController::JoystickInputController(const JoystickInputController& otherJoystickController) :
|
||||||
|
_name(otherJoystickController._name),
|
||||||
|
_axes(otherJoystickController._axes),
|
||||||
|
_buttons(otherJoystickController._buttons)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
JoystickInputController& JoystickInputController::operator=(const JoystickInputController& otherJoystickController) {
|
||||||
|
JoystickInputController temp(otherJoystickController);
|
||||||
|
swap(temp);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JoystickInputController::swap(JoystickInputController& otherJoystickController) {
|
||||||
|
using std::swap;
|
||||||
|
|
||||||
|
swap(_name, otherJoystickController._name);
|
||||||
|
swap(_axes, otherJoystickController._axes);
|
||||||
|
swap(_buttons, otherJoystickController._buttons);
|
||||||
|
}
|
50
libraries/script-engine/src/JoystickInputController.h
Normal file
50
libraries/script-engine/src/JoystickInputController.h
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
//
|
||||||
|
// JoystickInputController.h
|
||||||
|
// interface/src/devices
|
||||||
|
//
|
||||||
|
// Created by Stephen Birarda on 2014-09-23.
|
||||||
|
// Copyright 2014 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
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef hifi_JoystickInputController_h
|
||||||
|
#define hifi_JoystickInputController_h
|
||||||
|
|
||||||
|
#include <qobject.h>
|
||||||
|
#include <qvector.h>
|
||||||
|
|
||||||
|
class JoystickInputController : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(QString name READ getName)
|
||||||
|
|
||||||
|
Q_PROPERTY(int numAxes READ getNumAxes)
|
||||||
|
Q_PROPERTY(QVector<float> axes READ getAxes)
|
||||||
|
public:
|
||||||
|
JoystickInputController();
|
||||||
|
JoystickInputController(const QString& name, int numAxes, int numButtons);
|
||||||
|
JoystickInputController(const JoystickInputController& otherJoystickController);
|
||||||
|
JoystickInputController& operator=(const JoystickInputController& otherJoystickController);
|
||||||
|
|
||||||
|
const QString& getName() const { return _name; }
|
||||||
|
|
||||||
|
void updateAxis(int index, float value) { _axes[index] = value; }
|
||||||
|
void updateButton(int index, bool isActive) { _buttons[index] = isActive; }
|
||||||
|
|
||||||
|
const QVector<float>& getAxes() const { return _axes; }
|
||||||
|
const QVector<bool>& getButtons() const { return _buttons; }
|
||||||
|
|
||||||
|
int getNumAxes() const { return _axes.size(); }
|
||||||
|
int getNumButtons() const { return _buttons.size(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void swap(JoystickInputController& otherJoystickController);
|
||||||
|
|
||||||
|
QString _name;
|
||||||
|
QVector<float> _axes;
|
||||||
|
QVector<bool> _buttons;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // hifi_JoystickTracker_h
|
|
@ -35,6 +35,7 @@
|
||||||
#include "AnimationObject.h"
|
#include "AnimationObject.h"
|
||||||
#include "ArrayBufferViewClass.h"
|
#include "ArrayBufferViewClass.h"
|
||||||
#include "DataViewClass.h"
|
#include "DataViewClass.h"
|
||||||
|
#include "JoystickInputController.h"
|
||||||
#include "MenuItemProperties.h"
|
#include "MenuItemProperties.h"
|
||||||
#include "MIDIEvent.h"
|
#include "MIDIEvent.h"
|
||||||
#include "LocalVoxels.h"
|
#include "LocalVoxels.h"
|
||||||
|
@ -72,14 +73,22 @@ void injectorFromScriptValue(const QScriptValue &object, AudioInjector* &out) {
|
||||||
out = qobject_cast<AudioInjector*>(object.toQObject());
|
out = qobject_cast<AudioInjector*>(object.toQObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
QScriptValue injectorToScriptValueInputController(QScriptEngine *engine, AbstractInputController* const &in) {
|
QScriptValue inputControllerToScriptValue(QScriptEngine *engine, AbstractInputController* const &in) {
|
||||||
return engine->newQObject(in);
|
return engine->newQObject(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
void injectorFromScriptValueInputController(const QScriptValue &object, AbstractInputController* &out) {
|
void inputControllerFromScriptValue(const QScriptValue &object, AbstractInputController* &out) {
|
||||||
out = qobject_cast<AbstractInputController*>(object.toQObject());
|
out = qobject_cast<AbstractInputController*>(object.toQObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QScriptValue joystickToScriptValue(QScriptEngine *engine, JoystickInputController* const &in) {
|
||||||
|
return engine->newQObject(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
void joystickFromScriptValue(const QScriptValue &object, JoystickInputController* &out) {
|
||||||
|
out = qobject_cast<JoystickInputController*>(object.toQObject());
|
||||||
|
}
|
||||||
|
|
||||||
ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString,
|
ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString,
|
||||||
AbstractControllerScriptingInterface* controllerScriptingInterface) :
|
AbstractControllerScriptingInterface* controllerScriptingInterface) :
|
||||||
|
|
||||||
|
@ -277,7 +286,8 @@ void ScriptEngine::init() {
|
||||||
globalObject().setProperty("LocalVoxels", localVoxelsValue);
|
globalObject().setProperty("LocalVoxels", localVoxelsValue);
|
||||||
|
|
||||||
qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue);
|
qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue);
|
||||||
qScriptRegisterMetaType( this, injectorToScriptValueInputController, injectorFromScriptValueInputController);
|
qScriptRegisterMetaType(this, inputControllerToScriptValue, inputControllerFromScriptValue);
|
||||||
|
qScriptRegisterMetaType(this, joystickToScriptValue, joystickFromScriptValue);
|
||||||
|
|
||||||
qScriptRegisterMetaType(this, animationDetailsToScriptValue, animationDetailsFromScriptValue);
|
qScriptRegisterMetaType(this, animationDetailsToScriptValue, animationDetailsFromScriptValue);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue