/
ControlRectangle.cs
94 lines (82 loc) · 3.7 KB
/
ControlRectangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// COPYRIGHT 2024 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Orts.Viewer3D.RollingStock;
namespace Orts.Viewer3D.Popups
{
public class ControlRectangle : Window
{
private readonly Texture2D Line;
private readonly int Thickness = 3;
private readonly Color Color = Color.Yellow;
private readonly Viewer Viewer;
public ControlRectangle(WindowManager owner, Viewer viewer) : base(owner)
{
Line = new Texture2D(Owner.Viewer.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
Line.SetData(new[] { Color });
Viewer = viewer;
}
public override void Draw(SpriteBatch spriteBatch)
{
if (Viewer.Camera is CabCamera && (Viewer.PlayerLocomotiveViewer as MSTSLocomotiveViewer)._hasCabRenderer)
{
var cabRenderer = (Viewer.PlayerLocomotiveViewer as MSTSLocomotiveViewer)._CabRenderer;
foreach (var controlRenderer in cabRenderer.ControlMap.Values)
{
if ((Viewer.Camera as CabCamera).SideLocation == controlRenderer.Control.CabViewpoint && controlRenderer is ICabViewMouseControlRenderer mouseRenderer)
{
if (mouseRenderer.isMouseControl())
{
Rectangle rectangle = mouseRenderer.DestinationRectangleGet();
int width = rectangle.Width;
int height = rectangle.Height;
if (width > 0)
{
// do not know why rectangles with width and height = 0 are there
// top line
DrawLine(spriteBatch, rectangle.X, rectangle.Y, width, Thickness, 0);
// bottom line
DrawLine(spriteBatch, rectangle.X, rectangle.Y + height - Thickness, width, Thickness, 0);
// left line
DrawLine(spriteBatch, rectangle.X + Thickness, rectangle.Y, height, Thickness, 90);
// right line
DrawLine(spriteBatch, rectangle.X + width, rectangle.Y, height, Thickness, 90);
}
}
}
}
}
}
private void DrawLine(SpriteBatch spriteBatch, int X, int Y, int width, int height, int degrees)
{
spriteBatch.Draw(
Line,
new Rectangle(X, Y, width, height),
null,
Color,
ConvertToRadiansFromDegrees(degrees),
new Vector2(0, 0),
SpriteEffects.None, 0);
}
private float ConvertToRadiansFromDegrees(int angle)
{
return (float)((System.Math.PI / 180) * angle);
}
}
}