diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2016-02-25 18:32:01 +0100 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2016-02-29 11:12:57 +0000 |
commit | 8a30f6391791e468a680834d1e445102e6fe4d47 (patch) | |
tree | 2524bdb58338e0c7063fcd8172f338594946f4cd /examples | |
parent | 3609ef317c5b454548b25d31100fd3d2090a6e9d (diff) |
Inputs overhauled
- Remove QAxisActionHandler
- Remove AxisAxtionHandler, AxisActionPayload, UpdateHandlerJob
- Make QLogicalDevice a QComponent
- QAction sending action active update to frontend
- QAxis sending axis value update to frontend
- QFirstPersonCameraController rewritten in a more efficient manner
Change-Id: I09a83f95b97238e4393a88103f76d8ba93e1403d
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'examples')
4 files changed, 55 insertions, 203 deletions
diff --git a/examples/qt3d/examples-common/FirstPersonCameraController.qml b/examples/qt3d/examples-common/FirstPersonCameraController.qml index 81c20d71f..6c805a5bf 100644 --- a/examples/qt3d/examples-common/FirstPersonCameraController.qml +++ b/examples/qt3d/examples-common/FirstPersonCameraController.qml @@ -63,13 +63,13 @@ Entity { QtObject { id: d readonly property vector3d firstPersonUp: Qt.vector3d(0, 1, 0) - property bool leftMouseButtonPressed: false - property real vx: 0; - property real vy: 0; - property real vz: 0; - property real dx: 0 - property real dy: 0 - property bool fineMotion: false + readonly property bool leftMouseButtonPressed: leftMouseButtonAction.active + readonly property real vx: txAxis.value * linearSpeed; + readonly property real vy: tyAxis.value * linearSpeed; + readonly property real vz: tzAxis.value * linearSpeed; + readonly property real dx: rxAxis.value * lookSpeed + readonly property real dy: ryAxis.value * lookSpeed + readonly property bool fineMotion: fineMotionAction.active } KeyboardController { @@ -81,176 +81,83 @@ Entity { sensitivity: d.fineMotion ? 0.01 : 0.1 } - LogicalDevice { - id: cameraControlDevice + components: [ - actions: [ - Action { - name: "LMB" - inputs: [ + LogicalDevice { + actions: [ + Action { + id: leftMouseButtonAction ActionInput { sourceDevice: mouseSourceDevice keys: [MouseController.Left] } - ] - }, - Action { - name: "fineMotion" - inputs: [ + }, + Action { + id: fineMotionAction ActionInput { sourceDevice: keyboardSourceDevice keys: [Qt.Key_Shift] } - ] - } - ] // actions + } + ] // actions - axes: [ - // Rotation - Axis { - name: "RX" - inputs: [ + axes: [ + // Rotation + Axis { + id: rxAxis AxisInput { sourceDevice: mouseSourceDevice axis: MouseController.X } - ] - }, - Axis { - name: "RY" - inputs: [ + }, + Axis { + id: ryAxis AxisInput { sourceDevice: mouseSourceDevice axis: MouseController.Y } - ] - }, - // Translation - Axis { - name: "TX" - inputs: [ + }, + // Translation + Axis { + id: txAxis AxisInput { sourceDevice: keyboardSourceDevice keys: [Qt.Key_Left] scale: -1.0 - }, + } AxisInput { sourceDevice: keyboardSourceDevice keys: [Qt.Key_Right] scale: 1.0 } - ] - }, - Axis { - name: "TZ" - inputs: [ + }, + Axis { + id: tzAxis AxisInput { sourceDevice: keyboardSourceDevice keys: [Qt.Key_Up] scale: 1.0 - }, + } AxisInput { sourceDevice: keyboardSourceDevice keys: [Qt.Key_Down] scale: -1.0 } - ] - }, - Axis { - name: "TY" - inputs: [ + }, + Axis { + id: tyAxis AxisInput { sourceDevice: keyboardSourceDevice keys: [Qt.Key_PageUp] scale: 1.0 - }, + } AxisInput { sourceDevice: keyboardSourceDevice keys: [Qt.Key_PageDown] scale: -1.0 } - ] - } - ] // axes - } - - components: [ - AxisActionHandler { - id: handler - logicalDevice: cameraControlDevice - - onAxisValueChanged: { - - switch (name) { - - case "TX": { - d.vx = axisValue * linearSpeed - break; - } - - case "TY": { - d.vy = axisValue * linearSpeed - break; - } - - case "TZ": { - d.vz = axisValue * linearSpeed - break; - } - - case "RX": { - d.dx = axisValue; - break; - } - - case "RY": { - d.dy = axisValue; - break; - } - case "MouseWheel": { - d.zoom = axisValue; - break } - - } - } - - onActionStarted: { - - switch (name) { - - case "LMB": { - d.leftMouseButtonPressed = true; - break; - } - - case "fineMotion": { - console.log("fineMotion started") - d.fineMotion = true; - break; - } - - } - - } - - onActionFinished: { - - switch (name) { - - case "LMB": { - d.leftMouseButtonPressed = false; - break; - } - - case "fineMotion": { - console.log("fineMotion finished") - d.fineMotion = false; - break; - } - - } - } + ] // axes }, LogicComponent { @@ -260,8 +167,8 @@ Entity { root.camera.translate(Qt.vector3d(d.vx, d.vy, d.vz).times(dt)) if (d.leftMouseButtonPressed) { - root.camera.pan(root.lookSpeed * d.dx * dt, d.firstPersonUp) - root.camera.tilt(root.lookSpeed * d.dy * dt) + root.camera.pan(d.dx * dt, d.firstPersonUp) + root.camera.tilt(d.dy * dt) } } } diff --git a/examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp b/examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp index 1a06fb64e..dd12ec344 100644 --- a/examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp +++ b/examples/qt3d/examples-common/qfirstpersoncameracontroller.cpp @@ -56,7 +56,6 @@ #include <Qt3DInput/QAction> #include <Qt3DInput/QActionInput> #include <Qt3DInput/QLogicalDevice> -#include <Qt3DInput/QAxisActionHandler> #include <Qt3DInput/QKeyboardController> #include <Qt3DInput/QMouseController> #include <Qt3DLogic/QLogicComponent> @@ -89,12 +88,9 @@ QFirstPersonCameraControllerPrivate::QFirstPersonCameraControllerPrivate() , m_keyboardController(new QKeyboardController()) , m_mouseController(new QMouseController()) , m_logicalDevice(new QLogicalDevice()) - , m_axisActionHandler(new QAxisActionHandler()) , m_logicComponent(new Qt3DLogic::QLogicComponent()) , m_linearSpeed(10.0f) , m_lookSpeed(180.0f) - , m_fineMotion(false) - , m_leftMouseButtonPressed(false) , m_firstPersonUp(QVector3D(0.0f, 1.0f, 0.0f)) {} @@ -106,13 +102,11 @@ void QFirstPersonCameraControllerPrivate::init() m_leftMouseButtonInput->setKeys(QVariantList() << QMouseController::Left); m_leftMouseButtonInput->setSourceDevice(m_mouseController); m_leftMouseButtonAction->addInput(m_leftMouseButtonInput); - m_leftMouseButtonAction->setName(QStringLiteral("LMB")); // Fine Motion Action m_fineMotionKeyInput->setKeys(QVariantList() << Qt::Key_Shift); m_fineMotionKeyInput->setSourceDevice(m_keyboardController); m_fineMotionAction->addInput(m_fineMotionKeyInput); - m_fineMotionAction->setName(QStringLiteral("fineMotion")); //// Axes @@ -120,13 +114,11 @@ void QFirstPersonCameraControllerPrivate::init() m_mouseRxInput->setAxis(QMouseController::X); m_mouseRxInput->setSourceDevice(m_mouseController); m_rxAxis->addInput(m_mouseRxInput); - m_rxAxis->setName(QStringLiteral("RX")); // Mouse Y m_mouseRyInput->setAxis(QMouseController::Y); m_mouseRyInput->setSourceDevice(m_mouseController); m_ryAxis->addInput(m_mouseRyInput); - m_ryAxis->setName(QStringLiteral("RY")); // Keyboard Pos Tx m_keyboardTxPosInput->setKeys(QVariantList() << Qt::Key_Right); @@ -164,10 +156,6 @@ void QFirstPersonCameraControllerPrivate::init() m_keyboardTzNegInput->setSourceDevice(m_keyboardController); m_tzAxis->addInput(m_keyboardTzNegInput); - m_txAxis->setName(QStringLiteral("TX")); - m_tyAxis->setName(QStringLiteral("TY")); - m_tzAxis->setName(QStringLiteral("TZ")); - //// Logical Device m_logicalDevice->addAction(m_fineMotionAction); @@ -179,68 +167,28 @@ void QFirstPersonCameraControllerPrivate::init() m_logicalDevice->addAxis(m_tzAxis); Q_Q(QFirstPersonCameraController); - - //// AxisActionHandler - - QObject::connect(m_axisActionHandler, SIGNAL(actionStarted(QString)), - q, SLOT(_q_onActionStarted(QString))); - QObject::connect(m_axisActionHandler, SIGNAL(actionFinished(QString)), - q, SLOT(_q_onActionFinished(QString))); - QObject::connect(m_axisActionHandler, SIGNAL(axisValueChanged(QString,float)), - q, SLOT(_q_onAxisValueChanged(QString,float))); - - m_axisActionHandler->setLogicalDevice(m_logicalDevice); - //// LogicComponent QObject::connect(m_logicComponent, SIGNAL(frameUpdate(float)), q, SLOT(_q_onFrameUpdate(float))); q->addComponent(m_logicComponent); - q->addComponent(m_axisActionHandler); + q->addComponent(m_logicalDevice); } void QFirstPersonCameraControllerPrivate::_q_onFrameUpdate(float dt) { if (m_camera != Q_NULLPTR) { - m_camera->translate(m_vxyz * dt); - if (m_leftMouseButtonPressed) { - m_camera->pan(m_rxyz.x() * dt, m_firstPersonUp); - m_camera->tilt(m_rxyz.y() * dt); + m_camera->translate(QVector3D(m_txAxis->value() * m_linearSpeed, + m_tyAxis->value() * m_linearSpeed, + m_tzAxis->value() * m_linearSpeed) * dt); + if (m_leftMouseButtonAction->isActive()) { + m_camera->pan(m_rxAxis->value() * m_lookSpeed * dt, m_firstPersonUp); + m_camera->tilt(m_ryAxis->value() * m_lookSpeed * dt); } } } -void QFirstPersonCameraControllerPrivate::_q_onAxisValueChanged(QString name, float value) -{ - if (name == QStringLiteral("RX")) - m_rxyz.setX(value * m_lookSpeed); - else if (name == QStringLiteral("RY")) - m_rxyz.setY(value * m_lookSpeed); - else if (name == QStringLiteral("TX")) - m_vxyz.setX(value * m_linearSpeed); - else if (name == QStringLiteral("TY")) - m_vxyz.setY(value * m_linearSpeed); - else if (name == QStringLiteral("TZ")) - m_vxyz.setZ(value * m_linearSpeed); -} - -void QFirstPersonCameraControllerPrivate::_q_onActionStarted(QString name) -{ - if (name == QStringLiteral("LMB")) - m_leftMouseButtonPressed = true; - else if (name == QStringLiteral("fineMotion")) - m_fineMotion = true; -} - -void QFirstPersonCameraControllerPrivate::_q_onActionFinished(QString name) -{ - if (name == QStringLiteral("LMB")) - m_leftMouseButtonPressed = false; - else if (name == QStringLiteral("fineMotion")) - m_fineMotion = false; -} - QFirstPersonCameraController::QFirstPersonCameraController(Qt3DCore::QNode *parent) : Qt3DCore::QEntity(*new QFirstPersonCameraControllerPrivate, parent) { diff --git a/examples/qt3d/examples-common/qfirstpersoncameracontroller.h b/examples/qt3d/examples-common/qfirstpersoncameracontroller.h index dfc93cc7a..be1496936 100644 --- a/examples/qt3d/examples-common/qfirstpersoncameracontroller.h +++ b/examples/qt3d/examples-common/qfirstpersoncameracontroller.h @@ -90,9 +90,6 @@ Q_SIGNALS: private: Q_DECLARE_PRIVATE(QFirstPersonCameraController) Q_PRIVATE_SLOT(d_func(), void _q_onFrameUpdate(float)) - Q_PRIVATE_SLOT(d_func(), void _q_onAxisValueChanged(QString, float)) - Q_PRIVATE_SLOT(d_func(), void _q_onActionStarted(QString)) - Q_PRIVATE_SLOT(d_func(), void _q_onActionFinished(QString)) }; } // Qt3DInput diff --git a/examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h b/examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h index 29120e431..e1d53fa95 100644 --- a/examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h +++ b/examples/qt3d/examples-common/qfirstpersoncameracontroller_p.h @@ -84,15 +84,19 @@ public: void init(); Qt3DRender::QCamera *m_camera; + QAction *m_leftMouseButtonAction; QAction *m_fineMotionAction; + QAxis *m_rxAxis; QAxis *m_ryAxis; QAxis *m_txAxis; QAxis *m_tyAxis; QAxis *m_tzAxis; + QActionInput *m_leftMouseButtonInput; QActionInput *m_fineMotionKeyInput; + QAxisInput *m_mouseRxInput; QAxisInput *m_mouseRyInput; QAxisInput *m_keyboardTxPosInput; @@ -101,23 +105,19 @@ public: QAxisInput *m_keyboardTxNegInput; QAxisInput *m_keyboardTyNegInput; QAxisInput *m_keyboardTzNegInput; + QKeyboardController *m_keyboardController; QMouseController *m_mouseController; + QLogicalDevice *m_logicalDevice; - QAxisActionHandler *m_axisActionHandler; + Qt3DLogic::QLogicComponent *m_logicComponent; + float m_linearSpeed; float m_lookSpeed; - QVector3D m_vxyz; - bool m_fineMotion; - QVector3D m_rxyz; - bool m_leftMouseButtonPressed; QVector3D m_firstPersonUp; void _q_onFrameUpdate(float); - void _q_onAxisValueChanged(QString, float); - void _q_onActionStarted(QString); - void _q_onActionFinished(QString); Q_DECLARE_PUBLIC(QFirstPersonCameraController) }; |