Skip to content

Commit 62070ad

Browse files
authored
Merge pull request #1156 from cesarBLG/control-operations-enable
Fix incorrectly disabled options in train operations window
2 parents 31e3fe2 + f46d5f2 commit 62070ad

File tree

8 files changed

+134
-176
lines changed

8 files changed

+134
-176
lines changed

Source/Orts.Simulation/Common/Scripting/PowerSupply/ControlCarPowerSupply.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected override void SetCurrentMainPowerSupplyState(PowerSupplyState state) {
4949
protected override void SetCurrentAuxiliaryPowerSupplyState(PowerSupplyState state) {}
5050
protected override void SetCurrentElectricTrainSupplyState(PowerSupplyState state) {}
5151
protected override void SetCurrentDynamicBrakeAvailability(bool avail) {}
52+
public override PowerSupplyState GetPowerStatus() => PowerSupplyState.Unavailable;
5253
public void SignalEventToControlActiveLocomotive(PowerSupplyEvent evt)
5354
{
5455
ControlActiveLocomotive?.LocomotivePowerSupply.HandleEvent(evt);

Source/Orts.Simulation/Common/Scripting/PowerSupply/DieselPowerSupply.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,27 @@ public float DieselEngineMinRpm
9090
/// Closing authorization of the traction cut-off relay
9191
/// </summary>
9292
public bool TractionCutOffRelayClosingAuthorization() => TractionCutOffRelay.ClosingAuthorization;
93-
93+
94+
public override PowerSupplyState GetPowerStatus()
95+
{
96+
var status = base.GetPowerStatus();
97+
PowerSupplyState engineStatus;
98+
switch (CurrentDieselEnginesState())
99+
{
100+
case DieselEngineState.Running:
101+
engineStatus = PowerSupplyState.PowerOn;
102+
break;
103+
case DieselEngineState.Starting:
104+
engineStatus = PowerSupplyState.PowerOnOngoing;
105+
break;
106+
default:
107+
engineStatus = PowerSupplyState.PowerOff;
108+
break;
109+
}
110+
if (status == engineStatus) return status;
111+
return PowerSupplyState.PowerOnOngoing;
112+
}
113+
94114
/// <summary>
95115
/// Sends an event to all diesel engines
96116
/// </summary>

Source/Orts.Simulation/Common/Scripting/PowerSupply/ElectricPowerSupply.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ protected float PantographVoltageVDC
128128
/// </summary>
129129
protected void SetFilterVoltageV(float voltage) => EpsHost.FilterVoltageV = voltage;
130130

131+
public override PowerSupplyState GetPowerStatus()
132+
{
133+
var status = base.GetPowerStatus();
134+
PowerSupplyState electricStatus;
135+
switch (CurrentPantographState())
136+
{
137+
case PantographState.Up:
138+
switch (CurrentCircuitBreakerState())
139+
{
140+
case CircuitBreakerState.Closed:
141+
electricStatus = PowerSupplyState.PowerOn;
142+
break;
143+
default:
144+
electricStatus = PowerSupplyState.PowerOnOngoing;
145+
break;
146+
}
147+
break;
148+
case PantographState.Raising:
149+
electricStatus = PowerSupplyState.PowerOnOngoing;
150+
break;
151+
default:
152+
electricStatus = PowerSupplyState.PowerOff;
153+
break;
154+
}
155+
if (status == electricStatus) return status;
156+
return PowerSupplyState.PowerOnOngoing;
157+
}
158+
131159
/// <summary>
132160
/// Sends an event to the circuit breaker
133161
/// </summary>

Source/Orts.Simulation/Common/Scripting/PowerSupply/LocomotivePowerSupply.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,20 @@ protected void SetCustomizedCabviewControlName(int index, string name)
293293
/// </summary>
294294
protected virtual void SetCurrentDynamicBrakeAvailability(bool avail) => LpsHost.DynamicBrakeAvailable = avail;
295295

296+
/// <summary>
297+
/// Called by other subsystems to determine whether the locomotive is powered
298+
/// </summary>
299+
/// <returns>
300+
/// PowerSupplyState.PowerOff if the locomotive is unpowered
301+
/// PowerSupplyState.PowerOnOngoing if the locomotive is in a power on sequence, but not all subsystems are ready
302+
/// PowerSupplyState.PowerOn if the locomotive is ready for service (all necessary subystems are connected)
303+
/// </returns>
304+
public virtual PowerSupplyState GetPowerStatus()
305+
{
306+
if (LpsHost.MainPowerSupplyState == LpsHost.AuxiliaryPowerSupplyState) return LpsHost.MainPowerSupplyState;
307+
return PowerSupplyState.PowerOnOngoing;
308+
}
309+
296310
/// <summary>
297311
/// Sends an event to the master switch
298312
/// </summary>

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/ILocomotivePowerSupply.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public interface ILocomotivePowerSupply : IPowerSupply
4747
bool ServiceRetentionCancellationButton { get; set; }
4848
bool ServiceRetentionActive { get; set; }
4949

50+
PowerSupplyState GetPowerStatus();
51+
5052
void HandleEventFromTcs(PowerSupplyEvent evt);
5153
void HandleEventFromTcs(PowerSupplyEvent evt, int id);
5254
void HandleEventFromTcs(PowerSupplyEvent evt, string message);

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/LocomotivePowerSupply.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ public virtual void Update(float elapsedClockSeconds)
266266
ElectricTrainSupplySwitch.Update(elapsedClockSeconds);
267267
}
268268

269+
public PowerSupplyState GetPowerStatus() => AbstractScript?.GetPowerStatus() ?? PowerSupplyState.Unavailable;
270+
269271
public void HandleEvent(PowerSupplyEvent evt)
270272
{
271273
AbstractScript?.HandleEvent(evt);

Source/RunActivity/Viewer3D/Popups/TrainCarOperationsViewerWindow.cs

Lines changed: 28 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
using Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
2828
using ORTS.Common;
2929
using ORTS.Common.Input;
30+
using ORTS.Scripting.Api;
3031
using System;
3132
using System.Collections.Generic;
3233
using System.IO;
3334
using System.Linq;
35+
using System.Windows.Forms;
3436

3537
namespace Orts.Viewer3D.Popups
3638
{
@@ -81,10 +83,9 @@ public class TrainCarOperationsViewerWindow : Window
8183

8284
public List<bool> AngleCockAPartiallyOpened = new List<bool>();
8385
public List<bool> AngleCockBPartiallyOpened = new List<bool>();
84-
public string BatteryStatus;
85-
string CircuitBreakerState;
86+
public bool BatterySwitchOn;
87+
public PowerSupplyState PowerSupplyStatus;
8688
public int LocoRowCount;
87-
public string PowerSupplyStatus;
8889
public int RowsCount;
8990
public int SpacerRowCount;
9091
public int SymbolsRowCount;
@@ -437,11 +438,9 @@ void AddSpace(bool full)
437438
}
438439
if (isElectricDieselLocomotive)
439440
{
440-
if (locomotive.GetMultipleUnitsConfiguration() != null)
441-
{
442-
line.Add(new buttonToggleMU(0, 0, textHeight, Owner.Viewer, CarPosition));
443-
AddSpace(false);
444-
}
441+
line.Add(new buttonToggleMU(0, 0, textHeight, Owner.Viewer, CarPosition));
442+
AddSpace(false);
443+
445444
line.Add(new buttonTogglePower(0, 0, textHeight, Owner.Viewer, CarPosition));
446445
AddSpace(false);
447446
if ((wagon != null) && (wagon.PowerSupply is IPowerSupply))
@@ -539,10 +538,8 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
539538
carOperations.CarOperationChanged = carOperations.Visible && carOperations.CarOperationChanged;
540539
}
541540
// Updates power supply status
542-
else if (isElectricDieselLocomotive &&
543-
(PowerSupplyStatus != null && PowerSupplyStatus != Owner.Viewer.PlayerTrain.Cars[CarPosition].GetStatus()
544-
|| (BatteryStatus != null && BatteryStatus != Owner.Viewer.PlayerTrain.Cars[CarPosition].GetStatus())
545-
|| (CircuitBreakerState != null && CircuitBreakerState != (trainCar as MSTSElectricLocomotive).ElectricPowerSupply.CircuitBreaker.State.ToString())))
541+
else if ((trainCar is MSTSWagon wagon && wagon.PowerSupply != null && BatterySwitchOn != wagon.PowerSupply.BatterySwitch.On)
542+
|| (isElectricDieselLocomotive && PowerSupplyStatus != (trainCar as MSTSLocomotive).LocomotivePowerSupply.GetPowerStatus()))
546543
{
547544
Layout();
548545
UpdateWindowSize();
@@ -898,7 +895,6 @@ public buttonFrontAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
898895
Viewer = viewer;
899896
TrainCarViewer = Viewer.TrainCarOperationsViewerWindow;
900897
CurrentCar = Viewer.PlayerTrain.Cars[carPosition];
901-
var first = car == Viewer.PlayerTrain.Cars.First();
902898

903899
if (CurrentCar.BrakeSystem is VacuumSinglePipe)
904900
{
@@ -908,15 +904,11 @@ public buttonFrontAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
908904
{
909905
var carAngleCockAOpenAmount = CurrentCar.BrakeSystem.AngleCockAOpenAmount;
910906
var carAngleCockAOpen = (CurrentCar as MSTSWagon).BrakeSystem.AngleCockAOpen;
911-
Texture = !TrainCarViewer.TrainCarOperationsChanged && first ? FrontAngleCockClosed
912-
: carAngleCockAOpenAmount > 0 && carAngleCockAOpenAmount < 1 ? FrontAngleCockPartial
907+
Texture = carAngleCockAOpenAmount > 0 && carAngleCockAOpenAmount < 1 ? FrontAngleCockPartial
913908
: carAngleCockAOpen ? FrontAngleCockOpened
914909
: FrontAngleCockClosed;
915910

916-
if (!first)
917-
{
918-
Click += new Action<Control, Point>(buttonFrontAngleCock_Click);
919-
}
911+
Click += new Action<Control, Point>(buttonFrontAngleCock_Click);
920912
}
921913
Source = new Rectangle(0, 0, size, size);
922914
}
@@ -949,7 +941,6 @@ public buttonRearAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
949941
Viewer = viewer;
950942
TrainCarViewer = Viewer.TrainCarOperationsViewerWindow;
951943
CurrentCar = Viewer.PlayerTrain.Cars[carPosition];
952-
var last = car == Viewer.PlayerTrain.Cars.Last();
953944

954945
if (CurrentCar.BrakeSystem is VacuumSinglePipe)
955946
{
@@ -959,15 +950,11 @@ public buttonRearAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
959950
{
960951
var carAngleCockBOpenAmount = (CurrentCar as MSTSWagon).BrakeSystem.AngleCockBOpenAmount;
961952
var carAngleCockBOpen = (CurrentCar as MSTSWagon).BrakeSystem.AngleCockBOpen;
962-
Texture = last ? RearAngleCockClosed
963-
: carAngleCockBOpenAmount > 0 && carAngleCockBOpenAmount < 1 ? RearAngleCockPartial
953+
Texture = carAngleCockBOpenAmount > 0 && carAngleCockBOpenAmount < 1 ? RearAngleCockPartial
964954
: carAngleCockBOpen ? RearAngleCockOpened
965955
: RearAngleCockClosed;
966956

967-
if (!last)
968-
{
969-
Click += new Action<Control, Point>(buttonRearAngleCock_Click);
970-
}
957+
Click += new Action<Control, Point>(buttonRearAngleCock_Click);
971958
}
972959
Source = new Rectangle(0, 0, size, size);
973960
}
@@ -1094,18 +1081,16 @@ class buttonToggleMU : Image
10941081
readonly Viewer Viewer;
10951082
readonly TrainCarOperationsViewerWindow TrainCarViewer;
10961083
readonly TrainCar CurrentCar;
1097-
readonly string MultipleUnitsConfiguration;
10981084
public buttonToggleMU(int x, int y, int size, Viewer viewer, int carPosition)
10991085
: base(x, y, size, size)
11001086
{
11011087
Viewer = viewer;
11021088
TrainCarViewer = Viewer.TrainCarOperationsViewerWindow;
11031089
CurrentCar = Viewer.PlayerTrain.Cars[carPosition];
11041090

1105-
MultipleUnitsConfiguration = Viewer.PlayerLocomotive.GetMultipleUnitsConfiguration();
1106-
if (CurrentCar is MSTSDieselLocomotive && MultipleUnitsConfiguration != null)
1091+
if (CurrentCar is MSTSLocomotive)
11071092
{
1108-
Texture = Viewer.TrainCarOperationsWindow.ModifiedSetting || ((CurrentCar as MSTSLocomotive).RemoteControlGroup == 0 && MultipleUnitsConfiguration != "1") ? MUconnected : MUdisconnected;
1093+
Texture = Viewer.TrainCarOperationsWindow.ModifiedSetting || ((CurrentCar as MSTSLocomotive).RemoteControlGroup == 0) ? MUconnected : MUdisconnected;
11091094
}
11101095
else
11111096
{
@@ -1116,12 +1101,12 @@ public buttonToggleMU(int x, int y, int size, Viewer viewer, int carPosition)
11161101
}
11171102
void buttonToggleMU_Click(Control arg1, Point arg2)
11181103
{
1119-
if (CurrentCar is MSTSDieselLocomotive)
1104+
if (CurrentCar is MSTSLocomotive)
11201105
{
11211106
MSTSLocomotive locomotive = CurrentCar as MSTSLocomotive;
11221107

11231108
new ToggleMUCommand(Viewer.Log, locomotive, locomotive.RemoteControlGroup < 0);
1124-
if (locomotive.RemoteControlGroup == 0 && MultipleUnitsConfiguration != "1")
1109+
if (locomotive.RemoteControlGroup == 0)
11251110
{
11261111
Viewer.Simulator.Confirmer.Information(Viewer.Catalog.GetString("MU signal connected"));
11271112
Texture = MUconnected;
@@ -1167,10 +1152,10 @@ void buttonTogglePower_Click(Control arg1, Point arg2)
11671152
if ((CurrentCar is MSTSElectricLocomotive) || (CurrentCar is MSTSDieselLocomotive))
11681153
{
11691154
MSTSLocomotive locomotive = CurrentCar as MSTSLocomotive;
1155+
bool powerOn = locomotive.LocomotivePowerSupply.GetPowerStatus() == PowerSupplyState.PowerOff;
11701156

1171-
new PowerCommand(Viewer.Log, locomotive, !locomotive.LocomotivePowerSupply.MainPowerSupplyOn);
1172-
var mainPowerSupplyOn = locomotive.LocomotivePowerSupply.MainPowerSupplyOn;
1173-
if (mainPowerSupplyOn)
1157+
new PowerCommand(Viewer.Log, locomotive, powerOn);
1158+
if (!powerOn)
11741159
Viewer.Simulator.Confirmer.Information(Viewer.Catalog.GetString("Power OFF command sent"));
11751160
else
11761161
Viewer.Simulator.Confirmer.Information(Viewer.Catalog.GetString("Power ON command sent"));
@@ -1183,36 +1168,9 @@ void buttonTogglePower_Click(Control arg1, Point arg2)
11831168
}
11841169
public Texture2D locomotiveStatusPower(int CarPosition)
11851170
{
1186-
string locomotiveStatus = CurrentCar.GetStatus();
1187-
foreach (string data in locomotiveStatus.Split('\n').Where((string d) => !string.IsNullOrWhiteSpace(d)))
1188-
{
1189-
string[] parts = data.Split(new string[] { " = " }, 2, StringSplitOptions.None);
1190-
string keyPart = parts[0];
1191-
string valuePart = parts?[1];
1192-
if (keyPart.Contains(Viewer.Catalog.GetParticularString("DieselEngine","Engine")))
1193-
{
1194-
TrainCarViewer.PowerSupplyStatus = locomotiveStatus;
1195-
Texture = valuePart.Contains(Viewer.Catalog.GetParticularString("DieselEngine", "Running")) ? PowerOn
1196-
: valuePart.Contains(Viewer.Catalog.GetParticularString("DieselEngine", "Stopped")) ? PowerOff
1197-
: PowerChanging;
1198-
break;
1199-
}
1200-
1201-
MSTSElectricLocomotive locomotive = CurrentCar as MSTSElectricLocomotive;
1202-
switch (locomotive.ElectricPowerSupply.CircuitBreaker.State)
1203-
{
1204-
case ORTS.Scripting.Api.CircuitBreakerState.Closed:
1205-
Texture = PowerOn;
1206-
break;
1207-
case ORTS.Scripting.Api.CircuitBreakerState.Closing:
1208-
Texture = PowerChanging;
1209-
break;
1210-
case ORTS.Scripting.Api.CircuitBreakerState.Open:
1211-
Texture = PowerOff;
1212-
break;
1213-
}
1214-
TrainCarViewer.CircuitBreakerState = locomotive.ElectricPowerSupply.CircuitBreaker.State.ToString();
1215-
}
1171+
var powerStatus = (CurrentCar as MSTSLocomotive).LocomotivePowerSupply.GetPowerStatus();
1172+
TrainCarViewer.PowerSupplyStatus = powerStatus;
1173+
Texture = powerStatus == PowerSupplyState.PowerOn ? PowerOn : powerStatus == PowerSupplyState.PowerOff ? PowerOff : PowerChanging;
12161174
return Texture;
12171175
}
12181176
}
@@ -1235,6 +1193,7 @@ public ToggleBatterySwitch(int x, int y, int size, Viewer viewer, int carPositio
12351193
if (wagon.PowerSupply.BatterySwitch.Mode == BatterySwitch.ModeType.AlwaysOn)
12361194
{
12371195
Texture = BattAlwaysOn32;
1196+
TrainCarViewer.BatterySwitchOn = wagon.PowerSupply.BatterySwitch.On;
12381197
}
12391198
else
12401199
{
@@ -1272,18 +1231,11 @@ void ToggleBatterySwitch_Click(Control arg1, Point arg2)
12721231
}
12731232
public Texture2D locomotiveStatusBattery(int CarPosition)
12741233
{
1275-
string locomotiveStatus = CurrentCar.GetStatus();
1276-
foreach (string data in locomotiveStatus.Split('\n').Where((string d) => !string.IsNullOrWhiteSpace(d)))
1234+
if (CurrentCar is MSTSWagon wagon && wagon.PowerSupply != null)
12771235
{
1278-
string[] parts = data.Split(new string[] { " = " }, 2, StringSplitOptions.None);
1279-
string keyPart = parts[0];
1280-
string valuePart = parts?[1];
1281-
if (keyPart.Contains(Viewer.Catalog.GetString("Battery")))
1282-
{
1283-
TrainCarViewer.BatteryStatus = locomotiveStatus;
1284-
Texture = valuePart.Contains(Viewer.Catalog.GetString("On")) ? BattOn32 : BattOff32;
1285-
break;
1286-
}
1236+
bool on = wagon.PowerSupply.BatterySwitch.On;
1237+
TrainCarViewer.BatterySwitchOn = on;
1238+
Texture = on ? BattOn32 : BattOff32;
12871239
}
12881240
return Texture;
12891241
}

0 commit comments

Comments
 (0)