Skip to content

Commit 788f876

Browse files
author
Chris Jakeman
committed
refactoring
1 parent ad03e50 commit 788f876

File tree

1 file changed

+91
-84
lines changed

1 file changed

+91
-84
lines changed

Source/RunActivity/Viewer3D/Debugging/TimetableWindow.cs

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ public void PopulateItemLists()
276276
Trace.TraceWarning("Platform '{0}' is incomplete as the two ends do not match. It will not show in full in the Timetable Tab of the Map Window", p.Name);
277277
}
278278

279-
280279
/// <summary>
281280
/// Returns the mid-point between two locations
282281
/// </summary>
@@ -589,7 +588,7 @@ private void DrawTrains(Graphics g, PointF scaledA, PointF scaledB)
589588
var margin2 = 5000 * F.xScale;
590589

591590
//variable for drawing train path
592-
var mDist = 5000f; var pDist = 50; //segment length when draw path
591+
var mDist = 5000f; var pDist = 50; //segment length when drawing path
593592

594593
F.selectedTrainList.Clear();
595594

@@ -600,32 +599,32 @@ private void DrawTrains(Graphics g, PointF scaledA, PointF scaledB)
600599
F.selectedTrainList.Add(F.simulator.PlayerLocomotive.Train as Orts.Simulation.AIs.AITrain);
601600

602601
// and all the other trains
603-
foreach (var t in F.simulator.AI.AITrains)
604-
F.selectedTrainList.Add(t);
602+
foreach (var train in F.simulator.AI.AITrains)
603+
F.selectedTrainList.Add(train);
605604
}
606605
else
607606
{
608-
foreach (var t in F.simulator.Trains)
609-
F.selectedTrainList.Add(t);
607+
foreach (var train in F.simulator.Trains)
608+
F.selectedTrainList.Add(train);
610609
}
611610

612-
foreach (var t in F.selectedTrainList)
611+
foreach (var train in F.selectedTrainList)
613612
{
614613
string trainName;
615614
WorldPosition worldPos;
616615
TrainCar locoCar = null;
617-
if (t.LeadLocomotive != null)
616+
if (train.LeadLocomotive != null)
618617
{
619-
trainName = t.GetTrainName(t.LeadLocomotive.CarID);
620-
locoCar = t.LeadLocomotive;
618+
trainName = train.GetTrainName(train.LeadLocomotive.CarID);
619+
locoCar = train.LeadLocomotive;
621620
}
622-
else if (t.Cars != null && t.Cars.Count > 0)
621+
else if (train.Cars != null && train.Cars.Count > 0)
623622
{
624-
trainName = t.GetTrainName(t.Cars[0].CarID);
625-
if (t.TrainType == Train.TRAINTYPE.AI)
626-
trainName = t.Number.ToString() + ":" + t.Name;
623+
trainName = train.GetTrainName(train.Cars[0].CarID);
624+
if (train.TrainType == Train.TRAINTYPE.AI)
625+
trainName = train.Number.ToString() + ":" + train.Name;
627626

628-
locoCar = t.Cars.Where(r => r is MSTSLocomotive).FirstOrDefault();
627+
locoCar = train.Cars.Where(r => r is MSTSLocomotive).FirstOrDefault();
629628

630629
// Skip trains with no loco
631630
if (locoCar == null)
@@ -635,7 +634,7 @@ private void DrawTrains(Graphics g, PointF scaledA, PointF scaledB)
635634
continue;
636635

637636
// Draw the path, then each car of the train, then maybe the name
638-
var loc = t.FrontTDBTraveller.WorldLocation;
637+
var loc = train.FrontTDBTraveller.WorldLocation;
639638
float x = (loc.TileX * 2048 + loc.Location.X - F.subX) * F.xScale;
640639
float y = F.pbCanvas.Height - (loc.TileZ * 2048 + loc.Location.Z - F.subY) * F.yScale;
641640

@@ -644,95 +643,103 @@ private void DrawTrains(Graphics g, PointF scaledA, PointF scaledB)
644643
|| y < -margin2)
645644
continue;
646645

647-
F.DrawTrainPath(t, F.subX, F.subY, F.pathPen, g, scaledA, scaledB, pDist, mDist);
646+
F.DrawTrainPath(train, F.subX, F.subY, F.pathPen, g, scaledA, scaledB, pDist, mDist);
648647

649648
// If zoomed out, so train occupies less than 2 * minTrainPx pixels, then
650649
// draw the train as 2 squares of combined length minTrainPx.
651650
const int minTrainPx = 24;
652651

653-
// pen | train for a good presentation
652+
// pen | train | Values for a good presentation
654653
// 1 10
655654
// 2 12
656655
// 3 14
657656
// 4 16
658657
F.trainPen.Width = F.grayPen.Width * 6;
659658

660659
var minTrainLengthM = minTrainPx / F.xScale; // Calculate length equivalent to a set number of pixels
661-
bool drawEveryCar = IsDrawEveryCar(t, minTrainLengthM);
660+
bool drawEveryCar = IsDrawEveryCar(train, minTrainLengthM);
661+
662+
foreach (var car in train.Cars)
663+
DrawCar(g, train, car, locoCar, margin, minTrainPx, drawEveryCar);
662664

665+
worldPos = locoCar.WorldPosition;
663666
var scaledTrain = new PointF();
664-
foreach (var car in t.Cars)
665-
{
666-
if (drawEveryCar == false)
667-
// Skip the intermediate cars
668-
if (car != t.Cars.First() && car != t.Cars.Last())
669-
continue;
670-
671-
var t1 = new Traveller(t.RearTDBTraveller);
672-
worldPos = car.WorldPosition;
673-
var dist = t1.DistanceTo(worldPos.WorldLocation.TileX, worldPos.WorldLocation.TileZ, worldPos.WorldLocation.Location.X, worldPos.WorldLocation.Location.Y, worldPos.WorldLocation.Location.Z);
674-
if (dist > -1)
667+
scaledTrain.X = (worldPos.TileX * 2048 - F.subX + worldPos.Location.X) * F.xScale;
668+
scaledTrain.Y = -25 + F.pbCanvas.Height - (worldPos.TileZ * 2048 - F.subY + worldPos.Location.Z) * F.yScale;
669+
if (F.cbShowTrainLabels.Checked)
670+
DrawTrainLabels(g, train, trainName, locoCar, scaledTrain);
671+
}
672+
}
673+
674+
private void DrawCar(Graphics g, Train train, TrainCar car, TrainCar locoCar, float margin, int minTrainPx, bool drawEveryCar)
675+
{
676+
if (drawEveryCar == false)
677+
// Skip the intermediate cars
678+
if (car != train.Cars.First() && car != train.Cars.Last())
679+
return;
680+
681+
var t = new Traveller(train.RearTDBTraveller);
682+
var worldPos = car.WorldPosition;
683+
var dist = t.DistanceTo(worldPos.WorldLocation.TileX, worldPos.WorldLocation.TileZ, worldPos.WorldLocation.Location.X, worldPos.WorldLocation.Location.Y, worldPos.WorldLocation.Location.Z);
684+
if (dist > -1)
685+
{
686+
var scaledTrain = new PointF();
687+
float x;
688+
float y;
689+
if (drawEveryCar)
690+
{
691+
t.Move(dist + car.CarLengthM / 2); // Move along from centre of car to front of car
692+
x = (t.TileX * 2048 + t.Location.X - F.subX) * F.xScale;
693+
y = F.pbCanvas.Height - (t.TileZ * 2048 + t.Location.Z - F.subY) * F.yScale;
694+
695+
// If car out of view then skip it.
696+
if (x < -margin || y < -margin)
697+
return;
698+
699+
t.Move(-car.CarLengthM + (1 / F.xScale)); // Move from front of car to rear less 1 pixel to create a visible gap
700+
scaledTrain.X = x; scaledTrain.Y = y;
701+
}
702+
else // Draw the train as 2 boxes of fixed size
703+
{
704+
F.trainPen.Width = minTrainPx / 2;
705+
if (car == train.Cars.First())
675706
{
676-
if (drawEveryCar)
677-
{
678-
t1.Move(dist + car.CarLengthM / 2); // Move along from centre of car to front of car
679-
x = (t1.TileX * 2048 + t1.Location.X - F.subX) * F.xScale;
680-
y = F.pbCanvas.Height - (t1.TileZ * 2048 + t1.Location.Z - F.subY) * F.yScale;
681-
682-
// If car out of view then skip it.
683-
if (x < -margin || y < -margin)
684-
continue;
685-
686-
scaledTrain.X = x; scaledTrain.Y = y;
687-
t1.Move(-car.CarLengthM + (1 / F.xScale)); // Move from front of car to rear less 1 pixel to create a visible gap
688-
}
689-
else // Draw the train as 2 boxes of fixed size
690-
{
691-
F.trainPen.Width = minTrainPx / 2;
692-
if (car == t.Cars.First())
693-
{
694-
// Draw first half a train back from the front of the first car as abox
695-
t1.Move(dist + car.CarLengthM / 2);
696-
x = (t1.TileX * 2048 + t1.Location.X - F.subX) * F.xScale;
697-
y = F.pbCanvas.Height - (t1.TileZ * 2048 + t1.Location.Z - F.subY) * F.yScale;
698-
if (x < -margin || y < -margin)
699-
continue;
700-
t1.Move(-(minTrainPx - 2) / F.xScale / 2); // Move from front of car to rear less 1 pixel to create a visible gap
701-
}
702-
else // car == t.Cars.Last()
703-
{
704-
// Draw half a train back from the rear of the first box
705-
worldPos = t.Cars.First().WorldPosition;
706-
dist = t1.DistanceTo(worldPos.WorldLocation.TileX, worldPos.WorldLocation.TileZ, worldPos.WorldLocation.Location.X, worldPos.WorldLocation.Location.Y, worldPos.WorldLocation.Location.Z);
707-
t1.Move(dist + t.Cars.First().CarLengthM / 2 - minTrainPx / F.xScale / 2);
708-
x = (t1.TileX * 2048 + t1.Location.X - F.subX) * F.xScale;
709-
y = F.pbCanvas.Height - (t1.TileZ * 2048 + t1.Location.Z - F.subY) * F.yScale;
710-
if (x < -margin || y < -margin)
711-
continue;
712-
t1.Move(-minTrainPx / F.xScale / 2);
713-
}
714-
scaledTrain.X = x; scaledTrain.Y = y;
715-
}
716-
x = (t1.TileX * 2048 + t1.Location.X - F.subX) * F.xScale;
717-
y = F.pbCanvas.Height - (t1.TileZ * 2048 + t1.Location.Z - F.subY) * F.yScale;
707+
// Draw first half a train back from the front of the first car as abox
708+
t.Move(dist + car.CarLengthM / 2);
709+
x = (t.TileX * 2048 + t.Location.X - F.subX) * F.xScale;
710+
y = F.pbCanvas.Height - (t.TileZ * 2048 + t.Location.Z - F.subY) * F.yScale;
718711

719712
// If car out of view then skip it.
720713
if (x < -margin || y < -margin)
721-
continue;
714+
return;
722715

723-
scaledA.X = x; scaledA.Y = y;
724-
725-
SetTrainColor(t, locoCar, car);
726-
g.DrawLine(F.trainPen, scaledA, scaledTrain);
716+
t.Move(-(minTrainPx - 2) / F.xScale / 2); // Move from front of car to rear less 1 pixel to create a visible gap
727717
}
718+
else // car == t.Cars.Last()
719+
{
720+
// Draw half a train back from the rear of the first box
721+
worldPos = train.Cars.First().WorldPosition;
722+
dist = t.DistanceTo(worldPos.WorldLocation.TileX, worldPos.WorldLocation.TileZ, worldPos.WorldLocation.Location.X, worldPos.WorldLocation.Location.Y, worldPos.WorldLocation.Location.Z);
723+
t.Move(dist + train.Cars.First().CarLengthM / 2 - minTrainPx / F.xScale / 2);
724+
x = (t.TileX * 2048 + t.Location.X - F.subX) * F.xScale;
725+
y = F.pbCanvas.Height - (t.TileZ * 2048 + t.Location.Z - F.subY) * F.yScale;
726+
if (x < -margin || y < -margin)
727+
return;
728+
t.Move(-minTrainPx / F.xScale / 2);
729+
}
730+
scaledTrain.X = x; scaledTrain.Y = y;
728731
}
729-
worldPos = locoCar.WorldPosition;
730-
scaledTrain.X = (worldPos.TileX * 2048 - F.subX + worldPos.Location.X) * F.xScale;
731-
scaledTrain.Y = -25 + F.pbCanvas.Height - (worldPos.TileZ * 2048 - F.subY + worldPos.Location.Z) * F.yScale;
732-
if (F.cbShowTrainLabels.Checked)
733-
DrawTrainLabels(g, t, trainName, locoCar, scaledTrain);
734-
}
735-
}
732+
x = (t.TileX * 2048 + t.Location.X - F.subX) * F.xScale;
733+
y = F.pbCanvas.Height - (t.TileZ * 2048 + t.Location.Z - F.subY) * F.yScale;
734+
735+
// If car out of view then skip it.
736+
if (x < -margin || y < -margin)
737+
return;
738+
739+
SetTrainColor(train, locoCar, car);
740+
g.DrawLine(F.trainPen, new PointF(x, y), scaledTrain);
741+
}
742+
}
736743

737744
private void SetTrainColor(Train t, TrainCar locoCar, TrainCar car)
738745
{

0 commit comments

Comments
 (0)