summaryrefslogtreecommitdiffstats
path: root/util/qt3d/modeltweak/qml/ModelViewport.qml
blob: 875c116247c05683f1a07ed47ead3a5eb64255c4 (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
import QtQuick 1.0
import Qt3D 1.0
import ModelTweak 1.0
import "Widgets"

Rectangle {
    id: view
    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
    property alias viewportName: viewportText.text;

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

    // current xyz values of the Translation3D/Rotation3Ds/Scale3D when onPressed was triggered;
    // values are invalid if onRelease has occured
    property variant translate;
    property variant rotate;
    property variant scale3d;

    // 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;

    // the name of this viewport's state
    property string stateName

    // the smoothness values for the state change transformations
    // TODO: this should be alterable via the gui and saved in a settings file
    Behavior on x      { NumberAnimation { duration: 300 } }
    Behavior on y      { NumberAnimation { duration: 300 } }
    Behavior on width  { NumberAnimation { duration: 300 } }
    Behavior on height { NumberAnimation { duration: 300 } }

    signal mouseTranslateX(variant mouse)
    signal mouseTranslateY(variant mouse)
    signal mouseRotateX(variant mouse)
    signal mouseRotateY(variant mouse)
    signal mouseScaleX(variant mouse)
    signal mouseScaleY(variant mouse)

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

        // TODO: camera's position/rotation-around-origin/farplane should be alterable via gui
        camera: Camera {
            farPlane: 2000 // debugging
        }
        navigation: false

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

    MouseArea {
        property int mouseDown: Qt.NoButton

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

        onMouseXChanged: {
            if (mouseDown & Qt.LeftButton)
                mouseTranslateX(mouse)
            else if (mouseDown === Qt.RightButton)
                mouseRotateX(mouse);
            else if (mouseDown === Qt.MiddleButton)
                mouseScaleX(mouse)
        }

        onMouseYChanged: {
            if (mouseDown & Qt.LeftButton)
                mouseTranslateY(mouse)
            else if (mouseDown === Qt.RightButton)
                mouseRotateY(mouse);
            else if (mouseDown === Qt.MiddleButton)
                mouseScaleY(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;
            down      = Qt.point(mouse.x, mouse.y)
            translate = transformTranslate.translate
            rotate    = Qt.vector3d(transformRotateX.angle, transformRotateY.angle, transformRotateZ.angle)
            scale3d   = transformScale.scale
        }

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

    ModelViewportResize {
        anchors {
            top: view.top; topMargin: 2;
            right: view.right; rightMargin: 2;
        }
    }

    Text {
        id: viewportText
        anchors {
            top: view.top; topMargin: 4;
            left: view.left; leftMargin: 4;
        }
        color: "white"
        font.pixelSize: 16
    }
}