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