33
44using System ;
55using System . Collections . Generic ;
6+ using System . Collections . Immutable ;
67using System . Collections . ObjectModel ;
78using System . ComponentModel . Composition ;
89using System . Drawing ;
@@ -107,7 +108,7 @@ public void OnLoad()
107108 LedToggleSwitch = new ToggleSwitch
108109 {
109110 OnContent = "" , OffContent = "" ,
110- IsOn = Host . PluginSettings . GetSetting ( "LedDim" , false ) ,
111+ IsOn = Host ! . PluginSettings . GetSetting ( "LedDim" , false ) ,
111112 Margin = new Thickness { Left = 5 , Top = 3 , Right = 3 , Bottom = 3 } ,
112113 VerticalAlignment = VerticalAlignment . Center
113114 } ;
@@ -260,6 +261,21 @@ public void Update()
260261 jointEnumerator . Current . TrackingState = controller . IsControllerStable ( )
261262 ? TrackedJointState . StateTracked // Tracking is all fine!
262263 : TrackedJointState . StateInferred ; // Kinda unstable...?
264+
265+ // Update input actions (using extensions defined below)
266+ // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
267+ switch ( controller . m_Info . m_ControllerType )
268+ {
269+ case Constants . PSMControllerType . PSMController_Move :
270+ controller . m_Info . m_PSMoveState . UpdateActions ( Host ) ;
271+ break ;
272+ case Constants . PSMControllerType . PSMController_Navi :
273+ controller . m_Info . m_PSNaviState . UpdateActions ( Host ) ;
274+ break ;
275+ case Constants . PSMControllerType . PSMController_DualShock4 :
276+ controller . m_Info . m_PSDualShock4State . UpdateActions ( Host ) ;
277+ break ;
278+ }
263279 } ) ;
264280 }
265281 catch ( Exception e )
@@ -380,7 +396,20 @@ private void RefreshControllerList()
380396 ? controller . m_Info
381397 . m_ControllerId // Is the controller is virtual, also append its ID
382398 : "" ) , // If this controller is valid, its serial should be the bluetooth-mac address
383- Role = TrackedJointType . JointManual
399+ Role = TrackedJointType . JointManual ,
400+ SupportedInputActions = controller . m_Info . m_ControllerType switch
401+ {
402+ Constants . PSMControllerType . PSMController_Move =>
403+ controller . m_Info . m_PSMoveState . GetActions ( ) ,
404+ Constants . PSMControllerType . PSMController_Navi =>
405+ controller . m_Info . m_PSNaviState . GetActions ( ) ,
406+ Constants . PSMControllerType . PSMController_DualShock4 =>
407+ controller . m_Info . m_PSDualShock4State . GetActions ( ) ,
408+
409+ Constants . PSMControllerType . PSMController_Virtual => [ ] ,
410+ Constants . PSMControllerType . PSMController_None => [ ] ,
411+ _ => [ ]
412+ }
384413 } ) ;
385414 }
386415 }
@@ -466,4 +495,181 @@ public static void AddRange<T>(this ObservableCollection<T> collection, IEnumera
466495 {
467496 items . ToList ( ) . ForEach ( collection . Add ) ;
468497 }
498+
499+ public static void UpdateActions ( this Controllers . Info . PSMoveState state , IAmethystHost host )
500+ {
501+ try
502+ {
503+ state . GetButtonStates ( )
504+ . Select ( ( IKeyInputAction Action , object Data ) ( x ) => ( Action : new KeyInputAction < bool >
505+ {
506+ Name = x . Key ,
507+ Guid = x . Key
508+ } , Data : x . Value ) ) . Concat ( state . GetAnalogStates ( )
509+ . Select ( ( IKeyInputAction Action , object Data ) ( x ) => ( Action : new KeyInputAction < double >
510+ {
511+ Name = x . Key ,
512+ Guid = x . Key
513+ } , Data : x . Value ) ) ) . ToList ( ) . ForEach ( x => host . ReceiveKeyInput ( x . Action , x . Data ) ) ;
514+ }
515+ catch ( Exception e )
516+ {
517+ host ? . Log ( e ) ;
518+ }
519+ }
520+
521+ public static void UpdateActions ( this Controllers . Info . PSDualShock4State state , IAmethystHost host )
522+ {
523+ try
524+ {
525+ state . GetButtonStates ( )
526+ . Select ( ( IKeyInputAction Action , object Data ) ( x ) => ( Action : new KeyInputAction < bool >
527+ {
528+ Name = x . Key ,
529+ Guid = x . Key
530+ } , Data : x . Value ) ) . Concat ( state . GetAnalogStates ( )
531+ . Select ( ( IKeyInputAction Action , object Data ) ( x ) => ( Action : new KeyInputAction < double >
532+ {
533+ Name = x . Key ,
534+ Guid = x . Key
535+ } , Data : x . Value ) ) ) . ToList ( ) . ForEach ( x => host . ReceiveKeyInput ( x . Action , x . Data ) ) ;
536+ }
537+ catch ( Exception e )
538+ {
539+ host ? . Log ( e ) ;
540+ }
541+ }
542+
543+ public static void UpdateActions ( this Controllers . Info . PSNaviState state , IAmethystHost host )
544+ {
545+ try
546+ {
547+ state . GetButtonStates ( )
548+ . Select ( ( IKeyInputAction Action , object Data ) ( x ) => ( Action : new KeyInputAction < bool >
549+ {
550+ Name = x . Key ,
551+ Guid = x . Key
552+ } , Data : x . Value ) ) . ToList ( ) . ForEach ( x => host . ReceiveKeyInput ( x . Action , x . Data ) ) ;
553+ }
554+ catch ( Exception e )
555+ {
556+ host ? . Log ( e ) ;
557+ }
558+ }
559+
560+ public static SortedSet < IKeyInputAction > GetActions ( this Controllers . Info . PSMoveState state )
561+ {
562+ return new SortedSet < IKeyInputAction > ( state . GetButtonStates ( )
563+ . Select ( IKeyInputAction ( x ) => new KeyInputAction < bool >
564+ {
565+ Name = x . Key , Guid = x . Key
566+ } ) . Concat ( state . GetAnalogStates ( ) . Select ( IKeyInputAction ( x ) => new KeyInputAction < double >
567+ {
568+ Name = x . Key ,
569+ Guid = x . Key
570+ } ) ) . ToList ( ) ) ;
571+ }
572+
573+ public static SortedSet < IKeyInputAction > GetActions ( this Controllers . Info . PSDualShock4State state )
574+ {
575+ return new SortedSet < IKeyInputAction > ( state . GetButtonStates ( )
576+ . Select ( IKeyInputAction ( x ) => new KeyInputAction < bool >
577+ {
578+ Name = x . Key ,
579+ Guid = x . Key
580+ } ) . Concat ( state . GetAnalogStates ( ) . Select ( IKeyInputAction ( x ) => new KeyInputAction < double >
581+ {
582+ Name = x . Key ,
583+ Guid = x . Key
584+ } ) ) . ToList ( ) ) ;
585+ }
586+
587+ public static SortedSet < IKeyInputAction > GetActions ( this Controllers . Info . PSNaviState state )
588+ {
589+ return new SortedSet < IKeyInputAction > ( state . GetButtonStates ( )
590+ . Select ( IKeyInputAction ( x ) => new KeyInputAction < bool >
591+ {
592+ Name = x . Key ,
593+ Guid = x . Key
594+ } ) . ToList ( ) ) ;
595+ }
596+
597+ public static Dictionary < string , bool > GetButtonStates ( this Controllers . Info . PSMoveState state )
598+ {
599+ return new Dictionary < string , bool >
600+ {
601+ { "Triangle" , state ? . m_TriangleButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
602+ { "Circle" , state ? . m_CircleButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
603+ { "Cross" , state ? . m_CrossButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
604+ { "Square" , state ? . m_SquareButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
605+ { "Select" , state ? . m_SelectButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
606+ { "Start" , state ? . m_StartButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
607+ { "PS" , state ? . m_PSButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
608+ { "Move" , state ? . m_MoveButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
609+ { "Trigger" , state ? . m_TriggerButton == Constants . PSMButtonState . PSMButtonState_PRESSED }
610+ } ;
611+ }
612+
613+ public static Dictionary < string , double > GetAnalogStates ( this Controllers . Info . PSMoveState state )
614+ {
615+ return new Dictionary < string , double >
616+ {
617+ { "Trigger (Linear)" , ( state ? . m_TriggerValue ?? 0.0 ) / 255.0 }
618+ } ;
619+ }
620+
621+ public static Dictionary < string , bool > GetButtonStates ( this Controllers . Info . PSDualShock4State state )
622+ {
623+ return new Dictionary < string , bool >
624+ {
625+ { "D-Pad Up" , state ? . m_DPadUpButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
626+ { "D-Pad Down" , state ? . m_DPadDownButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
627+ { "D-Pad Left" , state ? . m_DPadLeftButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
628+ { "D-Pad Right" , state ? . m_DPadRightButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
629+ { "Square" , state ? . m_SquareButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
630+ { "Cross" , state ? . m_CrossButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
631+ { "Circle" , state ? . m_CircleButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
632+ { "Triangle" , state ? . m_TriangleButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
633+ { "L1" , state ? . m_L1Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
634+ { "R1" , state ? . m_R1Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
635+ { "L2" , state ? . m_L2Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
636+ { "R2" , state ? . m_R2Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
637+ { "L3" , state ? . m_L3Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
638+ { "R3" , state ? . m_R3Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
639+ { "Share" , state ? . m_ShareButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
640+ { "Options" , state ? . m_OptionsButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
641+ { "PS" , state ? . m_PSButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
642+ { "Trackpad" , state ? . m_TrackPadButton == Constants . PSMButtonState . PSMButtonState_PRESSED }
643+ } ;
644+ }
645+
646+ public static Dictionary < string , double > GetAnalogStates ( this Controllers . Info . PSDualShock4State state )
647+ {
648+ return new Dictionary < string , double >
649+ {
650+ { "Left X" , Math . Clamp ( state ? . m_LeftAnalogX ?? 0.0 , 0.0 , 1.0 ) } ,
651+ { "Left Y" , Math . Clamp ( state ? . m_LeftAnalogY ?? 0.0 , 0.0 , 1.0 ) } ,
652+ { "Right X" , Math . Clamp ( state ? . m_RightAnalogX ?? 0.0 , 0.0 , 1.0 ) } ,
653+ { "Right Y" , Math . Clamp ( state ? . m_RightAnalogY ?? 0.0 , 0.0 , 1.0 ) } ,
654+ { "Left Trigger" , Math . Clamp ( state ? . m_LeftTriggerValue ?? 0.0 , 0.0 , 1.0 ) } ,
655+ { "Right Trigger" , Math . Clamp ( state ? . m_RightTriggerValue ?? 0.0 , 0.0 , 1.0 ) }
656+ } ;
657+ }
658+
659+ public static Dictionary < string , bool > GetButtonStates ( this Controllers . Info . PSNaviState state )
660+ {
661+ return new Dictionary < string , bool >
662+ {
663+ { "D-Pad Up" , state ? . m_DPadUpButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
664+ { "D-Pad Down" , state ? . m_DPadDownButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
665+ { "D-Pad Left" , state ? . m_DPadLeftButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
666+ { "D-Pad Right" , state ? . m_DPadRightButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
667+ { "Cross" , state ? . m_CrossButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
668+ { "Circle" , state ? . m_CircleButton == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
669+ { "L1" , state ? . m_L1Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
670+ { "L2" , state ? . m_L2Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
671+ { "L3" , state ? . m_L3Button == Constants . PSMButtonState . PSMButtonState_PRESSED } ,
672+ { "PS" , state ? . m_PSButton == Constants . PSMButtonState . PSMButtonState_PRESSED }
673+ } ;
674+ }
469675}
0 commit comments