| 1 | // Copyright (C) 2017 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 "qlevelofdetailswitch.h" |
| 5 | #include "qlevelofdetailswitch_p.h" |
| 6 | #include "qlevelofdetail_p.h" |
| 7 | #include "qglobal.h" |
| 8 | #include <Qt3DCore/QEntity> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | namespace Qt3DRender { |
| 13 | |
| 14 | QLevelOfDetailSwitchPrivate::QLevelOfDetailSwitchPrivate() |
| 15 | : QLevelOfDetailPrivate() |
| 16 | { |
| 17 | |
| 18 | } |
| 19 | |
| 20 | void QLevelOfDetailSwitchPrivate::setCurrentIndex(int currentIndex) |
| 21 | { |
| 22 | Q_Q(QLevelOfDetailSwitch); |
| 23 | |
| 24 | bool changed = m_currentIndex != currentIndex; |
| 25 | QLevelOfDetailPrivate::setCurrentIndex(currentIndex); |
| 26 | |
| 27 | if (!changed) |
| 28 | return; |
| 29 | |
| 30 | int entityIndex = 0; |
| 31 | const auto entities = q->entities(); |
| 32 | for (Qt3DCore::QEntity *entity : entities) { |
| 33 | const auto childNodes = entity->childNodes(); |
| 34 | for (Qt3DCore::QNode *childNode : childNodes) { |
| 35 | Qt3DCore::QEntity *childEntity = qobject_cast<Qt3DCore::QEntity *>(object: childNode); |
| 36 | if (childEntity) { |
| 37 | childEntity->setEnabled(entityIndex == currentIndex); |
| 38 | entityIndex++; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | break; // only work on the first entity, LOD should not be shared |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /*! |
| 47 | \class Qt3DRender::QLevelOfDetailSwitch |
| 48 | \inmodule Qt3DRender |
| 49 | \inherits Qt3DRender::QLevelOfDetail |
| 50 | \since 5.9 |
| 51 | \brief Provides a way of enabling child entities based on distance or screen size. |
| 52 | |
| 53 | This component is assigned to an entity. When the entity changes distance relative |
| 54 | to the camera, the QLevelOfDetailSwitch will disable all the child entities except |
| 55 | the one matching index Qt3DRender::QLevelOfDetailSwitch::currentIndex. |
| 56 | */ |
| 57 | |
| 58 | /*! |
| 59 | \qmltype LevelOfDetailSwitch |
| 60 | \nativetype Qt3DRender::QLevelOfDetailSwitch |
| 61 | \inherits Component3D |
| 62 | \inqmlmodule Qt3D.Render |
| 63 | \since 5.9 |
| 64 | \brief Provides a way of enabling child entities based on distance or screen size. |
| 65 | |
| 66 | This component is assigned to an entity. When the entity changes distance relative |
| 67 | to the camera, the LevelOfDetailSwitch will disable all the child entities except |
| 68 | the one matching index \l currentIndex. |
| 69 | |
| 70 | \sa LevelOfDetail |
| 71 | */ |
| 72 | |
| 73 | /*! |
| 74 | \qmlproperty int LevelOfDetailSwitch::currentIndex |
| 75 | |
| 76 | The index of the presently selected child entity. |
| 77 | */ |
| 78 | |
| 79 | /*! \fn Qt3DRender::QLevelOfDetailSwitch::QLevelOfDetailSwitch(Qt3DCore::QNode *parent) |
| 80 | Constructs a new QLevelOfDetailSwitch with the specified \a parent. |
| 81 | */ |
| 82 | QLevelOfDetailSwitch::QLevelOfDetailSwitch(QNode *parent) |
| 83 | : QLevelOfDetail(*new QLevelOfDetailSwitchPrivate(), parent) |
| 84 | { |
| 85 | Q_D(QLevelOfDetailSwitch); |
| 86 | d->m_currentIndex = -1; |
| 87 | } |
| 88 | |
| 89 | /*! \internal */ |
| 90 | QLevelOfDetailSwitch::~QLevelOfDetailSwitch() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | /*! \internal */ |
| 95 | QLevelOfDetailSwitch::QLevelOfDetailSwitch(QLevelOfDetailPrivate &dd, QNode *parent) |
| 96 | : QLevelOfDetail(dd, parent) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | } // namespace Qt3DRender |
| 101 | |
| 102 | QT_END_NAMESPACE |
| 103 | |
| 104 | #include "moc_qlevelofdetailswitch.cpp" |
| 105 |
