@@ -421,3 +421,103 @@ fn generate_line(tool_data: &mut LineToolData, snap_data: SnapData, lock_angle:
421
421
422
422
document_points
423
423
}
424
+
425
+ #[ cfg( test) ]
426
+ mod test_line_tool {
427
+ use crate :: { messages:: tool:: common_functionality:: graph_modification_utils:: NodeGraphLayer , test_utils:: test_prelude:: * } ;
428
+ use graph_craft:: document:: value:: TaggedValue ;
429
+
430
+ async fn get_line_node_inputs ( editor : & mut EditorTestUtils ) -> Option < ( DVec2 , DVec2 ) > {
431
+ let document = editor. active_document ( ) ;
432
+ let network_interface = & document. network_interface ;
433
+ let node_id = network_interface
434
+ . selected_nodes ( )
435
+ . selected_visible_and_unlocked_layers ( network_interface)
436
+ . filter_map ( |layer| {
437
+ let node_inputs = NodeGraphLayer :: new ( layer, & network_interface) . find_node_inputs ( "Line" ) ?;
438
+ let ( Some ( & TaggedValue :: DVec2 ( start) ) , Some ( & TaggedValue :: DVec2 ( end) ) ) = ( node_inputs[ 1 ] . as_value ( ) , node_inputs[ 2 ] . as_value ( ) ) else {
439
+ return None ;
440
+ } ;
441
+ Some ( ( start, end) )
442
+ } )
443
+ . next ( ) ;
444
+ node_id
445
+ }
446
+
447
+ #[ tokio:: test]
448
+ async fn test_line_tool_basicdraw ( ) {
449
+ let mut editor = EditorTestUtils :: create ( ) ;
450
+ editor. new_document ( ) . await ;
451
+ editor. drag_tool ( ToolType :: Line , 0. , 0. , 100. , 100. , ModifierKeys :: empty ( ) ) . await ;
452
+ if let Some ( ( start_input, end_input) ) = get_line_node_inputs ( & mut editor) . await {
453
+ match ( start_input, end_input) {
454
+ ( start_input, end_input) => {
455
+ assert ! ( ( start_input - DVec2 :: ZERO ) . length( ) < 1.0 , "Start point should be near (0,0)" ) ;
456
+ assert ! ( ( end_input - DVec2 :: new( 100.0 , 100.0 ) ) . length( ) < 1.0 , "End point should be near (100,100)" ) ;
457
+ }
458
+ }
459
+ }
460
+ }
461
+
462
+ #[ tokio:: test]
463
+ async fn test_line_tool_ctrl_anglelock ( ) {
464
+ let mut editor = EditorTestUtils :: create ( ) ;
465
+ editor. new_document ( ) . await ;
466
+ editor. drag_tool ( ToolType :: Line , 0. , 0. , 100. , 100. , ModifierKeys :: CONTROL ) . await ;
467
+ if let Some ( ( start_input, end_input) ) = get_line_node_inputs ( & mut editor) . await {
468
+ match ( start_input, end_input) {
469
+ ( start_input, end_input) => {
470
+ let line_vec = end_input - start_input;
471
+ let original_angle = line_vec. angle_to ( DVec2 :: X ) ;
472
+ editor. drag_tool ( ToolType :: Line , 0. , 0. , 200. , 50. , ModifierKeys :: CONTROL ) . await ;
473
+ if let Some ( ( updated_start, updated_end) ) = get_line_node_inputs ( & mut editor) . await {
474
+ match ( updated_start, updated_end) {
475
+ ( updated_start, updated_end) => {
476
+ let updated_line_vec = updated_end - updated_start;
477
+ let updated_angle = updated_line_vec. angle_to ( DVec2 :: X ) ;
478
+ assert ! ( ( original_angle - updated_angle) . abs( ) < 0.1 , "Line angle should be locked when Ctrl is kept pressed" ) ;
479
+ assert ! ( ( updated_start - updated_end) . length( ) > 1.0 , "Line should be able to change length when Ctrl is kept pressed" ) ;
480
+ }
481
+ }
482
+ }
483
+ }
484
+ }
485
+ }
486
+ }
487
+
488
+ #[ tokio:: test]
489
+ async fn test_line_tool_alt ( ) {
490
+ let mut editor = EditorTestUtils :: create ( ) ;
491
+ editor. new_document ( ) . await ;
492
+ editor. drag_tool ( ToolType :: Line , 100. , 100. , 200. , 100. , ModifierKeys :: ALT ) . await ;
493
+ if let Some ( ( start_input, end_input) ) = get_line_node_inputs ( & mut editor) . await {
494
+ match ( start_input, end_input) {
495
+ ( start_input, end_input) => {
496
+ let expected_start = DVec2 :: new ( 0. , 100. ) ;
497
+ let expected_end = DVec2 :: new ( 200. , 100. ) ;
498
+ assert ! ( ( start_input - expected_start) . length( ) < 1.0 , "start point should be near (0,100)" ) ;
499
+ assert ! ( ( end_input - expected_end) . length( ) < 1.0 , "end point should be near (200,100)" ) ;
500
+ }
501
+ }
502
+ }
503
+ }
504
+
505
+ #[ tokio:: test]
506
+ async fn test_line_tool_alt_shift_drag ( ) {
507
+ let mut editor = EditorTestUtils :: create ( ) ;
508
+ editor. new_document ( ) . await ;
509
+ editor. drag_tool ( ToolType :: Line , 100. , 100. , 150. , 120. , ModifierKeys :: ALT | ModifierKeys :: SHIFT ) . await ;
510
+ if let Some ( ( start_input, end_input) ) = get_line_node_inputs ( & mut editor) . await {
511
+ match ( start_input, end_input) {
512
+ ( start_input, end_input) => {
513
+ let line_vec = end_input - start_input;
514
+ let angle_radians = line_vec. angle_to ( DVec2 :: X ) ;
515
+ let angle_degrees = angle_radians. to_degrees ( ) ;
516
+ let nearest_angle = ( angle_degrees / 15.0 ) . round ( ) * 15.0 ;
517
+
518
+ assert ! ( ( angle_degrees - nearest_angle) . abs( ) < 1.0 , "Angle should snap to the nearest 15 degrees" ) ;
519
+ }
520
+ }
521
+ }
522
+ }
523
+ }
0 commit comments