Add zoom to keyboard controls

This commit is contained in:
Aaron Fenyes 2024-09-10 04:08:49 -07:00
parent aceac5e5c4
commit d3c9a08d22

View File

@ -172,6 +172,8 @@ fn main() {
let yaw_left = create_signal(0.0);
let roll_ccw = create_signal(0.0);
let roll_cw = create_signal(0.0);
let zoom_in = create_signal(0.0);
let zoom_out = create_signal(0.0);
// controls for general example
let gen_controls = create_node_ref();
@ -262,21 +264,12 @@ fn main() {
let mut last_time = 0.0;
// scene parameters
const NAV_SPEED: f64 = 0.4; // in radians per second
const ROT_SPEED: f64 = 0.4; // in radians per second
const TURNTABLE_SPEED: f64 = 0.1; // in radians per second
const ZOOM_SPEED: f64 = 0.15;
let mut orientation = DMatrix::<f64>::identity(5, 5);
let mut rotation = DMatrix::<f64>::identity(5, 5);
let location = {
const LEN: f64 = -5.0;
const LEN_SQ: f64 = LEN*LEN;
DMatrix::from_column_slice(5, 5, &[
1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, LEN,
0.0, 0.0, 2.0*LEN, 1.0, LEN_SQ,
0.0, 0.0, 0.0, 0.0, 1.0
])
};
let mut location_z: f64 = 5.0;
/* INSTRUMENTS */
let performance = window().unwrap().performance().unwrap();
@ -389,6 +382,8 @@ fn main() {
let yaw_left_val = yaw_left.get();
let roll_ccw_val = roll_ccw.get();
let roll_cw_val = roll_cw.get();
let zoom_in_val = zoom_in.get();
let zoom_out_val = zoom_out.get();
let turntable_val = turntable.get();
// update the construction's orientation
@ -398,7 +393,7 @@ fn main() {
let roll = roll_ccw_val - roll_cw_val;
let ang_vel_from_keyboard =
if pitch != 0.0 || yaw != 0.0 || roll != 0.0 {
NAV_SPEED * Vector3::new(-pitch, yaw, roll).normalize()
ROT_SPEED * Vector3::new(-pitch, yaw, roll).normalize()
} else {
Vector3::zeros()
};
@ -416,6 +411,10 @@ fn main() {
);
orientation = &rotation * &orientation;
// update the construction's location
let zoom = zoom_out_val - zoom_in_val;
location_z *= (time_step * ZOOM_SPEED * zoom).exp();
if scene_changed.get() {
/* INSTRUMENTS */
// measure mean frame interval
@ -427,6 +426,16 @@ fn main() {
}
// find the map from construction space to world space
let location = {
let u = -location_z;
DMatrix::from_column_slice(5, 5, &[
1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, u,
0.0, 0.0, 2.0*u, 1.0, u*u,
0.0, 0.0, 0.0, 0.0, 1.0
])
};
let construction_to_world = &location * &orientation;
// update the construction
@ -492,6 +501,8 @@ fn main() {
|| yaw_right_val != 0.0
|| roll_cw_val != 0.0
|| roll_ccw_val != 0.0
|| zoom_in_val != 0.0
|| zoom_out_val != 0.0
|| turntable_val
);
} else {
@ -504,11 +515,14 @@ fn main() {
let set_nav_signal = move |event: KeyboardEvent, value: f64| {
let mut navigating = true;
let shift = event.shift_key();
match event.key().as_str() {
"ArrowUp" if shift => zoom_in.set(value),
"ArrowDown" if shift => zoom_out.set(value),
"ArrowUp" => pitch_up.set(value),
"ArrowDown" => pitch_down.set(value),
"ArrowRight" if event.shift_key() => roll_cw.set(value),
"ArrowLeft" if event.shift_key() => roll_ccw.set(value),
"ArrowRight" if shift => roll_cw.set(value),
"ArrowLeft" if shift => roll_ccw.set(value),
"ArrowRight" => yaw_right.set(value),
"ArrowLeft" => yaw_left.set(value),
_ => navigating = false
@ -551,8 +565,12 @@ fn main() {
if event.key() == "Shift" {
roll_cw.set(yaw_right.get());
roll_ccw.set(yaw_left.get());
zoom_in.set(pitch_up.get());
zoom_out.set(pitch_down.get());
yaw_right.set(0.0);
yaw_left.set(0.0);
pitch_up.set(0.0);
pitch_down.set(0.0);
} else {
set_nav_signal(event, 1.0);
}
@ -561,8 +579,12 @@ fn main() {
if event.key() == "Shift" {
yaw_right.set(roll_cw.get());
yaw_left.set(roll_ccw.get());
pitch_up.set(zoom_in.get());
pitch_down.set(zoom_out.get());
roll_cw.set(0.0);
roll_ccw.set(0.0);
zoom_in.set(0.0);
zoom_out.set(0.0);
} else {
set_nav_signal(event, 0.0);
}