diff --git a/libraries/controllers/src/controllers/NewControllerScriptingInterface.cpp b/libraries/controllers/src/controllers/NewControllerScriptingInterface.cpp index b8d5c851f5..bc915ba1a6 100644 --- a/libraries/controllers/src/controllers/NewControllerScriptingInterface.cpp +++ b/libraries/controllers/src/controllers/NewControllerScriptingInterface.cpp @@ -1,23 +1,36 @@ #include "NewControllerScriptingInterface.h" #include +#include -#include - -#include "GLMHelpers.h" +#include +#include +#include #include "impl/MappingBuilderProxy.h" namespace Controllers { + void NewControllerScriptingInterface::update() { + auto userInputMapper = DependencyManager::get(); + static float last = secTimestampNow(); + float now = secTimestampNow(); + userInputMapper->update(now - last); + last = now; + } -QObject* NewControllerScriptingInterface::newMapping() { - qDebug() << "Creating new Mapping proxy"; - return new MappingBuilderProxy(std::make_shared()); -} + QObject* NewControllerScriptingInterface::newMapping() { + qDebug() << "Creating new Mapping proxy"; + return new MappingBuilderProxy(std::make_shared()); + } -float NewControllerScriptingInterface::getValue(const QScriptValue& source) { - return 0; -} + float NewControllerScriptingInterface::getValue(const int& source) { + //UserInputMapper::Input input; input._id = source; + //auto userInputMapper = DependencyManager::get(); + //auto deviceProxy = userInputMapper->getDeviceProxy(input); + //return deviceProxy->getButton(input, 0) ? 1.0 : 0.0; + + return (sin(secTimestampNow()) + 1.0f) / 2.0f; + } } // namespace controllers diff --git a/libraries/controllers/src/controllers/NewControllerScriptingInterface.h b/libraries/controllers/src/controllers/NewControllerScriptingInterface.h index 8730c2da89..c4c42a2245 100644 --- a/libraries/controllers/src/controllers/NewControllerScriptingInterface.h +++ b/libraries/controllers/src/controllers/NewControllerScriptingInterface.h @@ -22,8 +22,9 @@ namespace Controllers { Q_OBJECT public: + Q_INVOKABLE void update(); Q_INVOKABLE QObject* newMapping(); - Q_INVOKABLE float getValue(const QScriptValue& source); + Q_INVOKABLE float getValue(const int& source); }; } diff --git a/tests/controllers/CMakeLists.txt b/tests/controllers/CMakeLists.txt index 4e31881141..34ab4c2eba 100644 --- a/tests/controllers/CMakeLists.txt +++ b/tests/controllers/CMakeLists.txt @@ -1,13 +1,19 @@ set(TARGET_NAME controllers-test) -AUTOSCRIBE_SHADER_LIB(gpu model render-utils ) - # This is not a testcase -- just set it up as a regular hifi project setup_hifi_project(Script) set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "Tests/manual-tests/") # link in the shared libraries -link_hifi_libraries(shared script-engine input-plugins controllers) +link_hifi_libraries(shared script-engine plugins input-plugins display-plugins controllers) + + +if (WIN32) + add_dependency_external_projects(OpenVR) + find_package(OpenVR REQUIRED) + target_include_directories(${TARGET_NAME} PRIVATE ${OPENVR_INCLUDE_DIRS}) + target_link_libraries(${TARGET_NAME} ${OPENVR_LIBRARIES}) +endif() copy_dlls_beside_windows_executable() \ No newline at end of file diff --git a/tests/controllers/qml/content.qml b/tests/controllers/qml/content.qml new file mode 100644 index 0000000000..ac171ac42c --- /dev/null +++ b/tests/controllers/qml/content.qml @@ -0,0 +1,137 @@ +import QtQuick 2.1 +import QtQuick.Controls 1.0 +import QtQuick.Layouts 1.0 +import QtQuick.Dialogs 1.0 + +Rectangle { + id: root + implicitHeight: column1.height + 24 + implicitWidth: column1.width + 24 + color: "lightgray" + + property real itemSize: 128 + + Component { + id: graphTemplate + Item { + implicitHeight: canvas.height + 2 + text.height + implicitWidth: canvas.width + property string text: loadText + + Canvas { + id: canvas + width: root.itemSize; height: root.itemSize; + antialiasing: false + property int controlId: control + property real value: 0.0 + property int drawWidth: 1 + + Timer { + interval: 50; running: true; repeat: true + onTriggered: { + parent.value = NewControllers.getValue(canvas.controlId) + parent.requestPaint(); + } + } + + onPaint: { + var ctx = canvas.getContext('2d'); + ctx.save(); + + var image = ctx.getImageData(0, 0, canvas.width, canvas.height); + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.drawImage(image, -drawWidth, 0, canvas.width, canvas.height) + ctx.fillStyle = 'green' + // draw a filles rectangle + var height = canvas.height * canvas.value + ctx.fillRect(canvas.width - drawWidth, canvas.height - height, + drawWidth, height) + ctx.restore() + } + } + + Text { + id: text + text: parent.text + anchors.topMargin: 2 + anchors.horizontalCenter: canvas.horizontalCenter + anchors.top: canvas.bottom + font.pointSize: 12; + } + + } + } + + Column { + id: column1 + x: 12; y: 12 + spacing: 24 + Row { + spacing: 16 + Loader { + sourceComponent: graphTemplate; + property string loadText: "Key Left" + property int control: ControllerIds.Hardware.Keyboard2.Left + } + Loader { + sourceComponent: graphTemplate; + property string loadText: "DPad Up" + property int control: ControllerIds.Hardware.X360Controller1.DPadUp + } + /* + Loader { + sourceComponent: graphTemplate; + property string loadText: "Yaw Left" + property int control: ControllerIds.Actions.YAW_LEFT + } + Loader { + sourceComponent: graphTemplate; + property string loadText: "Yaw Left" + property int control: ControllerIds.Actions.YAW_LEFT + } +*/ + +// Loader { sourceComponent: graphTemplate; } +// Loader { sourceComponent: graphTemplate; } +// Loader { sourceComponent: graphTemplate; } + } + /* + Row { + spacing: 16 + Loader { sourceComponent: graphTemplate; } + Loader { sourceComponent: graphTemplate; } + Loader { sourceComponent: graphTemplate; } + Loader { sourceComponent: graphTemplate; } + } + Row { + spacing: 16 + Loader { sourceComponent: graphTemplate; } + Loader { sourceComponent: graphTemplate; } + Loader { sourceComponent: graphTemplate; } + Loader { sourceComponent: graphTemplate; } + } + */ + + + Button { + text: "Go!" + onClicked: { + // + +// var newMapping = NewControllers.newMapping(); +// console.log("Mapping Object " + newMapping); +// var routeBuilder = newMapping.from("Hello"); +// console.log("Route Builder " + routeBuilder); +// routeBuilder.clamp(0, 1).clamp(0, 1).to("Goodbye"); + } + } + + Timer { + interval: 50; running: true; repeat: true + onTriggered: { + NewControllers.update(); + } + } + + } +} diff --git a/tests/controllers/qml/main.qml b/tests/controllers/qml/main.qml index 9f660e5d35..5ed68cc1fb 100644 --- a/tests/controllers/qml/main.qml +++ b/tests/controllers/qml/main.qml @@ -2,42 +2,13 @@ import QtQuick 2.1 import QtQuick.Controls 1.0 import QtQuick.Layouts 1.0 import QtQuick.Dialogs 1.0 -import com.highfidelity.test 1.0 ApplicationWindow { id: window visible: true - AppHook { - + Loader { + id: pageLoader + source: "content.qml" } - -// NewControllers { -// id: newControllers -// } - - Rectangle { - id: page - width: 320; height: 480 - color: "lightgray" - Text { - id: helloText - text: "Hello world!" - y: 30 - anchors.horizontalCenter: page.horizontalCenter - font.pointSize: 24; font.bold: true - } - - MouseArea { - anchors.fill: parent - onClicked: { - var newMapping = NewControllers.newMapping(); - console.log("Mapping Object " + newMapping); - var routeBuilder = newMapping.from("Hello"); - console.log("Route Builder " + routeBuilder); - routeBuilder.clamp(0, 1).clamp(0, 1).to("Goodbye"); - } - } - } - } diff --git a/tests/controllers/src/main.cpp b/tests/controllers/src/main.cpp index 8311b26dab..9840038521 100644 --- a/tests/controllers/src/main.cpp +++ b/tests/controllers/src/main.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include @@ -21,6 +23,7 @@ #include #include #include +#include #include #include @@ -32,6 +35,11 @@ #include #include +#include +#include +#include +#include +#include const QString& getQmlDir() { static QString dir; @@ -55,13 +63,72 @@ public: using namespace Controllers; -int main(int argc, char** argv) { + +QString sanatizeName(const QString& name) { + QString cleanName{ name }; + cleanName.remove(QRegularExpression{ "[\\(\\)\\.\\s]" }); + return cleanName; +} + +const QVariantMap& getInputMap() { + static std::once_flag once; + static QVariantMap map; + std::call_once(once, [&] { + { + QVariantMap hardwareMap; + // Controller.Hardware.* + auto devices = DependencyManager::get()->getDevices(); + for (const auto& deviceMapping : devices) { + auto device = deviceMapping.second.get(); + auto deviceName = sanatizeName(device->getName()); + auto deviceInputs = device->getAvailabeInputs(); + QVariantMap deviceMap; + for (const auto& inputMapping : deviceInputs) { + auto input = inputMapping.first; + auto inputName = sanatizeName(inputMapping.second); + deviceMap.insert(inputName, input.getID()); + } + hardwareMap.insert(deviceName, deviceMap); + } + map.insert("Hardware", hardwareMap); + } + + // Controller.Actions.* + { + QVariantMap actionMap; + auto actionNames = DependencyManager::get()->getActionNames(); + int actionNumber = 0; + for (const auto& actionName : actionNames) { + actionMap.insert(sanatizeName(actionName), actionNumber++); + } + map.insert("Actions", actionMap); + } + }); + return map; +} + +int main(int argc, char** argv) { + DependencyManager::set(); + PluginManager::getInstance()->getInputPlugins(); + + foreach(auto inputPlugin, PluginManager::getInstance()->getInputPlugins()) { + QString name = inputPlugin->getName(); + auto userInputMapper = DependencyManager::get(); + if (name == KeyboardMouseDevice::NAME) { + auto keyboardMouseDevice = static_cast(inputPlugin.data()); // TODO: this seems super hacky + keyboardMouseDevice->registerToUserInputMapper(*userInputMapper); + keyboardMouseDevice->assignDefaultInputMapping(*userInputMapper); + } + } + // Register our component type with QML. qmlRegisterType("com.highfidelity.test", 1, 0, "AppHook"); //qmlRegisterType("com.highfidelity.test", 1, 0, "NewControllers"); QGuiApplication app(argc, argv); - QQmlApplicationEngine engine(getQmlDir() + "main.qml"); + QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("NewControllers", new NewControllerScriptingInterface()); + engine.rootContext()->setContextProperty("ControllerIds", getInputMap()); + engine.load(getQmlDir() + "main.qml"); app.exec(); return 0; }