Skip to content

Commit f0010ab

Browse files
- replaced the "public List<bool> RearBrakes = new List<bool>();" and all it's code with save/restore of RearBrakeHoseConnected in AirSinglePipe.cs.
- CarPosition saved/restored for Train Operations Window and Viewer. And for the webpage. - webpage scrolling follows selected traincar
1 parent 9df6e1d commit f0010ab

File tree

7 files changed

+88
-71
lines changed

7 files changed

+88
-71
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/Brakes/MSTS/AirSinglePipe.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ public override void Save(BinaryWriter outf)
340340
outf.Write(FullServPressurePSI);
341341
outf.Write((int)TripleValveState);
342342
outf.Write(FrontBrakeHoseConnected);
343+
outf.Write(RearBrakeHoseConnected);
343344
outf.Write(AngleCockAOpen);
344345
outf.Write(AngleCockAOpenAmount);
345346
outf.Write(AngleCockBOpen);
@@ -368,6 +369,7 @@ public override void Restore(BinaryReader inf)
368369
FullServPressurePSI = inf.ReadSingle();
369370
TripleValveState = (ValveState)inf.ReadInt32();
370371
FrontBrakeHoseConnected = inf.ReadBoolean();
372+
RearBrakeHoseConnected = inf.ReadBoolean();
371373
AngleCockAOpen = inf.ReadBoolean();
372374
AngleCockAOpenAmount = inf.ReadSingle();
373375
AngleCockBOpen = inf.ReadBoolean();

Source/RunActivity/Viewer3D/Cameras.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,17 @@ protected override void OnActivate(bool sameCamera)
10661066
var carPosition = !(TrainCarViewer.CarPosition < trainCars.Count()) ? TrainCarViewer.CarPosition - 1 : TrainCarViewer.CarPosition;
10671067
var isDownCameraOutsideFront = UserInput.IsDown(UserCommand.CameraOutsideFront);
10681068
var isDownCameraOutsideRear = UserInput.IsDown(UserCommand.CameraOutsideRear);
1069-
var isVisibleTrainCarViewerOrWebpage = Viewer.TrainCarOperationsViewerWindow.Visible || (Viewer.TrainCarOperationsWebpage.TrainCarSelected);
1069+
1070+
bool isVisibleTrainCarViewerOrWebpage;
1071+
if (Viewer.TrainCarOperationsWebpage == null)
1072+
{
1073+
// when starting Open Rails by means of a restore Viewer.TrainCarOperationsWebpage not yet available
1074+
isVisibleTrainCarViewerOrWebpage = Viewer.TrainCarOperationsViewerWindow.Visible;
1075+
}
1076+
else
1077+
{
1078+
isVisibleTrainCarViewerOrWebpage = Viewer.TrainCarOperationsViewerWindow.Visible || (Viewer.TrainCarOperationsWebpage.TrainCarSelected);
1079+
}
10701080

10711081
if (attachedCar == null || attachedCar.Train != Viewer.SelectedTrain || carPosition != oldCarPosition || isDownCameraOutsideFront || isDownCameraOutsideRear)
10721082
{

Source/RunActivity/Viewer3D/Popups/TrainCarOperationsViewerWindow.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ protected internal override void Save(BinaryWriter outf)
144144
outf.Write(Location.Y);
145145
outf.Write(Location.Width);
146146
outf.Write(Location.Height);
147+
148+
outf.Write(CarPosition);
147149
}
148150
protected internal override void Restore(BinaryReader inf)
149151
{
@@ -154,6 +156,8 @@ protected internal override void Restore(BinaryReader inf)
154156
LocationRestore.Width = inf.ReadInt32();
155157
LocationRestore.Height = inf.ReadInt32();
156158

159+
CarPosition = inf.ReadInt32();
160+
157161
// Display window
158162
SizeTo(LocationRestore.Width, LocationRestore.Height);
159163
MoveTo(LocationRestore.X, LocationRestore.Y);

Source/RunActivity/Viewer3D/Popups/TrainCarOperationsWindow.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ public struct ListLabel
133133
}
134134
public List<ListLabel> Labels = new List<ListLabel>();
135135

136-
public List<bool> RearBrakes = new List<bool>();
137-
public bool RefreshRestoredData = false;
138-
139136
Train PlayerTrain;
140137
int LastPlayerTrainCars;
141138
bool LastPlayerLocomotiveFlippedState;
@@ -157,13 +154,7 @@ protected internal override void Save(BinaryWriter outf)
157154
outf.Write(Location.Width);
158155
outf.Write(Location.Height);
159156

160-
// Saves brake hose state
161-
var carsCount = PlayerTrain.Cars.Count();
162-
outf.Write(carsCount);
163-
foreach (var data in RearBrakes)
164-
{
165-
outf.Write(data);
166-
}
157+
outf.Write(SelectedCarPosition);
167158
}
168159
protected internal override void Restore(BinaryReader inf)
169160
{
@@ -174,13 +165,7 @@ protected internal override void Restore(BinaryReader inf)
174165
LocationRestore.Width = inf.ReadInt32();
175166
LocationRestore.Height = inf.ReadInt32();
176167

177-
// Restores brake hose state
178-
var carsCount = inf.ReadInt32();
179-
for (int i = 0; i < carsCount; i++)
180-
{
181-
RearBrakes.Add(inf.ReadBoolean());
182-
}
183-
RefreshRestoredData = true;
168+
SelectedCarPosition = inf.ReadInt32();
184169

185170
// Display window
186171
SizeTo(LocationRestore.Width, LocationRestore.Height);
@@ -334,19 +319,6 @@ void AddSpace()
334319
line.AddSpace(textHeight / 2, line.RemainingHeight);
335320
}
336321

337-
if (RefreshRestoredData)
338-
{ // Restores brake hose
339-
RefreshRestoredData = false;
340-
for (int i = 0; i < PlayerTrain.Cars.Count; i++)
341-
{
342-
(PlayerTrain.Cars[i] as MSTSWagon).BrakeSystem.RearBrakeHoseConnected = RearBrakes[i];
343-
}
344-
}
345-
if (RearBrakes.Count == 0)
346-
{ // Init brake hose
347-
RearBrakes = Enumerable.Repeat(true, PlayerTrain.Cars.Count).ToList();
348-
}
349-
350322
if (LabelPositionTop.Count == 0)
351323
{
352324
var n = scrollbox.Position.Y;// first row
@@ -874,7 +846,6 @@ public buttonRearBrakeHose(int x, int y, int size, Viewer viewer, TrainCar car,
874846
{
875847
trainCarOperations.updateWarningCarPosition(carPosition, Texture, BrakeHoseDis);
876848
}
877-
Viewer.TrainCarOperationsWindow.RearBrakes[carPosition] = (viewer.PlayerTrain.Cars[carPosition] as MSTSWagon).BrakeSystem.RearBrakeHoseConnected;
878849
}
879850
}
880851
class buttonFrontAngleCock : Image

Source/RunActivity/Viewer3D/Processes/GameStateRunActivity.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ public static void Save()
420420
if (MPManager.IsMultiPlayer() && MPManager.IsServer())
421421
MPManager.OnlineTrains.Save (outf);
422422

423+
Viewer.TrainCarOperationsWebpage.Save(outf);
424+
423425
SaveEvaluation(outf);
424426

425427
// Write out position within file so we can check when restoring.
@@ -500,6 +502,8 @@ void Resume(UserSettings settings, string[] args)
500502
if (MPManager.IsMultiPlayer() && MPManager.IsServer())
501503
MPManager.OnlineTrains.Restore(inf);
502504

505+
WebServices.TrainCarOperationsWebpage.Restore(inf);
506+
503507
ResumeEvaluation(inf);
504508

505509
var restorePosition = inf.BaseStream.Position;

Source/RunActivity/Viewer3D/WebServices/TrainCarOperationsWebpage.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using Orts.Common;
3131
using Orts.Simulation.RollingStocks.SubSystems.Brakes.MSTS;
3232
using ORTS.Scripting.Api;
33+
using System.IO;
3334

3435
namespace Orts.Viewer3D.WebServices
3536
{
@@ -74,7 +75,12 @@ public OperationsStatus(int amountOfCars)
7475

7576
private readonly Viewer Viewer;
7677

77-
public bool TrainCarSelected = false;
78+
// static fields because during the restore this objcet not yet created
79+
public static bool TrainCarFromRestore;
80+
public static bool TrainCarSelectedFromRestore;
81+
public static int TrainCarSelectedPositionFromRestore;
82+
83+
public bool TrainCarSelected;
7884
public int TrainCarSelectedPosition;
7985

8086
public int Connections = 0;
@@ -204,6 +210,20 @@ public void handleReceiveAndSend()
204210
}
205211
}
206212

213+
public void Save(BinaryWriter outf)
214+
{
215+
outf.Write(TrainCarSelected);
216+
outf.Write(TrainCarSelectedPosition);
217+
}
218+
219+
public static void Restore(BinaryReader inf)
220+
{
221+
TrainCarSelectedFromRestore = inf.ReadBoolean();
222+
TrainCarSelectedPositionFromRestore = inf.ReadInt32();
223+
224+
TrainCarFromRestore = true;
225+
}
226+
207227
private void handleReceive()
208228
{
209229
if (MessageReceived)
@@ -428,9 +448,16 @@ private void fillStatusArrowLeft(int carPosition)
428448
{
429449
string filename;
430450

431-
if (Viewer.TrainCarOperationsWindow.CarIdClicked) {
432-
// on the train operations window wagon has been clicked
433-
// wagon on webpage gets selected also
451+
if (TrainCarFromRestore)
452+
{
453+
TrainCarSelected = TrainCarSelectedFromRestore;
454+
TrainCarSelectedPosition = TrainCarSelectedPositionFromRestore;
455+
456+
TrainCarFromRestore = false;
457+
}
458+
459+
if (Viewer.TrainCarOperationsWindow.Visible) {
460+
// take the carposition of the Traincar Operations Window
434461
TrainCarSelected = true;
435462
TrainCarSelectedPosition = Viewer.TrainCarOperationsWindow.SelectedCarPosition;
436463
}

Source/RunActivity/Viewer3D/WebServices/Web/TrainCarOperations/index.js

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
// socket functions
2323
//
2424

25-
let initStillToBeDone = true;
25+
// scrolling
26+
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
27+
const possibleRows = Math.floor((windowHeight - 35) / 36);
28+
let rowToScrollToPrevious = 0;
2629

2730
function createSocket() {
2831

@@ -84,16 +87,17 @@ function handleMessage(json) {
8487
console.log("handleMessage: ", json);
8588

8689
if (json.Type == "init") {
87-
var html = document.body;
90+
const html = document.body;
8891
while (html.firstChild) {
8992
html.removeChild(html.firstChild);
9093
}
91-
var elemDiv = document.createElement('div');
94+
const elemDiv = document.createElement('div');
9295
elemDiv.id = "overlay";
9396
document.body.appendChild(elemDiv);
9497

9598
const tbl = document.createElement("table");
9699
const th = document.createElement("th");
100+
th.setAttribute("id", "th");
97101
const thText = document.createTextNode("Train Car Operations");
98102
th.appendChild(thText);
99103
th.colSpan = json.Columns + 1;
@@ -125,28 +129,44 @@ function handleMessage(json) {
125129
}
126130

127131
for (let i = 0; i < json.Operations.length; i++) {
128-
let row = json.Operations[i].Row;
129-
let column = json.Operations[i].Column;
130-
let id = "button:" + row + ":" + column;
131-
let button = document.getElementById(id);
132+
const row = json.Operations[i].Row;
133+
const column = json.Operations[i].Column;
134+
const id = "button:" + row + ":" + column;
135+
const button = document.getElementById(id);
132136
button.innerHTML = "<img src='" + json.Operations[i].Filename + "' />";
137+
133138
if (json.Operations[i].Filename.includes("Arrow")) {
134-
if (!isInView(button)) {
135-
console.log("scrollTo");
136-
button.scrollIntoView({ behavior: "smooth" });
139+
const rowToScrollTo = Math.floor(row - (possibleRows / 2));
140+
if (rowToScrollTo < 0) {
141+
const thToScrollTo = document.getElementById("th");
142+
thToScrollTo.scrollIntoView({ behavior: "smooth" });
143+
rowToScrollToPrevious = 0;
144+
} else {
145+
const tst1Boven = rowToScrollTo < rowToScrollToPrevious - 8;
146+
console.log("tst1Boven:" + tst1Boven);
147+
const tst1Onder = rowToScrollTo > rowToScrollToPrevious + (possibleRows / 2) - 5;
148+
console.log("tst1Onder:" + tst1Onder);
149+
if ((rowToScrollTo < rowToScrollToPrevious - 8) ||
150+
(rowToScrollTo > rowToScrollToPrevious + (possibleRows / 2) - 5)) {
151+
console.log("ScrollTo");
152+
const idToScrollTo = "button:" + rowToScrollTo + ":" + 0;
153+
const buttonToScrollTo = document.getElementById(idToScrollTo);
154+
buttonToScrollTo.scrollIntoView({ behavior: "smooth" });
155+
rowToScrollToPrevious = rowToScrollTo;
156+
}
137157
}
138158
}
139159
button.disabled = !json.Operations[i].Enabled;
140160
button.style.cursor = json.Operations[i].Enabled ? "pointer" : "";
141161
}
142162

143163
for (let i = 0; i < json.CarIdColor.length; i++) {
144-
let id = "label:" + i;
145-
let label = document.getElementById(id);
164+
const id = "label:" + i;
165+
const label = document.getElementById(id);
146166
label.style.color = json.CarIdColor[i];
147167
}
148168

149-
// turn off the dark overlay if it's still on
169+
// turn off the dark overlay if it's still on after a reconnect
150170
document.getElementById("overlay").style.display = "none";
151171
}
152172

@@ -163,28 +183,7 @@ function sleep(time) {
163183
return new Promise((resolve) => setTimeout(resolve, time));
164184
}
165185

166-
function isInView(el) {
167-
168-
var rect = el.getBoundingClientRect(),
169-
vWidth = window.innerWidth || document.documentElement.clientWidth,
170-
vHeight = window.innerHeight || document.documentElement.clientHeight,
171-
efp = function (x, y) { return document.elementFromPoint(x, y) };
172-
173-
// Return false if it's not in the viewport
174-
if (rect.right < 0 || rect.bottom < 0
175-
|| rect.left > vWidth || rect.top > vHeight)
176-
return false;
177-
178-
// Return true if any of its four corners are visible
179-
return (
180-
el.contains(efp(rect.left, rect.top))
181-
|| el.contains(efp(rect.right, rect.top))
182-
|| el.contains(efp(rect.right, rect.bottom))
183-
|| el.contains(efp(rect.left, rect.bottom))
184-
);
185-
}
186-
187186
//
188187
// main
189188
//
190-
let websocket = createSocket();
189+
const websocket = createSocket();

0 commit comments

Comments
 (0)