feat: add keys navigation

This commit is contained in:
Matthieu Bessat 2024-06-25 11:03:22 +02:00
parent e344fc8d42
commit d3ac120df3
2 changed files with 46 additions and 8 deletions

View file

@ -2,6 +2,8 @@
- 2024-06-07: Afficher un point avec raylib dans une fenêtre
- 2024-06-24: Basic navigation
- 2024-06-24: Keyboard navigations, location presets
- 2024-06-25: Afficher les quartiers d'Aubevoye, afficher les rues
- Pouvoir afficher les grandes villes mondiale
- Pouvoir se déplacer de façon basique

View file

@ -246,7 +246,49 @@ fn main() -> Result<()> {
break;
}
// comput mouse position
let mouse_pos = rl.get_mouse_position();
let top_left_bound = Vector2::new(camera.bounds.0.x, camera.bounds.1.y);
let real_mouse_pos = top_left_bound +
mouse_pos.invert_for_canvas().scale_by(camera.canvas_to_real_factor);
let mut zoom_factor = 1.0 + -0.2*rl.get_mouse_wheel_move();
let mut zoom_focus = real_mouse_pos;
let mut v = Vector2::zero();
let mut v1 = 0.1;
let mut v2 = 1.0;
if rl.is_key_down(KeyboardKey::KEY_LEFT_SHIFT) {
v1 = 0.33;
v2 = 1.8;
}
if rl.is_key_down(KeyboardKey::KEY_EQUAL) {
zoom_focus = camera.center;
zoom_factor -= v2*0.01;
}
if rl.is_key_down(KeyboardKey::KEY_MINUS) {
zoom_focus = camera.center;
zoom_factor += v2*0.01;
}
if rl.is_key_pressed(KeyboardKey::KEY_UP) || rl.is_key_pressed(KeyboardKey::KEY_K) {
let bounds_height = (camera.bounds.0.y - camera.bounds.1.y).abs();
v = Vector2::new(0.0, v1*bounds_height);
}
if rl.is_key_pressed(KeyboardKey::KEY_DOWN) || rl.is_key_pressed(KeyboardKey::KEY_J) {
let bounds_height = (camera.bounds.0.y - camera.bounds.1.y).abs();
v = Vector2::new(0.0, -v1*bounds_height);
}
if rl.is_key_pressed(KeyboardKey::KEY_RIGHT) || rl.is_key_pressed(KeyboardKey::KEY_L) {
let bounds_width = (camera.bounds.0.x - camera.bounds.1.x).abs();
v = Vector2::new(v1*bounds_width, 0.);
}
if rl.is_key_pressed(KeyboardKey::KEY_LEFT) || rl.is_key_pressed(KeyboardKey::KEY_H) {
let bounds_width = (camera.bounds.0.x - camera.bounds.1.x).abs();
v = Vector2::new(-v1*bounds_width, 0.);
}
camera.bounds.0 += v;
camera.bounds.1 += v;
let is_mouse_left_down = rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT);
let is_mouse_right_down = rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_RIGHT);
let is_mouse_right_released = rl.is_mouse_button_released(MouseButton::MOUSE_BUTTON_RIGHT);
@ -257,16 +299,10 @@ fn main() -> Result<()> {
// println!("real_to_canvas_factor = {real_to_canvas_factor:?}; canvas_to_real_factor: {canvas_to_real_factor:?}");
// Handle zoom
// when we zoom we want to have the currnet position that the mouse have, to be immobile,
// and everything will fall torward here
let zoom_factor = 1.0 + -0.2*rl.get_mouse_wheel_move();
let top_left_bound = Vector2::new(camera.bounds.0.x, camera.bounds.1.y);
let real_mouse_pos = top_left_bound +
mouse_pos.invert_for_canvas().scale_by(camera.canvas_to_real_factor);
// update the bounds
camera.bounds.0 = real_mouse_pos + (camera.bounds.0 - real_mouse_pos).scale_by(zoom_factor);
camera.bounds.1 = real_mouse_pos + (camera.bounds.1 - real_mouse_pos).scale_by(zoom_factor);
camera.bounds.0 = zoom_focus + (camera.bounds.0 - zoom_focus).scale_by(zoom_factor);
camera.bounds.1 = zoom_focus + (camera.bounds.1 - zoom_focus).scale_by(zoom_factor);
camera.zoom = 1.0/(camera.bounds.0.x - camera.bounds.1.x).abs();
camera.map_zoom = 4.680851064*camera.zoom;