summaryrefslogtreecommitdiff
path: root/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/mousearea3d.h
blob: 2d8dbc144163888b438cadf04a704c7b1606d62a (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
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
****************************************************************************/

#pragma once

#ifdef QUICK3D_MODULE

#include <QtGui/qvector3d.h>
#include <QtCore/qpoint.h>
#include <QtCore/qpointer.h>

#include <QtQuick3D/private/qquick3dnode_p.h>
#include <QtQuick3D/private/qquick3dmodel_p.h>
#include <QtQuick3D/private/qquick3dviewport_p.h>
#include <QtQuick3D/private/qtquick3dglobal_p.h>

namespace QmlDesigner {
namespace Internal {

class MouseArea3D : public QQuick3DNode
{
    Q_OBJECT
    Q_PROPERTY(QQuick3DViewport *view3D READ view3D WRITE setView3D NOTIFY view3DChanged)
    Q_PROPERTY(bool grabsMouse READ grabsMouse WRITE setGrabsMouse NOTIFY grabsMouseChanged)
    Q_PROPERTY(qreal x READ x WRITE setX NOTIFY xChanged)
    Q_PROPERTY(qreal y READ y WRITE setY NOTIFY yChanged)
    Q_PROPERTY(qreal width READ width WRITE setWidth NOTIFY widthChanged)
    Q_PROPERTY(qreal height READ height WRITE setHeight NOTIFY heightChanged)
    Q_PROPERTY(bool hovering READ hovering NOTIFY hoveringChanged)
    Q_PROPERTY(bool dragging READ dragging NOTIFY draggingChanged)
    Q_PROPERTY(int priority READ priority WRITE setPriority NOTIFY priorityChanged)
    Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
    Q_PROPERTY(QPointF circlePickArea READ circlePickArea WRITE setCirclePickArea NOTIFY circlePickAreaChanged)
    Q_PROPERTY(qreal minAngle READ minAngle WRITE setMinAngle NOTIFY minAngleChanged)
    Q_PROPERTY(QQuick3DNode *pickNode READ pickNode WRITE setPickNode NOTIFY pickNodeChanged)
    Q_PROPERTY(MouseArea3D *dragHelper READ dragHelper WRITE setDragHelper NOTIFY dragHelperChanged)

    Q_INTERFACES(QQmlParserStatus)

public:
    MouseArea3D(QQuick3DNode *parent = nullptr);

    QQuick3DViewport *view3D() const;

    qreal x() const;
    qreal y() const;
    qreal width() const;
    qreal height() const;
    int priority() const;

    bool hovering() const;
    bool dragging() const;
    bool grabsMouse() const;
    bool active() const;
    QPointF circlePickArea() const;
    qreal minAngle() const;
    QQuick3DNode *pickNode() const;
    MouseArea3D *dragHelper() const;

    static qreal mouseDragMultiplier() { return .02; }

public slots:
    void setView3D(QQuick3DViewport *view3D);
    void setGrabsMouse(bool grabsMouse);
    void setActive(bool active);
    void setCirclePickArea(const QPointF &pickArea);
    void setMinAngle(qreal angle);
    void setPickNode(QQuick3DNode *node);
    void setDragHelper(MouseArea3D *dragHelper);

    void setX(qreal x);
    void setY(qreal y);
    void setWidth(qreal width);
    void setHeight(qreal height);
    void setPriority(int level);

    Q_INVOKABLE QVector3D rayIntersectsPlane(const QVector3D &rayPos0,
                                             const QVector3D &rayPos1,
                                             const QVector3D &planePos,
                                             const QVector3D &planeNormal) const;

    Q_INVOKABLE QVector3D getNewScale(QQuick3DNode *node, const QVector3D &startScale,
                                      const QVector3D &pressPos,
                                      const QVector3D &sceneRelativeDistance, bool global);

    Q_INVOKABLE qreal getNewRotationAngle(QQuick3DNode *node, const QVector3D &pressPos,
                                          const QVector3D &currentPos, const QVector3D &nodePos,
                                          qreal prevAngle, bool trackBall);
    Q_INVOKABLE void applyRotationAngleToNode(QQuick3DNode *node, const QVector3D &startRotation,
                                              qreal angle);
    Q_INVOKABLE void applyFreeRotation(QQuick3DNode *node, const QVector3D &startRotation,
                                       const QVector3D &pressPos, const QVector3D &currentPos);

signals:
    void view3DChanged();

    void xChanged();
    void yChanged();
    void widthChanged();
    void heightChanged();
    void priorityChanged();

    void hoveringChanged();
    void draggingChanged();
    void activeChanged();
    void grabsMouseChanged();
    void circlePickAreaChanged();
    void minAngleChanged();
    void pickNodeChanged();
    void dragHelperChanged();

    // angle parameter is only set if circlePickArea is specified
    void pressed(const QVector3D &scenePos, const QPoint &screenPos, qreal angle);
    void released(const QVector3D &scenePos, const QPoint &screenPos);
    void dragged(const QVector3D &scenePos, const QPoint &screenPos);

protected:
    void classBegin() override {}
    void componentComplete() override;
    bool eventFilter(QObject *obj, QEvent *event) override;

private:
    void setDragging(bool enable);
    void setHovering(bool enable);
    QVector3D getNormal() const;
    QVector3D getCameraToNodeDir(QQuick3DNode *node) const;

    Q_DISABLE_COPY(MouseArea3D)
    QQuick3DViewport *m_view3D = nullptr;

    qreal m_x;
    qreal m_y;
    qreal m_width;
    qreal m_height;
    int m_priority = 0;

    bool m_hovering = false;
    bool m_dragging = false;
    bool m_active = false;

    QVector3D getMousePosInPlane(const MouseArea3D *helper, const QPointF &mousePosInView) const;

    static MouseArea3D *s_mouseGrab;
    bool m_grabsMouse = false;
    QVector3D m_mousePosInPlane;
    QPointF m_circlePickArea;
    qreal m_minAngle = 0.;
    QQuick3DNode *m_pickNode = nullptr;
    MouseArea3D *m_dragHelper = nullptr;
};

}
}

QML_DECLARE_TYPE(QmlDesigner::Internal::MouseArea3D)

#endif // QUICK3D_MODULE