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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use bevy::{
    input::mouse::{MouseMotion, MouseWheel},
    prelude::*,
    render::camera::Camera,
};

pub struct KeyboardConf {
    pub forward: Box<[KeyCode]>,
    pub backward: Box<[KeyCode]>,
    pub left: Box<[KeyCode]>,
    pub right: Box<[KeyCode]>,
    /// sensitivity is calcualted by mx + c where (m: f32, c: f32)
    /// and x is the camera distance
    pub move_sensitivity: (f32, f32),
    pub clockwise: Box<[KeyCode]>,
    pub counter_clockwise: Box<[KeyCode]>,
    pub rotate_sensitivity: f32,
}

impl Default for KeyboardConf {
    fn default() -> Self {
        KeyboardConf {
            forward: Box::new([KeyCode::W, KeyCode::Up]),
            backward: Box::new([KeyCode::S, KeyCode::Down]),
            left: Box::new([KeyCode::A, KeyCode::Left]),
            right: Box::new([KeyCode::D, KeyCode::Right]),
            move_sensitivity: (2.0, 0.1),
            clockwise: Box::new([KeyCode::Q]),
            counter_clockwise: Box::new([KeyCode::E]),
            rotate_sensitivity: std::f32::consts::PI / 100.,
        }
    }
}

pub struct MouseConf {
    pub rotate: MouseButton,
    pub rotate_sensitivity: f32,
    pub drag: MouseButton,
    /// sensitivity is calcualted by mx + c where (m: f32, c: f32)
    /// and x is the camera distance
    pub drag_sensitivity: (f32, f32),
    pub zoom_sensitivity: f32,
}

impl Default for MouseConf {
    fn default() -> Self {
        MouseConf {
            rotate: MouseButton::Right,
            rotate_sensitivity: std::f32::consts::PI / 1000.,
            drag: MouseButton::Left,
            drag_sensitivity: (1., std::f32::consts::PI / 1000.),
            zoom_sensitivity: 5.,
        }
    }
}

/// TODO: Add the ability set more input type here like gamepad
#[derive(Default)]
pub struct CameraRig {
    pub keyboard: KeyboardConf,
    pub mouse: MouseConf,
    // Transforms for (Rig, Camera)
    pub move_to: (Option<Transform>, Option<Transform>),
    pub disable: bool,
}

#[derive(Bundle, Default)]
pub struct CameraRigBundle {
    pub camera_rig: CameraRig,
    pub transform: Transform,
    pub global_transform: GlobalTransform,
}

#[derive(Default)]
struct MouseEventReader {
    motion: EventReader<MouseMotion>,
    wheel: EventReader<MouseWheel>,
}

fn camera_rig_movement(
    mut readers: Local<MouseEventReader>,
    time: Res<Time>,
    keyboard_input: Res<Input<KeyCode>>,
    mouse_input: Res<Input<MouseButton>>,
    mouse_motion_events: Res<Events<MouseMotion>>,
    mouse_wheel_events: Res<Events<MouseWheel>>,
    mut rig_query: Query<(&mut Transform, &mut CameraRig, &Children)>,
    mut camera_query: Query<&mut Transform, With<Camera>>,
    mut follow_query: Query<&mut CameraRigFollow>,
) {
    for (mut rig_transform, mut rig, children) in rig_query.iter_mut() {
        if rig.disable { continue; }
        
        let mut move_to_rig = if let Some(trans) = rig.move_to.0 {
            trans
        } else {
            *rig_transform
        };

        let mut translated = false;
        let move_sensitivity = rig_transform.translation.y * rig.keyboard.move_sensitivity.0
            + rig.keyboard.move_sensitivity.1;
        // Rig Keyboard Movement
        if rig
            .keyboard
            .forward
            .iter()
            .any(|key| keyboard_input.pressed(*key))
        {
            move_to_rig.translation += rig_transform.rotation * Vec3::unit_x() * move_sensitivity;
            translated = true;
        }
        if rig
            .keyboard
            .backward
            .iter()
            .any(|key| keyboard_input.pressed(*key))
        {
            move_to_rig.translation -= rig_transform.rotation * Vec3::unit_x() * move_sensitivity;
            translated = true;
        }
        if rig
            .keyboard
            .right
            .iter()
            .any(|key| keyboard_input.pressed(*key))
        {
            move_to_rig.translation += rig_transform.rotation * Vec3::unit_z() * move_sensitivity;
            translated = true;
        }
        if rig
            .keyboard
            .left
            .iter()
            .any(|key| keyboard_input.pressed(*key))
        {
            move_to_rig.translation -= rig_transform.rotation * Vec3::unit_z() * move_sensitivity;
            translated = true;
        }

        // Rig Keyboard Rotation
        if rig
            .keyboard
            .counter_clockwise
            .iter()
            .any(|key| keyboard_input.pressed(*key))
        {
            move_to_rig.rotate(Quat::from_rotation_y(rig.keyboard.rotate_sensitivity));
        }
        if rig
            .keyboard
            .clockwise
            .iter()
            .any(|key| keyboard_input.pressed(*key))
        {
            move_to_rig.rotate(Quat::from_rotation_y(-rig.keyboard.rotate_sensitivity));
        }

        // Rig Mouse Motion
        let mut mouse_delta_y = 0.;
        for event in readers.motion.iter(&mouse_motion_events) {
            if mouse_input.pressed(rig.mouse.rotate) {
                move_to_rig.rotate(Quat::from_rotation_y(
                    -rig.mouse.rotate_sensitivity * event.delta.x,
                ));
                mouse_delta_y += event.delta.y;
            }
            if mouse_input.pressed(rig.mouse.drag) {
                let drag_sensitivity = rig_transform.translation.y * rig.mouse.drag_sensitivity.0
                    + rig.mouse.drag_sensitivity.1;
                move_to_rig.translation += rig_transform.rotation
                    * Vec3::new(event.delta.y, 0., -event.delta.x)
                    * drag_sensitivity;
                translated = true;
            }
        }

        if translated {
            for mut followable in follow_query.iter_mut() {
                followable.0 = false;
            }
        }

        rig.move_to.0 = Some(move_to_rig);

        // Smoothly move the rig
        if move_to_rig.translation != rig_transform.translation {
            if move_to_rig
                .translation
                .distance(rig_transform.translation)
                .abs()
                > 0.005
            {
                rig_transform.translation = rig_transform.translation.lerp(
                    move_to_rig.translation,
                    time.delta().as_micros() as f32 / 100000.,
                );
            } else {
                rig_transform.translation = move_to_rig.translation;
            }
        }
        if move_to_rig.rotation != rig_transform.rotation {
            if !move_to_rig
                .rotation
                .abs_diff_eq(rig_transform.rotation, 0.00001)
            {
                rig_transform.rotation = rig_transform.rotation.lerp(
                    move_to_rig.rotation,
                    time.delta().as_micros() as f32 / 100000.,
                );
            } else {
                rig_transform.rotation = move_to_rig.rotation;
            }
        }

        let mut found_camera_child = false;
        for child in children.iter() {
            if let Ok(mut transform) = camera_query.get_mut(*child) {
                let mut move_to_camera = if let Some(trans) = rig.move_to.1 {
                    trans
                } else {
                    *transform
                };

                // Camera Mouse Zoom
                for event in readers.wheel.iter(&mouse_wheel_events) {
                    move_to_camera.translation -=
                        move_to_camera.forward() * event.y * rig.mouse.zoom_sensitivity;
                }

                // Camera Mouse Rotate
                if mouse_input.pressed(rig.mouse.rotate) {
                    move_to_camera.rotate(Quat::from_rotation_x(
                        -rig.mouse.rotate_sensitivity * mouse_delta_y,
                    ));
                    move_to_camera.translation =
                        Quat::from_rotation_z(-rig.mouse.rotate_sensitivity * mouse_delta_y)
                            * move_to_camera.translation;
                }

                rig.move_to.1 = Some(move_to_camera);

                // Smoothly move the camera
                if move_to_camera.translation != transform.translation {
                    if move_to_camera
                        .translation
                        .distance(transform.translation)
                        .abs()
                        > 0.005
                    {
                        transform.translation = transform.translation.lerp(
                            move_to_camera.translation,
                            time.delta().as_micros() as f32 / 100000.,
                        );
                    } else {
                        transform.translation = move_to_camera.translation;
                    }
                }
                if move_to_camera.rotation != transform.rotation {
                    if !move_to_camera
                        .rotation
                        .abs_diff_eq(transform.rotation, 0.00001)
                    {
                        transform.rotation = transform.rotation.lerp(
                            move_to_camera.rotation,
                            time.delta().as_micros() as f32 / 100000.,
                        );
                    } else {
                        transform.rotation = move_to_camera.rotation;
                    }
                }

                found_camera_child = true;
                break;
            }
        }

        if !found_camera_child {
            todo!("bevy diagnostics error here / should I panic here?");
        }
    }
}

pub struct CameraRigFollow(pub bool);

fn camera_rig_follow(
    time: Res<Time>,
    mut rig_query: Query<(&mut Transform, &mut CameraRig)>,
    mut follow_query: Query<(&mut Transform, &CameraRigFollow), Changed<Transform>>,
) {
    for (follow_transform, follow) in follow_query.iter_mut() {
        if follow.0 {
            for (mut transform, mut rig) in rig_query.iter_mut() {
                if follow_transform.translation != transform.translation {
                    if follow_transform
                        .translation
                        .distance(transform.translation)
                        .abs()
                        > 0.005
                    {
                        transform.translation = transform.translation.lerp(
                            follow_transform.translation,
                            time.delta().as_micros() as f32 / 100000.,
                        );
                    } else {
                        transform.translation = follow_transform.translation;
                    }
                }

                // Also update the rig translation
                if let Some(mut rig_transform) = rig.move_to.0.as_mut() {
                    rig_transform.translation = transform.translation;
                }
            }
        }
    }
}

pub struct FourXCameraPlugin;

impl Plugin for FourXCameraPlugin {
    fn build(&self, app: &mut AppBuilder) {
        app.add_system(camera_rig_movement.system())
            .add_system(camera_rig_follow.system());
    }
}