3232using System . Linq ;
3333using static Orts . Viewer3D . Popups . TrainCarOperationsWindow ;
3434using Orts . Simulation . RollingStocks . SubSystems . PowerSupplies ;
35- using Orts . Viewer3D . RollingStock ;
36- using Orts . MultiPlayer ;
3735
3836namespace Orts . Viewer3D . Popups
3937{
@@ -125,6 +123,7 @@ public class TrainCarOperationsWindow : Window
125123 public static bool FontChanged ;
126124 public static bool FontToBold ;
127125 public string LastCarIDSelected ; // Required when reversal
126+ public bool IsFormationReversed ; // Required when reversal
128127 public int OldCarPosition ;
129128
130129 //Electrical power
@@ -154,7 +153,7 @@ public struct ListLabel
154153
155154 Train PlayerTrain ;
156155 int LastPlayerTrainCars ;
157- public bool LastPlayerLocomotiveFlippedState ;
156+ bool LastPlayerLocomotiveFlippedState ;
158157
159158 public TrainCarOperationsWindow ( WindowManager owner )
160159 : base ( owner , Window . DecorationSize . X + CarListPadding + ( owner . TextFontDefault . Height * 15 ) , Window . DecorationSize . Y + ( owner . TextFontDefault . Height * 5 ) , Viewer . Catalog . GetString ( "Train Car Operations" ) )
@@ -520,7 +519,7 @@ void AddSpace()
520519
521520 // Allows to resize the window according to the carPosition value.
522521 if ( RowsCount > carPosition ) RowsCount = carPosition ;
523- if ( SeparatorCount > carPosition - 1 ) SeparatorCount = carPosition - 1 ;
522+ if ( SeparatorCount > carPosition - 1 ) SeparatorCount = carPosition - 1 ;
524523 }
525524 }
526525 return Vbox ;
@@ -599,7 +598,8 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
599598 var trainCarViewer = Owner . Viewer . TrainCarOperationsViewerWindow ;
600599 var carOperations = Owner . Viewer . CarOperationsWindow ;
601600 var trainCarWebpage = Owner . Viewer . TrainCarOperationsWebpage ;
602- var isFormationReversed = Owner . Viewer . IsFormationReversed ;
601+ var trainInfo = Owner . Viewer . PlayerTrain . GetTrainInfo ( ) ;
602+ IsFormationReversed = trainInfo . cabOrientation != 0 ;
603603
604604 CabCameraEnabled = Owner . Viewer . Camera is CabCamera || Owner . Viewer . Camera == Owner . Viewer . ThreeDimCabCamera ;
605605 if ( CarIdClicked && ! CabCameraEnabled && ! trainCarViewer . Visible && ( ! FrontActive || ! BackActive ) )
@@ -636,27 +636,18 @@ 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- }
648639
649640 UserCommand ? controlDiesel = GetPressedKey ( UserCommand . ControlDieselHelper , UserCommand . ControlDieselPlayer , UserCommand . ControlInitializeBrakes ) ;
650641 if ( controlDiesel == UserCommand . ControlDieselHelper || controlDiesel == UserCommand . ControlDieselPlayer || controlDiesel == UserCommand . ControlInitializeBrakes )
651642 {
652643 Layout ( ) ;
653- PowerSupplyStatus = Owner . Viewer . PlayerTrain . Cars [ Owner . Viewer . PlayerTrain . Cars . Count > CarPosition ? CarPosition : CarPosition - 1 ] . GetStatus ( ) ;
644+ PowerSupplyStatus = Owner . Viewer . PlayerTrain . Cars [ Owner . Viewer . PlayerTrain . Cars . Count > CarPosition ? CarPosition : CarPosition - 1 ] . GetStatus ( ) ;
654645 ModifiedSetting = true ;
655646 }
656647
657648 var carsCountChanged = Owner . Viewer . PlayerTrain . Cars . Count != LastPlayerTrainCars ;
658649 if ( PlayerTrain != Owner . Viewer . PlayerTrain || carsCountChanged || ( Owner . Viewer . PlayerLocomotive != null &&
659- LastPlayerLocomotiveFlippedState != isFormationReversed ) )
650+ LastPlayerLocomotiveFlippedState != IsFormationReversed ) )
660651 {
661652 PlayerTrain = Owner . Viewer . PlayerTrain ;
662653 if ( LastPlayerTrainCars != Owner . Viewer . PlayerTrain . Cars . Count )
@@ -690,7 +681,6 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
690681 }
691682 }
692683 }
693-
694684 if ( trainCarViewer . TrainCarOperationsChanged || trainCarViewer . RearBrakeHoseChanged
695685 || trainCarViewer . FrontBrakeHoseChanged || ModifiedSetting || CarIdClicked || carOperations . CarOperationChanged )
696686 {
@@ -703,22 +693,41 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
703693 carOperations . CarOperationChanged = carOperations . Visible && carOperations . CarOperationChanged ;
704694 }
705695
706- if ( ( ! trainCarViewer . Visible || trainCarViewer . UpdateTCOLayout ) && ( CarIdClicked || ( LastPlayerLocomotiveFlippedState != isFormationReversed ) ) )
707- { // Apply the reveral point to layout
708- _ = new FormationReversed ( Owner . Viewer , PlayerTrain ) ;
709- }
710-
711- if ( trainCarViewer . TrainCarOperationsChanged || trainCarViewer . RearBrakeHoseChanged
712- || trainCarViewer . FrontBrakeHoseChanged || ModifiedSetting || CarIdClicked || carOperations . CarOperationChanged )
696+ if ( ( ! trainCarViewer . Visible || trainCarViewer . UpdateTCOLayout ) && ( CarIdClicked || ( LastPlayerLocomotiveFlippedState != IsFormationReversed ) ) )
713697 {
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+ }
721+
722+ // Scroll LabelTop
723+ LabelTop = LabelPositionTop [ SelectedCarPosition ] ;
724+ Owner . Viewer . FrontCamera . IsCameraFront = Owner . Viewer . FrontCamera . AttachedCar != null ;
725+ LastPlayerLocomotiveFlippedState = IsFormationReversed ;
714726 Layout ( ) ;
715727 localScrollLayout ( SelectedCarPosition ) ;
716728 updateLayoutSize ( ) ;
717- ModifiedSetting = false ;
718- // Avoids bug
719- trainCarViewer . TrainCarOperationsChanged = WarningEnabled ;
720- carOperations . CarOperationChanged = carOperations . Visible && carOperations . CarOperationChanged ;
721729 CarIdClicked = false ;
730+ trainCarViewer . UpdateTCOLayout = false ;
722731 }
723732
724733 if ( CarPositionChanged || ( trainCarWebpage != null && CarPosition != trainCarViewer . CarPosition && trainCarWebpage . Connections > 0 ) )
@@ -741,7 +750,7 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
741750 }
742751 }
743752 }
744- public void SetCameraView ( )
753+ private void SetCameraView ( )
745754 {
746755 if ( Owner . Viewer . FrontCamera . AttachedCar != null )
747756 {
@@ -768,57 +777,6 @@ public void updateWarningCarPosition(int carPosition, Texture2D texture, Texture
768777 . Where ( ( UserCommand key ) => UserInput . IsDown ( key ) )
769778 . FirstOrDefault ( ) ;
770779 }
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-
822780 class buttonArrowRight : Image
823781 {
824782 readonly Viewer Viewer ;
0 commit comments