Skip to content

Commit 234becd

Browse files
committed
Fix: F9 reversal flickering.
1 parent 7dec772 commit 234becd

File tree

6 files changed

+110
-47
lines changed

6 files changed

+110
-47
lines changed

Source/RunActivity/Viewer3D/Cameras.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using Orts.Simulation.Physics;
3030
using Orts.Simulation.RollingStocks;
3131
using Orts.Simulation.Signalling;
32+
using Orts.Viewer3D.Popups;
3233
using ORTS.Common;
3334
using ORTS.Common.Input;
3435
using ORTS.Settings;
@@ -129,7 +130,10 @@ public virtual void Reset()
129130
public void Activate()
130131
{
131132
ScreenChanged();
132-
OnActivate(Viewer.Camera == this);
133+
if (!Viewer.IsFormationReversed)// Avoids flickering
134+
{
135+
OnActivate(Viewer.Camera == this);
136+
}
133137
Viewer.Camera = this;
134138
Viewer.Simulator.PlayerIsInCab = Style == Styles.Cab || Style == Styles.ThreeDimCab;
135139
Update(ElapsedTime.Zero);

Source/RunActivity/Viewer3D/Popups/OSDCars.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,17 @@ public override void PrepareFrame(RenderFrame frame, ORTS.Common.ElapsedTime ela
7878
var newLabels = new Dictionary<TrainCar, LabelPrimitive>(labels.Count);
7979
var cars = Owner.Viewer.World.Trains.Cars;
8080
var cameraLocation = Owner.Viewer.Camera.CameraWorldLocation;
81-
var carID = Owner.Viewer.TrainCarOperationsViewerWindow.CurrentCarID;
81+
8282
//data from Webpage
8383
var trainCarWebpage = Owner.Viewer.TrainCarOperationsWebpage;
84+
var trainCarWebpageActive = trainCarWebpage != null && trainCarWebpage.Connections > 0 && trainCarWebpage.TrainCarSelected;
8485
var carIDWebpage = trainCarWebpage != null && trainCarWebpage.Connections > 0 ? trainCarWebpage.CurrentCarID : "";
85-
bool isVisibleTrainCarViewerOrWebpage = (Owner.Viewer.TrainCarOperationsWindow.Visible && !Owner.Viewer.TrainCarOperationsViewerWindow.Visible) || Owner.Viewer.TrainCarOperationsViewerWindow.Visible || (trainCarWebpage != null && trainCarWebpage.Connections > 0 && Owner.Viewer.TrainCarOperationsWebpage.TrainCarSelected);
86+
87+
//shows only one CarID label when traincaroperationsviewer is not visible
88+
var trainCarOperationsViewer = Owner.Viewer.TrainCarOperationsViewerWindow;
89+
var carID = Owner.Viewer.TrainCarOperationsViewerWindow.Visible? trainCarOperationsViewer.CurrentCarID: trainCarWebpageActive ? trainCarWebpage.CurrentCarID : Owner.Viewer.TrainCarOperationsViewerWindow.CurrentCarID;
90+
91+
bool isVisibleTrainCarViewerOrWebpage = (Owner.Viewer.TrainCarOperationsWindow.Visible && !trainCarOperationsViewer.Visible) || trainCarOperationsViewer.Visible || trainCarWebpageActive;
8692
foreach (var car in cars.Keys)
8793
{
8894
// Calculates distance between camera and platform label.

Source/RunActivity/Viewer3D/Popups/TrainCarOperationsViewerWindow.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public struct ListLabel
143143

144144
Train PlayerTrain;
145145
bool LastPlayerLocomotiveFlippedState;
146-
public bool IsFormationReversed;// Required when reversal
147146
public bool UpdateTCOLayout;// Required when reversal
148147
int LastPlayerTrainCars;
149148
int OldCarPosition;
@@ -476,18 +475,17 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
476475
{
477476
var carOperations = Owner.Viewer.CarOperationsWindow;
478477
var trainCarOperations = Owner.Viewer.TrainCarOperationsWindow;
479-
var trainInfo = Owner.Viewer.PlayerTrain.GetTrainInfo();
480-
IsFormationReversed = trainInfo.cabOrientation != 0;
478+
var isFormationReversed = Owner.Viewer.IsFormationReversed;
481479

482480
if (CouplerChanged || PlayerTrain != Owner.Viewer.PlayerTrain || Owner.Viewer.PlayerTrain.Cars.Count != LastPlayerTrainCars || (Owner.Viewer.PlayerLocomotive != null &&
483-
LastPlayerLocomotiveFlippedState != IsFormationReversed))
481+
LastPlayerLocomotiveFlippedState != isFormationReversed))
484482
{
485483
CouplerChanged = false;
486484
PlayerTrain = Owner.Viewer.PlayerTrain;
487485

488486
LastPlayerTrainCars = Owner.Viewer.PlayerTrain.Cars.Count;
489487
CarPosition = CarPosition >= LastPlayerTrainCars ? LastPlayerTrainCars - 1 : CarPosition;
490-
if (Owner.Viewer.PlayerLocomotive != null) LastPlayerLocomotiveFlippedState = IsFormationReversed;
488+
if (Owner.Viewer.PlayerLocomotive != null) LastPlayerLocomotiveFlippedState = isFormationReversed;
491489

492490
Layout();
493491
UpdateWindowSize();

Source/RunActivity/Viewer3D/Popups/TrainCarOperationsWindow.cs

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
using System.Linq;
3333
using static Orts.Viewer3D.Popups.TrainCarOperationsWindow;
3434
using Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
35+
using Orts.Viewer3D.RollingStock;
36+
using Orts.MultiPlayer;
3537

3638
namespace Orts.Viewer3D.Popups
3739
{
@@ -123,7 +125,6 @@ public class TrainCarOperationsWindow : Window
123125
public static bool FontChanged;
124126
public static bool FontToBold;
125127
public string LastCarIDSelected;// Required when reversal
126-
public bool IsFormationReversed;// Required when reversal
127128
public int OldCarPosition;
128129

129130
//Electrical power
@@ -153,7 +154,7 @@ public struct ListLabel
153154

154155
Train PlayerTrain;
155156
int LastPlayerTrainCars;
156-
bool LastPlayerLocomotiveFlippedState;
157+
public bool LastPlayerLocomotiveFlippedState;
157158

158159
public TrainCarOperationsWindow(WindowManager owner)
159160
: base(owner, Window.DecorationSize.X + CarListPadding + (owner.TextFontDefault.Height * 15), Window.DecorationSize.Y + (owner.TextFontDefault.Height * 5), Viewer.Catalog.GetString("Train Car Operations"))
@@ -519,7 +520,7 @@ void AddSpace()
519520

520521
// Allows to resize the window according to the carPosition value.
521522
if (RowsCount > carPosition) RowsCount = carPosition;
522-
if (SeparatorCount > carPosition -1) SeparatorCount = carPosition - 1;
523+
if (SeparatorCount > carPosition - 1) SeparatorCount = carPosition - 1;
523524
}
524525
}
525526
return Vbox;
@@ -598,8 +599,7 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
598599
var trainCarViewer = Owner.Viewer.TrainCarOperationsViewerWindow;
599600
var carOperations = Owner.Viewer.CarOperationsWindow;
600601
var trainCarWebpage = Owner.Viewer.TrainCarOperationsWebpage;
601-
var trainInfo = Owner.Viewer.PlayerTrain.GetTrainInfo();
602-
IsFormationReversed = trainInfo.cabOrientation != 0;
602+
var isFormationReversed = Owner.Viewer.IsFormationReversed;
603603

604604
CabCameraEnabled = Owner.Viewer.Camera is CabCamera || Owner.Viewer.Camera == Owner.Viewer.ThreeDimCabCamera;
605605
if (CarIdClicked && !CabCameraEnabled && !trainCarViewer.Visible && (!FrontActive || !BackActive))
@@ -636,18 +636,27 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
636636
topCarPositionVisible();
637637
localScrollLayout(SelectedCarPosition);
638638
}
639+
// Restore LastCarIDSelected (F9) after returning from different camera views
640+
if (CarIdClicked && Owner.Viewer.Camera.AttachedCar.CarID != LastCarIDSelected)
641+
{
642+
trainCarViewer.CurrentCarID = LastCarIDSelected;
643+
trainCarViewer.CarPosition = CarPosition = PlayerTrain.Cars.TakeWhile(x => x.CarID != LastCarIDSelected).Count();
644+
SelectedCarPosition = CarPosition;
645+
trainCarViewer.TrainCarOperationsChanged = true;
646+
SetCameraView();
647+
}
639648

640649
UserCommand? controlDiesel = GetPressedKey(UserCommand.ControlDieselHelper, UserCommand.ControlDieselPlayer, UserCommand.ControlInitializeBrakes);
641650
if (controlDiesel == UserCommand.ControlDieselHelper || controlDiesel == UserCommand.ControlDieselPlayer || controlDiesel == UserCommand.ControlInitializeBrakes)
642651
{
643652
Layout();
644-
PowerSupplyStatus = Owner.Viewer.PlayerTrain.Cars[Owner.Viewer.PlayerTrain.Cars.Count > CarPosition? CarPosition : CarPosition - 1].GetStatus();
653+
PowerSupplyStatus = Owner.Viewer.PlayerTrain.Cars[Owner.Viewer.PlayerTrain.Cars.Count > CarPosition ? CarPosition : CarPosition - 1].GetStatus();
645654
ModifiedSetting = true;
646655
}
647656

648657
var carsCountChanged = Owner.Viewer.PlayerTrain.Cars.Count != LastPlayerTrainCars;
649658
if (PlayerTrain != Owner.Viewer.PlayerTrain || carsCountChanged || (Owner.Viewer.PlayerLocomotive != null &&
650-
LastPlayerLocomotiveFlippedState != IsFormationReversed))
659+
LastPlayerLocomotiveFlippedState != isFormationReversed))
651660
{
652661
PlayerTrain = Owner.Viewer.PlayerTrain;
653662
if (LastPlayerTrainCars != Owner.Viewer.PlayerTrain.Cars.Count)
@@ -681,6 +690,7 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
681690
}
682691
}
683692
}
693+
684694
if (trainCarViewer.TrainCarOperationsChanged || trainCarViewer.RearBrakeHoseChanged
685695
|| trainCarViewer.FrontBrakeHoseChanged || ModifiedSetting || CarIdClicked || carOperations.CarOperationChanged)
686696
{
@@ -693,41 +703,22 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
693703
carOperations.CarOperationChanged = carOperations.Visible && carOperations.CarOperationChanged;
694704
}
695705

696-
if ((!trainCarViewer.Visible || trainCarViewer.UpdateTCOLayout) && (CarIdClicked || (LastPlayerLocomotiveFlippedState != IsFormationReversed)))
697-
{
698-
var currentCameraCarID = Owner.Viewer.Camera.AttachedCar.CarID;
699-
700-
trainCarViewer.CurrentCarID = LastCarIDSelected;
701-
trainCarViewer.CarPosition = CarPosition = PlayerTrain.Cars.TakeWhile(x => x.CarID != LastCarIDSelected).Count();
702-
703-
if (CabCameraEnabled)// Displays camera 1
704-
{ // Setting the camera view
705-
CabCameraEnabled = false;
706-
}
707-
else if (OldCarPosition != SelectedCarPosition || (CarIdClicked && CarPosition == 0))
708-
{
709-
SetCameraView();
710-
OldCarPosition = SelectedCarPosition;
711-
}
712-
if (PlayerTrain.Cars.Count > CarPosition)
713-
{
714-
trainCarViewer.CarPosition = SelectedCarPosition = CarPosition = PlayerTrain.Cars.TakeWhile(x => x.CarID != LastCarIDSelected).Count();
715-
}
716-
else
717-
{
718-
trainCarViewer.CarPosition = SelectedCarPosition = CarPosition = 0;
719-
trainCarViewer.CurrentCarID = PlayerTrain.Cars[0].CarID;
720-
}
706+
if ((!trainCarViewer.Visible || trainCarViewer.UpdateTCOLayout) && (CarIdClicked || (LastPlayerLocomotiveFlippedState != isFormationReversed)))
707+
{ // Apply the reveral point to layout
708+
_ = new FormationReversed(Owner.Viewer, PlayerTrain);
709+
}
721710

722-
// Scroll LabelTop
723-
LabelTop = LabelPositionTop[SelectedCarPosition];
724-
Owner.Viewer.FrontCamera.IsCameraFront = Owner.Viewer.FrontCamera.AttachedCar != null;
725-
LastPlayerLocomotiveFlippedState = IsFormationReversed;
711+
if (trainCarViewer.TrainCarOperationsChanged || trainCarViewer.RearBrakeHoseChanged
712+
|| trainCarViewer.FrontBrakeHoseChanged || ModifiedSetting || CarIdClicked || carOperations.CarOperationChanged)
713+
{
726714
Layout();
727715
localScrollLayout(SelectedCarPosition);
728716
updateLayoutSize();
717+
ModifiedSetting = false;
718+
// Avoids bug
719+
trainCarViewer.TrainCarOperationsChanged = WarningEnabled;
720+
carOperations.CarOperationChanged = carOperations.Visible && carOperations.CarOperationChanged;
729721
CarIdClicked = false;
730-
trainCarViewer.UpdateTCOLayout = false;
731722
}
732723

733724
if (CarPositionChanged || (trainCarWebpage != null && CarPosition != trainCarViewer.CarPosition && trainCarWebpage.Connections > 0))
@@ -750,7 +741,7 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
750741
}
751742
}
752743
}
753-
private void SetCameraView()
744+
public void SetCameraView()
754745
{
755746
if (Owner.Viewer.FrontCamera.AttachedCar != null)
756747
{
@@ -777,6 +768,57 @@ public void updateWarningCarPosition(int carPosition, Texture2D texture, Texture
777768
.Where((UserCommand key) => UserInput.IsDown(key))
778769
.FirstOrDefault();
779770
}
771+
772+
class FormationReversed
773+
{
774+
readonly Viewer Viewer;
775+
readonly TrainCarOperationsWindow TrainCar;
776+
readonly TrainCarOperationsViewerWindow TrainCarViewer;
777+
public FormationReversed(Viewer viewer, Train PlayerTrain)
778+
{
779+
Viewer = viewer;
780+
TrainCar = Viewer.TrainCarOperationsWindow;
781+
TrainCarViewer = Viewer.TrainCarOperationsViewerWindow;
782+
var currentCameraCarID = Viewer.Camera.AttachedCar.CarID;
783+
784+
TrainCarViewer.CurrentCarID = TrainCar.LastCarIDSelected;
785+
TrainCarViewer.CarPosition = TrainCar.CarPosition = PlayerTrain.Cars.TakeWhile(x => x.CarID != TrainCar.LastCarIDSelected).Count();
786+
787+
if (TrainCar.CabCameraEnabled)// Displays camera 1
788+
{ // Setting the camera view
789+
TrainCar.CabCameraEnabled = false;
790+
}
791+
else if (TrainCar.OldCarPosition != TrainCar.SelectedCarPosition || (TrainCar.CarIdClicked && TrainCar.CarPosition == 0))
792+
{
793+
TrainCar.SetCameraView();
794+
TrainCar.OldCarPosition = TrainCar.SelectedCarPosition;
795+
}
796+
797+
if (PlayerTrain.Cars.Count > TrainCar.CarPosition)
798+
{
799+
TrainCarViewer.CarPosition = TrainCar.SelectedCarPosition = TrainCar.CarPosition = PlayerTrain.Cars.TakeWhile(x => x.CarID != TrainCar.LastCarIDSelected).Count();
800+
}
801+
else
802+
{
803+
TrainCarViewer.CarPosition = TrainCar.SelectedCarPosition = TrainCar.CarPosition = 0;
804+
TrainCarViewer.CurrentCarID = PlayerTrain.Cars[0].CarID;
805+
}
806+
807+
// Scroll LabelTop
808+
TrainCar.LabelTop = TrainCar.LabelPositionTop[TrainCar.SelectedCarPosition];
809+
Viewer.FrontCamera.IsCameraFront = Viewer.FrontCamera.AttachedCar != null;
810+
TrainCar.Layout();
811+
TrainCar.localScrollLayout(TrainCar.SelectedCarPosition);
812+
TrainCar.updateLayoutSize();
813+
814+
// Reset
815+
Viewer.IsFormationReversed = false;
816+
TrainCar.LastPlayerLocomotiveFlippedState = Viewer.IsFormationReversed;
817+
TrainCar.CarIdClicked = false;
818+
TrainCarViewer.UpdateTCOLayout = false;
819+
}
820+
}
821+
780822
class buttonArrowRight : Image
781823
{
782824
readonly Viewer Viewer;

Source/RunActivity/Viewer3D/Viewer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,9 @@ public bool IsBeforeNoon
268268
public Camera SuspendedCamera { get; private set; }
269269

270270
public static double DbfEvalAutoPilotTimeS = 0;//Debrief eval
271-
public static double DbfEvalIniAutoPilotTimeS = 0;//Debrief eval
271+
public static double DbfEvalIniAutoPilotTimeS = 0;//Debrief eval
272272
public static bool DbfEvalAutoPilot = false;//DebriefEval
273+
public bool IsFormationReversed; //Avoid flickering when reversal using TrainCarOperations window
273274

274275
/// <summary>
275276
/// Finds time of last entry to set ReplayEndsAt and provide the Replay started message.
@@ -795,6 +796,7 @@ public void Update(RenderFrame frame, float elapsedRealTime)
795796
// We need to do it also here, because passing from manual to auto a ReverseFormation may be needed
796797
if (Camera is TrackingCamera && Camera.AttachedCar != null && Camera.AttachedCar.Train != null && Camera.AttachedCar.Train.FormationReversed)
797798
{
799+
IsFormationReversed = TrainCarOperationsWindow.Visible || TrainCarOperationsWebpage.Connections > 0;
798800
Camera.AttachedCar.Train.FormationReversed = false;
799801
(Camera as TrackingCamera).SwapCameras();
800802
}
@@ -850,6 +852,7 @@ public void Update(RenderFrame frame, float elapsedRealTime)
850852
// Check if you need to swap camera
851853
if (Camera is TrackingCamera && Camera.AttachedCar != null && Camera.AttachedCar.Train != null && Camera.AttachedCar.Train.FormationReversed)
852854
{
855+
IsFormationReversed = TrainCarOperationsWindow.Visible || TrainCarOperationsWebpage.Connections > 0;
853856
Camera.AttachedCar.Train.FormationReversed = false;
854857
(Camera as TrackingCamera).SwapCameras();
855858
}

Source/RunActivity/Viewer3D/WebServices/TrainCarOperationsWebpage.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using Orts.Simulation.RollingStocks.SubSystems.Brakes.MSTS;
3232
using ORTS.Scripting.Api;
3333
using System.IO;
34+
using Orts.Simulation.Physics;
3435

3536
namespace Orts.Viewer3D.WebServices
3637
{
@@ -276,6 +277,14 @@ private void handleSend()
276277

277278
private void fillStatus(OperationsStatus operationStatus)
278279
{
280+
// Apply reveral point when TrainCarOperations/Viewer windows are not visibles
281+
// Makes this Webpage version, more autonoumus
282+
if (!Viewer.TrainCarOperationsWindow.Visible && !Viewer.TrainCarOperationsViewerWindow.Visible && Viewer.IsFormationReversed)
283+
{
284+
_ = new FormationReversed(Viewer, Viewer.PlayerTrain);
285+
Viewer.IsFormationReversed = false;
286+
}
287+
279288
int carPosition = 0;
280289

281290
foreach (TrainCar trainCar in Viewer.PlayerTrain.Cars)
@@ -507,6 +516,7 @@ private void fillStatusArrowLeft(int carPosition)
507516
if (TrainCarSelected && (carPosition == TrainCarSelectedPosition))
508517
{
509518
filename = "TrainOperationsArrowRight32.png";
519+
CurrentCarID = Viewer.PlayerTrain.Cars[carPosition].CarID;// Requiered by OSDCars.cs
510520
}
511521
else
512522
{

0 commit comments

Comments
 (0)