summaryrefslogtreecommitdiffstats
path: root/src/render/backend/entityvisitor.cpp
blob: e0e3921591ddc0288d04b4e521cea6239efdae63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "entityvisitor_p.h"
#include <Qt3DRender/private/managers_p.h>
#include <Qt3DRender/private/nodemanagers_p.h>

QT_USE_NAMESPACE
using namespace Qt3DRender::Render;

EntityVisitor::EntityVisitor(NodeManagers *manager)
    : m_manager(manager)
    , m_pruneDisabled(false)
{

}

EntityVisitor::~EntityVisitor() = default;

/*!
 * \internal
 *
 * Override in derived class to do work on the current entity
 *
 * Return value (Continue, Prune, Stop) will affect traversal
 */
EntityVisitor::Operation EntityVisitor::visit(Entity *entity) {
    // return false to stop traversal
    if (!entity)
        return Stop;
    return Continue;
}

/*!
 * \internal
 *
 * If true, disabled entities and all their children will be ignored
 * during traversal
 *
 */
bool EntityVisitor::pruneDisabled() const
{
    return m_pruneDisabled;
}

void EntityVisitor::setPruneDisabled(bool pruneDisabled)
{
    m_pruneDisabled = pruneDisabled;
}

/*!
 * \internal
 *
 * Call on the root of the tree that should be traversed.
 * Returns false if any visit resulted in Stop
 */
bool EntityVisitor::apply(Entity *root) {
    if (!root)
        return false;
    if (m_pruneDisabled && !root->isEnabled())
        return true;

    const auto op = visit(root);
    if (op == Stop)
        return false;
    if (op == Prune)
        return true;

    const auto &childrenHandles = root->childrenHandles();
    for (const HEntity &handle : childrenHandles) {
        Entity *child = m_manager->renderNodesManager()->data(handle);
        if (child != nullptr && !apply(child))
            return false;
    }

    return true;
}