| 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 "entityvisitor_p.h" |
| 5 | #include <Qt3DRender/private/managers_p.h> |
| 6 | #include <Qt3DRender/private/nodemanagers_p.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | namespace Qt3DRender { |
| 11 | namespace Render { |
| 12 | |
| 13 | EntityVisitor::EntityVisitor(NodeManagers *manager) |
| 14 | : m_manager(manager) |
| 15 | , m_pruneDisabled(false) |
| 16 | { |
| 17 | |
| 18 | } |
| 19 | |
| 20 | EntityVisitor::~EntityVisitor() = default; |
| 21 | |
| 22 | /*! |
| 23 | * \internal |
| 24 | * |
| 25 | * Override in derived class to do work on the current entity |
| 26 | * |
| 27 | * Return value (Continue, Prune, Stop) will affect traversal |
| 28 | */ |
| 29 | EntityVisitor::Operation EntityVisitor::visit(Entity *entity) { |
| 30 | // return false to stop traversal |
| 31 | if (!entity) |
| 32 | return Stop; |
| 33 | return Continue; |
| 34 | } |
| 35 | |
| 36 | /*! |
| 37 | * \internal |
| 38 | * |
| 39 | * If true, disabled entities and all their children will be ignored |
| 40 | * during traversal |
| 41 | * |
| 42 | */ |
| 43 | bool EntityVisitor::pruneDisabled() const |
| 44 | { |
| 45 | return m_pruneDisabled; |
| 46 | } |
| 47 | |
| 48 | void EntityVisitor::setPruneDisabled(bool pruneDisabled) |
| 49 | { |
| 50 | m_pruneDisabled = pruneDisabled; |
| 51 | } |
| 52 | |
| 53 | /*! |
| 54 | * \internal |
| 55 | * |
| 56 | * Call on the root of the tree that should be traversed. |
| 57 | * Returns false if any visit resulted in Stop |
| 58 | */ |
| 59 | bool EntityVisitor::apply(Entity *root) { |
| 60 | if (!root) |
| 61 | return false; |
| 62 | if (m_pruneDisabled && !root->isEnabled()) |
| 63 | return true; |
| 64 | |
| 65 | const auto op = visit(entity: root); |
| 66 | if (op == Stop) |
| 67 | return false; |
| 68 | if (op == Prune) |
| 69 | return true; |
| 70 | |
| 71 | const auto &childrenHandles = root->childrenHandles(); |
| 72 | for (const HEntity &handle : childrenHandles) { |
| 73 | Entity *child = m_manager->renderNodesManager()->data(handle); |
| 74 | if (child != nullptr && !apply(root: child)) |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | } // namespace Render |
| 82 | } // namespace Qt3DRender |
| 83 | |
| 84 | QT_END_NAMESPACE |
| 85 |
