Skip to content

Commit 45d8b7f

Browse files
authored
Merge pull request #853 from sweiland-openrails/OutOfFocus
Notify out of focus
2 parents 3d7b054 + d05f581 commit 45d8b7f

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// COPYRIGHT 2023 by the Open Rails project.
2+
//
3+
// This file is part of Open Rails.
4+
//
5+
// Open Rails is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Open Rails is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17+
//
18+
19+
/*
20+
* This out of focus window is a somewhat strange Window
21+
* When the main window is out of focus (so not receving any keystrokes)
22+
* it is surrounded by a red square to notify the user
23+
* The red square is the only functionality, so no save, no move, no resize etc.
24+
*/
25+
26+
using Microsoft.Xna.Framework;
27+
using Microsoft.Xna.Framework.Graphics;
28+
29+
namespace Orts.Viewer3D.Popups
30+
{
31+
public class OutOfFocusWindow : Window
32+
{
33+
private readonly Texture2D Line;
34+
private readonly int Thickness = 3;
35+
private readonly Color Color = Color.Red;
36+
37+
public OutOfFocusWindow(WindowManager owner) : base(owner)
38+
{
39+
Line = new Texture2D(Owner.Viewer.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
40+
Line.SetData(new[] { Color });
41+
}
42+
43+
public override void Draw(SpriteBatch spriteBatch)
44+
{
45+
int width = Owner.Viewer.GraphicsDevice.Viewport.Width;
46+
int height = Owner.Viewer.GraphicsDevice.Viewport.Height;
47+
48+
// top
49+
DrawLine(spriteBatch, 0, 0, width, Thickness, 0);
50+
51+
// bottom
52+
DrawLine(spriteBatch, 0, height - Thickness, width, Thickness, 0);
53+
54+
// left
55+
DrawLine(spriteBatch, Thickness, Thickness, height, Thickness, 90);
56+
57+
// right
58+
DrawLine(spriteBatch, width, Thickness, height, Thickness, 90);
59+
}
60+
61+
private void DrawLine(SpriteBatch spriteBatch, int X, int Y, int width, int height, int degrees)
62+
{
63+
spriteBatch.Draw(
64+
Line,
65+
new Rectangle(X, Y, width, height),
66+
null,
67+
Color,
68+
ConvertToRadiansFromDegrees(degrees),
69+
new Vector2(0, 0),
70+
SpriteEffects.None, 0);
71+
}
72+
73+
private float ConvertToRadiansFromDegrees(int angle)
74+
{
75+
return (float)((System.Math.PI / 180) * angle);
76+
}
77+
}
78+
}

Source/RunActivity/Viewer3D/Popups/Window.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public abstract class Window : RenderPrimitive
4545
VertexBuffer WindowVertexBuffer;
4646
IndexBuffer WindowIndexBuffer;
4747

48+
public bool DoNotDisplayWindow // true for a NotInFocus window
49+
{
50+
get;
51+
private set;
52+
}
53+
4854
public Window(WindowManager owner, int width, int height, string caption)
4955
{
5056
Owner = owner;
@@ -63,6 +69,14 @@ public Window(WindowManager owner, int width, int height, string caption)
6369
}
6470

6571
Caption = caption;
72+
DoNotDisplayWindow = false;
73+
Owner.Add(this);
74+
}
75+
76+
protected Window(WindowManager owner)
77+
{
78+
Owner = owner;
79+
DoNotDisplayWindow = true;
6680
Owner.Add(this);
6781
}
6882

@@ -224,6 +238,11 @@ public virtual void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime, boo
224238

225239
public override void Draw(GraphicsDevice graphicsDevice)
226240
{
241+
if (DoNotDisplayWindow)
242+
{
243+
return;
244+
}
245+
227246
if (WindowVertexBuffer == null)
228247
{
229248
// Edges/corners are 32px (1/4th image size).

Source/RunActivity/Viewer3D/Popups/WindowManager.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,24 @@ public void Initialize()
171171
public void Save(BinaryWriter outf)
172172
{
173173
foreach (var window in Windows)
174-
window.Save(outf);
174+
if (!window.DoNotDisplayWindow)
175+
{
176+
// This if is added to not do the save in case of the out of focus window
177+
// See Source\RunActivity\Viewer3D\Popups\OutOfFocusWindow.cs
178+
window.Save(outf);
179+
}
175180
}
176181

177182
[CallOnThread("Render")]
178183
public void Restore(BinaryReader inf)
179184
{
180185
foreach (var window in Windows)
181-
window.Restore(inf);
186+
if (!window.DoNotDisplayWindow)
187+
{
188+
// This if is added to not do the restore in case of the out of focus window
189+
// See Source\RunActivity\Viewer3D\Popups\OutOfFocusWindow.cs
190+
window.Restore(inf);
191+
}
182192
}
183193

184194
[CallOnThread("Updater")]

Source/RunActivity/Viewer3D/Viewer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class Viewer
103103
public TrainListWindow TrainListWindow { get; private set; } // for switching driven train
104104
public TTDetachWindow TTDetachWindow { get; private set; } // for detaching player train in timetable mode
105105
public EOTListWindow EOTListWindow { get; private set; } // to select EOT
106+
private OutOfFocusWindow OutOfFocusWindow; // to show colored rectangle around the main window when not in focus
106107

107108
// Route Information
108109
public TileManager Tiles { get; private set; }
@@ -500,6 +501,10 @@ internal void Initialize()
500501
TrainListWindow = new TrainListWindow(WindowManager);
501502
TTDetachWindow = new TTDetachWindow(WindowManager);
502503
EOTListWindow = new EOTListWindow(WindowManager);
504+
if (Settings.SuppressConfirmations < (int)ConfirmLevel.Error)
505+
// confirm level Error might be set to suppressed when taking a movie
506+
// do not show the out of focus red square in that case
507+
OutOfFocusWindow = new OutOfFocusWindow(WindowManager);
503508
WindowManager.Initialize();
504509

505510
InfoDisplay = new InfoDisplay(this);
@@ -853,6 +858,11 @@ public void Update(RenderFrame frame, float elapsedRealTime)
853858
// TODO: This is not correct. The ActivityWindow's PrepareFrame is already called by the WindowManager!
854859
if (Simulator.ActivityRun != null) ActivityWindow.PrepareFrame(elapsedTime, true);
855860

861+
if (Settings.SuppressConfirmations < (int)ConfirmLevel.Error)
862+
// confirm level Error might be set to suppressed when taking a movie
863+
// do not show the out of focus red square in that case
864+
OutOfFocusWindow.Visible = !this.Game.IsActive;
865+
856866
WindowManager.PrepareFrame(frame, elapsedTime);
857867

858868
SwitchPanelModule.SendSwitchPanelIfChanged();

0 commit comments

Comments
 (0)