Skip to content

Commit

Permalink
Automatic merge of T1.5.1-237-g5c9f646ff and 11 pull requests
Browse files Browse the repository at this point in the history
- Pull request #719 at 0f4d975: Upgraded to MonoGame 3.8.0 (+ small update for other libraries)
- Pull request #722 at fb9079e: Fix Windows Forms deprecations in ActivityEditor
- Pull request #732 at 83002d7: Improvements for air brakes
- Pull request #735 at b9a0ea3: Added new parameter for battery switch
- Pull request #751 at f947f29: Web interface to control cab controls with external hardware
- Pull request #753 at ab8fe32: Extends CabControls for user input
- Pull request #760 at 113dd63: Updated EditorConfig with .NET and StyleCop analyzer configuration
- Pull request #764 at 5f9a01b: fix: Improve sky dome distortion causing bug #1471416
- Pull request #766 at c9657de: Control Car additional functionality
- Pull request #767 at 9317f8c: Refine sunrise and sunset
- Pull request #768 at bc8aea4: OpenRailway Map enhancements
  • Loading branch information
openrails-bot committed Jan 20, 2023
13 parents 750ce51 + 5c9f646 + 0f4d975 + fb9079e + 83002d7 + b9a0ea3 + f947f29 + ab8fe32 + 113dd63 + 5f9a01b + c9657de + 9317f8c + bc8aea4 commit 1435554
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 147 deletions.
5 changes: 5 additions & 0 deletions Source/Orts.Formats.Msts/CabViewFile.cs
Expand Up @@ -343,6 +343,11 @@ public struct CabViewControlType
{
public CABViewControlTypes Type;
public int Id;
public CabViewControlType(CABViewControlTypes type)
{
Type = type;
Id = 0;
}
public CabViewControlType(string name)
{
Type = CABViewControlTypes.NONE;
Expand Down
Expand Up @@ -3860,6 +3860,8 @@ public void SetThrottlePercent(float percent)
if (!CruiseControl.UseThrottleInCombinedControl) ThrottleController.SetPercent(percent);
return;
}
else if (CruiseControl.UseThrottleAsSpeedSelector && CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto)
CruiseControl.SetSpeed(MpS.FromMpS(percent * MaxSpeedMpS / 100, !CruiseControl.SpeedIsMph));
else
ThrottleController.SetPercent(percent);
}
Expand Down
77 changes: 51 additions & 26 deletions Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs
Expand Up @@ -236,46 +236,71 @@ public override void HandleUserInput(ElapsedTime elapsedTime)
if (UserInput.IsPressed(UserCommand.DebugResetWheelSlip)) { Locomotive.Train.SignalEvent(Event._ResetWheelSlip); }
if (UserInput.IsPressed(UserCommand.DebugToggleAdvancedAdhesion)) { Locomotive.Train.SignalEvent(Event._ResetWheelSlip); Locomotive.Simulator.UseAdvancedAdhesion = !Locomotive.Simulator.UseAdvancedAdhesion; }

if (UserInput.RDState != null)
ExternalDeviceState[] externalDevices = {UserInput.RDState, UserInput.WebDeviceState};
foreach (var external in externalDevices)
{
if (UserInput.RDState.BailOff)
if (external == null) continue;
// Handle other cabcontrols
foreach (var kvp in external.CabControls)
{
Locomotive.SetBailOff(true);
if (_CabRenderer == null) break;
if (!kvp.Value.Changed) continue;
float val = kvp.Value.Value;
switch (kvp.Key.Item1.Type)
{
// Some cab controls need specific handling for better results
case CABViewControlTypes.THROTTLE:
Locomotive.SetThrottlePercentWithSound(val * 100);
break;
case CABViewControlTypes.DIRECTION:
if (Locomotive is MSTSSteamLocomotive steam)
{
steam.SetCutoffPercent(val * 100);
}
if (UserInput.RDState.Changed)
{
Locomotive.AlerterReset();

Locomotive.SetThrottlePercentWithSound(UserInput.RDState.ThrottlePercent);
Locomotive.SetTrainBrakePercent(UserInput.RDState.TrainBrakePercent);
Locomotive.SetEngineBrakePercent(UserInput.RDState.EngineBrakePercent);
// Locomotive.SetBrakemanBrakePercent(UserInput.RDState.BrakemanBrakePercent); // For Raildriver control not complete for this value?
if (Locomotive.CombinedControlType != MSTSLocomotive.CombinedControl.ThrottleAir)
Locomotive.SetDynamicBrakePercentWithSound(UserInput.RDState.DynamicBrakePercent);
if (UserInput.RDState.DirectionPercent > 50)
else if (val > 0.5f)
Locomotive.SetDirection(Direction.Forward);
else if (UserInput.RDState.DirectionPercent < -50)
else if (val < -0.5f)
Locomotive.SetDirection(Direction.Reverse);
else
Locomotive.SetDirection(Direction.N);
if (UserInput.RDState.Emergency)
new EmergencyPushButtonCommand(Viewer.Log, true);
else
new EmergencyPushButtonCommand(Viewer.Log, false);
if (UserInput.RDState.Wipers == 1 && Locomotive.Wiper)
Locomotive.SignalEvent(Event.WiperOff);
if (UserInput.RDState.Wipers != 1 && !Locomotive.Wiper)
Locomotive.SignalEvent(Event.WiperOn);
break;
case CABViewControlTypes.TRAIN_BRAKE:
Locomotive.SetTrainBrakePercent(val * 100);
break;
case CABViewControlTypes.DYNAMIC_BRAKE:
if (Locomotive.CombinedControlType != MSTSLocomotive.CombinedControl.ThrottleAir)
Locomotive.SetDynamicBrakePercentWithSound(val * 100);
break;
case CABViewControlTypes.ENGINE_BRAKE:
Locomotive.SetEngineBrakePercent(val * 100);
break;
case CABViewControlTypes.FRONT_HLIGHT:
// changing Headlight more than one step at a time doesn't work for some reason
if (Locomotive.Headlight < UserInput.RDState.Lights - 1)
if (Locomotive.Headlight < val - 1)
{
Locomotive.Headlight++;
Locomotive.SignalEvent(Event.LightSwitchToggle);
}
if (Locomotive.Headlight > UserInput.RDState.Lights - 1)
if (Locomotive.Headlight > val - 1)
{
Locomotive.Headlight--;
Locomotive.SignalEvent(Event.LightSwitchToggle);
}
break;
case CABViewControlTypes.ORTS_SELECTED_SPEED_SELECTOR:
Locomotive.CruiseControl.SelectedSpeedMpS = val;
break;
// Other controls can hopefully be controlled faking mouse input
// TODO: refactor HandleUserInput()
default:
if (_CabRenderer.ControlMap.TryGetValue(kvp.Key, out var renderer) && renderer is CabViewDiscreteRenderer discrete)
{
var oldChanged = discrete.ChangedValue;
discrete.ChangedValue = (oldval) => val;
discrete.HandleUserInput();
discrete.ChangedValue = oldChanged;
}
break;
}
}
}
Expand Down Expand Up @@ -1982,7 +2007,7 @@ public class CabViewDiscreteRenderer : CabViewControlRenderer, ICabViewMouseCont
/// <summary>
/// Function calculating response value for mouse events (movement, left-click), determined by configured style.
/// </summary>
readonly Func<float, float> ChangedValue;
public Func<float, float> ChangedValue;

public CabViewDiscreteRenderer(Viewer viewer, MSTSLocomotive locomotive, CVCWithFrames control, CabShader shader)
: base(viewer, locomotive, control, shader)
Expand Down
Expand Up @@ -225,9 +225,6 @@ public override void HandleUserInput(ElapsedTime elapsedTime)
// Keeping separated, since it is not a real engine control. (Probably wrong classification?)
if (UserInput.IsPressed(UserCommand.ControlAIFireReset)) new AIFireResetCommand(Viewer.Log);

if (UserInput.RDState != null && UserInput.RDState.Changed)
SteamLocomotive.SetCutoffPercent(UserInput.RDState.DirectionPercent);

base.HandleUserInput(elapsedTime);

#if DEBUG_DUMP_STEAM_POWER_CURVE
Expand Down
6 changes: 3 additions & 3 deletions Source/RunActivity/Viewer3D/UserInput.cs
Expand Up @@ -49,8 +49,8 @@ public static class UserInput
static bool MouseButtonsSwapped;
public static int MouseSpeedX;
public static int MouseSpeedY;

public static RailDriverState RDState;
public static ExternalDeviceState WebDeviceState = new ExternalDeviceState();

static InputSettings InputSettings;

Expand Down Expand Up @@ -136,8 +136,8 @@ static Keys[] GetKeysWithPrintScreenFix(KeyboardState keyboardState)

public static void Handled()
{
if (RDState != null)
RDState.Handled();
RDState?.Handled();
WebDeviceState?.Handled();
}

public static bool IsPressed(UserCommand command)
Expand Down
109 changes: 109 additions & 0 deletions Source/RunActivity/Viewer3D/UserInputExternal.cs
@@ -0,0 +1,109 @@
// COPYRIGHT 2022 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using Orts.Formats.Msts;
using Orts.Simulation.RollingStocks;
using ORTS.Common;
using ORTS.Common.Input;

namespace Orts.Viewer3D
{
/// <summary>
/// Processed external device data sent to UserInput class
/// </summary>
public class ExternalDeviceState
{
public Dictionary<(CabViewControlType,int), ExternalDeviceCabControl> CabControls;
public Dictionary<UserCommand, ExternalDeviceButton> Commands;
public ExternalDeviceState()
{
Commands = new Dictionary<UserCommand, ExternalDeviceButton>();
CabControls = new Dictionary<(CabViewControlType,int), ExternalDeviceCabControl>();
}

public virtual void Handled()
{
foreach (var button in Commands.Values)
{
button.Changed = false;
}
foreach (var control in CabControls.Values)
{
control.Changed = false;
}
}

public bool IsPressed(UserCommand command)
{
return Commands.TryGetValue(command, out var button) && button.IsPressed;
}

public bool IsReleased(UserCommand command)
{
return Commands.TryGetValue(command, out var button) && button.IsReleased;
}

public bool IsDown(UserCommand command)
{
return Commands.TryGetValue(command, out var button) && button.IsDown;
}
}
public class ExternalDeviceButton
{
bool isDown;
public bool IsDown
{
get
{
return isDown;
}
set
{
if (isDown != value)
{
isDown = value;
Changed = true;
}
}
}
public bool IsPressed { get { return IsDown && Changed; } }
public bool IsReleased { get { return !IsDown && Changed; } }
public bool Changed;
}
public class ExternalDeviceCabControl
{
float value;
public bool Changed;
public float Value
{
get
{
return value;
}
set
{
if (this.value != value)
{
this.value = value;
Changed = true;
}
}
}
}
}

0 comments on commit 1435554

Please sign in to comment.