/// <summary> /// Gets the input from the controller. /// </summary> internal static void GetInput() { // Store the current notches so we can later tell if they have been moved PreviousBrakeNotch = BrakeNotch; PreviousPowerNotch = PowerNotch; Controllers[ActiveControllerGuid].ReadInput(); }
/// <summary> /// Reads the input from the controller. /// </summary> /// <param name="joystick">The state of the joystick to read input from.</param> internal static void ReadInput(JoystickState joystick) { PowerNotches powerNotch = PowerNotches.None; BrakeNotches brakeNotch = BrakeNotches.None; powerNotch = joystick.IsButtonDown(ButtonIndex.Power1) ? powerNotch | PowerNotches.Power1 : powerNotch & ~PowerNotches.Power1; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake1) ? brakeNotch | BrakeNotches.Brake1 : brakeNotch & ~BrakeNotches.Brake1; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake2) ? brakeNotch | BrakeNotches.Brake2 : brakeNotch & ~BrakeNotches.Brake2; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake3) ? brakeNotch | BrakeNotches.Brake3 : brakeNotch & ~BrakeNotches.Brake3; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake4) ? brakeNotch | BrakeNotches.Brake4 : brakeNotch & ~BrakeNotches.Brake4; if (usesHat) { // The adapter uses the hat to map the direction buttons. // This is the case of some PlayStation adapters. powerNotch = joystick.GetHat((JoystickHat)hatIndex).IsLeft ? powerNotch | PowerNotches.Power2 : powerNotch & ~PowerNotches.Power2; powerNotch = joystick.GetHat((JoystickHat)hatIndex).IsRight ? powerNotch | PowerNotches.Power3 : powerNotch & ~PowerNotches.Power3; } else { // The adapter maps the direction buttons to independent buttons. powerNotch = joystick.IsButtonDown(ButtonIndex.Power2) ? powerNotch | PowerNotches.Power2 : powerNotch & ~PowerNotches.Power2; powerNotch = joystick.IsButtonDown(ButtonIndex.Power3) ? powerNotch | PowerNotches.Power3 : powerNotch & ~PowerNotches.Power3; } if (usesHat && powerNotch == PowerNotches.P4) { if (InputTranslator.PreviousPowerNotch < InputTranslator.PowerNotches.P3) { // Hack for adapters which map the direction buttons to a hat and confuse N with P4 InputTranslator.PowerNotch = InputTranslator.PowerNotches.N; } else { InputTranslator.PowerNotch = InputTranslator.PowerNotches.P4; } } else if (powerNotch != PowerNotches.Transition) { // Set notch only if it is not a transition InputTranslator.PowerNotch = PowerNotchMap[powerNotch]; } if (brakeNotch != BrakeNotches.Transition && (brakeNotch == BrakeNotches.Emergency || brakeNotch >= BrakeNotches.B8)) { // Set notch only if it is not a transition nor an unmarked notch InputTranslator.BrakeNotch = BrakeNotchMap[brakeNotch]; } InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(ButtonIndex.Select); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start] = joystick.GetButton(ButtonIndex.Start); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] = joystick.GetButton(ButtonIndex.A); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] = joystick.GetButton(ButtonIndex.B); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] = joystick.GetButton(ButtonIndex.C); }
/// <summary> /// Gets the input from the controller. /// </summary> internal static void GetInput() { // Store the current notches so we can later tell if they have been moved PreviousBrakeNotch = BrakeNotch; PreviousPowerNotch = PowerNotch; // Read the input from the controller according to the type switch (ControllerModel) { case ControllerModels.Classic: ControllerClassic.ReadInput(Joystick.GetState(ConnectedControllers[activeControllerGuid])); return; case ControllerModels.Unbalance: ControllerUnbalance.ReadInput(Joystick.GetState(ConnectedControllers[activeControllerGuid])); return; } }
internal HandleViewModel(Handle handle, Train train) { HandleType = handle .ToReactivePropertyAsSynchronized(x => x.HandleType) .AddTo(disposable); PowerNotches = handle .ToReactivePropertyAsSynchronized(x => x.PowerNotches, ignoreValidationErrorValue: true) .AddTo(disposable); PowerNotches.Subscribe(_ => train.ApplyPowerNotchesToCar()).AddTo(disposable); BrakeNotches = handle .ToReactivePropertyAsSynchronized(x => x.BrakeNotches, ignoreValidationErrorValue: true) .SetValidateNotifyError(x => { if (x == 0 && train.Device.HoldBrake) { return("BrakeNotches must be at least 1 if HoldBrake is set."); } return(null); }) .AddTo(disposable); BrakeNotches.Subscribe(_ => train.ApplyBrakeNotchesToCar()).AddTo(disposable); PowerNotchReduceSteps = handle .ToReactivePropertyAsSynchronized(x => x.PowerNotchReduceSteps) .AddTo(disposable); HandleBehaviour = handle .ToReactivePropertyAsSynchronized(x => x.HandleBehaviour) .AddTo(disposable); LocoBrake = handle .ToReactivePropertyAsSynchronized(x => x.LocoBrake) .AddTo(disposable); LocoBrakeNotches = handle .ToReactivePropertyAsSynchronized(x => x.LocoBrakeNotches) .AddTo(disposable); LocoBrakeNotches.Subscribe(_ => train.ApplyLocoBrakeNotchesToCar()).AddTo(disposable); DriverPowerNotches = handle .ToReactivePropertyAsSynchronized(x => x.DriverPowerNotches, ignoreValidationErrorValue: true) .AddTo(disposable); DriverBrakeNotches = handle .ToReactivePropertyAsSynchronized(x => x.DriverBrakeNotches, ignoreValidationErrorValue: true) .AddTo(disposable); PowerNotches .SetValidateNotifyError(x => { if (x < DriverPowerNotches.Value) { return("DriverPowerNotches must be less than or equal to PowerNotches."); } return(null); }) .Subscribe(_ => DriverPowerNotches.ForceValidate()) .AddTo(disposable); PowerNotches .ObserveHasErrors .ToReadOnlyReactivePropertySlim(mode: ReactivePropertyMode.DistinctUntilChanged) .Where(x => !x) .Subscribe(_ => PowerNotches.ForceNotify()) .AddTo(disposable); BrakeNotches .SetValidateNotifyError(x => { if (x < DriverBrakeNotches.Value) { return("DriverBrakeNotches must be less than or equal to BrakeNotches."); } return(null); }) .Subscribe(_ => DriverBrakeNotches.ForceValidate()) .AddTo(disposable); BrakeNotches .ObserveHasErrors .ToReadOnlyReactivePropertySlim(mode: ReactivePropertyMode.DistinctUntilChanged) .Where(x => !x) .Subscribe(_ => BrakeNotches.ForceNotify()) .AddTo(disposable); DriverPowerNotches .SetValidateNotifyError(x => { if (x > PowerNotches.Value) { return("DriverPowerNotches must be less than or equal to PowerNotches."); } return(null); }) .Subscribe(_ => PowerNotches.ForceValidate()) .AddTo(disposable); DriverPowerNotches .ObserveHasErrors .ToReadOnlyReactivePropertySlim(mode: ReactivePropertyMode.DistinctUntilChanged) .Where(x => !x) .Subscribe(_ => DriverPowerNotches.ForceNotify()) .AddTo(disposable); DriverBrakeNotches .SetValidateNotifyError(x => { if (x > BrakeNotches.Value) { return("DriverBrakeNotches must be less than or equal to BrakeNotches."); } return(null); }) .Subscribe(_ => BrakeNotches.ForceValidate()) .AddTo(disposable); DriverBrakeNotches .ObserveHasErrors .ToReadOnlyReactivePropertySlim(mode: ReactivePropertyMode.DistinctUntilChanged) .Where(x => !x) .Subscribe(_ => DriverBrakeNotches.ForceNotify()) .AddTo(disposable); }