Skip to content

Commit 65eeaa7

Browse files
committed
https://blueprints.launchpad.net/or/+spec/animating-trainset-windows
1 parent 0914a53 commit 65eeaa7

File tree

13 files changed

+289
-22
lines changed

13 files changed

+289
-22
lines changed

Source/ORTS.Common/Input/UserCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public enum UserCommand
162162
[GetString("Control Pantograph 2")] ControlPantograph2,
163163
[GetString("Control Pantograph 3")] ControlPantograph3,
164164
[GetString("Control Pantograph 4")] ControlPantograph4,
165+
[GetString("Control Window Left")] ControlWindowLeft,
166+
[GetString("Control Window Right")] ControlWindowRight,
165167
[GetString("Control Battery Close")] ControlBatterySwitchClose,
166168
[GetString("Control Battery Open")] ControlBatterySwitchOpen,
167169
[GetString("Control Master Key")] ControlMasterKey,

Source/ORTS.Settings/InputSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ static void InitializeCommands(UserCommandInput[] Commands)
373373
Commands[(int)UserCommand.ControlDieselPlayer] = new UserCommandKeyInput(0x15, KeyModifiers.Shift);
374374
Commands[(int)UserCommand.ControlDoorLeft] = new UserCommandKeyInput(0x10);
375375
Commands[(int)UserCommand.ControlDoorRight] = new UserCommandKeyInput(0x10, KeyModifiers.Shift);
376+
Commands[(int)UserCommand.ControlWindowLeft] = new UserCommandKeyInput(0x10, KeyModifiers.Control);
377+
Commands[(int)UserCommand.ControlWindowRight] = new UserCommandKeyInput(0x10, KeyModifiers.Control | KeyModifiers.Shift);
376378
Commands[(int)UserCommand.ControlDynamicBrakeDecrease] = new UserCommandKeyInput(0x33);
377379
Commands[(int)UserCommand.ControlDynamicBrakeIncrease] = new UserCommandKeyInput(0x34);
378380
Commands[(int)UserCommand.ControlElectricTrainSupply] = new UserCommandKeyInput(0x30, KeyModifiers.Alt);

Source/Orts.Formats.Msts/CabViewFile.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ public enum CABViewControlTypes
184184
ORTS_MIRRORS,
185185
ORTS_PANTOGRAPH3,
186186
ORTS_PANTOGRAPH4,
187+
ORTS_LEFTWINDOW,
188+
ORTS_RIGHTWINDOW,
187189
ORTS_LARGE_EJECTOR,
188190
ORTS_WATER_SCOOP,
189191
ORTS_HOURDIAL,
@@ -206,6 +208,8 @@ public enum CABViewControlTypes
206208
ORTS_ELECTRIC_TRAIN_SUPPLY_COMMAND_SWITCH,
207209
ORTS_ELECTRIC_TRAIN_SUPPLY_ON,
208210
ORTS_2DEXTERNALWIPERS,
211+
ORTS_2DEXTERNALLEFTWINDOW,
212+
ORTS_2DEXTERNALRIGHTWINDOW,
209213
ORTS_GENERIC_ITEM1,
210214
ORTS_GENERIC_ITEM2,
211215
ORTS_SCREEN_SELECT,
@@ -273,6 +277,10 @@ public enum CABViewControlTypes
273277
ORTS_ITEM2CONTINUOUS,
274278
ORTS_ITEM1TWOSTATE,
275279
ORTS_ITEM2TWOSTATE,
280+
ORTS_EXTERNALLEFTWINDOWFRONT,
281+
ORTS_EXTERNALRIGHTWINDOWFRONT,
282+
ORTS_EXTERNALLEFTWINDOWREAR,
283+
ORTS_EXTERNALRIGHTWINDOWREAR,
276284
}
277285

278286
public enum CABViewControlStyles

Source/Orts.Simulation/Common/Commands.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,47 @@ public override void Redo()
12431243
}
12441244
}
12451245

1246+
[Serializable()]
1247+
public sealed class ToggleWindowLeftCommand : Command
1248+
{
1249+
public static MSTSWagon Receiver { get; set; }
1250+
1251+
public ToggleWindowLeftCommand(CommandLog log)
1252+
: base(log)
1253+
{
1254+
Redo();
1255+
}
1256+
1257+
public override void Redo()
1258+
{
1259+
if (Receiver is MSTSLocomotive locomotive && locomotive.UsingRearCab)
1260+
locomotive.ToggleWindow(rear: true, left: false);
1261+
else
1262+
Receiver.ToggleWindow(rear: false, left: true);
1263+
}
1264+
}
1265+
1266+
[Serializable()]
1267+
public sealed class ToggleWindowRightCommand : Command
1268+
{
1269+
public static MSTSWagon Receiver { get; set; }
1270+
1271+
public ToggleWindowRightCommand(CommandLog log)
1272+
: base(log)
1273+
{
1274+
Redo();
1275+
}
1276+
1277+
public override void Redo()
1278+
{
1279+
if (Receiver is MSTSLocomotive locomotive && locomotive.UsingRearCab)
1280+
locomotive.ToggleWindow(rear: true, left: true);
1281+
else
1282+
Receiver.ToggleWindow(rear: false, left: false);
1283+
}
1284+
}
1285+
1286+
12461287
[Serializable()]
12471288
public sealed class ToggleBatterySwitchCommand : BooleanCommand
12481289
{

Source/Orts.Simulation/Common/Events.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ public enum Event
198198
VigilanceAlarmReset,
199199
WaterScoopDown,
200200
WaterScoopUp,
201+
WindowClosing,
202+
WindowOpening,
203+
WindowsClosed,
204+
WindowsOpen,
201205
WiperOff,
202206
WiperOn,
203207
_HeadlightDim,
@@ -524,6 +528,11 @@ public static Event From(Source source, int eventID)
524528
case 250: return Event.OverchargeBrakingOn;
525529
case 251: return Event.OverchargeBrakingOff;
526530

531+
case 260: return Event.WindowClosing;
532+
case 261: return Event.WindowOpening;
533+
case 262: return Event.WindowsClosed;
534+
case 263: return Event.WindowsOpen;
535+
527536
// Cruise Control
528537
case 298: return Event.LeverFromZero;
529538
case 299: return Event.LeverToZero;

Source/Orts.Simulation/Simulation/Confirmer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public enum CabControl {
107107
, DoorsLeft
108108
, DoorsRight
109109
, Mirror
110+
, WindowLeft
111+
, WindowRight
110112
// Track Devices
111113
, SwitchAhead
112114
, SwitchBehind
@@ -243,7 +245,7 @@ public Confirmer(Simulator simulator, double defaultDurationS)
243245
, new string [] { GetString("Bell"), GetString("off"), null, GetString("ring") }
244246
, new string [] { GetString("Headlight"), GetString("off"), GetString("dim"), GetString("bright") }
245247
, new string [] { GetString("Cab Light"), GetString("off"), null, GetString("on") }
246-
, new string [] { GetString("Wipers"), GetString("off"), null, GetString("on") }
248+
, new string [] { GetString("Wipers"), GetString("off"), null, GetString("on") }
247249
, new string [] { GetString("Cab"), null, null, GetParticularString("Cab", "change"), null, null, GetString("changing is not available"), GetString("changing disabled. Close throttle, set reverser to neutral, stop train then re-try.") }
248250
, new string [] { GetString("Odometer"), null, null, GetParticularString("Odometer", "reset"), GetParticularString("Odometer", "counting down"), GetParticularString("Odometer", "counting up") }
249251
, new string [] { GetString("Battery"), GetString("off"), null, GetString("on") }
@@ -254,7 +256,9 @@ public Confirmer(Simulator simulator, double defaultDurationS)
254256
// Train Devices
255257
, new string [] { GetString("Doors Left"), GetString("close"), null, GetString("open") }
256258
, new string [] { GetString("Doors Right"), GetString("close"), null, GetString("open") }
257-
, new string [] { GetString("Mirror"), GetString("retract"), null, GetString("extend") }
259+
, new string [] { GetString("Mirror"), GetString("retract"), null, GetString("extend") }
260+
, new string [] { GetString("Window Left"), GetString("closing"), null, GetString("opening") }
261+
, new string [] { GetString("Window Right"), GetString("closing"), null, GetString("opening") }
258262
// Track Devices
259263
, new string [] { GetString("Switch Ahead"), null, null, GetParticularString("Switch", "change"), null, null, GetString("locked. Use Control+M to change signals to manual mode then re-try.") }
260264
, new string [] { GetString("Switch Behind"), null, null, GetParticularString("Switch", "change"), null, null, GetString("locked. Use Control+M to change signals to manual mode then re-try.") }

Source/Orts.Simulation/Simulation/RollingStocks/MSTSLocomotive.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5798,6 +5798,16 @@ public virtual float GetDataOf(CabViewControl cvc)
57985798
data = state >= DoorState.Opening ? 1 : 0;
57995799
}
58005800
break;
5801+
case CABViewControlTypes.ORTS_LEFTWINDOW:
5802+
case CABViewControlTypes.ORTS_2DEXTERNALLEFTWINDOW:
5803+
data = UsingRearCab ? (WindowStates[RightWindowRearIndex] == WindowState.Closing || WindowStates[RightWindowRearIndex] == WindowState.Opening ? 1 : 0) :
5804+
(WindowStates[LeftWindowFrontIndex] == WindowState.Closing || WindowStates[LeftWindowFrontIndex] == WindowState.Opening ? 1 : 0);
5805+
break;
5806+
case CABViewControlTypes.ORTS_RIGHTWINDOW:
5807+
case CABViewControlTypes.ORTS_2DEXTERNALRIGHTWINDOW:
5808+
data = UsingRearCab ? (WindowStates[LeftWindowRearIndex] == WindowState.Closing || WindowStates[LeftWindowRearIndex] == WindowState.Opening ? 1 : 0) :
5809+
(WindowStates[RightWindowFrontIndex] == WindowState.Closing || WindowStates[RightWindowFrontIndex] == WindowState.Opening ? 1 : 0);
5810+
break;
58015811
case CABViewControlTypes.ORTS_MIRRORS:
58025812
data = MirrorOpen ? 1 : 0;
58035813
break;

Source/Orts.Simulation/Simulation/RollingStocks/MSTSWagon.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ public class MSTSWagon : TrainCar
7272
public Doors Doors;
7373
public Door RightDoor => Doors.RightDoor;
7474
public Door LeftDoor => Doors.LeftDoor;
75+
76+
public enum WindowState
77+
// Don't change the order of entries within this enum
78+
{
79+
Closed,
80+
Closing,
81+
Opening,
82+
Open,
83+
}
84+
85+
public static int LeftWindowFrontIndex = 0;
86+
public static int RightWindowFrontIndex = 1;
87+
public static int LeftWindowRearIndex = 2;
88+
public static int RightWindowRearIndex = 3;
89+
public WindowState[] WindowStates = new WindowState[4];
90+
public float[] SoundHeardInternallyCorrection = new float[2];
91+
7592
public bool MirrorOpen;
7693
public bool UnloadingPartsOpen;
7794
public bool WaitForAnimationReady; // delay counter to start loading/unliading is on;
@@ -1745,6 +1762,10 @@ public override void Save(BinaryWriter outf)
17451762
outf.Write(DerailPossible);
17461763
outf.Write(DerailExpected);
17471764
outf.Write(DerailElapsedTimeS);
1765+
for (int index = 0; index < 4; index++)
1766+
{
1767+
outf.Write((int)WindowStates[index]);
1768+
}
17481769

17491770
LocomotiveAxles.Save(outf);
17501771

@@ -1800,6 +1821,10 @@ public override void Restore(BinaryReader inf)
18001821
DerailPossible = inf.ReadBoolean();
18011822
DerailExpected = inf.ReadBoolean();
18021823
DerailElapsedTimeS = inf.ReadSingle();
1824+
for (int index = 0; index < 4; index++)
1825+
{
1826+
WindowStates[index] = (WindowState)inf.ReadInt32();
1827+
}
18031828

18041829
MoveParamsToAxle();
18051830
LocomotiveAxles.Restore(inf);
@@ -3430,6 +3455,22 @@ public void ToggleMirrors()
34303455
if (Simulator.PlayerLocomotive == this) Simulator.Confirmer.Confirm(CabControl.Mirror, MirrorOpen ? CabSetting.On : CabSetting.Off);
34313456
}
34323457

3458+
public void ToggleWindow(bool rear, bool left)
3459+
{
3460+
var open = false;
3461+
var index = (left ? 0 : 1) + 2 * (rear ? 1 : 0);
3462+
if (WindowStates[index] == WindowState.Closed || WindowStates[index] == WindowState.Closing)
3463+
WindowStates[index] = WindowState.Opening;
3464+
else if (WindowStates[index] == WindowState.Open || WindowStates[index] == WindowState.Opening)
3465+
WindowStates[index] = WindowState.Closing;
3466+
if (WindowStates[index] == WindowState.Opening) open = true;
3467+
3468+
3469+
if (open) SignalEvent(Event.WindowOpening); // hook for sound trigger
3470+
else SignalEvent(Event.WindowClosing);
3471+
if (Simulator.PlayerLocomotive == this) Simulator.Confirmer.Confirm(left ^ rear ? CabControl.WindowLeft : CabControl.WindowRight, open ? CabSetting.On : CabSetting.Off);
3472+
}
3473+
34333474
public void FindControlActiveLocomotive()
34343475
{
34353476
// Find the active locomotive associated with a control car

Source/Orts.Simulation/Simulation/Simulator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,8 @@ public void SetCommandReceivers()
699699
ToggleDoorsLeftCommand.Receiver = (MSTSLocomotive)PlayerLocomotive;
700700
ToggleDoorsRightCommand.Receiver = (MSTSLocomotive)PlayerLocomotive;
701701
ToggleMirrorsCommand.Receiver = (MSTSLocomotive)PlayerLocomotive;
702+
ToggleWindowLeftCommand.Receiver = (MSTSLocomotive)PlayerLocomotive;
703+
ToggleWindowRightCommand.Receiver = (MSTSLocomotive)PlayerLocomotive;
702704
CabRadioCommand.Receiver = (MSTSLocomotive)PlayerLocomotive;
703705
ToggleHelpersEngineCommand.Receiver = (MSTSLocomotive)PlayerLocomotive;
704706
BatterySwitchCommand.Receiver = (PlayerLocomotive as MSTSLocomotive).LocomotivePowerSupply;

Source/RunActivity/Viewer3D/AnimatedPart.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ public void UpdateState(bool state, ElapsedTime elapsedTime)
140140
SetFrameClamp(AnimationKey + (state ? 1 : -1) * elapsedTime.ClockSeconds);
141141
}
142142

143+
/// <summary>
144+
/// Updates an animated part that toggles between two states and returns relative value of
145+
/// animation key (between 0 and 1).
146+
/// </summary>
147+
public float UpdateAndReturnState(bool state, ElapsedTime elapsedTime)
148+
{
149+
SetFrameClamp(AnimationKey + (state ? 1 : -1) * elapsedTime.ClockSeconds);
150+
return AnimationKey / FrameCount;
151+
}
152+
143153
/// <summary>
144154
/// Updates an animated part that loops (e.g. running gear), changing by the given amount.
145155
/// </summary>

0 commit comments

Comments
 (0)