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 | using namespace Qt3DCore; |
23 | |
24 | namespace Qt3DLogic { |
25 | |
26 | /*! |
27 | \class Qt3DLogic::QLogicAspect |
28 | \inherits Qt3DCore::QAbstractAspect |
29 | \inmodule Qt3DLogic |
30 | \brief Responsible for handling frame synchronization jobs. |
31 | \since 5.7 |
32 | */ |
33 | |
34 | QLogicAspectPrivate::QLogicAspectPrivate() |
35 | : QAbstractAspectPrivate() |
36 | , m_time(0) |
37 | , m_manager(new Logic::Manager) |
38 | , m_executor(new Logic::Executor) |
39 | , m_callbackJob(new Logic::CallbackJob) |
40 | { |
41 | m_callbackJob->setManager(m_manager.data()); |
42 | m_manager->setExecutor(m_executor.data()); |
43 | } |
44 | |
45 | void QLogicAspectPrivate::onEngineAboutToShutdown() |
46 | { |
47 | m_executor->setScene(nullptr); |
48 | } |
49 | |
50 | void QLogicAspectPrivate::registerBackendTypes() |
51 | { |
52 | Q_Q(QLogicAspect); |
53 | q->registerBackendType<QFrameAction>(functor: QBackendNodeMapperPtr(new Logic::HandlerFunctor(m_manager.data()))); |
54 | } |
55 | |
56 | /*! |
57 | Constructs a new Qt3DLogic::QLogicAspect instance with \a parent. |
58 | */ |
59 | QLogicAspect::QLogicAspect(QObject *parent) |
60 | : QLogicAspect(*new QLogicAspectPrivate(), parent) {} |
61 | |
62 | /*! \internal */ |
63 | QLogicAspect::QLogicAspect(QLogicAspectPrivate &dd, QObject *parent) |
64 | : QAbstractAspect(dd, parent) |
65 | { |
66 | Q_D(QLogicAspect); |
67 | setObjectName(QStringLiteral("Logic Aspect")); |
68 | d->registerBackendTypes(); |
69 | d_func()->m_manager->setLogicAspect(this); |
70 | } |
71 | |
72 | /*! \internal */ |
73 | QLogicAspect::~QLogicAspect() |
74 | { |
75 | } |
76 | |
77 | /*! \internal */ |
78 | std::vector<QAspectJobPtr> QLogicAspect::jobsToExecute(qint64 time) |
79 | { |
80 | Q_D(QLogicAspect); |
81 | const qint64 deltaTime = time - d->m_time; |
82 | const float dt = static_cast<float>(deltaTime) / 1.0e9; |
83 | d->m_manager->setDeltaTime(dt); |
84 | d->m_time = time; |
85 | |
86 | // Create jobs that will get executed by the threadpool |
87 | if (d->m_manager->hasFrameActions()) |
88 | return {d->m_callbackJob}; |
89 | |
90 | return {}; |
91 | } |
92 | |
93 | /*! \internal */ |
94 | void QLogicAspect::onRegistered() |
95 | { |
96 | } |
97 | |
98 | /*! \internal */ |
99 | void QLogicAspect::onEngineStartup() |
100 | { |
101 | Q_D(QLogicAspect); |
102 | d->m_executor->setScene(d->m_arbiter->scene()); |
103 | } |
104 | |
105 | } // namespace Qt3DLogic |
106 | |
107 | QT_END_NAMESPACE |
108 | |
109 | QT3D_REGISTER_NAMESPACED_ASPECT("logic", QT_PREPEND_NAMESPACE(Qt3DLogic), QLogicAspect) |
110 | |
111 | #include "moc_qlogicaspect.cpp" |
112 |