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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#![allow(dead_code)]
use vecmath::{ Vector3, vec3_add, vec3_scale };
use vecmath::traits::Float;
use quaternion;
use quaternion::Quaternion;
use input;
use input::{ GenericEvent, Key, MouseButton };
use input::Button::{ Keyboard, Mouse };
use Camera;
bitflags!(
flags Keys: u8 {
const ZOOM = 0b00000001,
const PAN = 0b00000010,
const ORBIT = 0b00000100,
}
);
pub struct OrbitZoomCameraSettings<T=f32> {
pub orbit_button: input::Button,
pub zoom_button: input::Button,
pub pan_button: input::Button,
pub orbit_speed: T,
pub pitch_speed: T,
pub pan_speed: T,
pub zoom_speed: T,
}
impl<T: Float> OrbitZoomCameraSettings<T> {
pub fn default() -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
orbit_button : Mouse(MouseButton::Left),
zoom_button : Keyboard(Key::LCtrl),
pan_button : Keyboard(Key::LShift),
orbit_speed: T::from_f32(0.05),
pitch_speed: T::from_f32(0.1),
pan_speed: T::from_f32(0.1),
zoom_speed: T::from_f32(0.1),
}
}
pub fn orbit_button(self, button: input::Button) -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
orbit_button: button,
.. self
}
}
pub fn zoom_button(self, button: input::Button) -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
zoom_button: button,
.. self
}
}
pub fn pan_button(self, button: input::Button) -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
pan_button: button,
.. self
}
}
pub fn orbit_speed(self, s: T) -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
orbit_speed: s,
.. self
}
}
pub fn pitch_speed(self, s: T) -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
pitch_speed: s,
.. self
}
}
pub fn pan_speed(self, s: T) -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
pan_speed: s,
.. self
}
}
pub fn zoom_speed(self, s: T) -> OrbitZoomCameraSettings<T> {
OrbitZoomCameraSettings {
zoom_speed: s,
.. self
}
}
}
pub struct OrbitZoomCamera<T=f32> {
pub target: Vector3<T>,
pub rotation: Quaternion<T>,
pub pitch: T,
pub yaw: T,
pub distance: T,
pub settings: OrbitZoomCameraSettings<T>,
keys: Keys,
}
impl<T: Float>
OrbitZoomCamera<T> {
pub fn new(target: [T; 3], settings: OrbitZoomCameraSettings<T>) -> OrbitZoomCamera<T> {
OrbitZoomCamera {
target: target,
rotation: quaternion::id(),
distance: T::from_f32(10.0),
pitch: T::zero(),
yaw: T::zero(),
keys: Keys::empty(),
settings: settings
}
}
pub fn camera(&self, _dt: f64) -> Camera<T> {
let target_to_camera = quaternion::rotate_vector(
self.rotation,
[T::zero(), T::zero(), self.distance]
);
let mut camera = Camera::new(vec3_add(self.target, target_to_camera));
camera.set_rotation(self.rotation);
camera
}
fn control_camera(&mut self, dx: T, dy: T) {
let _1 = T::one();
let _0 = T::zero();
if self.keys.contains(PAN) {
let dx = dx * self.settings.pan_speed;
let dy = dy * self.settings.pan_speed;
let right = quaternion::rotate_vector(self.rotation, [_1, _0, _0]);
let up = quaternion::rotate_vector(self.rotation, [_0, _1, _0]);
self.target = vec3_add(
vec3_add(self.target, vec3_scale(up, dy)),
vec3_scale(right,dx)
);
} else if self.keys.contains(ZOOM) {
self.distance = self.distance + dy * self.settings.zoom_speed;
} else {
let dx = dx * self.settings.orbit_speed;
let dy = dy * self.settings.orbit_speed;
self.yaw = self.yaw + dx;
self.pitch = self.pitch + dy*self.settings.pitch_speed;
self.rotation = quaternion::mul(
quaternion::axis_angle([_0, _1, _0], self.yaw),
quaternion::axis_angle([_1, _0, _0], self.pitch)
);
}
}
pub fn event<E: GenericEvent>(&mut self, e: &E) {
use input::{ MouseRelativeEvent, MouseScrollEvent, PressEvent, ReleaseEvent };
e.mouse_scroll(|dx, dy| {
let dx = T::from_f64(dx);
let dy = T::from_f64(dy);
self.control_camera(dx, dy);
});
e.mouse_relative(|dx, dy| {
let dx = T::from_f64(dx);
let dy = T::from_f64(dy);
if self.keys.contains(ORBIT){
self.control_camera(-dx, dy);
}
});
e.press(|button| {
match button {
x if x == self.settings.orbit_button => self.keys.insert(ORBIT),
x if x == self.settings.pan_button => self.keys.insert(PAN),
x if x == self.settings.zoom_button => self.keys.insert(ZOOM),
_ => {}
}
});
e.release(|button| {
match button {
x if x == self.settings.orbit_button => self.keys.remove(ORBIT),
x if x == self.settings.pan_button => self.keys.remove(PAN),
x if x == self.settings.zoom_button => self.keys.remove(ZOOM),
_ => {}
}
});
}
}