MCPcopy Create free account
hub / github.com/PerroEngine/Perro / update_freecam

Function update_freecam

perro_editor/res/scripts/scene/editor_nav.rs:29–107  ·  view source on GitHub ↗
(ctx: &mut ScriptContext<'_, API>)

Source from the content-addressed store, hash-verified

27use std::path::{Path, PathBuf};
28use std::str::FromStr;
29pub fn update_freecam<API: ScriptAPI + ?Sized>(ctx: &mut ScriptContext<'_, API>) {
30 let mode = with_state!(ctx.run, EditorState, ctx.id, |state| {
31 state.viewport_mode.clone()
32 }).unwrap_or_default();
33 if mode == "2D" {
34 update_freecam_2d(ctx);
35 return;
36 }
37 if mode != "3D" {
38 return;
39 }
40
41 let dt = delta_time!(ctx.run).clamp(0.0, 1.0 / 30.0);
42 let mut dx = 0.0;
43 let mut dy = 0.0;
44 let mut dz = 0.0;
45 let text_focus = editor_text_box_has_focus(ctx);
46 if !text_focus && key_down!(ctx.ipt, KeyCode::KeyW) {
47 dz -= 1.0;
48 }
49 if !text_focus && key_down!(ctx.ipt, KeyCode::KeyS) {
50 dz += 1.0;
51 }
52 if !text_focus && key_down!(ctx.ipt, KeyCode::KeyA) {
53 dx -= 1.0;
54 }
55 if !text_focus && key_down!(ctx.ipt, KeyCode::KeyD) {
56 dx += 1.0;
57 }
58 if !text_focus && key_down!(ctx.ipt, KeyCode::Space) {
59 dy += 1.0;
60 }
61 if !text_focus
62 && (key_down!(ctx.ipt, KeyCode::ShiftLeft) || key_down!(ctx.ipt, KeyCode::ShiftRight))
63 {
64 dy -= 1.0;
65 }
66
67 let mouse = if mouse_down!(ctx.ipt, MouseButton::Middle) {
68 mouse_delta!(ctx.ipt)
69 } else {
70 Vector2::ZERO
71 };
72
73 // Zero input means the camera state cannot change: skip the node writes.
74 // Rebuild paths call `apply_freecam` themselves.
75 let moved = dx != 0.0 || dy != 0.0 || dz != 0.0 || mouse.x != 0.0 || mouse.y != 0.0;
76 if moved {
77 let _ = with_state_mut!(ctx.run, EditorState, ctx.id, |state| {
78 let speed = if key_down!(ctx.ipt, KeyCode::ControlLeft) {
79 18.0
80 } else {
81 7.0
82 };
83 let rotation = Quaternion::from_euler_xyz(state.cam_pitch, state.cam_yaw, 0.0);
84 let right = rotation.rotate_vector3(Vector3::new(1.0, 0.0, 0.0));
85 let forward = rotation.rotate_vector3(Vector3::new(0.0, 0.0, -1.0));
86 let up = Vector3::new(0.0, 1.0, 0.0);

Callers

nothing calls this directly

Calls 6

update_freecam_2dFunction · 0.85
find_namedFunction · 0.85
apply_freecamFunction · 0.85
set_labelFunction · 0.85
clampMethod · 0.45

Tested by

no test coverage detected