|
15 | 15 | // You should have received a copy of the GNU General Public License |
16 | 16 | // along with Open Rails. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
|
| 18 | +using System.Collections.Generic; |
18 | 19 | using System.Linq; |
19 | 20 | using Microsoft.Xna.Framework; |
20 | 21 | using Microsoft.Xna.Framework.Graphics; |
21 | 22 | using Orts.Simulation.RollingStocks; |
22 | 23 | using Orts.Viewer3D.RollingStock; |
| 24 | +using SpriteBatch = Microsoft.Xna.Framework.Graphics.SpriteBatch; |
23 | 25 |
|
24 | 26 | namespace Orts.Viewer3D.Popups |
25 | 27 | { |
26 | 28 | public class ControlRectangle : Window |
27 | 29 | { |
28 | 30 | private readonly Texture2D Line; |
29 | | - private readonly int Thickness = 3; |
| 31 | + private int Thickness = 1; |
30 | 32 | private readonly Color Color = Color.Yellow; |
31 | 33 | private readonly Viewer Viewer; |
| 34 | + private bool CabViewFront; |
| 35 | + private bool IsOverRectangle = false; |
| 36 | + private class ListRect |
| 37 | + { |
| 38 | + public bool Front; |
| 39 | + public Rectangle CabRectangle; |
| 40 | + public string Name; |
| 41 | + } |
| 42 | + private List<ListRect> ListRectangles = new List<ListRect>(); |
| 43 | + private ListRect ListRects; |
32 | 44 |
|
33 | 45 | public ControlRectangle(WindowManager owner, Viewer viewer) : base(owner) |
34 | 46 | { |
35 | 47 | Line = new Texture2D(Owner.Viewer.GraphicsDevice, 1, 1, false, SurfaceFormat.Color); |
36 | 48 | Line.SetData(new[] { Color }); |
37 | 49 | Viewer = viewer; |
38 | 50 | } |
39 | | - |
40 | 51 | public override void Draw(SpriteBatch spriteBatch) |
41 | 52 | { |
42 | 53 | if (Viewer.Camera is CabCamera && (Viewer.PlayerLocomotiveViewer as MSTSLocomotiveViewer)._hasCabRenderer) |
43 | 54 | { |
44 | 55 | var cabRenderer = (Viewer.PlayerLocomotiveViewer as MSTSLocomotiveViewer)._CabRenderer; |
45 | 56 |
|
46 | 57 | var loco = Viewer.PlayerLocomotive as MSTSLocomotive; |
47 | | - var cabViewFrontRear = loco.UsingRearCab ? 1 : 0; |
| 58 | + CabViewFront = !loco.UsingRearCab; |
| 59 | + |
48 | 60 | var itemsFrontCount = loco.CabViewList[(int)CabViewType.Front].CVFFile.CabViewControls.Count(); |
49 | 61 | var itemsRearCount = loco.CabViewList.Count > 1 ? loco.CabViewList[(int)CabViewType.Rear].CVFFile.CabViewControls.Count() : 0; |
50 | | - |
51 | | - foreach (var controlRenderer in cabRenderer.ControlMap.Values.Skip(cabViewFrontRear == 0 ? 0 : itemsFrontCount).Take(cabViewFrontRear == 0 ? itemsFrontCount : itemsRearCount)) |
| 62 | + |
| 63 | + foreach (var controlRenderer in cabRenderer.ControlMap.Values.Skip(CabViewFront ? 0 : itemsFrontCount).Take(CabViewFront ? itemsFrontCount : itemsRearCount)) |
52 | 64 | { |
53 | 65 | if ((Viewer.Camera as CabCamera).SideLocation == controlRenderer.Control.CabViewpoint && controlRenderer is ICabViewMouseControlRenderer mouseRenderer) |
54 | | - { |
| 66 | + { |
55 | 67 | if (mouseRenderer.isMouseControl()) |
56 | 68 | { |
57 | 69 | Rectangle rectangle = mouseRenderer.DestinationRectangleGet(); |
58 | | - |
59 | 70 | int width = rectangle.Width; |
60 | 71 | int height = rectangle.Height; |
61 | 72 |
|
62 | 73 | if (width > 0) |
63 | 74 | { |
64 | 75 | // do not know why rectangles with width and height = 0 are there |
| 76 | + ListRects = ListRectangles.FirstOrDefault(c => c.Name == mouseRenderer.GetControlName() && c.Front == CabViewFront); |
| 77 | + if (ListRects == null) |
| 78 | + { |
| 79 | + ListRectangles.Add(new ListRect |
| 80 | + { |
| 81 | + CabRectangle = rectangle, |
| 82 | + Front = CabViewFront, |
| 83 | + Name = mouseRenderer.GetControlName(), |
| 84 | + }); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + ListRects.CabRectangle = rectangle; |
| 89 | + } |
65 | 90 |
|
66 | | - // top line |
67 | | - DrawLine(spriteBatch, rectangle.X, rectangle.Y, width, Thickness, 0); |
68 | | - |
69 | | - // bottom line |
70 | | - DrawLine(spriteBatch, rectangle.X, rectangle.Y + height - Thickness, width, Thickness, 0); |
| 91 | + Thickness = 1; // default |
| 92 | + if (mouseRenderer.IsMouseWithin()) |
| 93 | + { |
| 94 | + ListRects = ListRectangles.FirstOrDefault(c => c.CabRectangle == rectangle && c.Front == CabViewFront); |
71 | 95 |
|
72 | | - // left line |
73 | | - DrawLine(spriteBatch, rectangle.X + Thickness, rectangle.Y, height, Thickness, 90); |
| 96 | + if (ListRects != null && rectangle.Intersects(ListRects.CabRectangle) && ListRects.Name == mouseRenderer.GetControlName() && !IsOverRectangle) |
| 97 | + { |
| 98 | + Thickness = 3; // Highlights the currently selected rectangle |
| 99 | + IsOverRectangle = true; |
| 100 | + } |
| 101 | + } |
74 | 102 |
|
75 | | - // right line |
76 | | - DrawLine(spriteBatch, rectangle.X + width, rectangle.Y, height, Thickness, 90); |
| 103 | + DrawRectangle(spriteBatch, rectangle.X, rectangle.Y, width, height, Thickness, Color); |
77 | 104 | } |
78 | 105 | } |
79 | 106 | } |
80 | 107 | } |
| 108 | + IsOverRectangle = false; |
81 | 109 | } |
82 | 110 | } |
83 | 111 |
|
84 | | - private void DrawLine(SpriteBatch spriteBatch, int X, int Y, int width, int height, int degrees) |
| 112 | + private void DrawRectangle(SpriteBatch spriteBatch, int newX, int newY, int width, int height, int Thickness, Color Color) |
| 113 | + { // top line |
| 114 | + DrawLine(spriteBatch, newX, newY, width, Thickness, 0, Color); |
| 115 | + // bottom line |
| 116 | + DrawLine(spriteBatch, newX, newY + height - Thickness, width, Thickness, 0, Color); |
| 117 | + // left line |
| 118 | + DrawLine(spriteBatch, newX + Thickness, newY, height, Thickness, 90, Color); |
| 119 | + // right line |
| 120 | + DrawLine(spriteBatch, newX + width, newY, height, Thickness, 90, Color); |
| 121 | + } |
| 122 | + |
| 123 | + private void DrawLine(SpriteBatch spriteBatch, int X, int Y, int width, int height, int degrees, Color Color) |
85 | 124 | { |
86 | 125 | spriteBatch.Draw( |
87 | 126 | Line, |
|
0 commit comments