| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include "qlogicaspect.h" |
| 6 | #include "qlogicaspect_p.h" |
| 7 | |
| 8 | #include <Qt3DLogic/qframeaction.h> |
| 9 | #include <Qt3DCore/qnode.h> |
| 10 | #include <QtCore/QThread> |
| 11 | #include <QtGui/QWindow> |
| 12 | |
| 13 | #include <Qt3DLogic/private/executor_p.h> |
| 14 | #include <Qt3DLogic/private/handler_p.h> |
| 15 | #include <Qt3DLogic/private/manager_p.h> |
| 16 | #include <Qt3DCore/private/qchangearbiter_p.h> |
| 17 | #include <Qt3DCore/private/qscene_p.h> |
| 18 | #include <Qt3DCore/private/qservicelocator_p.h> |
| 19 | |
| 20 | QT_BEGIN_NAMESPACE |
| 21 | |
| 22 | |
| 23 | namespace Qt3DLogic { |
| 24 | |
| 25 | using namespace Qt3DCore; |
| 26 | |
| 27 | /*! |
| 28 | \class Qt3DLogic::QLogicAspect |
| 29 | \inherits Qt3DCore::QAbstractAspect |
| 30 | \inmodule Qt3DLogic |
| 31 | \brief Responsible for handling frame synchronization jobs. |
| 32 | \since 5.7 |
| 33 | */ |
| 34 | |
| 35 | QLogicAspectPrivate::QLogicAspectPrivate() |
| 36 | : QAbstractAspectPrivate() |
| 37 | , m_time(0) |
| 38 | , m_manager(new Logic::Manager) |
| 39 | , m_executor(new Logic::Executor) |
| 40 | , m_callbackJob(new Logic::CallbackJob) |
| 41 | { |
| 42 | m_callbackJob->setManager(m_manager.data()); |
| 43 | m_manager->setExecutor(m_executor.data()); |
| 44 | } |
| 45 | |
| 46 | void QLogicAspectPrivate::onEngineAboutToShutdown() |
| 47 | { |
| 48 | m_executor->setScene(nullptr); |
| 49 | } |
| 50 | |
| 51 | void QLogicAspectPrivate::registerBackendTypes() |
| 52 | { |
| 53 | Q_Q(QLogicAspect); |
| 54 | q->registerBackendType<QFrameAction>(functor: QBackendNodeMapperPtr(new Logic::HandlerFunctor(m_manager.data()))); |
| 55 | } |
| 56 | |
| 57 | /*! |
| 58 | Constructs a new Qt3DLogic::QLogicAspect instance with \a parent. |
| 59 | */ |
| 60 | QLogicAspect::QLogicAspect(QObject *parent) |
| 61 | : QLogicAspect(*new QLogicAspectPrivate(), parent) {} |
| 62 | |
| 63 | /*! \internal */ |
| 64 | QLogicAspect::QLogicAspect(QLogicAspectPrivate &dd, QObject *parent) |
| 65 | : QAbstractAspect(dd, parent) |
| 66 | { |
| 67 | Q_D(QLogicAspect); |
| 68 | setObjectName(QStringLiteral("Logic Aspect")); |
| 69 | d->registerBackendTypes(); |
| 70 | d_func()->m_manager->setLogicAspect(this); |
| 71 | } |
| 72 | |
| 73 | /*! \internal */ |
| 74 | QLogicAspect::~QLogicAspect() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | /*! \internal */ |
| 79 | std::vector<QAspectJobPtr> QLogicAspect::jobsToExecute(qint64 time) |
| 80 | { |
| 81 | Q_D(QLogicAspect); |
| 82 | const qint64 deltaTime = time - d->m_time; |
| 83 | const float dt = static_cast<float>(deltaTime) / 1.0e9; |
| 84 | d->m_manager->setDeltaTime(dt); |
| 85 | d->m_time = time; |
| 86 | |
| 87 | // Create jobs that will get executed by the threadpool |
| 88 | if (d->m_manager->hasFrameActions()) |
| 89 | return {d->m_callbackJob}; |
| 90 | |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | /*! \internal */ |
| 95 | void QLogicAspect::onRegistered() |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | /*! \internal */ |
| 100 | void QLogicAspect::onEngineStartup() |
| 101 | { |
| 102 | Q_D(QLogicAspect); |
| 103 | d->m_executor->setScene(d->m_arbiter->scene()); |
| 104 | } |
| 105 | |
| 106 | } // namespace Qt3DLogic |
| 107 | |
| 108 | QT_END_NAMESPACE |
| 109 | |
| 110 | QT3D_REGISTER_NAMESPACED_ASPECT("logic", QT_PREPEND_NAMESPACE(Qt3DLogic), QLogicAspect) |
| 111 | |
| 112 | #include "moc_qlogicaspect.cpp" |
| 113 |
