Skip to content

Commit f9f6804

Browse files
committed
Car-label-improvements
1 parent a055bca commit f9f6804

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

Source/RunActivity/Viewer3D/Materials.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,7 @@ public class Label3DMaterial : SpriteBatchMaterial
12721272
{
12731273
public readonly Texture2D Texture;
12741274
public readonly WindowTextFont Font;
1275+
public readonly WindowTextFont BigFont;
12751276

12761277
readonly List<Rectangle> TextBoxes = new List<Rectangle>();
12771278

@@ -1281,6 +1282,7 @@ public Label3DMaterial(Viewer viewer)
12811282
Texture = new Texture2D(SpriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
12821283
Texture.SetData(new[] { Color.White });
12831284
Font = Viewer.WindowManager.TextManager.GetScaled("Arial", 12, System.Drawing.FontStyle.Bold, 1);
1285+
BigFont = Viewer.WindowManager.TextManager.GetScaled("Arial", 24, System.Drawing.FontStyle.Bold, 2);
12841286
}
12851287

12861288
public override void SetState(GraphicsDevice graphicsDevice, Material previousMaterial)

Source/RunActivity/Viewer3D/Popups/LabelPrimitive.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class LabelPrimitive : RenderPrimitive
3434

3535
readonly Viewer Viewer;
3636
readonly float OffsetY;
37-
37+
public bool InsideBox { set; get; }
3838
public LabelPrimitive(Label3DMaterial material, Color color, Color outline, float offsetY)
3939
{
4040
Material = material;
@@ -43,6 +43,15 @@ public LabelPrimitive(Label3DMaterial material, Color color, Color outline, floa
4343
Outline = outline;
4444
OffsetY = offsetY;
4545
}
46+
public LabelPrimitive(Label3DMaterial material, Color color, Color outline, float offsetY, bool insideBox)
47+
{
48+
Material = material;
49+
Viewer = material.Viewer;
50+
Color = color;
51+
Outline = outline;
52+
OffsetY = offsetY;
53+
InsideBox = insideBox;
54+
}
4655

4756
public override void Draw(GraphicsDevice graphicsDevice)
4857
{
@@ -55,13 +64,17 @@ public override void Draw(GraphicsDevice graphicsDevice)
5564
if (lineLocation2DStart.Z > 1 || lineLocation2DStart.Z < 0)
5665
return; // Out of range or behind the camera
5766

58-
lineLocation3D.Y += 10;
67+
lineLocation3D.Y += InsideBox ? 0 : 10;
5968
var lineLocation2DEndY = Viewer.GraphicsDevice.Viewport.Project(lineLocation3D, Viewer.Camera.XnaProjection, Viewer.Camera.XnaView, Matrix.Identity).Y;
6069

6170
var labelLocation2D = Material.GetTextLocation((int)lineLocation2DStart.X, (int)lineLocation2DEndY - Material.Font.Height, Text);
6271
lineLocation2DEndY = labelLocation2D.Y + Material.Font.Height;
6372

64-
Material.Font.Draw(Material.SpriteBatch, labelLocation2D, Text, Color, Outline);
73+
if (InsideBox)
74+
Material.BigFont.Draw(Material.SpriteBatch, labelLocation2D, Text, Color, Outline);
75+
else
76+
Material.Font.Draw(Material.SpriteBatch, labelLocation2D, Text, Color, Outline);
77+
6578
Material.SpriteBatch.Draw(Material.Texture, new Vector2(lineLocation2DStart.X - 1, lineLocation2DEndY), null, Outline, 0, Vector2.Zero, new Vector2(4, lineLocation2DStart.Y - lineLocation2DEndY), SpriteEffects.None, lineLocation2DStart.Z);
6679
Material.SpriteBatch.Draw(Material.Texture, new Vector2(lineLocation2DStart.X, lineLocation2DEndY), null, Color, 0, Vector2.Zero, new Vector2(2, lineLocation2DStart.Y - lineLocation2DEndY), SpriteEffects.None, lineLocation2DStart.Z);
6780
}

Source/RunActivity/Viewer3D/Popups/OSDCars.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,54 @@ public override void PrepareFrame(RenderFrame frame, ORTS.Common.ElapsedTime ela
7878
var newLabels = new Dictionary<TrainCar, LabelPrimitive>(labels.Count);
7979
var cars = Owner.Viewer.World.Trains.Cars;
8080
var cameraLocation = Owner.Viewer.Camera.CameraWorldLocation;
81+
var carID = Owner.Viewer.TrainCarOperationsViewerWindow.CurrentCarID;
82+
//data from Webpage
83+
var trainCarWebpage = Owner.Viewer.TrainCarOperationsWebpage;
84+
var carIDWebpage = trainCarWebpage != null && trainCarWebpage.Connections > 0 ? trainCarWebpage.CurrentCarID : "";
85+
bool isVisibleTrainCarViewerOrWebpage = Owner.Viewer.TrainCarOperationsViewerWindow.Visible || (trainCarWebpage != null && trainCarWebpage.Connections > 0 && Owner.Viewer.TrainCarOperationsWebpage.TrainCarSelected);
8186
foreach (var car in cars.Keys)
8287
{
8388
// Calculates distance between camera and platform label.
8489
var distance = WorldLocation.GetDistance(car.WorldPosition.WorldLocation, cameraLocation).Length();
85-
if (distance <= MaximumDistance)
90+
var carBodyLength = car.CarBodyLengthM < 21 ? 21 : car.CarBodyLengthM;
91+
if ((!isVisibleTrainCarViewerOrWebpage || !car.IsPlayerTrain) && distance <= MaximumDistance)
8692
{
8793
if ((State == DisplayState.Cars) || (State == DisplayState.Trains && (car.Train == null || car.Train.FirstCar == car)))
8894
{
8995
if (labels.ContainsKey(car))
9096
newLabels[car] = labels[car];
9197
else
92-
newLabels[car] = new LabelPrimitive(Owner.Label3DMaterial, Color.Blue, Color.White, car.CarHeightM) { Position = car.WorldPosition };
98+
newLabels[car] = new LabelPrimitive(Owner.Label3DMaterial, Color.Blue, Color.White, car.CarHeightM, false) { Position = car.WorldPosition };
9399

100+
newLabels[car].InsideBox = false;
94101
newLabels[car].Text = State == DisplayState.Cars || car.Train == null ? car.CarID : car.Train.Name;
95102

96103
// Change color with distance.
97104
var ratio = (MathHelper.Clamp(distance, MinimumDistance, MaximumDistance) - MinimumDistance) / (MaximumDistance - MinimumDistance);
98105
newLabels[car].Color.A = newLabels[car].Outline.A = (byte)MathHelper.Lerp(255, 0, ratio);
99106
}
100107
}
108+
else if (car.IsPlayerTrain && isVisibleTrainCarViewerOrWebpage && (car.CarID == carID || car.CarID == carIDWebpage) && distance <= carBodyLength)
109+
{
110+
if ((State == DisplayState.Cars) || (State == DisplayState.Trains && (car.Train == null || car.Train.FirstCar == car)))
111+
{
112+
if (labels.ContainsKey(car))
113+
newLabels[car] = labels[car];
114+
else
115+
newLabels[car] = new LabelPrimitive(Owner.Label3DMaterial, Color.Blue, Color.White, car.CarHeightM, true) { Position = car.WorldPosition };
116+
117+
newLabels[car].InsideBox = true;
118+
newLabels[car].Text = State == DisplayState.Cars || car.Train == null ? car.CarID : car.Train.Name;
119+
}
120+
}
101121
}
102122
Labels = newLabels;
103123
}
104124

105125
foreach (var primitive in Labels.Values)
126+
{
106127
frame.AddPrimitive(Owner.Label3DMaterial, primitive, RenderPrimitiveGroup.Labels, ref Identity);
128+
}
107129
}
108130

109131
public DisplayState CurrentDisplayState

Source/RunActivity/Viewer3D/Popups/TrainCarOperationsViewerWindow.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ public int CarPosition
9191
set;
9292
get;
9393
}
94-
public int NewCarPosition
94+
public string CurrentCarID
95+
{
96+
set;
97+
get;
98+
}
99+
public int NewCarPosition
95100
{
96101
set;
97102
get;
@@ -270,6 +275,7 @@ protected override ControlLayout Layout(ControlLayout layout)
270275
: $" {Viewer.Catalog.GetString(wagon.WagonType.ToString())}";
271276

272277
Vbox.Add(buttonClose = new Label(Vbox.RemainingWidth, Owner.TextFontDefault.Height, $"{Viewer.Catalog.GetString("Car ID")} {(CarPosition >= PlayerTrain.Cars.Count ? " " : PlayerTrain.Cars[CarPosition].CarID + wagonType)}", LabelAlignment.Center));
278+
CurrentCarID = CarPosition >= PlayerTrain.Cars.Count ? " " : PlayerTrain.Cars[CarPosition].CarID;
273279
buttonClose.Click += new Action<Control, Point>(buttonClose_Click);
274280
buttonClose.Color = Owner.Viewer.TrainCarOperationsWindow.WarningCarPosition.Find(x => x == true) ? Color.Cyan : Color.White;
275281
Vbox.AddHorizontalSeparator();

Source/RunActivity/Viewer3D/WebServices/TrainCarOperationsWebpage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public OperationsStatus(int amountOfCars)
8282

8383
public bool TrainCarSelected;
8484
public int TrainCarSelectedPosition;
85+
public string CurrentCarID { get; set; }
8586

8687
public int Connections = 0;
8788

@@ -352,6 +353,8 @@ private string getCarId(TrainCar trainCar, int carPosition)
352353
var isElectric = trainCar is MSTSElectricLocomotive;
353354
var isSteam = trainCar is MSTSSteamLocomotive;
354355
var isEngine = isDiesel || isElectric || isSteam;
356+
// Required by OSDCar.cs
357+
CurrentCarID = Viewer.PlayerTrain.Cars[TrainCarSelectedPosition].CarID;
355358
var wagonType = isEngine ?
356359
$" {Viewer.Catalog.GetString(locomotive.WagonType.ToString())}" + $":{Viewer.Catalog.GetString(locomotive.EngineType.ToString())}" :
357360
$" {Viewer.Catalog.GetString(trainCar.WagonType.ToString())}";

0 commit comments

Comments
 (0)