-
Notifications
You must be signed in to change notification settings - Fork 63
/
OurCameraController.qml
190 lines (168 loc) · 4.25 KB
/
OurCameraController.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import QtQml 2.2
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Logic 2.0
import "."
Entity {
id: root
/*
With Qt3D Logic aspect, we can handle various input devices more easily
the flag bits described with LogicalDevice, neat and tidy
Refer: qthelp://org.qt-project.qt3d.570/qt3d/qml-qt3d-input-logicaldevice.html
NOTE: Wheel Axis supported since Qt 5.8
See: http://lists.qt-project.org/pipermail/interest/2016-August/024010.html
and qt3d/src/input/backend/mousedevice.cpp
*/
property Entity camera
property real mouseSensitivity: .5 // Units.dp
property real cameraSpeed: 5.
QtObject {
id: d
property real fineScale: fineModifier.active ? .1 : 1. // with shift key
property real mouseSensitivity: root.mouseSensitivity*fineScale
property real cameraSpeed: root.cameraSpeed*fineScale
property bool lookActionActived: false
property bool orbitActionActived: false
property bool moveActionActived: false
}
KeyboardDevice {
id: keyboardDevice
}
MouseDevice {
id: mouseDevice
sensitivity: d.mouseSensitivity
}
MouseHandler {
sourceDevice: mouseDevice
}
LogicalDevice {
// The LogicalDevice, the "managed flag bits"
enabled: true
actions: [
Action {
// Generally with ActionInput, for button pressing
// including single key press, key chord and key sequence
id: lookAction
ActionInput {
sourceDevice: mouseDevice
buttons: [MouseEvent.LeftButton]
}
},
Action {
id: orbitAction
ActionInput {
sourceDevice: mouseDevice
buttons: [MouseEvent.RightButton]
}
},
Action {
id: moveAction
ActionInput {
sourceDevice: mouseDevice
buttons: [MouseEvent.MiddleButton]
}
},
Action {
id: leftMove
ActionInput {
sourceDevice: keyboardDevice
buttons: [Qt.Key_Left, Qt.Key_A]
}
},
Action {
id: rightMove
ActionInput {
sourceDevice: keyboardDevice
buttons: [Qt.Key_Right, Qt.Key_D]
}
},
Action {
id: upMove
ActionInput {
sourceDevice: keyboardDevice
buttons: [Qt.Key_Up, Qt.Key_W]
}
},
Action {
id: downMove
ActionInput {
sourceDevice: keyboardDevice
buttons: [Qt.Key_Down, Qt.Key_S]
}
},
Action {
id: fineModifier
ActionInput {
sourceDevice: keyboardDevice
buttons: [Qt.Key_Shift]
}
}
]
axes: [
Axis {
// Generally with AxisInput, for analog device value changing
// usually for mouses and joysticks
id: xAxis
AnalogAxisInput {
sourceDevice: mouseDevice
axis: MouseDevice.X
}
},
Axis {
id: yAxis
AnalogAxisInput {
sourceDevice: mouseDevice
axis: MouseDevice.Y
}
},
Axis {
id: wAxis
AnalogAxisInput {
sourceDevice: mouseDevice
axis: MouseDevice.WheelY
}
}
]
}
FrameSwap {
onTriggered: {
var position = root.camera.position;
var yaw = root.camera.yaw;
var pitch = root.camera.pitch;
var fov = root.camera.fieldOfView;
if (upMove.active)
position = position.plus(root.camera.frontVector.times(d.cameraSpeed * dt));
if (downMove.active)
position = position.minus(root.camera.frontVector.times(d.cameraSpeed * dt));
if (rightMove.active)
position = position.plus(root.camera.rightVector.times(d.cameraSpeed * dt));
if (leftMove.active)
position = position.minus(root.camera.rightVector.times(d.cameraSpeed * dt));
// ensure relative drags, not click starts
if (d.lookActionActived && lookAction.active) {
yaw += xAxis.value;
pitch -= yAxis.value;
pitch = (pitch>89.)?89.:(pitch<-89.)?-89.:pitch;
}
d.lookActionActived = lookAction.active;
if (d.orbitActionActived && orbitAction.active) {
}
d.orbitActionActived = orbitAction.active;
if (d.moveActionActived && moveAction.active) {
position = position.plus(
root.camera.upVector.times(-yAxis.value)).plus(
root.camera.rightVector.times(-xAxis.value));
}
d.moveActionActived = moveAction.active;
if (wAxis.value>0)
fov = Geo.mix(fov, 1., wAxis.value * 1e-1 * d.fineScale);
else
fov = Geo.mix(fov, 180., -wAxis.value * 1e-1 * d.fineScale);
root.camera.position = position;
root.camera.yaw = yaw;
root.camera.pitch = pitch;
root.camera.fieldOfView = fov;
}
}
}