summaryrefslogtreecommitdiffstats
path: root/src/render
diff options
context:
space:
mode:
authorMike Krus <mike.krus@kdab.com>2019-04-03 18:04:22 +0100
committerMike Krus <mike.krus@kdab.com>2019-05-25 09:38:47 +0100
commit09daf5451c03a96514d7d96cc12aaa279d99fa3e (patch)
tree5da9a698fdab5cc46267de30541b42fb4dd1b876 /src/render
parent8dcb93c51e0a9ad8fddfcce1faef18d519c7caa3 (diff)
Introduce EntityAccumulator
Used to traverse scene graph (using EntityVisitor) and collect list of entity matching a predicate. By default, collects all entities, but can also collect those that have a certain type of component, or use a custom predicate Change-Id: I9da877d629fe146c1307f5afead7502c440aca9f Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'src/render')
-rw-r--r--src/render/backend/entityaccumulator.cpp99
-rw-r--r--src/render/backend/entityaccumulator_p.h83
-rw-r--r--src/render/backend/render-backend.pri2
3 files changed, 184 insertions, 0 deletions
diff --git a/src/render/backend/entityaccumulator.cpp b/src/render/backend/entityaccumulator.cpp
new file mode 100644
index 000000000..f003420dd
--- /dev/null
+++ b/src/render/backend/entityaccumulator.cpp
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "entityaccumulator_p.h"
+#include "entityvisitor_p.h"
+
+QT_USE_NAMESPACE
+using namespace Qt3DRender::Render;
+
+namespace {
+
+class Accumulator : public EntityVisitor
+{
+public:
+ Accumulator(std::function<bool(Entity *)> predicate, NodeManagers *manager)
+ : EntityVisitor(manager)
+ , m_predicate(predicate)
+ {
+ }
+
+ EntityVisitor::Operation visit(Entity *entity) override {
+ if (m_predicate(entity))
+ m_entities << entity;
+ return Continue;
+ }
+
+ QVector<Entity *> m_entities;
+
+private:
+ std::function<bool(Entity *)> m_predicate;
+};
+
+}
+
+EntityAccumulator::EntityAccumulator(NodeManagers *manager)
+ : m_manager(manager)
+ , m_predicate([](Entity*) { return true; })
+{
+
+}
+
+EntityAccumulator::EntityAccumulator(std::function<bool (Entity *)> predicate, NodeManagers *manager)
+ : m_manager(manager)
+ , m_predicate(predicate)
+{
+
+}
+
+/*!
+ * \internal
+ *
+ * Call this to traverse the scene graph and return all entities for
+ * which the predicate returns true.
+ *
+ * Can be useful to get all the entities that contain a specific type
+ * of component.
+ */
+QVector<Entity *> EntityAccumulator::apply(Entity *root) const
+{
+ Accumulator a(m_predicate, m_manager);
+ a.apply(root);
+ return a.m_entities;
+}
diff --git a/src/render/backend/entityaccumulator_p.h b/src/render/backend/entityaccumulator_p.h
new file mode 100644
index 000000000..69656815f
--- /dev/null
+++ b/src/render/backend/entityaccumulator_p.h
@@ -0,0 +1,83 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QT3DRENDER_RENDER_ENTITYACCUMULATOR_H
+#define QT3DRENDER_RENDER_ENTITYACCUMULATOR_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of other Qt classes. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <Qt3DRender/private/entity_p.h>
+#include <Qt3DRender/private/qt3drender_global_p.h>
+#include <QVector>
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3DRender {
+namespace Render {
+
+class NodeManagers;
+
+class Q_3DRENDERSHARED_PRIVATE_EXPORT EntityAccumulator
+{
+public:
+ EntityAccumulator(NodeManagers *manager);
+ EntityAccumulator(std::function<bool(Entity*)> predicate, NodeManagers *manager);
+
+ QVector<Entity *> apply(Entity *root) const;
+
+private:
+ NodeManagers *m_manager;
+ std::function<bool(Entity *)> m_predicate;
+};
+
+} // namespace Render
+} // namespace Qt3DRender
+
+QT_END_NAMESPACE
+
+#endif // QT3DRENDER_RENDER_ENTITYACCUMULATOR_H
diff --git a/src/render/backend/render-backend.pri b/src/render/backend/render-backend.pri
index 10dc1b759..6b60dfcda 100644
--- a/src/render/backend/render-backend.pri
+++ b/src/render/backend/render-backend.pri
@@ -12,6 +12,7 @@ HEADERS += \
$$PWD/cameralens_p.h \
$$PWD/entity_p.h \
$$PWD/entityvisitor_p.h \
+ $$PWD/entityaccumulator_p.h \
$$PWD/layer_p.h \
$$PWD/levelofdetail_p.h \
$$PWD/nodefunctor_p.h \
@@ -46,6 +47,7 @@ SOURCES += \
$$PWD/cameralens.cpp \
$$PWD/entity.cpp \
$$PWD/entityvisitor.cpp \
+ $$PWD/entityaccumulator.cpp \
$$PWD/layer.cpp \
$$PWD/levelofdetail.cpp \
$$PWD/transform.cpp \