summaryrefslogtreecommitdiffstats
path: root/src/imports/threed/DefaultNavigator.qml
blob: 70b0ac6b15ad24686491d7e8127109cbfc5381fa (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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtQuick3D examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

import Qt 4.7
import Qt3D 1.0

// This is an example of how to implement a custom navigation strategy
// to replace the default strategy implemented by "Viewport".

MouseArea {
    // Set this property to the "Viewport" item to be controlled
    // by this navigator.  By default we assume that it is our parent.
    property variant viewport: parent

    // The navigator fills the entire viewport.
    anchors.fill: viewport

    // Private variables.
    QtObject {
        id: d_ptr
        property bool panning: false
        property int startX: 0
        property int startY: 0
        property variant startEye
        property variant startCenter
        property variant startUpVector
        property variant pressedObject: null
        property variant enteredObject: null
        property bool sawDoubleClick: false
    }

    // Handle mouse press events.
    onPressed: {
        // Determine which object was pressed in the viewport.
        var objectUnderMouse;
        if (!d_ptr.panning && viewport.picking)
            objectUnderMouse = viewport.objectForPoint(mouse.x, mouse.y);
        else
            objectUnderMouse = null;
        if (d_ptr.pressedObject) {
            // We already have a pressed object, so nothing to do.
            mouse.accepted = true;
        } else if (Qt.isQtObject(objectUnderMouse)) {
            // Mouse press on a new object.
            d_ptr.pressedObject = objectUnderMouse;
            d_ptr.enteredObject = null;
            d_ptr.sawDoubleClick = false;
            objectUnderMouse.pressed();
            mouse.accepted = true;
        } else if (viewport.navigation && mouse.button == Qt.LeftButton) {
            // Start panning the view.
            d_ptr.panning = true;
            d_ptr.startX = mouse.x;
            d_ptr.startY = mouse.y;
            d_ptr.startEye = viewport.camera.eye;
            d_ptr.startCenter = viewport.camera.center;
            d_ptr.startUpVector = viewport.camera.upVector;
            mouse.accepted = true;
        }
    }

    // Handle mouse release events.
    onReleased: {
        if (d_ptr.panning && mouse.button == Qt.LeftButton) {
            d_ptr.panning = false;
            mouse.accepted = true;
        }
        if (d_ptr.pressedObject && mouse.button == Qt.LeftButton) {
            // Deliver the release event to the pressed object.
            var objectUnderMouse = viewport.objectForPoint(mouse.x, mouse.y);
            if (objectUnderMouse == d_ptr.pressedObject && !d_ptr.sawDoubleClick) {
                d_ptr.pressedObject.released();
                d_ptr.pressedObject.clicked();
            } else {
                d_ptr.pressedObject.released();
            }
            if (hoverEnabled) {
                if (Qt.isQtObject(objectUnderMouse))
                    d_ptr.enteredObject = objectUnderMouse;
                else
                    d_ptr.enteredObject = null;
                if (d_ptr.enteredObject != d_ptr.pressedObject) {
                    d_ptr.pressedObject.hoverLeave();
                    if (d_ptr.enteredObject)
                        d_ptr.enteredObject.hoverEnter();
                }
            }
            d_ptr.pressedObject = null;
            mouse.accepted = true;
        }
    }

    // Handle mouse double click events.
    onDoubleClicked: {
        if (d_ptr.pressedObject) {
            var objectUnderMouse = viewport.objectForPoint(mouse.x, mouse.y);
            if (objectUnderMouse == d_ptr.pressedObject) {
                d_ptr.pressedObject.doubleClicked();
                d_ptr.sawDoubleClick = true;
                mouse.accepted = true;
            }
        }
    }

    // Handle mouse move events.
    onPositionChanged: {
        if (d_ptr.panning) {
            var deltaX = mouse.x - d_ptr.startX;
            var deltaY = mouse.y - d_ptr.startY;
            var angleAroundY = deltaX * 90 / width;
            var angleAroundX = deltaY * 90 / height;

            viewport.camera.eye = d_ptr.startEye;
            viewport.camera.center = d_ptr.startCenter;
            viewport.camera.upVector = d_ptr.startUpVector;
            viewport.camera.tiltPanRollCenter
                (-angleAroundX, -angleAroundY, 0, "TiltPanRoll");
        } else if (viewport.picking && hoverEnabled) {
            // Handle object enter/leave events when mouse hover is enabled.
            var objectUnderMouse = viewport.objectForPoint(mouse.x, mouse.y);
            if (!d_ptr.pressedObject) {
                if (Qt.isQtObject(objectUnderMouse)) {
                    if (d_ptr.enteredObject != objectUnderMouse) {
                        if (d_ptr.enteredObject)
                            d_ptr.enteredObject.hoverLeave();
                        d_ptr.enteredObject = objectUnderMouse;
                        d_ptr.enteredObject.hoverEnter();
                    }
                } else if (d_ptr.enteredObject) {
                    d_ptr.enteredObject.hoverLeave();
                    d_ptr.enteredObject = null;
                }
            }
        }
    }

    // Handle leave events.
    onExited: {
        if (hoverEnabled && !d_ptr.pressedObject && d_ptr.enteredObject) {
            d_ptr.enteredObject.hoverLeave();
            d_ptr.enteredObject = null;
        }
    }
}