3232using System . Linq ;
3333using static Orts . Viewer3D . Popups . TrainCarOperationsWindow ;
3434using Orts . Simulation . RollingStocks . SubSystems . PowerSupplies ;
35+ using Orts . Viewer3D . RollingStock ;
36+ using Orts . MultiPlayer ;
3537
3638namespace 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 ;
0 commit comments