mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 08:04:01 +02:00
move JoystickManager to JoystickScriptingInterface, add Joystick class for SDL Joystick
This commit is contained in:
parent
900daf48eb
commit
00b0806c4d
13 changed files with 217 additions and 219 deletions
|
@ -10,15 +10,8 @@
|
|||
// 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")
|
||||
joysticks = Joysticks.availableJoystickNames;
|
||||
|
||||
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);
|
||||
for (i = 0; i < joysticks.length; i++) {
|
||||
print("Joystick " + i + " is " + joysticks[i]);
|
||||
}
|
|
@ -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"
|
||||
|
@ -2155,7 +2156,7 @@ void Application::update(float deltaTime) {
|
|||
updateFaceshift();
|
||||
updateVisage();
|
||||
_sixenseManager.update(deltaTime);
|
||||
_joystickManager.update();
|
||||
JoystickScriptingInterface::getInstance().update();
|
||||
_prioVR.update(deltaTime);
|
||||
|
||||
}
|
||||
|
@ -3847,6 +3848,8 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser
|
|||
scriptEngine->registerGlobalObject("GlobalServices", GlobalServicesScriptingInterface::getInstance());
|
||||
|
||||
scriptEngine->registerGlobalObject("AvatarManager", &_avatarManager);
|
||||
|
||||
scriptEngine->registerGlobalObject("Joysticks", &JoystickScriptingInterface::getInstance());
|
||||
|
||||
#ifdef HAVE_RTMIDI
|
||||
scriptEngine->registerGlobalObject("MIDI", &MIDIManager::getInstance());
|
||||
|
|
|
@ -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
|
||||
|
|
60
interface/src/devices/Joystick.cpp
Normal file
60
interface/src/devices/Joystick.cpp
Normal 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
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// JoystickInputController.h
|
||||
// Joystick.h
|
||||
// interface/src/devices
|
||||
//
|
||||
// Created by Stephen Birarda on 2014-09-23.
|
||||
|
@ -9,13 +9,18 @@
|
|||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_JoystickInputController_h
|
||||
#define hifi_JoystickInputController_h
|
||||
#ifndef hifi_Joystick_h
|
||||
#define hifi_Joystick_h
|
||||
|
||||
#include <qobject.h>
|
||||
#include <qvector.h>
|
||||
|
||||
class JoystickInputController : public QObject {
|
||||
#ifdef HAVE_SDL
|
||||
#include <SDL.h>
|
||||
#undef main
|
||||
#endif
|
||||
|
||||
class Joystick : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QString name READ getName)
|
||||
|
@ -23,28 +28,31 @@ class JoystickInputController : public QObject {
|
|||
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);
|
||||
Joystick();
|
||||
~Joystick();
|
||||
|
||||
#ifdef HAVE_SDL
|
||||
Joystick(const QString& name, SDL_Joystick* sdlJoystick);
|
||||
#endif
|
||||
|
||||
void update();
|
||||
|
||||
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(); }
|
||||
|
||||
signals:
|
||||
void axisValueChanged(int axis, float newValue, float oldValue);
|
||||
void buttonStateChanged(int button, float newValue, float oldValue);
|
||||
private:
|
||||
void swap(JoystickInputController& otherJoystickController);
|
||||
|
||||
QString _name;
|
||||
QVector<float> _axes;
|
||||
QVector<bool> _buttons;
|
||||
|
||||
#ifdef HAVE_SDL
|
||||
SDL_Joystick* _sdlJoystick;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // hifi_JoystickTracker_h
|
|
@ -1,67 +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* sdlJoystick = SDL_JoystickOpen(i);
|
||||
if (sdlJoystick) {
|
||||
_sdlJoysticks.append(sdlJoystick);
|
||||
|
||||
JoystickInputController controller(SDL_JoystickName(i),
|
||||
SDL_JoystickNumAxes(sdlJoystick), SDL_JoystickNumButtons(sdlJoystick));
|
||||
_joysticks.append(controller);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
JoystickManager::~JoystickManager() {
|
||||
#ifdef HAVE_SDL
|
||||
foreach (SDL_Joystick* sdlJoystick, _sdlJoysticks) {
|
||||
SDL_JoystickClose(sdlJoystick);
|
||||
}
|
||||
SDL_Quit();
|
||||
#endif
|
||||
}
|
||||
|
||||
void JoystickManager::update() {
|
||||
#ifdef HAVE_SDL
|
||||
PerformanceTimer perfTimer("joystick");
|
||||
SDL_JoystickUpdate();
|
||||
|
||||
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;
|
||||
joystick.updateAxis(j, glm::abs(value) < DEAD_ZONE ? 0.0f : value);
|
||||
}
|
||||
for (int j = 0; j < joystick.getNumButtons(); j++) {
|
||||
joystick.updateButton(j, SDL_JoystickGetButton(sdlJoystick, j));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -1,47 +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
|
||||
|
||||
#include "JoystickInputController.h"
|
||||
|
||||
class JoystickState;
|
||||
|
||||
/// Handles joystick input through SDL.
|
||||
class JoystickManager : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
JoystickManager();
|
||||
virtual ~JoystickManager();
|
||||
|
||||
QVector<JoystickInputController>& getJoysticks() { return _joysticks; }
|
||||
|
||||
void update();
|
||||
|
||||
private:
|
||||
QVector<JoystickInputController> _joysticks;
|
||||
|
||||
#ifdef HAVE_SDL
|
||||
QVector<SDL_Joystick*> _sdlJoysticks;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // hifi_JoystickManager_h
|
|
@ -297,17 +297,6 @@ 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());
|
||||
}
|
||||
|
|
|
@ -113,7 +113,6 @@ public slots:
|
|||
|
||||
/// Factory to create an InputController
|
||||
virtual AbstractInputController* createInputController(const QString& deviceName, const QString& tracker);
|
||||
JoystickInputController* joystick() const;
|
||||
|
||||
virtual void releaseInputController(AbstractInputController* input);
|
||||
|
||||
|
|
82
interface/src/scripting/JoystickScriptingInterface.cpp
Normal file
82
interface/src/scripting/JoystickScriptingInterface.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
//
|
||||
// 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 <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);
|
||||
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.";
|
||||
}
|
||||
|
||||
return matchingJoystick;
|
||||
}
|
||||
|
||||
|
43
interface/src/scripting/JoystickScriptingInterface.h
Normal file
43
interface/src/scripting/JoystickScriptingInterface.h
Normal 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
|
|
@ -1,52 +0,0 @@
|
|||
//
|
||||
// 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);
|
||||
}
|
|
@ -35,7 +35,6 @@
|
|||
#include "AnimationObject.h"
|
||||
#include "ArrayBufferViewClass.h"
|
||||
#include "DataViewClass.h"
|
||||
#include "JoystickInputController.h"
|
||||
#include "MenuItemProperties.h"
|
||||
#include "MIDIEvent.h"
|
||||
#include "LocalVoxels.h"
|
||||
|
@ -81,14 +80,6 @@ void inputControllerFromScriptValue(const QScriptValue &object, AbstractInputCon
|
|||
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) :
|
||||
|
||||
|
@ -287,7 +278,6 @@ void ScriptEngine::init() {
|
|||
|
||||
qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue);
|
||||
qScriptRegisterMetaType(this, inputControllerToScriptValue, inputControllerFromScriptValue);
|
||||
qScriptRegisterMetaType(this, joystickToScriptValue, joystickFromScriptValue);
|
||||
|
||||
qScriptRegisterMetaType(this, animationDetailsToScriptValue, animationDetailsFromScriptValue);
|
||||
|
||||
|
|
Loading…
Reference in a new issue