@@ -238,55 +238,67 @@ public override void HandleUserInput(ElapsedTime elapsedTime)
238
238
foreach ( var external in externalDevices )
239
239
{
240
240
if ( external == null ) continue ;
241
- if ( external . Throttle . Changed )
242
- Locomotive . SetThrottlePercentWithSound ( external . Throttle . Value * 100 ) ;
243
- if ( external . TrainBrake . Changed )
244
- Locomotive . SetTrainBrakePercent ( external . TrainBrake . Value * 100 ) ;
245
- if ( external . EngineBrake . Changed )
246
- Locomotive . SetEngineBrakePercent ( external . EngineBrake . Value * 100 ) ;
247
- // Locomotive.SetBrakemanBrakePercent(external.BrakemanBrakePercent); // For Raildriver control not complete for this value?
248
- if ( external . DynamicBrake . Changed && Locomotive . CombinedControlType != MSTSLocomotive . CombinedControl . ThrottleAir )
249
- Locomotive . SetDynamicBrakePercentWithSound ( external . DynamicBrake . Value * 100 ) ;
250
-
251
- if ( external . Direction . Changed )
252
- {
253
- if ( Locomotive is MSTSSteamLocomotive steam )
254
- {
255
- steam . SetCutoffPercent ( UserInput . RDState . Direction . Value * 100 ) ;
256
- }
257
- else if ( external . Direction . Value > 0.5f )
258
- Locomotive . SetDirection ( Direction . Forward ) ;
259
- else if ( external . Direction . Value < - 0.5f )
260
- Locomotive . SetDirection ( Direction . Reverse ) ;
261
- else
262
- Locomotive . SetDirection ( Direction . N ) ;
263
- }
264
-
265
- if ( external . Lights . Changed )
266
- {
267
- // changing Headlight more than one step at a time doesn't work for some reason
268
- if ( Locomotive . Headlight < external . Lights . Value - 1 )
269
- {
270
- Locomotive . Headlight ++ ;
271
- Locomotive . SignalEvent ( Event . LightSwitchToggle ) ;
272
- }
273
- if ( Locomotive . Headlight > external . Lights . Value - 1 )
274
- {
275
- Locomotive . Headlight -- ;
276
- Locomotive . SignalEvent ( Event . LightSwitchToggle ) ;
277
- }
278
- }
279
241
// Handle other cabcontrols
280
242
foreach ( var kvp in external . CabControls )
281
243
{
282
244
if ( _CabRenderer == null ) break ;
283
245
if ( ! kvp . Value . Changed ) continue ;
284
- if ( _CabRenderer . ControlMap . TryGetValue ( kvp . Key , out var renderer ) && renderer is CabViewDiscreteRenderer discrete )
246
+ float val = kvp . Value . Value ;
247
+ switch ( kvp . Key . Item1 . Type )
285
248
{
286
- var oldChanged = discrete . ChangedValue ;
287
- discrete . ChangedValue = ( val ) => kvp . Value . Value ;
288
- discrete . HandleUserInput ( ) ;
289
- discrete . ChangedValue = oldChanged ;
249
+ // Some cab controls need specific handling for better results
250
+ case CABViewControlTypes . THROTTLE :
251
+ Locomotive . SetThrottlePercentWithSound ( val * 100 ) ;
252
+ break ;
253
+ case CABViewControlTypes . DIRECTION :
254
+ if ( Locomotive is MSTSSteamLocomotive steam )
255
+ {
256
+ steam . SetCutoffPercent ( val * 100 ) ;
257
+ }
258
+ else if ( val > 0.5f )
259
+ Locomotive . SetDirection ( Direction . Forward ) ;
260
+ else if ( val < - 0.5f )
261
+ Locomotive . SetDirection ( Direction . Reverse ) ;
262
+ else
263
+ Locomotive . SetDirection ( Direction . N ) ;
264
+ break ;
265
+ case CABViewControlTypes . TRAIN_BRAKE :
266
+ Locomotive . SetTrainBrakePercent ( val * 100 ) ;
267
+ break ;
268
+ case CABViewControlTypes . DYNAMIC_BRAKE :
269
+ if ( Locomotive . CombinedControlType != MSTSLocomotive . CombinedControl . ThrottleAir )
270
+ Locomotive . SetDynamicBrakePercentWithSound ( val * 100 ) ;
271
+ break ;
272
+ case CABViewControlTypes . ENGINE_BRAKE :
273
+ Locomotive . SetEngineBrakePercent ( val * 100 ) ;
274
+ break ;
275
+ case CABViewControlTypes . FRONT_HLIGHT :
276
+ // changing Headlight more than one step at a time doesn't work for some reason
277
+ if ( Locomotive . Headlight < val - 1 )
278
+ {
279
+ Locomotive . Headlight ++ ;
280
+ Locomotive . SignalEvent ( Event . LightSwitchToggle ) ;
281
+ }
282
+ if ( Locomotive . Headlight > val - 1 )
283
+ {
284
+ Locomotive . Headlight -- ;
285
+ Locomotive . SignalEvent ( Event . LightSwitchToggle ) ;
286
+ }
287
+ break ;
288
+ case CABViewControlTypes . ORTS_SELECTED_SPEED_SELECTOR :
289
+ Locomotive . CruiseControl . SelectedSpeedMpS = val ;
290
+ break ;
291
+ // Other controls can hopefully be controlled faking mouse input
292
+ // TODO: refactor HandleUserInput()
293
+ default :
294
+ if ( _CabRenderer . ControlMap . TryGetValue ( kvp . Key , out var renderer ) && renderer is CabViewDiscreteRenderer discrete )
295
+ {
296
+ var oldChanged = discrete . ChangedValue ;
297
+ discrete . ChangedValue = ( oldval ) => val ;
298
+ discrete . HandleUserInput ( ) ;
299
+ discrete . ChangedValue = oldChanged ;
300
+ }
301
+ break ;
290
302
}
291
303
}
292
304
}
0 commit comments