| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qproximityfilter.h" |
| 41 | #include "qproximityfilter_p.h" |
| 42 | #include <Qt3DCore/qentity.h> |
| 43 | #include <Qt3DRender/qframegraphnodecreatedchange.h> |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | namespace Qt3DRender { |
| 48 | |
| 49 | QProximityFilterPrivate::QProximityFilterPrivate() |
| 50 | : QFrameGraphNodePrivate() |
| 51 | , m_entity(nullptr) |
| 52 | , m_distanceThreshold(0.0f) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | /*! |
| 57 | \class Qt3DRender::QProximityFilter |
| 58 | \inmodule Qt3DRender |
| 59 | \since 5.10 |
| 60 | |
| 61 | \brief Select entities which are within a distance threshold of a target |
| 62 | entity. |
| 63 | |
| 64 | A \l Qt3DRender::QProximityFilter can be used to select entities to render |
| 65 | when they are placed within a given distance threshold of another entity. |
| 66 | */ |
| 67 | |
| 68 | /*! |
| 69 | \property Qt3DRender::QProximityFilter::entity |
| 70 | |
| 71 | Holds the entity against which we should compare the distance to. |
| 72 | */ |
| 73 | |
| 74 | /*! |
| 75 | \property Qt3DRender::QProximityFilter::distanceThreshold |
| 76 | |
| 77 | Holds the distance to the target entity above which entities are filtered |
| 78 | out. |
| 79 | */ |
| 80 | |
| 81 | /*! |
| 82 | \qmltype ProximityFilter |
| 83 | \instantiates Qt3DRender::QProximityFilter |
| 84 | \inherits FrameGraphNode |
| 85 | \inqmlmodule Qt3D.Render |
| 86 | \since 5.10 |
| 87 | |
| 88 | \brief Select entities which are within a distance threshold of a target |
| 89 | entity. |
| 90 | |
| 91 | A \l ProximityFilter can be used to select entities to render |
| 92 | when they are placed within a given distance threshold of another entity. |
| 93 | |
| 94 | \badcode |
| 95 | import Qt3DRender 2.10 |
| 96 | ... |
| 97 | RenderSetting { |
| 98 | Viewport { |
| 99 | CameraSelector { |
| 100 | camera: mainCamera |
| 101 | ProximityFilter { |
| 102 | entity: mainCamera |
| 103 | distanceThreshold: 50 // select entities within 50m metre radius of mainCamera |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | \endcode |
| 109 | */ |
| 110 | |
| 111 | /*! |
| 112 | \qmlproperty Entity Qt3D.Render::ProximityFilter::entity |
| 113 | |
| 114 | Holds the entity against which we should compare the distance to. |
| 115 | */ |
| 116 | |
| 117 | /*! |
| 118 | \qmlproperty real Qt3D.Render::ProximityFilter::distanceThreshold |
| 119 | |
| 120 | Holds the distance to the target entity above which entities are filtered |
| 121 | out. |
| 122 | */ |
| 123 | |
| 124 | |
| 125 | QProximityFilter::QProximityFilter(Qt3DCore::QNode *parent) |
| 126 | : QFrameGraphNode(*new QProximityFilterPrivate, parent) |
| 127 | { |
| 128 | |
| 129 | } |
| 130 | |
| 131 | /*! \internal */ |
| 132 | QProximityFilter::QProximityFilter(QProximityFilterPrivate &dd, QNode *parent) |
| 133 | : QFrameGraphNode(dd, parent) |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | /*! \internal */ |
| 138 | QProximityFilter::~QProximityFilter() |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | Qt3DCore::QEntity *QProximityFilter::entity() const |
| 143 | { |
| 144 | Q_D(const QProximityFilter); |
| 145 | return d->m_entity; |
| 146 | } |
| 147 | |
| 148 | float QProximityFilter::distanceThreshold() const |
| 149 | { |
| 150 | Q_D(const QProximityFilter); |
| 151 | return d->m_distanceThreshold; |
| 152 | } |
| 153 | |
| 154 | void QProximityFilter::setEntity(Qt3DCore::QEntity *entity) |
| 155 | { |
| 156 | Q_D(QProximityFilter); |
| 157 | if (d->m_entity != entity) { |
| 158 | |
| 159 | if (d->m_entity) |
| 160 | d->unregisterDestructionHelper(node: d->m_entity); |
| 161 | |
| 162 | if (entity && !entity->parent()) |
| 163 | entity->setParent(this); |
| 164 | |
| 165 | d->m_entity = entity; |
| 166 | |
| 167 | if (d->m_entity) |
| 168 | d->registerDestructionHelper(node: d->m_entity, func: &QProximityFilter::setEntity, d->m_entity); |
| 169 | |
| 170 | emit entityChanged(entity); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void QProximityFilter::setDistanceThreshold(float distanceThreshold) |
| 175 | { |
| 176 | Q_D(QProximityFilter); |
| 177 | if (d->m_distanceThreshold == distanceThreshold) |
| 178 | return; |
| 179 | |
| 180 | d->m_distanceThreshold = distanceThreshold; |
| 181 | emit distanceThresholdChanged(distanceThreshold); |
| 182 | } |
| 183 | |
| 184 | Qt3DCore::QNodeCreatedChangeBasePtr QProximityFilter::createNodeCreationChange() const |
| 185 | { |
| 186 | auto creationChange = QFrameGraphNodeCreatedChangePtr<QProximityFilterData>::create(arguments: this); |
| 187 | QProximityFilterData &data = creationChange->data; |
| 188 | Q_D(const QProximityFilter); |
| 189 | data.entityId = Qt3DCore::qIdForNode(node: d->m_entity); |
| 190 | data.distanceThreshold = d->m_distanceThreshold; |
| 191 | return creationChange; |
| 192 | } |
| 193 | |
| 194 | } // Qt3DRender |
| 195 | |
| 196 | QT_END_NAMESPACE |
| 197 | |