summaryrefslogtreecommitdiff
path: root/source/ui/guis/DebugGUI.cs
blob: 7ee44ae48bf89370e7b2f607215e1e9e86a33763 (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
using System.Diagnostics;
using Celesteia.Resources;
using Celesteia.UI;
using Celesteia.UI.Elements;
using Celesteia.UI.Properties;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using MonoGame.Extended;

namespace Celesteia.GUIs {
    public class DebugGUI : GUI {
        private new GameInstance Game => (GameInstance) base.Game;
        public DebugGUI(GameInstance game) : base(game, Rect.ScreenFull) {}

        private Label updateLabel;
        private Label drawLabel;

        public override void LoadContent(ContentManager Content) {
            float fontSize = 12f;

            Label template = new Label(new Rect(
                AbsoluteUnit.WithValue(10),
                AbsoluteUnit.WithValue(10),
                AbsoluteUnit.WithValue(200),
                AbsoluteUnit.WithValue(50)
            ))
                .SetTextProperties(new TextProperties()
                    .SetColor(Color.White)
                    .SetFont(ResourceManager.Fonts.GetFontType("Hobo"))
                    .SetFontSize(fontSize)
                    .SetTextAlignment(TextAlignment.Top | TextAlignment.Left)
                );
            float textSpacing = 4f;
            float textRow(int number) => 10f + number * (fontSize + textSpacing);

            Root.AddChild(template.Clone().SetNewRect(template.GetRect().SetY(AbsoluteUnit.WithValue(textRow(0)))).SetText($"Celesteia {GameInstance.Version}"));
            Root.AddChild(updateLabel = template.Clone().SetNewRect(template.GetRect().SetY(AbsoluteUnit.WithValue(textRow(1)))).SetText(""));
            Root.AddChild(drawLabel = template.Clone().SetNewRect(template.GetRect().SetY(AbsoluteUnit.WithValue(textRow(2)))).SetText(""));
            
            Debug.WriteLine("Loaded Debug GUI.");
        }

        private double updateTime;
        private double lastUpdate;
        public override void Update(GameTime gameTime, out bool clickedAnything) {
            clickedAnything = false;

            updateTime = gameTime.TotalGameTime.TotalMilliseconds - lastUpdate;
            lastUpdate = gameTime.TotalGameTime.TotalMilliseconds;

            updateLabel.SetText($"Update: {updateTime.ToString("0.00")}ms");
        }
        
        private double drawTime;
        private double lastDraw;
        public override void Draw(GameTime gameTime)
        {
            drawTime = gameTime.TotalGameTime.TotalMilliseconds - lastDraw;
            lastDraw = gameTime.TotalGameTime.TotalMilliseconds;

            drawLabel.SetText($"Draw: {drawTime.ToString("0.00")}ms");

            if (GameInstance.DebugMode) base.Draw(gameTime);
        }
    }
}