summaryrefslogtreecommitdiffstats
path: root/src/render/picking/qobjectpicker.cpp
blob: 4f039b36150438b145deb5385033fb4273a0f5c3 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qobjectpicker.h"
#include "qobjectpicker_p.h"
#include <Qt3DCore/qentity.h>
#include <Qt3DCore/private/qcomponent_p.h>
#include <Qt3DCore/private/qscene_p.h>
#include <Qt3DRender/qpickevent.h>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/private/qpickevent_p.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

/*!
    \class Qt3DRender::QObjectPicker
    \inmodule Qt3DRender

    \brief The QObjectPicker class instantiates a component that can
    be used to interact with a QEntity by a process known as picking.

    For every combination of viewport and camera, picking casts a ray through the scene to
    find entities who's bounding volume intersects the ray. The bounding volume is computed using
    the values in the attribute buffer specified by the boundingVolumePositionAttribute of the
    geometry.

    The signals pressed(), released(), clicked(), moved(), entered(), and exited() are
    emitted when the bounding volume defined by the pickAttribute property intersects
    with a ray.

    Most signals carry a QPickEvent instance. If QPickingSettings::pickMode() is set to
    QPickingSettings::TrianglePicking, the actual type of the pick parameter will be
    QPickTriangleEvent.

    Pick queries are performed on mouse press and mouse release.
    If drag is enabled, queries also happen on each mouse move while any button is pressed.
    If hover is enabled, queries happen on every mouse move even if no button is pressed.

    For generalised ray casting queries, see Qt3DRender::QRayCaster and Qt3DRender::QScreenRayCaster.

    \sa Qt3DRender::QPickingSettings, Qt3DRender::QGeometry, Qt3DRender::QAttribute,
        Qt3DRender::QPickEvent, Qt3DRender::QPickTriangleEvent, Qt3DRender::QNoPicking

    \note Instances of this component shouldn't be shared, not respecting that
    condition will most likely result in undefined behavior.

    \note The camera far plane value affects picking and produces incorrect results due to
    floating-point precision if it is greater than ~100 000.

    \since 5.6
*/

/*!
    \qmltype ObjectPicker
    \instantiates Qt3DRender::QObjectPicker
    \inqmlmodule Qt3D.Render
    \brief The ObjectPicker class instantiates a component that can
    be used to interact with an Entity by a process known as picking.

    For every combination of viewport and camera, picking casts a ray through the scene to
    find entities who's bounding volume intersects the ray. The bounding volume is computed using
    the values in the attribute buffer specified by the boundingVolumePositionAttribute of the
    geometry.

    The signals pressed(), released(), clicked(), moved(), entered(), and exited() are
    emitted when the bounding volume defined by the pickAttribute property intersects
    with a ray.

    Most signals carry a PickEvent instance. If PickingSettings.pickMode is set to
    PickingSettings.TrianglePicking, the actual type of the pick parameter will be
    PickTriangleEvent.

    Pick queries are performed on mouse press and mouse release.
    If drag is enabled, queries also happen on each mouse move while any button is pressed.
    If hover is enabled, queries happen on every mouse move even if no button is pressed.

    \sa PickingSettings, Geometry, Attribute, PickEvent, PickTriangleEvent, NoPicking

    \note To receive hover events in QtQuick, the hoverEnabled property of Scene3D must also be set.

    \note Instances of this component shouldn't be shared, not respecting that
    condition will most likely result in undefined behavior.

    \note The camera far plane value affects picking and produces incorrect results due to
    floating-point precision if it is greater than ~100 000.
 */

/*!
    \qmlsignal Qt3D.Render::ObjectPicker::clicked(PickEvent pick)

    This signal is emitted when the bounding volume defined by the pickAttribute
    property intersects with a ray on a mouse click the PickEvent \a pick contains
    details of the event.
*/

/*!
    \qmlsignal Qt3D.Render::ObjectPicker::pressed(PickEvent pick)

    This signal is emitted when the bounding volume defined by the
    pickAttribute property intersects with a ray on a mouse press. Intersection
    information are accessible through the \a pick parameter.
*/

/*!
    \qmlsignal Qt3D.Render::ObjectPicker::released(PickEvent pick)

    This signal is emitted when the bounding volume defined by the
    pickAttribute property intersects with a ray on a mouse release.
    Intersection information are accessible through the \a pick parameter.
*/

/*!
    \qmlsignal Qt3D.Render::ObjectPicker::clicked(PickEvent pick)

    This signal is emitted when the bounding volume defined by the
    pickAttribute property intersects with a ray on a mouse click. Intersection
    information are accessible through the \a pick parameter.
*/

/*!
    \qmlsignal Qt3D.Render::ObjectPicker::moved(PickEvent pick)

    This signal is emitted when the bounding volume defined by the
    pickAttribute property intersects with a ray on a mouse move with a button
    pressed. Intersection information are accessible through the \a pick
    parameter.
*/

/*!
    \qmlsignal Qt3D.Render::ObjectPicker::entered()

    This signal is emitted when the bounding volume defined by the pickAttribute
    property intersects with a ray on the mouse entering the volume.
*/

/*!
    \qmlsignal Qt3D.Render::ObjectPicker::exited()

    This signal is emitted when the bounding volume defined by the pickAttribute
    property intersects with a ray on the ray exiting the volume.
*/

/*!
    \fn Qt3DRender::QObjectPicker::clicked(Qt3DRender::QPickEvent *pick)

    This signal is emitted when the bounding volume defined by the pickAttribute
    property intersects with a ray on a mouse click the QPickEvent \a pick contains
    details of the event.
*/

/*!
    \fn Qt3DRender::QObjectPicker::entered()

    This signal is emitted when the bounding volume defined by the pickAttribute
    property intersects with a ray on the mouse entering the volume.
*/

/*!
    \fn Qt3DRender::QObjectPicker::exited()

    This signal is emitted when the bounding volume defined by the pickAttribute
    property intersects with a ray on the ray exiting the volume.
*/

/*!
    \fn Qt3DRender::QObjectPicker::moved(Qt3DRender::QPickEvent *pick)

    This signal is emitted when the bounding volume defined by the
    pickAttribute property intersects with a ray on a mouse move with a button
    pressed the QPickEvent \a pick contains details of the event.
*/

/*!
    \fn Qt3DRender::QObjectPicker::pressed(Qt3DRender::QPickEvent *pick)

    This signal is emitted when the bounding volume defined by the
    pickAttribute property intersects with a ray on a mouse press the
    QPickEvent \a pick contains details of the event.
*/

/*!
    \fn Qt3DRender::QObjectPicker::released(Qt3DRender::QPickEvent *pick)

    This signal is emitted when the bounding volume defined by the
    pickAttribute property intersects with a ray on a mouse release the
    QPickEvent \a pick contains details of the event.
*/

QObjectPicker::QObjectPicker(Qt3DCore::QNode *parent)
    : Qt3DCore::QComponent(*new QObjectPickerPrivate(), parent)
{
}

/*! \internal */
QObjectPicker::~QObjectPicker()
{
}

/*!
 * Sets the hoverEnabled Property to \a hoverEnabled
 */
void QObjectPicker::setHoverEnabled(bool hoverEnabled)
{
    Q_D(QObjectPicker);
    if (hoverEnabled != d->m_hoverEnabled) {
        d->m_hoverEnabled = hoverEnabled;
        emit hoverEnabledChanged(hoverEnabled);
    }
}

/*!
    \qmlproperty bool Qt3D.Render::ObjectPicker::hoverEnabled
    Specifies if hover is enabled
*/
/*!
  \property Qt3DRender::QObjectPicker::hoverEnabled
    Specifies if hover is enabled
 */
/*!
 * \return true if hover enabled
 */
bool QObjectPicker::isHoverEnabled() const
{
    Q_D(const QObjectPicker);
    return d->m_hoverEnabled;
}

/*!
 * Sets the dragEnabled Property to \a dragEnabled
 */
void QObjectPicker::setDragEnabled(bool dragEnabled)
{
    Q_D(QObjectPicker);
    if (dragEnabled != d->m_dragEnabled) {
        d->m_dragEnabled = dragEnabled;
        emit dragEnabledChanged(dragEnabled);
    }
}

/*!
 * Sets the picker's priority to \a priority. This is used when the pick result
 * mode on QPickingSettings is set to QPickingSettings::NearestPriorityPick.
 * Picking results are sorted by highest priority and shortest picking
 * distance.
 *
 * \since 5.13
 */
void QObjectPicker::setPriority(int priority)
{
    Q_D(QObjectPicker);
    if (priority != d->m_priority) {
        d->m_priority = priority;
        emit priorityChanged(priority);
    }
}

// TODO Unused remove in Qt6
void QObjectPicker::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &)
{
}

/*!
    \qmlproperty bool Qt3D.Render::ObjectPicker::dragEnabled
*/
/*!
  \property Qt3DRender::QObjectPicker::dragEnabled
    Specifies if drag is enabled
 */
/*!
 * \return true if dragging is enabled
 */
bool QObjectPicker::isDragEnabled() const
{
    Q_D(const QObjectPicker);
    return d->m_dragEnabled;
}

/*!
    \qmlproperty bool Qt3D.Render::ObjectPicker::containsMouse
    Specifies if the object picker currently contains the mouse
*/
/*!
  \property Qt3DRender::QObjectPicker::containsMouse
    Specifies if the object picker currently contains the mouse
 */
/*!
 * \return true if the object picker currently contains the mouse
 */
bool QObjectPicker::containsMouse() const
{
    Q_D(const QObjectPicker);
    return d->m_containsMouse;
}

/*!
    \qmlproperty bool Qt3D.Render::ObjectPicker::pressed
    Specifies if the object picker is currently pressed
*/
/*!
  \property Qt3DRender::QObjectPicker::pressed
    Specifies if the object picker is currently pressed
 */
bool QObjectPicker::isPressed() const
{
    Q_D(const QObjectPicker);
    return d->m_pressed;
}

/*!
    \qmlproperty int Qt3D.Render::ObjectPicker::priority

    The priority to be used when filtering pick results by priority when
    PickingSettings.pickResultMode is set to PickingSettings.PriorityPick.
*/
/*!
  \property Qt3DRender::QObjectPicker::priority

    The priority to be used when filtering pick results by priority when
    QPickingSettings::pickResultMode is set to
    QPickingSettings::NearestPriorityPick.
*/
int QObjectPicker::priority() const
{
    Q_D(const QObjectPicker);
    return d->m_priority;
}

/*!
    \internal
 */
void QObjectPickerPrivate::setPressed(bool pressed)
{
    Q_Q(QObjectPicker);
    if (m_pressed != pressed) {
        const bool blocked = q->blockNotifications(true);
        m_pressed = pressed;
        emit q->pressedChanged(pressed);
        q->blockNotifications(blocked);
    }
}

/*!
    \internal
*/
void QObjectPickerPrivate::setContainsMouse(bool containsMouse)
{
    Q_Q(QObjectPicker);
    if (m_containsMouse != containsMouse) {
        const bool blocked = q->blockNotifications(true);
        m_containsMouse = containsMouse;
        emit q->containsMouseChanged(containsMouse);
        q->blockNotifications(blocked);
    }
}

void QObjectPickerPrivate::propagateEvent(QPickEvent *event, EventType type)
{
    if (!m_entities.isEmpty()) {
        Qt3DCore::QEntity *entity = m_entities.first();
        Qt3DCore::QEntity *parentEntity = nullptr;
        while (entity != nullptr && entity->parentEntity() != nullptr && !event->isAccepted()) {
            parentEntity = entity->parentEntity();
            const auto components = parentEntity->components();
            for (Qt3DCore::QComponent *c : components) {
                if (auto objectPicker = qobject_cast<Qt3DRender::QObjectPicker *>(c)) {
                    QObjectPickerPrivate *objectPickerPrivate = static_cast<QObjectPickerPrivate *>(QObjectPickerPrivate::get(objectPicker));
                    switch (type) {
                    case Pressed:
                        objectPickerPrivate->pressedEvent(event);
                        break;
                    case Released:
                        objectPickerPrivate->releasedEvent(event);
                        break;
                    case Clicked:
                        objectPickerPrivate->clickedEvent(event);
                        break;
                    case EventType::Moved:
                        objectPickerPrivate->movedEvent(event);
                        break;
                    }
                    break;
                }
            }
            entity = parentEntity;
        }
    }
}

/*!
    \internal
 */
void QObjectPickerPrivate::pressedEvent(QPickEvent *event)
{
    Q_Q(QObjectPicker);
    emit q->pressed(event);

    m_acceptedLastPressedEvent = event->isAccepted();
    if (!m_acceptedLastPressedEvent) {
        // Travel parents to transmit the event
        propagateEvent(event, Pressed);
    } else {
        setPressed(true);
    }
}

/*!
    \internal
 */
void QObjectPickerPrivate::clickedEvent(QPickEvent *event)
{
    Q_Q(QObjectPicker);
    emit q->clicked(event);
    if (!event->isAccepted())
        propagateEvent(event, Clicked);
}

/*!
    \internal
 */
void QObjectPickerPrivate::movedEvent(QPickEvent *event)
{
    Q_Q(QObjectPicker);
    emit q->moved(event);
    if (!event->isAccepted())
        propagateEvent(event, EventType::Moved);
}

/*!
    \internal
 */
void QObjectPickerPrivate::releasedEvent(QPickEvent *event)
{
    Q_Q(QObjectPicker);
    if (m_acceptedLastPressedEvent) {
        emit q->released(event);
        setPressed(false);
    } else {
        event->setAccepted(false);
        propagateEvent(event, Released);
    }
}

Qt3DCore::QNodeCreatedChangeBasePtr QObjectPicker::createNodeCreationChange() const
{
    auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QObjectPickerData>::create(this);
    auto &data = creationChange->data;
    Q_D(const QObjectPicker);
    data.hoverEnabled = d->m_hoverEnabled;
    data.dragEnabled = d->m_dragEnabled;
    data.priority = d->m_priority;
    return creationChange;
}

} // Qt3DRender

QT_END_NAMESPACE