Merge pull request #5650 from ZappoMan/fixCrashInController

Fix a couple crashes in the Controller scripting interface
This commit is contained in:
Brad Davis 2015-08-24 18:49:50 -07:00
commit f9f5743fd7
4 changed files with 55 additions and 13 deletions

View file

@ -469,20 +469,20 @@ int ControllerScriptingInterface::findDevice(QString name) {
return DependencyManager::get<UserInputMapper>()->findDevice(name);
}
QVector<QString> ControllerScriptingInterface::getDeviceNames() {
return DependencyManager::get<UserInputMapper>()->getDeviceNames();
}
float ControllerScriptingInterface::getActionValue(int action) {
return DependencyManager::get<UserInputMapper>()->getActionState(UserInputMapper::Action(action));
}
int ControllerScriptingInterface::findAction(QString actionName) {
auto userInputMapper = DependencyManager::get<UserInputMapper>();
auto actions = getAllActions();
for (auto action : actions) {
if (userInputMapper->getActionName(action) == actionName) {
return action;
}
}
// If the action isn't found, return -1
return -1;
return DependencyManager::get<UserInputMapper>()->findAction(actionName);
}
QVector<QString> ControllerScriptingInterface::getActionNames() const {
return DependencyManager::get<UserInputMapper>()->getActionNames();
}
InputController::InputController(int deviceTrackerId, int subTrackerId, QObject* parent) :

View file

@ -101,8 +101,10 @@ public slots:
Q_INVOKABLE virtual void resetDevice(unsigned int device);
Q_INVOKABLE virtual void resetAllDeviceBindings();
Q_INVOKABLE virtual int findDevice(QString name);
Q_INVOKABLE virtual QVector<QString> getDeviceNames();
Q_INVOKABLE virtual int findAction(QString actionName);
Q_INVOKABLE virtual QVector<QString> getActionNames() const;
virtual bool isPrimaryButtonPressed() const;
virtual glm::vec2 getPrimaryJoystickPosition() const;

View file

@ -32,6 +32,14 @@ UserInputMapper::DeviceProxy::Pointer UserInputMapper::getDeviceProxy(const Inpu
}
}
QString UserInputMapper::getDeviceName(uint16 deviceID) {
if (_registeredDevices.find(deviceID) != _registeredDevices.end()) {
return _registeredDevices[deviceID]->_name;
}
return QString("unknown");
}
void UserInputMapper::resetAllDeviceBindings() {
for (auto device : _registeredDevices) {
device.second->resetDeviceBindings();
@ -54,6 +62,16 @@ int UserInputMapper::findDevice(QString name) {
return 0;
}
QVector<QString> UserInputMapper::getDeviceNames() {
QVector<QString> result;
for (auto device : _registeredDevices) {
QString deviceName = device.second->_name.split(" (")[0];
result << deviceName;
}
return result;
}
bool UserInputMapper::addInputChannel(Action action, const Input& input, float scale) {
return addInputChannel(action, input, Input(), scale);
}
@ -217,7 +235,7 @@ void UserInputMapper::update(float deltaTime) {
}
}
QVector<UserInputMapper::Action> UserInputMapper::getAllActions() {
QVector<UserInputMapper::Action> UserInputMapper::getAllActions() const {
QVector<Action> actions;
for (auto i = 0; i < NUM_ACTIONS; i++) {
actions.append(Action(i));
@ -235,6 +253,25 @@ QVector<UserInputMapper::InputChannel> UserInputMapper::getInputChannelsForActio
return inputChannels;
}
int UserInputMapper::findAction(const QString& actionName) const {
auto actions = getAllActions();
for (auto action : actions) {
if (getActionName(action) == actionName) {
return action;
}
}
// If the action isn't found, return -1
return -1;
}
QVector<QString> UserInputMapper::getActionNames() const {
QVector<QString> result;
for (auto i = 0; i < NUM_ACTIONS; i++) {
result << _actionNames[i];
}
return result;
}
void UserInputMapper::assignDefaulActionScales() {
_actionScales[LONGITUDINAL_BACKWARD] = 1.0f; // 1m per unit
_actionScales[LONGITUDINAL_FORWARD] = 1.0f; // 1m per unit

View file

@ -123,11 +123,12 @@ public:
uint16 getFreeDeviceID() { return _nextFreeDeviceID++; }
bool registerDevice(uint16 deviceID, const DeviceProxy::Pointer& device);
DeviceProxy::Pointer getDeviceProxy(const Input& input);
QString getDeviceName(uint16 deviceID) { return _registeredDevices[deviceID]->_name; }
QString getDeviceName(uint16 deviceID);
QVector<InputPair> getAvailableInputs(uint16 deviceID) { return _registeredDevices[deviceID]->getAvailabeInputs(); }
void resetAllDeviceBindings();
void resetDevice(uint16 deviceID);
int findDevice(QString name);
QVector<QString> getDeviceNames();
// Actions are the output channels of the Mapper, that's what the InputChannel map to
// For now the Actions are hardcoded, this is bad, but we will fix that in the near future
@ -167,10 +168,12 @@ public:
std::vector<QString> _actionNames = std::vector<QString>(NUM_ACTIONS);
void createActionNames();
QVector<Action> getAllActions();
QString getActionName(Action action) { return UserInputMapper::_actionNames[(int) action]; }
QVector<Action> getAllActions() const;
QString getActionName(Action action) const { return UserInputMapper::_actionNames[(int) action]; }
float getActionState(Action action) const { return _actionStates[action]; }
PoseValue getPoseState(Action action) const { return _poseStates[action]; }
int findAction(const QString& actionName) const;
QVector<QString> getActionNames() const;
void assignDefaulActionScales();
// Add input channel to the mapper and check that all the used channels are registered.