Merge pull request #3476 from birarda/xbox-controller

add script support for general SDL controllers
This commit is contained in:
Brad Hefta-Gaub 2014-09-25 09:25:08 -07:00
commit 712f88baa3
12 changed files with 306 additions and 136 deletions

19
examples/xbox.js Normal file
View file

@ -0,0 +1,19 @@
//
// 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 = Joysticks.joystickWithName("Wireless 360 Controller");
function reportAxisValue(axis, newValue, oldValue) {
print("The value for axis " + axis + " has changed to " + newValue + ". It was " + oldValue);
}
gamepad.axisValueChanged.connect(reportAxisValue);

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

@ -82,6 +82,7 @@
#include "scripting/AccountScriptingInterface.h"
#include "scripting/AudioDeviceScriptingInterface.h"
#include "scripting/ClipboardScriptingInterface.h"
#include "scripting/JoystickScriptingInterface.h"
#include "scripting/GlobalServicesScriptingInterface.h"
#include "scripting/LocationScriptingInterface.h"
#include "scripting/MenuScriptingInterface.h"
@ -2160,7 +2161,7 @@ void Application::update(float deltaTime) {
updateFaceshift();
updateVisage();
_sixenseManager.update(deltaTime);
_joystickManager.update();
JoystickScriptingInterface::getInstance().update();
_prioVR.update(deltaTime);
}
@ -3770,6 +3771,14 @@ void Application::saveScripts() {
_settings->endArray();
}
QScriptValue joystickToScriptValue(QScriptEngine *engine, Joystick* const &in) {
return engine->newQObject(in);
}
void joystickFromScriptValue(const QScriptValue &object, Joystick* &out) {
out = qobject_cast<Joystick*>(object.toQObject());
}
ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUserLoaded,
bool loadScriptFromEditor, bool activateMainWindow) {
QUrl scriptUrl(scriptFilename);
@ -3852,6 +3861,9 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser
scriptEngine->registerGlobalObject("GlobalServices", GlobalServicesScriptingInterface::getInstance());
scriptEngine->registerGlobalObject("AvatarManager", &_avatarManager);
scriptEngine->registerGlobalObject("Joysticks", &JoystickScriptingInterface::getInstance());
qScriptRegisterMetaType(scriptEngine, joystickToScriptValue, joystickFromScriptValue);
#ifdef HAVE_RTMIDI
scriptEngine->registerGlobalObject("MIDI", &MIDIManager::getInstance());

View file

@ -59,7 +59,6 @@
#include "avatar/MyAvatar.h"
#include "devices/Faceplus.h"
#include "devices/Faceshift.h"
#include "devices/JoystickManager.h"
#include "devices/PrioVR.h"
#include "devices/SixenseManager.h"
#include "devices/Visage.h"
@ -221,7 +220,6 @@ public:
FaceTracker* getActiveFaceTracker();
SixenseManager* getSixenseManager() { return &_sixenseManager; }
PrioVR* getPrioVR() { return &_prioVR; }
JoystickManager* getJoystickManager() { return &_joystickManager; }
BandwidthMeter* getBandwidthMeter() { return &_bandwidthMeter; }
QUndoStack* getUndoStack() { return &_undoStack; }
QSystemTrayIcon* getTrayIcon() { return _trayIcon; }
@ -511,7 +509,6 @@ private:
SixenseManager _sixenseManager;
PrioVR _prioVR;
JoystickManager _joystickManager;
Camera _myCamera; // My view onto the world
Camera _viewFrustumOffsetCamera; // The camera we use to sometimes show the view frustum from an offset mode

View file

@ -0,0 +1,60 @@
//
// Joystick.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 <limits>
#include <glm/glm.hpp>
#include "Joystick.h"
#ifdef HAVE_SDL
Joystick::Joystick(const QString& name, SDL_Joystick* sdlJoystick) :
_name(name),
_axes(QVector<float>(SDL_JoystickNumAxes(sdlJoystick))),
_buttons(QVector<bool>(SDL_JoystickNumButtons(sdlJoystick))),
_sdlJoystick(sdlJoystick)
{
}
#endif
Joystick::~Joystick() {
#ifdef HAVE_SDL
SDL_JoystickClose(_sdlJoystick);
#endif
}
void Joystick::update() {
#ifdef HAVE_SDL
// update our current values, emit a signal when there is a change
for (int j = 0; j < getNumAxes(); j++) {
float value = glm::round(SDL_JoystickGetAxis(_sdlJoystick, j) + 0.5f) / std::numeric_limits<short>::max();
const float DEAD_ZONE = 0.1f;
float cleanValue = glm::abs(value) < DEAD_ZONE ? 0.0f : value;
if (_axes[j] != cleanValue) {
float oldValue = _axes[j];
_axes[j] = cleanValue;
emit axisValueChanged(j, cleanValue, oldValue);
}
}
for (int j = 0; j < getNumButtons(); j++) {
bool newValue = SDL_JoystickGetButton(_sdlJoystick, j);
if (_buttons[j] != newValue) {
bool oldValue = _buttons[j];
_buttons[j] = newValue;
emit buttonStateChanged(j, newValue, oldValue);
}
}
#endif
}

View file

@ -0,0 +1,61 @@
//
// Joystick.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_Joystick_h
#define hifi_Joystick_h
#include <qobject.h>
#include <qvector.h>
#ifdef HAVE_SDL
#include <SDL.h>
#undef main
#endif
class Joystick : public QObject {
Q_OBJECT
Q_PROPERTY(QString name READ getName)
Q_PROPERTY(int numAxes READ getNumAxes)
Q_PROPERTY(int numButtons READ getNumButtons)
public:
Joystick();
~Joystick();
#ifdef HAVE_SDL
Joystick(const QString& name, SDL_Joystick* sdlJoystick);
#endif
void update();
const QString& getName() const { return _name; }
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(); }
signals:
void axisValueChanged(int axis, float newValue, float oldValue);
void buttonStateChanged(int button, float newValue, float oldValue);
private:
QString _name;
QVector<float> _axes;
QVector<bool> _buttons;
#ifdef HAVE_SDL
SDL_Joystick* _sdlJoystick;
#endif
};
#endif // hifi_JoystickTracker_h

View file

@ -1,66 +0,0 @@
//
// JoystickManager.cpp
// interface/src/devices
//
// Created by Andrzej Kapolka on 5/15/14.
// 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 <limits>
#include <QtDebug>
#include <glm/glm.hpp>
#include <PerfStat.h>
#include "JoystickManager.h"
using namespace std;
JoystickManager::JoystickManager() {
#ifdef HAVE_SDL
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);
}
}
#endif
}
JoystickManager::~JoystickManager() {
#ifdef HAVE_SDL
foreach (SDL_Joystick* joystick, _joysticks) {
SDL_JoystickClose(joystick);
}
SDL_Quit();
#endif
}
void JoystickManager::update() {
#ifdef HAVE_SDL
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();
const float DEAD_ZONE = 0.1f;
state.axes[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);
}
}
#endif
}

View file

@ -1,53 +0,0 @@
//
// JoystickManager.h
// interface/src/devices
//
// Created by Andrzej Kapolka on 5/15/14.
// 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_JoystickManager_h
#define hifi_JoystickManager_h
#include <QObject>
#include <QVector>
#ifdef HAVE_SDL
#include <SDL.h>
#undef main
#endif
class JoystickState;
/// Handles joystick input through SDL.
class JoystickManager : public QObject {
Q_OBJECT
public:
JoystickManager();
virtual ~JoystickManager();
const QVector<JoystickState>& getJoystickStates() const { return _joystickStates; }
void update();
private:
QVector<JoystickState> _joystickStates;
#ifdef HAVE_SDL
QVector<SDL_Joystick*> _joysticks;
#endif
};
class JoystickState {
public:
QString name;
QVector<float> axes;
QVector<bool> buttons;
};
#endif // hifi_JoystickManager_h

View file

@ -17,6 +17,7 @@
#include "Application.h"
#include "PrioVR.h"
#include "scripting/JoystickScriptingInterface.h"
#include "ui/TextRenderer.h"
#ifdef HAVE_PRIOVR
@ -61,18 +62,22 @@ static void setPalm(float deltaTime, int index) {
palm->setActive(true);
// Read controller buttons and joystick into the hand
if (!Application::getInstance()->getJoystickManager()->getJoystickStates().isEmpty()) {
const JoystickState& state = Application::getInstance()->getJoystickManager()->getJoystickStates().at(0);
if (state.axes.size() >= 4 && state.buttons.size() >= 4) {
const QString PRIO_JOYSTICK_NAME = "PrioVR";
Joystick* prioJoystick = JoystickScriptingInterface::getInstance().joystickWithName(PRIO_JOYSTICK_NAME);
if (prioJoystick) {
const QVector<float> axes = prioJoystick->getAxes();
const QVector<bool> buttons = prioJoystick->getButtons();
if (axes.size() >= 4 && buttons.size() >= 4) {
if (index == LEFT_HAND_INDEX) {
palm->setControllerButtons(state.buttons.at(1) ? BUTTON_FWD : 0);
palm->setTrigger(state.buttons.at(0) ? 1.0f : 0.0f);
palm->setJoystick(state.axes.at(0), -state.axes.at(1));
palm->setControllerButtons(buttons[1] ? BUTTON_FWD : 0);
palm->setTrigger(buttons[0] ? 1.0f : 0.0f);
palm->setJoystick(axes[0], -axes[1]);
} else {
palm->setControllerButtons(state.buttons.at(3) ? BUTTON_FWD : 0);
palm->setTrigger(state.buttons.at(2) ? 1.0f : 0.0f);
palm->setJoystick(state.axes.at(2), -state.axes.at(3));
palm->setControllerButtons(buttons[3] ? BUTTON_FWD : 0);
palm->setTrigger(buttons[2] ? 1.0f : 0.0f);
palm->setJoystick(axes[2], -axes[3]);
}
}
}

View file

@ -0,0 +1,84 @@
//
// JoystickScriptingInterface.cpp
// interface/src/devices
//
// Created by Andrzej Kapolka on 5/15/14.
// 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 <QtDebug>
#ifdef HAVE_SDL
#include <SDL.h>
#undef main
#endif
#include <PerfStat.h>
#include "JoystickScriptingInterface.h"
JoystickScriptingInterface& JoystickScriptingInterface::getInstance() {
static JoystickScriptingInterface sharedInstance;
return sharedInstance;
}
JoystickScriptingInterface::JoystickScriptingInterface() :
_openJoysticks(),
_availableDeviceNames()
{
#ifdef HAVE_SDL
SDL_Init(SDL_INIT_JOYSTICK);
int joystickCount = SDL_NumJoysticks();
for (int i = 0; i < joystickCount; i++) {
_availableDeviceNames << SDL_JoystickName(i);
}
#endif
}
JoystickScriptingInterface::~JoystickScriptingInterface() {
qDeleteAll(_openJoysticks);
#ifdef HAVE_SDL
SDL_Quit();
#endif
}
void JoystickScriptingInterface::update() {
#ifdef HAVE_SDL
PerformanceTimer perfTimer("JoystickScriptingInterface::update");
SDL_JoystickUpdate();
foreach(Joystick* joystick, _openJoysticks) {
joystick->update();
}
#endif
}
Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) {
Joystick* matchingJoystick = _openJoysticks.value(name);
#ifdef HAVE_SDL
if (!matchingJoystick) {
// we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists
int joystickCount = SDL_NumJoysticks();
for (int i = 0; i < joystickCount; i++) {
if (SDL_JoystickName(i) == name) {
matchingJoystick = _openJoysticks.insert(name, new Joystick(name, SDL_JoystickOpen(i))).value();
break;
}
}
qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer.";
}
#endif
return matchingJoystick;
}

View file

@ -0,0 +1,43 @@
//
// JoystickScriptingInterface.h
// interface/src/devices
//
// Created by Andrzej Kapolka on 5/15/14.
// 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_JoystickScriptingInterface_h
#define hifi_JoystickScriptingInterface_h
#include <QObject>
#include <QVector>
#include "devices/Joystick.h"
/// Handles joystick input through SDL.
class JoystickScriptingInterface : public QObject {
Q_OBJECT
Q_PROPERTY(QStringList availableJoystickNames READ getAvailableJoystickNames)
public:
static JoystickScriptingInterface& getInstance();
const QStringList& getAvailableJoystickNames() const { return _availableDeviceNames; }
void update();
public slots:
Joystick* joystickWithName(const QString& name);
private:
JoystickScriptingInterface();
~JoystickScriptingInterface();
QMap<QString, Joystick*> _openJoysticks;
QStringList _availableDeviceNames;
};
#endif // hifi_JoystickScriptingInterface_h

View file

@ -72,11 +72,11 @@ 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());
}
@ -277,7 +277,7 @@ void ScriptEngine::init() {
globalObject().setProperty("LocalVoxels", localVoxelsValue);
qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue);
qScriptRegisterMetaType( this, injectorToScriptValueInputController, injectorFromScriptValueInputController);
qScriptRegisterMetaType(this, inputControllerToScriptValue, inputControllerFromScriptValue);
qScriptRegisterMetaType(this, animationDetailsToScriptValue, animationDetailsFromScriptValue);