aboutsummaryrefslogtreecommitdiff
path: root/V3/Screens/DebugScreen.cs
blob: a1e0820f6049cb60e6d430b8d846e79a6fdaf7b8 (plain)
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ninject;
using V3.Data;
using V3.Input;
using V3.Objects;
using V3.Widgets;

namespace V3.Screens
{
    /// <summary>
    /// A special screen that counts and show the number of frames per
    /// second (if debug is enabled).  This screen is not added to the
    /// screen stack, but is always drawn and updated by the screen manager.
    /// </summary>
    // ReSharper disable once ClassNeverInstantiated.Global
    public sealed class DebugScreen : AbstractScreen, IInitializable
    {
        private readonly GraphicsDeviceManager mGraphicsDeviceManager;
        private readonly IObjectsManager mObjectsManager;
        private readonly IOptionsManager mOptionsManager;
        private readonly WidgetFactory mWidgetFactory;
        private FpsCounter mFpsCounter;
        private Label mFpsLabel;
        private Label mUnitCountLabel;
        private int mUnitCount;

        /// <summary>
        /// Creates a new debug screen.
        /// </summary>
        public DebugScreen(GraphicsDeviceManager graphicsDeviceManager,
            IObjectsManager objectsManager, IOptionsManager optionsManager,
            WidgetFactory widgetFactory) : base(true, true)
        {
            mGraphicsDeviceManager = graphicsDeviceManager;
            mObjectsManager = objectsManager;
            mOptionsManager = optionsManager;
            mWidgetFactory = widgetFactory;
        }

        public void Initialize()
        {
            mFpsCounter = new FpsCounter();

            mFpsLabel = mWidgetFactory.CreateLabel("");
            mFpsLabel.PaddingX = 10;
            mFpsLabel.PaddingY = 0;
            mFpsLabel.HorizontalOrientation = HorizontalOrientation.Left;
            mFpsLabel.Color = Color.Red;

            mUnitCountLabel = mWidgetFactory.CreateLabel("");
            mUnitCountLabel.PaddingX = 10;
            mUnitCountLabel.PaddingY = 0;
            mUnitCountLabel.HorizontalOrientation = HorizontalOrientation.Left;
            mUnitCountLabel.Color = Color.Red;
        }

        public override bool HandleMouseEvent(IMouseEvent mouseEvent)
        {
            return false;
        }

        public override void Update(GameTime gameTime)
        {
            mFpsCounter.Update(gameTime);
            mUnitCount = mObjectsManager.CreatureList?.Count ?? 0;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            mFpsCounter.AddFrame();

            if (mOptionsManager.Options.DebugMode == DebugMode.Off)
                return;

            var viewport = mGraphicsDeviceManager.GraphicsDevice.Viewport;

            mFpsLabel.Text = $"FPS: {mFpsCounter.Fps} " + (gameTime.IsRunningSlowly ? "!" : "");
            mFpsLabel.Size = mFpsLabel.GetMinimumSize();
            mFpsLabel.Position = new Vector2(0, viewport.Height - mFpsLabel.Size.Y);

            mUnitCountLabel.Text = $"# units: {mUnitCount}";
            mUnitCountLabel.Size = mUnitCountLabel.GetMinimumSize();
            mUnitCountLabel.Position = mFpsLabel.Position - new Vector2(0, mFpsLabel.Size.Y);

            spriteBatch.Begin();
            mFpsLabel.Draw(spriteBatch);
            mUnitCountLabel.Draw(spriteBatch);
            spriteBatch.End();
        }
    }
}