initial working connection of an xbox gamepad

This commit is contained in:
Stephen Birarda 2014-09-23 14:48:44 -07:00
parent 4733945300
commit 900daf48eb
10 changed files with 183 additions and 32 deletions

24
examples/xbox.js Normal file
View 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);

View file

@ -2,7 +2,7 @@ set(TARGET_NAME interface)
project(${TARGET_NAME})
# 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})
string(TOUPPER ${EXTERNAL} ${EXTERNAL}_UPPERCASE)
if (NOT ${${EXTERNAL}_UPPERCASE}_ROOT_DIR)
@ -120,6 +120,10 @@ foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
add_definitions(-DHAVE_${${EXTERNAL}_UPPERCASE})
# 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})
# perform the system include hack for OS X to ignore warnings
@ -129,6 +133,10 @@ foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
endforeach()
endif ()
if (NOT ${${EXTERNAL}_UPPERCASE}_LIBRARIES)
set(${${EXTERNAL}_UPPERCASE}_LIBRARIES ${${${EXTERNAL}_UPPERCASE}_LIBRARY})
endif ()
target_link_libraries(${TARGET_NAME} ${${${EXTERNAL}_UPPERCASE}_LIBRARIES})
endif ()

View file

@ -25,12 +25,13 @@ JoystickManager::JoystickManager() {
SDL_Init(SDL_INIT_JOYSTICK);
int joystickCount = SDL_NumJoysticks();
for (int i = 0; i < joystickCount; i++) {
SDL_Joystick* joystick = SDL_JoystickOpen(i);
if (joystick) {
JoystickState state = { SDL_JoystickName(i), QVector<float>(SDL_JoystickNumAxes(joystick)),
QVector<bool>(SDL_JoystickNumButtons(joystick)) };
_joystickStates.append(state);
_joysticks.append(joystick);
SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
if (sdlJoystick) {
_sdlJoysticks.append(sdlJoystick);
JoystickInputController controller(SDL_JoystickName(i),
SDL_JoystickNumAxes(sdlJoystick), SDL_JoystickNumButtons(sdlJoystick));
_joysticks.append(controller);
}
}
#endif
@ -38,8 +39,8 @@ JoystickManager::JoystickManager() {
JoystickManager::~JoystickManager() {
#ifdef HAVE_SDL
foreach (SDL_Joystick* joystick, _joysticks) {
SDL_JoystickClose(joystick);
foreach (SDL_Joystick* sdlJoystick, _sdlJoysticks) {
SDL_JoystickClose(sdlJoystick);
}
SDL_Quit();
#endif
@ -50,16 +51,16 @@ void JoystickManager::update() {
PerformanceTimer perfTimer("joystick");
SDL_JoystickUpdate();
for (int i = 0; i < _joystickStates.size(); i++) {
SDL_Joystick* joystick = _joysticks.at(i);
JoystickState& state = _joystickStates[i];
for (int j = 0; j < state.axes.size(); j++) {
float value = glm::round(SDL_JoystickGetAxis(joystick, j) + 0.5f) / numeric_limits<short>::max();
for (int i = 0; i < _joysticks.size(); i++) {
SDL_Joystick* sdlJoystick = _sdlJoysticks.at(i);
JoystickInputController& joystick = _joysticks[i];
for (int j = 0; j < joystick.getNumAxes(); j++) {
float value = glm::round(SDL_JoystickGetAxis(sdlJoystick, j) + 0.5f) / numeric_limits<short>::max();
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++) {
state.buttons[j] = SDL_JoystickGetButton(joystick, j);
for (int j = 0; j < joystick.getNumButtons(); j++) {
joystick.updateButton(j, SDL_JoystickGetButton(sdlJoystick, j));
}
}
#endif

View file

@ -20,6 +20,8 @@
#undef main
#endif
#include "JoystickInputController.h"
class JoystickState;
/// Handles joystick input through SDL.
@ -27,27 +29,19 @@ class JoystickManager : public QObject {
Q_OBJECT
public:
JoystickManager();
virtual ~JoystickManager();
const QVector<JoystickState>& getJoystickStates() const { return _joystickStates; }
QVector<JoystickInputController>& getJoysticks() { return _joysticks; }
void update();
private:
QVector<JoystickState> _joystickStates;
QVector<JoystickInputController> _joysticks;
#ifdef HAVE_SDL
QVector<SDL_Joystick*> _joysticks;
QVector<SDL_Joystick*> _sdlJoysticks;
#endif
};
class JoystickState {
public:
QString name;
QVector<float> axes;
QVector<bool> buttons;
};
#endif // hifi_JoystickManager_h

View file

@ -61,7 +61,7 @@ static void setPalm(float deltaTime, int index) {
palm->setActive(true);
// 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);
if (state.axes.size() >= 4 && state.buttons.size() >= 4) {
if (index == LEFT_HAND_INDEX) {

View file

@ -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) {
_inputControllers.erase(input->getKey());
}

View file

@ -113,6 +113,7 @@ public slots:
/// Factory to create an InputController
virtual AbstractInputController* createInputController(const QString& deviceName, const QString& tracker);
JoystickInputController* joystick() const;
virtual void releaseInputController(AbstractInputController* input);

View 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);
}

View 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

View file

@ -35,6 +35,7 @@
#include "AnimationObject.h"
#include "ArrayBufferViewClass.h"
#include "DataViewClass.h"
#include "JoystickInputController.h"
#include "MenuItemProperties.h"
#include "MIDIEvent.h"
#include "LocalVoxels.h"
@ -72,14 +73,22 @@ void injectorFromScriptValue(const QScriptValue &object, AudioInjector* &out) {
out = qobject_cast<AudioInjector*>(object.toQObject());
}
QScriptValue injectorToScriptValueInputController(QScriptEngine *engine, AbstractInputController* const &in) {
QScriptValue inputControllerToScriptValue(QScriptEngine *engine, AbstractInputController* const &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());
}
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,
AbstractControllerScriptingInterface* controllerScriptingInterface) :
@ -277,7 +286,8 @@ void ScriptEngine::init() {
globalObject().setProperty("LocalVoxels", localVoxelsValue);
qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue);
qScriptRegisterMetaType( this, injectorToScriptValueInputController, injectorFromScriptValueInputController);
qScriptRegisterMetaType(this, inputControllerToScriptValue, inputControllerFromScriptValue);
qScriptRegisterMetaType(this, joystickToScriptValue, joystickFromScriptValue);
qScriptRegisterMetaType(this, animationDetailsToScriptValue, animationDetailsFromScriptValue);