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,46 +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- if ( isVisibleTrainCarViewerOrWebpage )
1094+ if ( IsVisibleTrainCarViewerOrWebpage )
10871095 {
10881096 // Update the camera view
10891097 oldCarPosition = oldCarPosition == 0 && carPosition == 0 ? - 1 : oldCarPosition ;
10901098 }
10911099
1092- if ( attachedCar != null && ! isVisibleTrainCarViewerOrWebpage )
1100+ if ( attachedCar != null && ! IsVisibleTrainCarViewerOrWebpage )
10931101 { // Reset behaviour of camera 2 and camera 3, after closing F9-window and F9-web.
1094- var attachedCarPosition = attachedCar . Train . Cars . TakeWhile ( x => x . CarID != attachedCar . CarID ) . Count ( ) ;
1095- if ( Front && attachedCarPosition == carPosition )
1102+ var attachedCarPosition = Front ? Viewer . CameraOutsideFrontPosition : Viewer . CameraOutsideRearPosition ;
1103+
1104+ Viewer . FirstLoop = false ;
1105+ if ( Front )
10961106 {
1097- attachedCar = trainCars . First ( ) ;
1107+ SetCameraCar ( trainCars [ Viewer . CameraOutsideFrontPosition ] ) ;
1108+ Viewer . CameraFrontUpdated = true ;
10981109 }
1099- if ( ! Front && attachedCarPosition == carPosition )
1110+ else
11001111 {
1101- attachedCar = trainCars . Last ( ) ;
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 ;
11021128 }
1129+ Viewer . IsCameraPositionUpdated = Viewer . CameraFrontUpdated && Viewer . CameraRearUpdated ;
11031130 }
11041131
11051132 if ( attachedCar == null || attachedCar . Train != Viewer . SelectedTrain || carPosition != oldCarPosition )
11061133 {
11071134 if ( Front )
11081135 {
1109- if ( ! isVisibleTrainCarViewerOrWebpage && isDownCameraOutsideFront )
1136+ if ( ! IsVisibleTrainCarViewerOrWebpage && isDownCameraOutsideFront )
11101137 {
1111- SetCameraCar ( GetCameraCars ( ) . First ( ) ) ;
1138+ if ( Viewer . CameraOutsideFrontPosition == 0 ) SetCameraCar ( GetCameraCars ( ) . First ( ) ) ;
11121139 oldCarPosition = 0 ;
11131140 }
1114- else if ( isVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
1141+ else if ( IsVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
11151142 {
11161143 if ( carPosition < trainCars . Count )
11171144 {
@@ -1130,12 +1157,12 @@ protected override void OnActivate(bool sameCamera)
11301157 }
11311158 else
11321159 {
1133- if ( ! isVisibleTrainCarViewerOrWebpage && isDownCameraOutsideRear )
1160+ if ( ! IsVisibleTrainCarViewerOrWebpage && isDownCameraOutsideRear )
11341161 {
1135- SetCameraCar ( GetCameraCars ( ) . Last ( ) ) ;
1162+ if ( Viewer . CameraOutsideRearPosition == 0 ) SetCameraCar ( GetCameraCars ( ) . Last ( ) ) ;
11361163 oldCarPosition = 0 ;
11371164 }
1138- else if ( carPosition < trainCars . Count && isVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
1165+ else if ( carPosition < trainCars . Count && IsVisibleTrainCarViewerOrWebpage && carPosition >= 0 )
11391166 {
11401167 SetCameraCar ( trainCars [ carPosition ] ) ;
11411168 oldCarPosition = carPosition ;
@@ -1151,6 +1178,24 @@ protected override void OnActivate(bool sameCamera)
11511178 BrowseDistance = attachedCar . CarLengthM * 0.5f ;
11521179 }
11531180 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 ;
11541199 }
11551200
11561201 protected override bool IsCameraFlipped ( )
@@ -1230,6 +1275,16 @@ public override void HandleUserInput(ElapsedTime elapsedTime)
12301275 new ToggleBrowseBackwardsCommand ( Viewer . Log ) ;
12311276 if ( UserInput . IsPressed ( UserCommand . CameraBrowseForwards ) )
12321277 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+ }
12331288 }
12341289
12351290 public override void Update ( ElapsedTime elapsedTime )
0 commit comments