mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-29 13:42:55 +02:00
Adding the loadMapping feature
This commit is contained in:
parent
974d2e20ff
commit
20416455db
3 changed files with 53 additions and 13 deletions
examples/controllers
libraries/controllers/src/controllers
|
@ -12,7 +12,7 @@
|
|||
|
||||
// Assumes you only have the default keyboard connected
|
||||
|
||||
myFirstMapping = function() {
|
||||
/*myFirstMapping = function() {
|
||||
return {
|
||||
"name": "example",
|
||||
"channels": [
|
||||
|
@ -42,31 +42,36 @@ return {
|
|||
]
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
mySecondMapping = function() {
|
||||
return {
|
||||
"name": "example2",
|
||||
"channels": [
|
||||
{ "from": "Standard.LY", "to": "Actions.TRANSLATE_Z" },
|
||||
{ "from": "Standard.LX", "to": "Actions.YAW" },
|
||||
{ "from": "Standard.LY", "to": "Actions.TranslateZ" },
|
||||
{ "from": "Standard.LX", "to": "Actions.Yaw" },
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
//Script.include('mapping-test0.json');
|
||||
var myFirstMappingJSON = myFirstMapping();
|
||||
/*var myFirstMappingJSON = myFirstMapping();
|
||||
print('myFirstMappingJSON' + JSON.stringify(myFirstMappingJSON));
|
||||
|
||||
var mapping = Controller.parseMapping(JSON.stringify(myFirstMappingJSON));
|
||||
|
||||
Controller.enableMapping("example");
|
||||
|
||||
var myFirstMappingJSON = myFirstMapping();
|
||||
print('myFirstMappingJSON' + JSON.stringify(myFirstMappingJSON));
|
||||
Controller.enableMapping("example3");
|
||||
|
||||
var mapping = Controller.parseMapping(JSON.stringify(myFirstMappingJSON));
|
||||
var mySecondMappingJSON = mySecondMapping();
|
||||
print('mySecondMappingJSON' + JSON.stringify(mySecondMappingJSON));
|
||||
|
||||
Controller.enableMapping("example");
|
||||
var mapping2 = Controller.parseMapping(JSON.stringify(mySecondMappingJSON));
|
||||
mapping2.enable();
|
||||
|
||||
Controller.enableMapping("example2");
|
||||
*/
|
||||
var mapping3 = Controller.loadMapping("E:/Github/hifi/examples/controllers/example3.json");
|
||||
Controller.enableMapping("example3");
|
||||
|
||||
/*
|
||||
Object.keys(Controller.Standard).forEach(function (input) {
|
||||
|
|
|
@ -14,14 +14,18 @@
|
|||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QEventLoop>
|
||||
|
||||
#include <GLMHelpers.h>
|
||||
#include <DependencyManager.h>
|
||||
|
||||
#include <ResourceManager.h>
|
||||
|
||||
#include "impl/MappingBuilderProxy.h"
|
||||
#include "Logging.h"
|
||||
#include "InputDevice.h"
|
||||
|
||||
|
||||
namespace controller {
|
||||
|
||||
class VirtualEndpoint : public Endpoint {
|
||||
|
@ -176,7 +180,8 @@ namespace controller {
|
|||
QObject* ScriptingInterface::parseMapping(const QString& json) {
|
||||
|
||||
QJsonObject obj;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
QJsonParseError error;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8(), &error);
|
||||
// check validity of the document
|
||||
if (!doc.isNull()) {
|
||||
if (doc.isObject()) {
|
||||
|
@ -194,12 +199,35 @@ namespace controller {
|
|||
qDebug() << "Mapping json Document is not an object" << endl;
|
||||
}
|
||||
} else {
|
||||
qDebug() << "Invalid JSON...\n" << json << endl;
|
||||
qDebug() << "Invalid JSON...\n";
|
||||
qDebug() << error.errorString();
|
||||
qDebug() << "JSON was:\n" << json << endl;
|
||||
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
QObject* ScriptingInterface::loadMapping(const QString& jsonUrl) {
|
||||
auto request = ResourceManager::createResourceRequest(nullptr, QUrl(jsonUrl));
|
||||
if (request) {
|
||||
QEventLoop eventLoop;
|
||||
request->setCacheEnabled(false);
|
||||
connect(request, &ResourceRequest::finished, &eventLoop, &QEventLoop::quit);
|
||||
request->send();
|
||||
if (request->getState() != ResourceRequest::Finished) {
|
||||
eventLoop.exec();
|
||||
}
|
||||
|
||||
if (request->getResult() == ResourceRequest::Success) {
|
||||
return parseMapping(QString(request->getData()));
|
||||
} else {
|
||||
qDebug() << "Failed to load mapping url <" << jsonUrl << ">" << endl;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Q_INVOKABLE QObject* newMapping(const QJsonObject& json);
|
||||
|
||||
void ScriptingInterface::enableMapping(const QString& mappingName, bool enable) {
|
||||
|
@ -286,6 +314,11 @@ namespace controller {
|
|||
|
||||
for (const auto& route : routes) {
|
||||
const auto& destination = route->_destination;
|
||||
// THis could happen if the route destination failed to create
|
||||
// FIXME: Maybe do not create the route if the destination failed and avoid this case ?
|
||||
if (!destination) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (writtenEndpoints.count(destination)) {
|
||||
continue;
|
||||
|
|
|
@ -73,6 +73,8 @@ namespace controller {
|
|||
enableMapping(mappingName, false);
|
||||
}
|
||||
Q_INVOKABLE QObject* parseMapping(const QString& json);
|
||||
Q_INVOKABLE QObject* loadMapping(const QString& jsonUrl);
|
||||
|
||||
|
||||
Q_INVOKABLE bool isPrimaryButtonPressed() const;
|
||||
Q_INVOKABLE glm::vec2 getPrimaryJoystickPosition() const;
|
||||
|
|
Loading…
Reference in a new issue