@@ -2,6 +2,198 @@ use super::*;
22use crate :: test_ui:: ContextExt as _;
33use 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]
6198fn walk_button_places_eye_on_a_face_and_movement_keys_do_not_edit ( ) {
7199 let mut h = Harness :: new ( ) ;
0 commit comments