mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 04:44:11 +02:00
more work on exposing device inputs as JavaScript named constants
This commit is contained in:
parent
37f530f97f
commit
d3ee9b0f39
5 changed files with 67 additions and 11 deletions
|
@ -11,6 +11,12 @@
|
|||
|
||||
// Assumes you only have the default keyboard connected
|
||||
|
||||
Object.keys(Controller.Hardware).forEach(function (deviceName) {
|
||||
Object.keys(Controller.Hardware[deviceName]).forEach(function (input) {
|
||||
print(deviceName + "." + input + ":" + Controller.Hardware[deviceName][input]);
|
||||
});
|
||||
});
|
||||
|
||||
// Resets every device to its default key bindings:
|
||||
Controller.resetAllDeviceBindings();
|
||||
|
||||
|
|
|
@ -383,21 +383,29 @@ glm::vec2 ControllerScriptingInterface::getViewportDimensions() const {
|
|||
return qApp->getUiSize();
|
||||
}
|
||||
|
||||
QString ControllerScriptingInterface::sanatizeName(const QString& name) {
|
||||
QString cleanName { name };
|
||||
cleanName.replace(QString(" "), QString("")).replace(QString("."), QString("")).replace(QString("("), QString("")).replace(QString(")"), QString(""));
|
||||
return cleanName;
|
||||
}
|
||||
|
||||
void ControllerScriptingInterface::wireUpControllers(ScriptEngine* engine) {
|
||||
|
||||
qDebug() << "------------------- wire up controllers --------------------------";
|
||||
///_registeredDevices
|
||||
|
||||
auto devices = DependencyManager::get<UserInputMapper>()->getDevices();
|
||||
|
||||
for(const auto& deviceMapping : devices) {
|
||||
auto device = deviceMapping.second.get();
|
||||
qDebug() << device->getName();
|
||||
auto deviceName = sanatizeName(device->getName());
|
||||
auto deviceInputs = device->getAvailabeInputs();
|
||||
for (const auto& inputMapping : deviceInputs) {
|
||||
auto input = inputMapping.first;
|
||||
auto inputName = inputMapping.second;
|
||||
qDebug() << device->getName() << "." << inputName << "["<< input.getID() <<"]";
|
||||
auto inputName = sanatizeName(inputMapping.second);
|
||||
inputName.replace(QString(" "), QString(""));
|
||||
QString deviceInputName { "Controller.Hardware." + deviceName + "." + inputName };
|
||||
engine->registerValue(deviceInputName, input.getID());
|
||||
qDebug() << deviceInputName << "[" << input.getID() << "]";
|
||||
}
|
||||
}
|
||||
qDebug() << "------------------- DONE wire up controllers --------------------------";
|
||||
|
|
|
@ -150,6 +150,8 @@ public slots:
|
|||
virtual void releaseInputController(AbstractInputController* input);
|
||||
|
||||
private:
|
||||
QString sanatizeName(const QString& name); /// makes a name clean for inclusing in JavaScript
|
||||
|
||||
const PalmData* getPrimaryPalm() const;
|
||||
const PalmData* getPalm(int palmIndex) const;
|
||||
int getNumberOfActivePalms() const;
|
||||
|
|
|
@ -277,10 +277,6 @@ void ScriptEngine::init() {
|
|||
registerAvatarTypes(this);
|
||||
registerAudioMetaTypes(this);
|
||||
|
||||
if (_controllerScriptingInterface) {
|
||||
_controllerScriptingInterface->registerControllerTypes(this);
|
||||
}
|
||||
|
||||
qScriptRegisterMetaType(this, EntityPropertyFlagsToScriptValue, EntityPropertyFlagsFromScriptValue);
|
||||
qScriptRegisterMetaType(this, EntityItemPropertiesToScriptValue, EntityItemPropertiesFromScriptValueHonorReadOnly);
|
||||
qScriptRegisterMetaType(this, EntityItemIDtoScriptValue, EntityItemIDfromScriptValue);
|
||||
|
@ -323,6 +319,43 @@ void ScriptEngine::init() {
|
|||
|
||||
// constants
|
||||
globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE)));
|
||||
|
||||
if (_controllerScriptingInterface) {
|
||||
_controllerScriptingInterface->registerControllerTypes(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) {
|
||||
if (QThread::currentThread() != thread()) {
|
||||
#ifdef THREAD_DEBUGGING
|
||||
qDebug() << "*** WARNING *** ScriptEngine::registerValue() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;
|
||||
#endif
|
||||
QMetaObject::invokeMethod(this, "registerValue",
|
||||
Q_ARG(const QString&, valueName),
|
||||
Q_ARG(QScriptValue, value));
|
||||
return;
|
||||
}
|
||||
|
||||
QStringList pathToValue = valueName.split(".");
|
||||
int partsToGo = pathToValue.length();
|
||||
QScriptValue partObject = globalObject();
|
||||
|
||||
for (const auto& pathPart : pathToValue) {
|
||||
partsToGo--;
|
||||
if (!partObject.property(pathPart).isValid()) {
|
||||
if (partsToGo > 0) {
|
||||
//QObject *object = new QObject;
|
||||
QScriptValue partValue = newArray(); //newQObject(object, QScriptEngine::ScriptOwnership);
|
||||
qDebug() << "partValue[" << pathPart<<"].isArray() :" << partValue.isArray();
|
||||
partObject.setProperty(pathPart, partValue);
|
||||
} else {
|
||||
partObject.setProperty(pathPart, value);
|
||||
}
|
||||
}
|
||||
partObject = partObject.property(pathPart);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) {
|
||||
|
@ -339,9 +372,13 @@ void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) {
|
|||
qDebug() << "ScriptEngine::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name;
|
||||
#endif
|
||||
|
||||
if (object) {
|
||||
QScriptValue value = newQObject(object);
|
||||
globalObject().setProperty(name, value);
|
||||
if (!globalObject().property(name).isValid()) {
|
||||
if (object) {
|
||||
QScriptValue value = newQObject(object);
|
||||
globalObject().setProperty(name, value);
|
||||
} else {
|
||||
globalObject().setProperty(name, QScriptValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,6 +84,9 @@ public:
|
|||
Q_INVOKABLE void registerFunction(const QString& parent, const QString& name, QScriptEngine::FunctionSignature fun,
|
||||
int numArguments = -1);
|
||||
|
||||
/// registers a global object by name
|
||||
Q_INVOKABLE void registerValue(const QString& valueName, QScriptValue value);
|
||||
|
||||
/// evaluate some code in the context of the ScriptEngine and return the result
|
||||
Q_INVOKABLE QScriptValue evaluate(const QString& program, const QString& fileName = QString(), int lineNumber = 1); // this is also used by the script tool widget
|
||||
|
||||
|
|
Loading…
Reference in a new issue