@@ -54,7 +54,7 @@ func _ready() -> void:
5454 profile = _load_profile ()
5555 _update_mapping_elements ()
5656 delete_button .button_up .connect (on_delete )
57-
57+
5858 # Setup the gamepad type dropdown
5959 gamepad_type_dropdown .clear ()
6060 for gamepad_type : String in self .gamepad_types :
@@ -64,16 +64,29 @@ func _ready() -> void:
6464 if self .profile :
6565 var gamepad_type := self .get_selected_target_gamepad ()
6666 profile_gamepad = InputPlumberProfile .get_target_device_string (gamepad_type )
67+ logger .debug ("Setting gamepad to " + profile_gamepad )
68+ else :
69+ logger .debug ("No profile, unable to set gamepad type." )
6770 self ._update_mapping_elements ()
6871 gamepad_type_dropdown .item_selected .connect (on_gamepad_selected )
69-
72+
73+ # Load the default profile
74+ var profile_path = settings_manager .get_value ("input" , "gamepad_profile" )
75+ profile_gamepad = settings_manager .get_value ("input" , "gamepad_profile_target" )
76+ for gamepad in input_plumber .composite_devices :
77+ _set_gamepad_profile (gamepad , profile_path )
78+
7079 # Grab focus when the mapper exits
7180 var on_state_changed := func (_from : State , to : State ):
7281 if to :
7382 return
7483 mapping_focus_group .grab_focus ()
7584 state_machine .state_changed .connect (on_state_changed )
7685
86+ # Ensure new devices are set to the correct profile when added
87+ input_plumber .composite_device_added .connect (_set_gamepad_profile )
88+ input_plumber .composite_device_changed .connect (_set_gamepad_profile )
89+
7790
7891## Called when the gamepad settings state is entered
7992func _on_state_entered (_from : State ) -> void :
@@ -145,6 +158,7 @@ func _on_state_entered(_from: State) -> void:
145158 # Set the profile text to the game name
146159 profile_label .text = library_item .name
147160
161+
148162 # Check to see if the given game has a gamepad profile
149163 var profile_path := settings_manager .get_library_value (library_item , "gamepad_profile" , "" ) as String
150164 var profile_target_gamepad := settings_manager .get_library_value (library_item , "gamepad_profile_target" , "" ) as String
@@ -558,13 +572,37 @@ func get_target_gamepad_text(gamepad_type: InputPlumberProfile.TargetDevice) ->
558572 return "Generic Gamepad"
559573
560574
575+ # Set the given profile for the given composte device.
576+ func _set_gamepad_profile (gamepad : InputPlumber .CompositeDevice , profile_path : String = "" ) -> void :
577+ if profile_path == "" :
578+ if gamepad_state .has_meta ("item" ):
579+ library_item = gamepad_state .get_meta ("item" ) as LibraryItem
580+
581+ # If no library item was set, but there's a running app, try to see if
582+ # there is a library item for it instead.
583+ if not library_item :
584+ library_item = launch_manager .get_current_app_library_item ()
585+
586+ # If no library item was set with the state, then use the default
587+ if not library_item :
588+ profile_path = settings_manager .get_value ("input" , "gamepad_profile" )
589+ else :
590+ profile_path = settings_manager .get_library_value (library_item , "gamepad_profile" , "" )
591+
592+ logger .debug ("Setting " + gamepad .name + " to profile: " + profile_path )
593+ gamepad .load_profile_path (profile_path )
594+ if not profile_gamepad .is_empty ():
595+ logger .debug ("Setting target gamepad to: " + profile_gamepad )
596+ gamepad .set_target_devices ([profile_gamepad , "keyboard" , "mouse" ])
597+
598+
561599# Save the current profile to a file
562600func _save_profile () -> void :
563601 var notify := Notification .new ("" )
564602 if not profile :
565603 logger .debug ("No profile loaded to save" )
566604 return
567-
605+
568606 # Handle global gamepad profiles
569607 if not library_item :
570608 logger .debug ("No library item loaded to associate profile with" )
@@ -576,10 +614,14 @@ func _save_profile() -> void:
576614 notify .text = "Failed to save global gamepad profile"
577615 notification_manager .show (notify )
578616 return
579-
617+
580618 # Update the game settings to use this global profile
581619 settings_manager .set_value ("input" , "gamepad_profile" , path )
582620 settings_manager .set_value ("input" , "gamepad_profile_target" , profile_gamepad )
621+
622+ for gamepad in input_plumber .composite_devices :
623+ _set_gamepad_profile (gamepad , path )
624+
583625 logger .debug ("Saved global gamepad profile to: " + path )
584626 notify .text = "Global gamepad profile saved"
585627 notification_manager .show (notify )
0 commit comments