summaryrefslogtreecommitdiffstats
path: root/util/qt3d/modeltweak/qml/ModelViewport.qml
blob: 2dc2d31ec134a4cd91fe2ccd6fb8335906963e2b (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
import QtQuick 1.0
import Qt3D 1.0

Rectangle {
    width: parent.width/2;
    height: parent.height/2
    color: parent.color
    border.color: parent.border.color

    property alias itemPosition: mainItem.position;
    property alias itemScale: mainItem.scale;
    property alias camera: viewport.camera

    // the current x/y positions of the mouse when the onPressed event was triggered;
    // values are invalid if onRelease has occured
    property int downX;
    property int downY;

    // current xyz positions of the Translation3D when onPressed was triggered;
    // values are invalid if onRelease has occured
    property double translateX;
    property double translateY;
    property double translateZ;

    // current angles of the three Rotation3Ds when onPressed was triggered;
    // values are invalid if onRelease has occured
    property double rotateX;
    property double rotateY;
    property double rotateZ;

    // current xyz sizes of the Scale3D when onPressed was triggered;
    // values are invalid if onRelease has occured
    property double scaleX;
    property double scaleY;
    property double scaleZ;

    // the pixel sensitivity values of the mousemovements
    // TODO: expose via api
    // TODO: maybe save in settings api?
    property double translateSensitivity: 32;
    property double rotateSensitivity: 8;
    property double scaleSensitivity: 32;

    Viewport {
        id: viewport
        anchors.fill: parent
        picking: false
        blending: true

        camera: Camera {
            farPlane: 2000 // debugging
        }
        navigation: false

        Item3D {
            id: mainItem
            mesh: source_mesh
            effect: Effect {}
            transform: [
                transformTranslate,
                transformRotateX,
                transformRotateY,
                transformRotateZ,
                transformScale
            ]
        }
    }

    MouseArea {
        property int mouseDown: Qt.NoButton

        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton

        onMouseXChanged: {
            if (mouseDown & Qt.LeftButton)
                translateMouseX(mouse);
            else if (mouseDown === Qt.RightButton)
                rotateMouseX(mouse);
            else if (mouseDown === Qt.MiddleButton)
                scaleMouseX(mouse);
        }

        onMouseYChanged: {
            if (mouseDown & Qt.LeftButton)
                translateMouseY(mouse);
            else if (mouseDown === Qt.RightButton)
                rotateMouseY(mouse);
            else if (mouseDown === Qt.MiddleButton)
                scaleMouseY(mouse);
        }

        onPressed: {
            // if we already have a mouse button down we don't want to do anything else until it's up again
            if (mouseDown !== Qt.NoButton)
                return;

            mouseDown = mouse.button;

            downX = mouse.x;
            downY = mouse.y;

            if (mouseDown & Qt.LeftButton) {
                translateX = transformTranslate.translate.x;
                translateY = transformTranslate.translate.y;
                translateZ = transformTranslate.translate.z;
            }

            if (mouseDown & Qt.RightButton) {
                rotateX = transformRotateX.angle
                rotateY = transformRotateY.angle
                rotateZ = transformRotateZ.angle
            }
            if (mouseDown & Qt.MiddleButton) {
                scaleX = transformScale.scale.x
                scaleY = transformScale.scale.y
                scaleZ = transformScale.scale.z
            }
        }

        // clear the current mouse button upon release
        onReleased: { mouseDown = Qt.NoButton }
    }
}