1+ using System ;
12using System . Linq ;
23using System . Text . Json ;
34using Godot ;
@@ -13,14 +14,38 @@ public static class SaveSystem
1314 private static ConfigFile _curConfigData ;
1415
1516 private const float DefaultVolume = 1f ;
16- private const string DefaultInput = "WASD" ;
17+ private const string DefaultInputType = "WASD" ;
18+ private const int DefaultInputKeyboardUp = 87 ; //W
19+ private const int DefaultInputKeyboardLeft = 65 ; //A
20+ private const int DefaultInputKeyboardDown = 83 ; //S
21+ private const int DefaultInputKeyboardRight = 68 ; //D
22+ private const int DefaultInputKeyboardSecondary = 4194325 ; //Shift
23+ private const int DefaultInputKeyboardInventory = 73 ; //I
24+ private const int DefaultInputControllerUp = 3 ; //Y
25+ private const int DefaultInputControllerLeft = 2 ; //X
26+ private const int DefaultInputControllerDown = 0 ; //A
27+ private const int DefaultInputControllerRight = 1 ; //B
28+ private const int DefaultInputControllerSecondary = 10 ; //right bumper
29+ private const int DefaultInputControllerInventory = 4 ; //back button
1730 private const string DefaultLanguage = "en" ;
1831 private const bool DefaultHighCon = false ;
1932
2033 public enum ConfigSettings
2134 {
2235 Volume ,
23- InputKey ,
36+ InputType ,
37+ InputKeyboardUp ,
38+ InputKeyboardLeft ,
39+ InputKeyboardDown ,
40+ InputKeyboardRight ,
41+ InputKeyboardSecondary ,
42+ InputKeyboardInventory ,
43+ InputControllerUp ,
44+ InputControllerLeft ,
45+ InputControllerDown ,
46+ InputControllerRight ,
47+ InputControllerSecondary ,
48+ InputControllerInventory ,
2449 LanguageKey ,
2550 HighContrast ,
2651 }
@@ -32,7 +57,19 @@ private static void InitConfig()
3257 {
3358 _curConfigData = new ConfigFile ( ) ;
3459 UpdateConfig ( ConfigSettings . Volume , DefaultVolume ) ;
35- UpdateConfig ( ConfigSettings . InputKey , DefaultInput ) ;
60+ UpdateConfig ( ConfigSettings . InputType , DefaultInputType ) ;
61+ UpdateConfig ( ConfigSettings . InputKeyboardUp , DefaultInputKeyboardUp ) ;
62+ UpdateConfig ( ConfigSettings . InputKeyboardLeft , DefaultInputKeyboardLeft ) ;
63+ UpdateConfig ( ConfigSettings . InputKeyboardDown , DefaultInputKeyboardDown ) ;
64+ UpdateConfig ( ConfigSettings . InputKeyboardRight , DefaultInputKeyboardRight ) ;
65+ UpdateConfig ( ConfigSettings . InputKeyboardSecondary , DefaultInputKeyboardSecondary ) ;
66+ UpdateConfig ( ConfigSettings . InputKeyboardInventory , DefaultInputKeyboardInventory ) ;
67+ UpdateConfig ( ConfigSettings . InputControllerUp , DefaultInputControllerUp ) ;
68+ UpdateConfig ( ConfigSettings . InputControllerLeft , DefaultInputControllerLeft ) ;
69+ UpdateConfig ( ConfigSettings . InputControllerDown , DefaultInputControllerDown ) ;
70+ UpdateConfig ( ConfigSettings . InputControllerRight , DefaultInputControllerRight ) ;
71+ UpdateConfig ( ConfigSettings . InputControllerSecondary , DefaultInputControllerSecondary ) ;
72+ UpdateConfig ( ConfigSettings . InputControllerInventory , DefaultInputControllerInventory ) ;
3673 UpdateConfig ( ConfigSettings . LanguageKey , DefaultLanguage ) ;
3774 UpdateConfig ( ConfigSettings . HighContrast , DefaultHighCon ) ;
3875 }
@@ -51,9 +88,45 @@ public static void UpdateConfig(ConfigSettings setting, Variant value)
5188 case ConfigSettings . Volume :
5289 _curConfigData . SetValue ( "Options" , "Volume" , value ) ;
5390 break ;
54- case ConfigSettings . InputKey :
91+ case ConfigSettings . InputType :
5592 _curConfigData . SetValue ( "Options" , "InputKey" , value ) ;
5693 break ;
94+ case ConfigSettings . InputKeyboardUp :
95+ _curConfigData . SetValue ( "Options" , "InputKeyboardUp" , value ) ;
96+ break ;
97+ case ConfigSettings . InputKeyboardLeft :
98+ _curConfigData . SetValue ( "Options" , "InputKeyboardLeft" , value ) ;
99+ break ;
100+ case ConfigSettings . InputKeyboardDown :
101+ _curConfigData . SetValue ( "Options" , "InputKeyboardDown" , value ) ;
102+ break ;
103+ case ConfigSettings . InputKeyboardRight :
104+ _curConfigData . SetValue ( "Options" , "InputKeyboardRight" , value ) ;
105+ break ;
106+ case ConfigSettings . InputKeyboardSecondary :
107+ _curConfigData . SetValue ( "Options" , "InputKeyboardSecondary" , value ) ;
108+ break ;
109+ case ConfigSettings . InputKeyboardInventory :
110+ _curConfigData . SetValue ( "Options" , "InputKeyboardInventory" , value ) ;
111+ break ;
112+ case ConfigSettings . InputControllerUp :
113+ _curConfigData . SetValue ( "Options" , "InputControllerUp" , value ) ;
114+ break ;
115+ case ConfigSettings . InputControllerLeft :
116+ _curConfigData . SetValue ( "Options" , "InputControllerLeft" , value ) ;
117+ break ;
118+ case ConfigSettings . InputControllerDown :
119+ _curConfigData . SetValue ( "Options" , "InputControllerDown" , value ) ;
120+ break ;
121+ case ConfigSettings . InputControllerRight :
122+ _curConfigData . SetValue ( "Options" , "InputControllerRight" , value ) ;
123+ break ;
124+ case ConfigSettings . InputControllerSecondary :
125+ _curConfigData . SetValue ( "Options" , "InputControllerSecondary" , value ) ;
126+ break ;
127+ case ConfigSettings . InputControllerInventory :
128+ _curConfigData . SetValue ( "Options" , "InputControllerInventory" , value ) ;
129+ break ;
57130 case ConfigSettings . LanguageKey :
58131 _curConfigData . SetValue ( "Options" , "LanguageKey" , value ) ;
59132 break ;
@@ -74,6 +147,7 @@ private static void AssertConfigFile()
74147 if ( _curConfigData == null )
75148 {
76149 LoadConfigData ( ) ;
150+ ApplySavedInputBindings ( ) ;
77151 }
78152 }
79153
@@ -118,8 +192,80 @@ public static Variant GetConfigValue(ConfigSettings setting)
118192 {
119193 case ConfigSettings . Volume :
120194 return _curConfigData . GetValue ( "Options" , "Volume" , DefaultVolume ) ;
121- case ConfigSettings . InputKey :
122- return _curConfigData . GetValue ( "Options" , "InputKey" , DefaultInput ) ;
195+ case ConfigSettings . InputType :
196+ return _curConfigData . GetValue ( "Options" , "InputKey" , DefaultInputType ) ;
197+ case ConfigSettings . InputKeyboardUp :
198+ return _curConfigData . GetValue (
199+ "Options" ,
200+ "InputKeyboardUp" ,
201+ DefaultInputKeyboardUp
202+ ) ;
203+ case ConfigSettings . InputKeyboardLeft :
204+ return _curConfigData . GetValue (
205+ "Options" ,
206+ "InputKeyboardLeft" ,
207+ DefaultInputKeyboardLeft
208+ ) ;
209+ case ConfigSettings . InputKeyboardDown :
210+ return _curConfigData . GetValue (
211+ "Options" ,
212+ "InputKeyboardDown" ,
213+ DefaultInputKeyboardDown
214+ ) ;
215+ case ConfigSettings . InputKeyboardRight :
216+ return _curConfigData . GetValue (
217+ "Options" ,
218+ "InputKeyboardRight" ,
219+ DefaultInputKeyboardRight
220+ ) ;
221+ case ConfigSettings . InputKeyboardSecondary :
222+ return _curConfigData . GetValue (
223+ "Options" ,
224+ "InputKeyboardSecondary" ,
225+ DefaultInputKeyboardSecondary
226+ ) ;
227+ case ConfigSettings . InputKeyboardInventory :
228+ return _curConfigData . GetValue (
229+ "Options" ,
230+ "InputKeyboardInventory" ,
231+ DefaultInputKeyboardInventory
232+ ) ;
233+ case ConfigSettings . InputControllerUp :
234+ return _curConfigData . GetValue (
235+ "Options" ,
236+ "InputControllerUp" ,
237+ DefaultInputControllerUp
238+ ) ;
239+ case ConfigSettings . InputControllerLeft :
240+ return _curConfigData . GetValue (
241+ "Options" ,
242+ "InputControllerLeft" ,
243+ DefaultInputControllerLeft
244+ ) ;
245+ case ConfigSettings . InputControllerDown :
246+ return _curConfigData . GetValue (
247+ "Options" ,
248+ "InputControllerDown" ,
249+ DefaultInputControllerDown
250+ ) ;
251+ case ConfigSettings . InputControllerRight :
252+ return _curConfigData . GetValue (
253+ "Options" ,
254+ "InputControllerRight" ,
255+ DefaultInputControllerRight
256+ ) ;
257+ case ConfigSettings . InputControllerSecondary :
258+ return _curConfigData . GetValue (
259+ "Options" ,
260+ "InputControllerSecondary" ,
261+ DefaultInputControllerSecondary
262+ ) ;
263+ case ConfigSettings . InputControllerInventory :
264+ return _curConfigData . GetValue (
265+ "Options" ,
266+ "InputControllerInventory" ,
267+ DefaultInputControllerInventory
268+ ) ;
123269 case ConfigSettings . LanguageKey :
124270 return _curConfigData . GetValue ( "Options" , "LanguageKey" , DefaultLanguage ) ;
125271 case ConfigSettings . HighContrast :
@@ -212,5 +358,90 @@ public static void ClearSave()
212358 DirAccess . RemoveAbsolute ( UserSavePath ) ;
213359 }
214360
361+ public static void ApplySavedInputBindings ( )
362+ {
363+ InputMap . ActionEraseEvents ( "WASD_arrowUp" ) ;
364+ InputMap . ActionEraseEvents ( "WASD_arrowDown" ) ;
365+ InputMap . ActionEraseEvents ( "WASD_arrowRight" ) ;
366+ InputMap . ActionEraseEvents ( "WASD_arrowLeft" ) ;
367+ InputMap . ActionEraseEvents ( "WASD_secondaryPlacement" ) ;
368+ InputMap . ActionEraseEvents ( "WASD_inventory" ) ;
369+ InputMap . ActionEraseEvents ( "CONTROLLER_arrowUp" ) ;
370+ InputMap . ActionEraseEvents ( "CONTROLLER_arrowDown" ) ;
371+ InputMap . ActionEraseEvents ( "CONTROLLER_arrowLeft" ) ;
372+ InputMap . ActionEraseEvents ( "CONTROLLER_arrowRight" ) ;
373+ InputMap . ActionEraseEvents ( "CONTROLLER_secondaryPlacement" ) ;
374+ InputMap . ActionEraseEvents ( "CONTROLLER_inventory" ) ;
375+
376+ // Keyboard bindings
377+ AddKeyBinding ( "WASD_arrowUp" , GetConfigValue ( ConfigSettings . InputKeyboardUp ) . ToString ( ) ) ;
378+ AddKeyBinding (
379+ "WASD_arrowDown" ,
380+ GetConfigValue ( ConfigSettings . InputKeyboardDown ) . ToString ( )
381+ ) ;
382+ AddKeyBinding (
383+ "WASD_arrowLeft" ,
384+ GetConfigValue ( ConfigSettings . InputKeyboardLeft ) . ToString ( )
385+ ) ;
386+ AddKeyBinding (
387+ "WASD_arrowRight" ,
388+ GetConfigValue ( ConfigSettings . InputKeyboardRight ) . ToString ( )
389+ ) ;
390+ AddKeyBinding (
391+ "WASD_secondaryPlacement" ,
392+ GetConfigValue ( ConfigSettings . InputKeyboardSecondary ) . ToString ( )
393+ ) ;
394+ AddKeyBinding (
395+ "WASD_inventory" ,
396+ GetConfigValue ( ConfigSettings . InputKeyboardInventory ) . ToString ( )
397+ ) ;
398+
399+ // Controller bindings
400+ AddJoypadBinding (
401+ "CONTROLLER_arrowUp" ,
402+ GetConfigValue ( ConfigSettings . InputControllerUp ) . ToString ( )
403+ ) ;
404+ AddJoypadBinding (
405+ "CONTROLLER_arrowDown" ,
406+ GetConfigValue ( ConfigSettings . InputControllerDown ) . ToString ( )
407+ ) ;
408+ AddJoypadBinding (
409+ "CONTROLLER_arrowLeft" ,
410+ GetConfigValue ( ConfigSettings . InputControllerLeft ) . ToString ( )
411+ ) ;
412+ AddJoypadBinding (
413+ "CONTROLLER_arrowRight" ,
414+ GetConfigValue ( ConfigSettings . InputControllerRight ) . ToString ( )
415+ ) ;
416+ AddJoypadBinding (
417+ "CONTROLLER_secondaryPlacement" ,
418+ GetConfigValue ( ConfigSettings . InputControllerSecondary ) . ToString ( )
419+ ) ;
420+ AddJoypadBinding (
421+ "CONTROLLER_inventory" ,
422+ GetConfigValue ( ConfigSettings . InputControllerInventory ) . ToString ( )
423+ ) ;
424+ }
425+
426+ private static void AddKeyBinding ( string action , string keyString )
427+ {
428+ Key key = ( Key ) Enum . Parse ( typeof ( Key ) , keyString , ignoreCase : true ) ;
429+ InputEventKey inputEvent = new InputEventKey { PhysicalKeycode = key } ;
430+ InputMap . ActionAddEvent ( action , inputEvent ) ;
431+ }
432+
433+ private static void AddJoypadBinding ( string action , string buttonString )
434+ {
435+ if ( Enum . TryParse < JoyButton > ( buttonString , true , out JoyButton button ) )
436+ {
437+ InputEventJoypadButton inputEvent = new InputEventJoypadButton { ButtonIndex = button } ;
438+ InputMap . ActionAddEvent ( action , inputEvent ) ;
439+ }
440+ else
441+ {
442+ GD . PushWarning ( $ "Could not parse joypad button: { buttonString } ") ;
443+ }
444+ }
445+
215446 #endregion
216447}
0 commit comments