Skip to content

Commit 5cd45ad

Browse files
committed
Improve Walk controls, mouse capture, and responsiveness
Add pointer capture, Shift crouching, sidebar shortcuts, gait motion, and saved walking speed and look sensitivity. Reuse collision geometry across camera movement and publish browser capture state only on transitions. Cover input, persistence, physics, and browser fallback behavior.
1 parent 4a5ea54 commit 5cd45ad

10 files changed

Lines changed: 1160 additions & 175 deletions

File tree

‎crates/ketchup-app/src/app.rs‎

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,12 +2072,35 @@ impl KetchupApp {
20722072
ctx.request_repaint();
20732073
}
20742074

2075+
fn sidebar_shortcut(&mut self, ctx: &egui::Context) {
2076+
let walking = self.tool == Tool::Walk;
2077+
let consumed = ctx.input_mut(|i| {
2078+
let mut consumed = false;
2079+
if i.focused {
2080+
i.events.retain(|event| {
2081+
if let egui::Event::Key { key: Key::Tab, pressed: true, repeat, modifiers, .. } = event
2082+
&& (!modifiers.any() || (walking && *modifiers == egui::Modifiers::SHIFT))
2083+
{
2084+
consumed = true;
2085+
if !repeat { self.inspector = !self.inspector; }
2086+
false
2087+
} else { true }
2088+
});
2089+
}
2090+
consumed
2091+
});
2092+
if consumed {
2093+
ctx.memory_mut(|m| m.move_focus(egui::FocusDirection::None));
2094+
ctx.request_repaint();
2095+
}
2096+
}
2097+
20752098
fn keyboard(&mut self, ctx: &egui::Context) {
20762099
// Track Ctrl through dialogs and shortcut early returns. When modeling
20772100
// input is allowed, replay modifier events from the previous state.
20782101
let pull_modifier_was_down = self.push_pull_modifier_down;
20792102
self.push_pull_modifier_down = ctx.input(|i| i.modifiers.ctrl);
2080-
// Walk owns movement keys, including Space and Ctrl, before modeling
2103+
// Walk owns movement keys, including Space and Shift, before modeling
20812104
// shortcuts can select tools, edit geometry, or open project dialogs.
20822105
if self.tool == Tool::Walk {
20832106
self.walk_keyboard(ctx);
@@ -2229,36 +2252,7 @@ impl KetchupApp {
22292252
};
22302253
return;
22312254
}
2232-
let sidebar_tab = ctx.input_mut(|i| {
2233-
let mut consumed = false;
2234-
if i.focused {
2235-
i.events.retain(|event| {
2236-
if let egui::Event::Key {
2237-
key: Key::Tab,
2238-
pressed: true,
2239-
repeat,
2240-
modifiers,
2241-
..
2242-
} = event
2243-
&& !modifiers.any()
2244-
{
2245-
consumed = true;
2246-
if !repeat {
2247-
self.inspector = !self.inspector;
2248-
}
2249-
false
2250-
} else {
2251-
true
2252-
}
2253-
});
2254-
}
2255-
consumed
2256-
});
2257-
if sidebar_tab {
2258-
// egui schedules Tab focus traversal before application shortcuts run.
2259-
ctx.memory_mut(|m| m.move_focus(egui::FocusDirection::None));
2260-
ctx.request_repaint();
2261-
}
2255+
self.sidebar_shortcut(ctx);
22622256
let events = ctx.input(|i| i.events.clone());
22632257
let copy_event = events.iter().any(|e| matches!(e, egui::Event::Copy));
22642258
let cut_event = events.iter().any(|e| matches!(e, egui::Event::Cut));
@@ -6380,6 +6374,7 @@ impl eframe::App for KetchupApp {
63806374
self.update_window(&ctx);
63816375
self.project_dialogs(&ctx);
63826376
self.screenshot_dialog(&ctx);
6377+
self.sync_walk_cursor(&ctx);
63836378
#[cfg(not(target_arch = "wasm32"))]
63846379
Self::window_resize_handles(ui);
63856380
self.sharing_after_frame(&ctx);

‎crates/ketchup-app/src/app_tests.rs‎

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,198 @@ use super::*;
22
use crate::test_ui::ContextExt as _;
33
use eframe::App;
44

5+
fn walk_fixture() -> Harness {
6+
let mut h = Harness::new();
7+
properties_outline(&mut h.app.doc.model, &[
8+
DVec3::new(-10., -10., 0.), DVec3::new(10., -10., 0.),
9+
DVec3::new(10., 10., 0.), DVec3::new(-10., 10., 0.),
10+
]);
11+
h.app.doc.camera.set_standard_view(ketchup_core::StandardView::Top);
12+
h.app.fit(&h.ctx);
13+
h.app.set_tool(Tool::Walk);
14+
h.frame(vec![], egui::Modifiers::NONE);
15+
h.click(h.at(DVec3::ZERO));
16+
h
17+
}
18+
19+
#[test]
20+
fn shift_crouches_ctrl_does_not_and_tab_still_toggles_while_crouching() {
21+
let mut h = walk_fixture();
22+
let standing = h.app.doc.camera.eye();
23+
h.frame(vec![], egui::Modifiers::CTRL);
24+
assert!(!h.app.walk.person.as_ref().unwrap().crouched);
25+
assert!(h.app.doc.camera.eye().abs_diff_eq(standing, 1e-8));
26+
h.frame(vec![], egui::Modifiers::SHIFT);
27+
assert!(h.app.walk.person.as_ref().unwrap().crouched);
28+
assert!((h.app.doc.camera.eye().z - h.app.general_settings.walk.eye_height * 0.6).abs() < 1e-8);
29+
let visible = h.app.inspector;
30+
h.frame(vec![egui::Event::Key { key: Key::Tab, physical_key: None,
31+
pressed: true, repeat: false, modifiers: egui::Modifiers::SHIFT }], egui::Modifiers::SHIFT);
32+
assert_eq!(h.app.inspector, !visible);
33+
assert!(h.app.walk.person.as_ref().unwrap().crouched);
34+
assert_eq!(h.app.tool, Tool::Walk);
35+
h.frame(vec![], egui::Modifiers::NONE);
36+
assert!(!h.app.walk.person.as_ref().unwrap().crouched);
37+
assert!(h.app.doc.camera.eye().abs_diff_eq(standing, 1e-8));
38+
}
39+
40+
#[test]
41+
fn tab_toggles_sidebar_while_walking_and_sitting_without_changing_camera() {
42+
let mut h = walk_fixture();
43+
for sitting in [false, true] {
44+
if sitting { h.frame(vec![], egui::Modifiers::ALT); }
45+
let camera = h.app.doc.camera.clone();
46+
let visible = h.app.inspector;
47+
h.key(Key::Tab);
48+
assert_eq!(h.app.inspector, !visible);
49+
assert_eq!(h.app.tool, Tool::Walk);
50+
assert_eq!(h.app.doc.camera, camera);
51+
assert_eq!(h.app.walk.person.as_ref().unwrap().sitting, sitting);
52+
h.key(Key::Tab);
53+
assert_eq!(h.app.inspector, visible);
54+
assert_eq!(h.app.doc.camera, camera);
55+
}
56+
assert!(h.app.doc.history.is_empty());
57+
}
58+
59+
#[test]
60+
fn walk_camera_updates_reuse_collision_and_document_geometry() {
61+
let mut h = walk_fixture();
62+
let builds = h.app.walk.world_builds;
63+
let revision = h.app.document_model.revision();
64+
h.app.general_settings.orbit_speed = 2.;
65+
let yaw = h.app.doc.camera.yaw;
66+
h.frame(vec![egui::Event::MouseMoved(egui::vec2(10., 0.))], egui::Modifiers::NONE);
67+
assert!((h.app.doc.camera.yaw - yaw + 0.091).abs() < 1e-8);
68+
for _ in 0..20 {
69+
h.frame(vec![egui::Event::MouseMoved(egui::vec2(3., 1.))], egui::Modifiers::NONE);
70+
assert_eq!(h.app.walk.world_builds, builds);
71+
assert_eq!(h.app.document_model.revision(), revision);
72+
assert!(!h.app.document_model_dirty);
73+
}
74+
h.key(Key::W);
75+
assert_eq!(h.app.walk.world_builds, builds);
76+
assert_eq!(h.app.document_model.revision(), revision);
77+
// A real model change must still invalidate the collision world.
78+
h.app.doc.model.add_edge(DVec3::Z * 3., DVec3::new(1., 0., 3.)).unwrap();
79+
h.frame(vec![], egui::Modifiers::NONE);
80+
assert!(h.app.walk.world_builds > builds);
81+
assert!(h.app.walk.person.is_none());
82+
}
83+
84+
#[cfg(not(target_arch = "wasm32"))]
85+
#[test]
86+
fn walking_look_sensitivity_applies_to_both_axes_without_repositioning() {
87+
let mut h = walk_fixture();
88+
for (sensitivity, yaw_delta, pitch_delta) in [(0.25, 0.0175, 0.007), (0.65, 0.0455, 0.0182), (1.5, 0.105, 0.042)] {
89+
let before = h.app.doc.camera.clone();
90+
h.app.general_settings.walk.look_sensitivity = sensitivity;
91+
h.frame(vec![egui::Event::MouseMoved(egui::vec2(10., 4.))], egui::Modifiers::NONE);
92+
assert!((h.app.doc.camera.yaw - before.yaw + yaw_delta).abs() < 1e-8);
93+
assert!((h.app.doc.camera.pitch - before.pitch - pitch_delta).abs() < 1e-8);
94+
assert!(h.app.doc.camera.eye().abs_diff_eq(before.eye(), 1e-8));
95+
assert_eq!(h.app.tool, Tool::Walk);
96+
assert!(h.app.walk.person.is_some());
97+
}
98+
}
99+
100+
#[test]
101+
fn walking_has_a_small_eye_bob_that_does_not_move_the_feet_and_settles_at_rest() {
102+
let mut h = walk_fixture();
103+
let event = |pressed| egui::Event::Key { key: Key::W, physical_key: None,
104+
pressed, repeat: false, modifiers: egui::Modifiers::NONE };
105+
h.frame(vec![event(true)], egui::Modifiers::NONE);
106+
let mut lowest = 0_f64;
107+
for _ in 0..60 {
108+
h.frame(vec![], egui::Modifiers::NONE);
109+
let person = h.app.walk.person.as_ref().unwrap();
110+
let bob = h.app.doc.camera.eye().z - person.feet.z - h.app.general_settings.walk.eye_height;
111+
lowest = lowest.min(bob);
112+
assert!((-0.006000001..=1e-8).contains(&bob));
113+
assert_eq!(person.feet.z, 0.);
114+
}
115+
assert!(lowest < -0.002);
116+
h.frame(vec![event(false)], egui::Modifiers::NONE);
117+
let feet = h.app.walk.person.as_ref().unwrap().feet;
118+
for _ in 0..120 { h.frame(vec![], egui::Modifiers::NONE); }
119+
assert_eq!(h.app.walk.person.as_ref().unwrap().feet, feet);
120+
assert!((h.app.doc.camera.eye().z - h.app.general_settings.walk.eye_height).abs() < 1e-8);
121+
}
122+
123+
#[cfg(not(target_arch = "wasm32"))]
124+
#[test]
125+
fn walk_mouse_look_needs_no_button_uses_raw_motion_and_releases_capture() {
126+
let mut h = Harness::new();
127+
properties_outline(&mut h.app.doc.model, &[
128+
DVec3::new(-10., -10., 0.), DVec3::new(10., -10., 0.),
129+
DVec3::new(10., 10., 0.), DVec3::new(-10., 10., 0.),
130+
]);
131+
h.app.doc.camera.set_standard_view(ketchup_core::StandardView::Top);
132+
h.app.fit(&h.ctx);
133+
let original = h.app.doc.camera.clone();
134+
h.app.set_tool(Tool::Walk);
135+
h.frame(vec![], egui::Modifiers::NONE);
136+
let pos = h.at(DVec3::ZERO);
137+
let placed = h.click(pos);
138+
assert!(window_commands(&placed).contains(&egui::ViewportCommand::CursorVisible(false)));
139+
let eye = h.app.doc.camera.eye();
140+
let yaw = h.app.doc.camera.yaw;
141+
h.frame(vec![egui::Event::PointerMoved(pos + egui::vec2(10., -5.))], egui::Modifiers::NONE);
142+
assert!(!h.ctx.input(|i| i.pointer.any_down()));
143+
assert!((h.app.doc.camera.yaw - yaw + 0.0455).abs() < 1e-8);
144+
assert!((h.app.doc.camera.pitch + 0.02275).abs() < 1e-8);
145+
assert!(h.app.doc.camera.eye().abs_diff_eq(eye, 1e-8));
146+
let yaw = h.app.doc.camera.yaw;
147+
let pitch = h.app.doc.camera.pitch;
148+
// Absolute and raw events often arrive together. Count raw motion once.
149+
h.frame(vec![egui::Event::PointerMoved(pos + egui::vec2(300., 100.)),
150+
egui::Event::MouseMoved(egui::vec2(12., 6.))], egui::Modifiers::NONE);
151+
assert!((h.app.doc.camera.yaw - yaw + 0.0546).abs() < 1e-8);
152+
assert!((h.app.doc.camera.pitch - pitch - 0.0273).abs() < 1e-8);
153+
let yaw = h.app.doc.camera.yaw;
154+
h.frame(vec![egui::Event::MouseMoved(egui::vec2(12., 0.))], egui::Modifiers::NONE);
155+
assert!((h.app.doc.camera.yaw - yaw + 0.0546).abs() < 1e-8);
156+
let before_dialog = h.app.doc.camera.clone();
157+
h.app.settings_open = true;
158+
let paused = h.frame(vec![egui::Event::MouseMoved(egui::vec2(100., 100.))], egui::Modifiers::NONE);
159+
assert_eq!(h.app.doc.camera, before_dialog);
160+
assert!(window_commands(&paused).contains(&egui::ViewportCommand::CursorVisible(true)));
161+
h.app.settings_open = false;
162+
h.frame(vec![], egui::Modifiers::NONE);
163+
let exited = h.frame(vec![egui::Event::Key {
164+
key: Key::Escape, physical_key: None, pressed: true, repeat: false,
165+
modifiers: egui::Modifiers::NONE,
166+
}], egui::Modifiers::NONE);
167+
assert_eq!(h.app.doc.camera, original);
168+
assert_eq!(h.app.tool, Tool::Select);
169+
assert!(window_commands(&exited).contains(&egui::ViewportCommand::CursorGrab(egui::CursorGrab::None)));
170+
assert!(window_commands(&exited).contains(&egui::ViewportCommand::CursorVisible(true)));
171+
}
172+
173+
#[cfg(not(target_arch = "wasm32"))]
174+
#[test]
175+
fn seated_mouse_look_without_buttons_keeps_eye_fixed_and_yaw_within_180_degrees() {
176+
let mut h = Harness::new();
177+
properties_outline(&mut h.app.doc.model, &[
178+
DVec3::new(-10., -10., 0.), DVec3::new(10., -10., 0.),
179+
DVec3::new(10., 10., 0.), DVec3::new(-10., 10., 0.),
180+
]);
181+
h.app.doc.camera.set_standard_view(ketchup_core::StandardView::Top);
182+
h.app.fit(&h.ctx);
183+
h.app.set_tool(Tool::Walk);
184+
h.frame(vec![], egui::Modifiers::NONE);
185+
h.click(h.at(DVec3::ZERO));
186+
h.frame(vec![], egui::Modifiers::ALT);
187+
let eye = h.app.doc.camera.eye();
188+
let center = h.app.doc.camera.yaw;
189+
h.frame(vec![egui::Event::MouseMoved(egui::vec2(1000., 0.))], egui::Modifiers::NONE);
190+
assert!((h.app.doc.camera.yaw - center + std::f64::consts::FRAC_PI_2).abs() < 1e-8);
191+
assert!(h.app.doc.camera.eye().abs_diff_eq(eye, 1e-8));
192+
h.frame(vec![egui::Event::MouseMoved(egui::vec2(-2000., 0.))], egui::Modifiers::NONE);
193+
assert!((h.app.doc.camera.yaw - center - std::f64::consts::FRAC_PI_2).abs() < 1e-8);
194+
assert!(h.app.doc.camera.eye().abs_diff_eq(eye, 1e-8));
195+
}
196+
5197
#[test]
6198
fn walk_button_places_eye_on_a_face_and_movement_keys_do_not_edit() {
7199
let mut h = Harness::new();

‎crates/ketchup-app/src/settings.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,20 @@ impl KetchupApp {
460460
);
461461
ui.separator();
462462
ui.text_label(RichText::new("Walking person").strong());
463+
inspector_field(ui, "walk_speed", "Walking speed", |ui| {
464+
ui.add(egui::DragValue::new(&mut self.general_settings.walk.speed_kmh)
465+
.speed(0.1).range(0.5..=12.).suffix(" km/h"))
466+
});
467+
inspector_field(ui, "walk_look_sensitivity", "Look sensitivity", |ui| {
468+
let mut percent = self.general_settings.walk.look_sensitivity * 100.;
469+
let response = ui.add(egui::DragValue::new(&mut percent)
470+
.speed(1.).range(10. ..=200.).suffix(" %"))
471+
.on_hover_text("Scales mouse look while walking. 100% matches Orbit sensitivity.");
472+
if response.changed() {
473+
self.general_settings.walk.look_sensitivity = percent / 100.;
474+
}
475+
response
476+
});
463477
ui.text_weak("Eye height is measured above the floor. Body width and depth determine obstacle clearance; allow 12 cm above the eyes for the head.");
464478
let unit = self.doc.display.units.length;
465479
for (key, label, value, min, max) in [
@@ -479,7 +493,7 @@ impl KetchupApp {
479493
response
480494
});
481495
}
482-
ui.text_weak("WASD / arrows: walk · Ctrl: crouch · Space: jump · Alt: sit / stand · Drag: look · Escape: exit");
496+
ui.text_weak("WASD / arrows: walk · Shift: crouch · Space: jump · Alt: sit / stand · Move mouse: look · Escape: exit");
483497
}
484498
Section::Shortcuts => {
485499
ui.text_weak(tr!("settings.click_a_shortcut_then_press_a_letter_or"));

‎crates/ketchup-app/src/shortcuts.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ impl KetchupApp {
195195
(tr!("shortcuts.toggle_sidebar"), tr!("shortcuts.tab")),
196196
("Walk through model", "Walk button → click a floor face"),
197197
("Move while walking", "WASD / Arrow keys"),
198-
("Look while walking", "Drag mouse"),
199-
("Crouch / Jump / Sit or stand", "Ctrl / Space / Alt"),
198+
("Look while walking", "Move mouse"),
199+
("Crouch / Jump / Sit or stand", "Shift / Space / Alt"),
200200
("Exit Walk", "Escape"),
201201
],
202202
),

‎crates/ketchup-app/src/sidebar.rs‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ mod guide_preference_tests {
148148
app.set_tool(Tool::Walk);
149149
app.walk.sitting = true;
150150
app.general_settings.section = settings::Section::Navigation;
151-
app.general_settings.walk = walk::Settings { eye_height: 1.8, width: 0.6, depth: 0.4 };
151+
app.general_settings.walk = walk::Settings { eye_height: 1.8, width: 0.6, depth: 0.4, ..Default::default() };
152152
app.persist_inspector_changes(&ctx, Some(&mut storage), false);
153153
assert!(storage.flushed);
154154
assert!(!app.project.modified);
@@ -165,6 +165,56 @@ mod guide_preference_tests {
165165
assert_eq!(restored.general_settings.walk, app.general_settings.walk);
166166
}
167167

168+
#[test]
169+
fn walk_speed_defaults_and_saves_immediately_without_a_model_edit() {
170+
let old: InspectorPreferences = serde_json::from_str(
171+
r#"{"general":{"walk":{"eye_height":1.8,"width":0.6,"depth":0.4}}}"#,
172+
).unwrap();
173+
assert_eq!(old.general.walk.speed_kmh, 6.);
174+
assert_eq!(old.general.walk.eye_height, 1.8);
175+
let ctx = egui::Context::default();
176+
let mut app = KetchupApp::with_document(Document::default(), None, true);
177+
let mut storage = Storage::default();
178+
for speed in [2., 6., 9.5] {
179+
app.general_settings.walk.speed_kmh = speed;
180+
storage.flushed = false;
181+
app.persist_inspector_changes(&ctx, Some(&mut storage), false);
182+
assert!(storage.flushed);
183+
assert!(!app.project.modified);
184+
assert!(app.dirty_since.is_none());
185+
let mut restored = KetchupApp::with_document(Document::default(), None, true);
186+
restored.restore_inspector_preferences(&egui::Context::default(), Some(&storage));
187+
assert_eq!(restored.general_settings.walk.speed_kmh, speed);
188+
restored.replace_project(Document::default(), None, &ctx);
189+
assert_eq!(restored.general_settings.walk.speed_kmh, speed);
190+
}
191+
}
192+
193+
#[test]
194+
fn walk_look_sensitivity_defaults_and_saves_without_a_model_edit() {
195+
let old: InspectorPreferences = serde_json::from_str(
196+
r#"{"general":{"walk":{"speed_kmh":9,"eye_height":1.8}}}"#,
197+
).unwrap();
198+
assert_eq!(old.general.walk.look_sensitivity, 0.65);
199+
assert_eq!(old.general.walk.speed_kmh, 9.);
200+
let ctx = egui::Context::default();
201+
let mut app = KetchupApp::with_document(Document::default(), None, true);
202+
let mut storage = Storage::default();
203+
for sensitivity in [0.25, 0.65, 1.5] {
204+
app.general_settings.walk.look_sensitivity = sensitivity;
205+
storage.flushed = false;
206+
app.persist_inspector_changes(&ctx, Some(&mut storage), false);
207+
assert!(storage.flushed);
208+
assert!(!app.project.modified);
209+
assert!(app.dirty_since.is_none());
210+
let mut restored = KetchupApp::with_document(Document::default(), None, true);
211+
restored.restore_inspector_preferences(&egui::Context::default(), Some(&storage));
212+
assert_eq!(restored.general_settings.walk.look_sensitivity, sensitivity);
213+
restored.replace_project(Document::default(), None, &ctx);
214+
assert_eq!(restored.general_settings.walk.look_sensitivity, sensitivity);
215+
}
216+
}
217+
168218
#[test]
169219
fn push_pull_face_mode_defaults_and_saves_immediately_without_a_model_edit() {
170220
for json in ["{}", r#"{"tools":{}}"#] {

0 commit comments

Comments
 (0)