| 1 | // Copyright (C) 2019 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 | #include "entityaccumulator_p.h" |
| 5 | #include "entityvisitor_p.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | namespace Qt3DRender { |
| 10 | namespace Render { |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | class Accumulator : public EntityVisitor |
| 15 | { |
| 16 | public: |
| 17 | Accumulator(std::function<bool(Entity *)> predicate, NodeManagers *manager) |
| 18 | : EntityVisitor(manager) |
| 19 | , m_predicate(predicate) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | EntityVisitor::Operation visit(Entity *entity) override { |
| 24 | if (m_predicate(entity)) |
| 25 | m_entities << entity; |
| 26 | return Continue; |
| 27 | } |
| 28 | |
| 29 | QList<Entity *> m_entities; |
| 30 | |
| 31 | private: |
| 32 | std::function<bool(Entity *)> m_predicate; |
| 33 | }; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | EntityAccumulator::EntityAccumulator(NodeManagers *manager) |
| 38 | : m_manager(manager) |
| 39 | , m_predicate([](Entity*) { return true; }) |
| 40 | { |
| 41 | |
| 42 | } |
| 43 | |
| 44 | EntityAccumulator::EntityAccumulator(std::function<bool (Entity *)> predicate, NodeManagers *manager) |
| 45 | : m_manager(manager) |
| 46 | , m_predicate(predicate) |
| 47 | { |
| 48 | |
| 49 | } |
| 50 | |
| 51 | /*! |
| 52 | * \internal |
| 53 | * |
| 54 | * Call this to traverse the scene graph and return all entities for |
| 55 | * which the predicate returns true. |
| 56 | * |
| 57 | * Can be useful to get all the entities that contain a specific type |
| 58 | * of component. |
| 59 | */ |
| 60 | QList<Entity *> EntityAccumulator::apply(Entity *root) const |
| 61 | { |
| 62 | Accumulator a(m_predicate, m_manager); |
| 63 | a.apply(root); |
| 64 | return a.m_entities; |
| 65 | } |
| 66 | |
| 67 | } // namespace Render |
| 68 | } // namespace Qt3DRender |
| 69 | |
| 70 | QT_END_NAMESPACE |
| 71 |
