2929using Orts . Simulation . Physics ;
3030using Orts . Simulation . RollingStocks ;
3131using Orts . Simulation . Signalling ;
32+ using Orts . Viewer3D ;
3233using Orts . Viewer3D . Popups ;
3334using ORTS . Common ;
3435using ORTS . Common . Input ;
@@ -992,6 +993,14 @@ public enum AttachedTo { Front, Rear }
992993 protected float HighWagonOffsetLimit ;
993994 public int oldCarPosition ;
994995 public bool IsCameraFront ;
996+ public bool IsVisibleTrainCarViewerOrWebpage ;
997+ public bool IsVisibleTrainCarWebPage ;
998+ public bool IsDownCameraOutsideFront ;
999+ public bool IsDownCameraOutsideRear ;
1000+ public UserCommand ? CameraCommand ;
1001+ private static UserCommand ? GetPressedKey ( params UserCommand [ ] keysToTest ) => keysToTest
1002+ . Where ( ( UserCommand key ) => UserInput . IsDown ( key ) )
1003+ . FirstOrDefault ( ) ;
9951004 public override bool IsUnderground
9961005 {
9971006 get
@@ -1072,30 +1081,64 @@ protected override void OnActivate(bool sameCamera)
10721081 var isDownCameraOutsideFront = UserInput . IsDown ( UserCommand . CameraOutsideFront ) ;
10731082 var isDownCameraOutsideRear = UserInput . IsDown ( UserCommand . CameraOutsideRear ) ;
10741083
1075- bool isVisibleTrainCarViewerOrWebpage ;
10761084 if ( Viewer . TrainCarOperationsWebpage == null )
10771085 {
10781086 // when starting Open Rails by means of a restore Viewer.TrainCarOperationsWebpage not yet available
1079- isVisibleTrainCarViewerOrWebpage = Viewer . TrainCarOperationsViewerWindow . Visible ;
1087+ IsVisibleTrainCarViewerOrWebpage = Viewer . TrainCarOperationsViewerWindow . Visible ;
10801088 }
10811089 else
10821090 {
1083- isVisibleTrainCarViewerOrWebpage = ( Viewer . TrainCarOperationsWindow . Visible && ! Viewer . TrainCarOperationsViewerWindow . Visible ) || Viewer . TrainCarOperationsViewerWindow . Visible || ( Viewer . TrainCarOperationsWebpage ? . Connections > 0 && Viewer . TrainCarOperationsWebpage . TrainCarSelected ) ;
1091+ IsVisibleTrainCarViewerOrWebpage = ( Viewer . TrainCarOperationsWindow . Visible && ! Viewer . TrainCarOperationsViewerWindow . Visible ) || Viewer . TrainCarOperationsViewerWindow . Visible || ( Viewer . TrainCarOperationsWebpage ? . Connections > 0 && Viewer . TrainCarOperationsWebpage . TrainCarSelected ) ;
10841092 }
10851093
1086- // Update the camera view
1087- oldCarPosition = oldCarPosition == 0 && carPosition == 0 ? - 1 : oldCarPosition ;
1094+ if ( IsVisibleTrainCarViewerOrWebpage )
1095+ {
1096+ // Update the camera view
1097+ oldCarPosition = oldCarPosition == 0 && carPosition == 0 ? - 1 : oldCarPosition ;
1098+ }
1099+
1100+ if ( attachedCar != null && ! IsVisibleTrainCarViewerOrWebpage )
1101+ { // Reset behaviour of camera 2 and camera 3, after closing F9-window and F9-web.
1102+ var attachedCarPosition = Front ? Viewer . CameraOutsideFrontPosition : Viewer . CameraOutsideRearPosition ;
1103+
1104+ Viewer . FirstLoop = false ;
1105+ if ( Front )
1106+ {
1107+ SetCameraCar ( trainCars [ Viewer . CameraOutsideFrontPosition ] ) ;
1108+ Viewer . CameraFrontUpdated = true ;
1109+ }
1110+ else
1111+ {
1112+ if ( Viewer . CameraOutsideRearPosition == 0 )
1113+ {
1114+ SetCameraCar ( GetCameraCars ( ) . Last ( ) ) ;
1115+ }
1116+ else
1117+ {
1118+ if ( Viewer . CameraOutsideRearPosition < trainCars . Count )
1119+ {
1120+ SetCameraCar ( trainCars [ Viewer . CameraOutsideRearPosition ] ) ;
1121+ }
1122+ else
1123+ {
1124+ SetCameraCar ( trainCars [ trainCars . Count - 1 ] ) ;
1125+ }
1126+ }
1127+ Viewer . CameraRearUpdated = true ;
1128+ }
1129+ Viewer . IsCameraPositionUpdated = Viewer . CameraFrontUpdated && Viewer . CameraRearUpdated ;
1130+ }
10881131
10891132 if ( attachedCar == null || attachedCar . Train != Viewer . SelectedTrain || carPosition != oldCarPosition )
10901133 {
10911134 if ( Front )
10921135 {
1093- if ( ! isVisibleTrainCarViewerOrWebpage && isDownCameraOutsideFront )
1136+ if ( ! IsVisibleTrainCarViewerOrWebpage && isDownCameraOutsideFront )
10941137 {
1095- SetCameraCar ( GetCameraCars ( ) . First ( ) ) ;
1138+ if ( Viewer . CameraOutsideFrontPosition == 0 ) SetCameraCar ( GetCameraCars ( ) . First ( ) ) ;
10961139 oldCarPosition = 0 ;
10971140 }
1098- else if ( isVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
1141+ else if ( IsVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
10991142 {
11001143 if ( carPosition < trainCars . Count )
11011144 {
@@ -1114,12 +1157,12 @@ protected override void OnActivate(bool sameCamera)
11141157 }
11151158 else
11161159 {
1117- if ( ! isVisibleTrainCarViewerOrWebpage && isDownCameraOutsideRear )
1160+ if ( ! IsVisibleTrainCarViewerOrWebpage && isDownCameraOutsideRear )
11181161 {
1119- SetCameraCar ( GetCameraCars ( ) . Last ( ) ) ;
1162+ if ( Viewer . CameraOutsideRearPosition == 0 ) SetCameraCar ( GetCameraCars ( ) . Last ( ) ) ;
11201163 oldCarPosition = 0 ;
11211164 }
1122- else if ( carPosition < trainCars . Count && isVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
1165+ else if ( carPosition < trainCars . Count && IsVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
11231166 {
11241167 SetCameraCar ( trainCars [ carPosition ] ) ;
11251168 oldCarPosition = carPosition ;
@@ -1135,6 +1178,24 @@ protected override void OnActivate(bool sameCamera)
11351178 BrowseDistance = attachedCar . CarLengthM * 0.5f ;
11361179 }
11371180 base . OnActivate ( sameCamera ) ;
1181+ CameraOutsidePosition ( ) ;
1182+ }
1183+
1184+ public void CameraOutsidePosition ( )
1185+ {
1186+ if ( ! IsVisibleTrainCarViewerOrWebpage )
1187+ {
1188+ var attachedCarIdPos = attachedCar . Train . Cars . TakeWhile ( x => x . CarID != attachedCar . CarID ) . Count ( ) ;
1189+ if ( Front )
1190+ {
1191+ Viewer . CameraOutsideFrontPosition = attachedCarIdPos ;
1192+ }
1193+ else if ( ! Front )
1194+ {
1195+ Viewer . CameraOutsideRearPosition = attachedCarIdPos ;
1196+ }
1197+ }
1198+ Viewer . IsCameraPositionUpdated = ! IsVisibleTrainCarViewerOrWebpage ;
11381199 }
11391200
11401201 protected override bool IsCameraFlipped ( )
@@ -1214,6 +1275,16 @@ public override void HandleUserInput(ElapsedTime elapsedTime)
12141275 new ToggleBrowseBackwardsCommand ( Viewer . Log ) ;
12151276 if ( UserInput . IsPressed ( UserCommand . CameraBrowseForwards ) )
12161277 new ToggleBrowseForwardsCommand ( Viewer . Log ) ;
1278+
1279+ UserCommand ? CameraCommand = GetPressedKey ( UserCommand . CameraCarPrevious , UserCommand . CameraCarNext ,
1280+ UserCommand . CameraCarFirst , UserCommand . CameraCarLast ) ;
1281+
1282+ if ( CameraCommand == UserCommand . CameraCarPrevious || CameraCommand == UserCommand . CameraCarNext || CameraCommand == UserCommand . CameraCarFirst
1283+ || CameraCommand == UserCommand . CameraCarLast )
1284+ { // updates camera out side car position
1285+ CameraOutsidePosition ( ) ;
1286+ Viewer . IsDownCameraChanged = IsDownCameraOutsideFront = IsDownCameraOutsideRear = false ;
1287+ }
12171288 }
12181289
12191290 public override void Update ( ElapsedTime elapsedTime )
0 commit comments