forked from Match-Yang/SOrbitCameraController
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SOrbitCameraController.qml
172 lines (116 loc) · 3.6 KB
/
SOrbitCameraController.qml
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
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
Entity{
id: root
property Camera camera;
property real dt: 0.001
property real linearSpeed: 1
property real lookSpeed: 500
property real zoomLimit: 0.16
MouseDevice {
id: mouseDevice
sensitivity: 0.001 // Make it more smooth
}
KeyboardDevice {
id: keyboardDevice
}
MouseHandler {
id: mh
readonly property vector3d upVect: Qt.vector3d(0, 1, 0)
property point lastPos;
property real pan;
property real tilt;
sourceDevice: mouseDevice
onPanChanged: root.camera.panAboutViewCenter(pan, upVect);
onTiltChanged: root.camera.tiltAboutViewCenter(tilt);
onPressed: {
lastPos = Qt.point(mouse.x, mouse.y);
}
onPositionChanged: {
if(!root.enabled) return;
// You can change the button as you like for rotation or translation
if (mouse.buttons === 1){ // Left button for rotation
pan = -(mouse.x - lastPos.x) * dt * lookSpeed;
tilt = (mouse.y - lastPos.y) * dt * lookSpeed;
} else if (mouse.buttons === 2) { // Right button for translate
var rx = -(mouse.x - lastPos.x) * dt * linearSpeed;
var ry = (mouse.y - lastPos.y) * dt * linearSpeed;
camera.translate(Qt.vector3d(rx, ry, 0))
} else if (mouse.buttons === 3) { // Left & Right button for zoom
ry = (mouse.y - lastPos.y) * dt * linearSpeed
zoom(ry)
}
lastPos = Qt.point(mouse.x, mouse.y)
}
onWheel: {
if(!root.enabled) return;
zoom(wheel.angleDelta.y * dt * linearSpeed)
}
}
KeyboardHandler {
id: keyboardHandler
sourceDevice: keyboardDevice
focus: true
onPressed: {
switch(event.key)
{
case Qt.Key_PageUp:
{
zoom(120 * dt * linearSpeed)
break;
}
case Qt.Key_PageDown:
{
zoom(-120 * dt * linearSpeed)
break;
}
case Qt.Key_Up:
{
upODown(100 * dt * linearSpeed)
break;
}
case Qt.Key_Down:
{
upODown(-100 * dt * linearSpeed)
break;
}
case Qt.Key_Left:
{
leftORight(-100 * dt * linearSpeed)
break;
}
case Qt.Key_Right:
{
leftORight(100 * dt * linearSpeed)
break;
}
}
}
}
function zoom(rz) {
if (rz > 0 && zoomDistance(camera.position, camera.viewCenter) < zoomLimit) {
return
}
camera.translate(Qt.vector3d(0, 0, rz), Camera.DontTranslateViewCenter)
}
function zoomDistance(posFirst, posSecond) {
return posSecond.minus(posFirst).length()
}
function leftORight(rx)
{
if (rx > 0 && zoomDistance(camera.position, camera.viewCenter) < zoomLimit) {
return
}
camera.setUpVector(Qt.vector3d( 0.0, 1.0, 0.0 ))
camera.translate(Qt.vector3d(rx, 0, 0))
}
function upODown(ry)
{
if (ry > 0 && zoomDistance(camera.position, camera.viewCenter) < zoomLimit) {
return
}
camera.setUpVector(Qt.vector3d( 0.0, 1.0, 0.0 ))
camera.translate(Qt.vector3d(0, ry, 0))
}
}