diff --git a/Features/Hud/HudPositioner.cs b/Features/Hud/HudPositioner.cs new file mode 100644 index 0000000..b7c3d51 --- /dev/null +++ b/Features/Hud/HudPositioner.cs @@ -0,0 +1,48 @@ +using FEZUG.Features.Console; +using FEZUG.Helpers; +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Text; + +namespace FEZUG.Features.Hud +{ + internal class HudPositioner + { + public FezugVariable XCoordVariable { get; private set; } + public FezugVariable YCoordVariable { get; private set; } + + public HudPositioner(string name, string helpText, float defaultX, float defaultY) + { + XCoordVariable = new FezugVariable( + $"{name}_hud_position_x", + $"Changes the X position of {helpText} HUD (value between 0 and 1)", + defaultX.ToString() + ) + { + SaveOnChange = true, + Min = 0, + Max = 1 + }; + + YCoordVariable = new FezugVariable( + $"{name}_hud_position_y", + $"Changes the Y position of {helpText} HUD (value between 0 and 1)", + defaultY.ToString() + ) + { + SaveOnChange = true, + Min = 0, + Max = 1 + }; + } + + public Vector2 GetPosition(float width, float height) + { + return new Vector2( + (DrawingTools.GetViewport().Width - width) * XCoordVariable.ValueFloat, + (DrawingTools.GetViewport().Height - height) * YCoordVariable.ValueFloat + ); + } + } +} diff --git a/Features/TextHud.cs b/Features/Hud/TextHud.cs similarity index 85% rename from Features/TextHud.cs rename to Features/Hud/TextHud.cs index 6d0dd16..7f188b5 100644 --- a/Features/TextHud.cs +++ b/Features/Hud/TextHud.cs @@ -10,7 +10,7 @@ using System.Text; using System.Threading.Tasks; -namespace FEZUG.Features +namespace FEZUG.Features.Hud { public class TextHud : IFezugFeature { @@ -23,6 +23,8 @@ public class TextHud : IFezugFeature private float lastWidth; private TimeSpan lastWidthUpdateTime; + private HudPositioner Positioner; + [ServiceDependency] public IPlayerManager PlayerManager { private get; set; } @@ -49,6 +51,8 @@ public void Initialize() hud_velocity = CreateHudVariable("hud_velocity", "Gomez's velocity"); hud_state = CreateHudVariable("hud_state", "Gomez's state"); hud_viewpoint = CreateHudVariable("hud_viewpoint", "camera viewpoint"); + + Positioner = new HudPositioner("text", "global text", 0.0f, 0.0f); } private void DrawText(string text, Vector2 pos) @@ -124,20 +128,26 @@ public void DrawHUD(GameTime gameTime) } float maxWidth = linesToDraw.Select(str => DrawingTools.DefaultFont.MeasureString(str).X * 2).Max(); - if(maxWidth > lastWidth || (gameTime.TotalGameTime - lastWidthUpdateTime).TotalSeconds > 1.0f) + if(maxWidth > lastWidth || (gameTime.TotalGameTime - lastWidthUpdateTime).TotalSeconds > 5.0f) { lastWidthUpdateTime = gameTime.TotalGameTime; lastWidth = maxWidth; } float padX = 10.0f; - DrawingTools.DrawRect(new Rectangle(2, 2, (int)(lastWidth + padX * 2), linesToDraw.Count * 30 + 20), new Color(0, 0, 0, 220)); + float width = lastWidth + padX * 2; + float height = linesToDraw.Count * 30 + 15; + int margin = 5; + var position = Positioner.GetPosition(width + 2 * margin, height + 2 * margin); - for(int i = 0; i < linesToDraw.Count; i++) + DrawingTools.DrawRect(new Rectangle((int)(position.X + margin), (int)(position.Y + margin), (int)width, (int)height), new Color(10, 10, 10, 220)); + + + for (int i = 0; i < linesToDraw.Count; i++) { var line = linesToDraw[i]; - DrawText(line, new Vector2(padX, i * 30.0f)); - if (i == 0) DrawText(line, new Vector2(padX, (i*30.0f)-1.0f)); + DrawText(line, position + new Vector2(padX, i * 30.0f)); + if (i == 0) DrawText(line, position + new Vector2(padX, (i*30.0f)-1.0f)); } } diff --git a/Features/LevelTimer.cs b/Features/LevelTimer.cs index 88ba47a..4143559 100644 --- a/Features/LevelTimer.cs +++ b/Features/LevelTimer.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Reflection; using Microsoft.Xna.Framework; +using FEZUG.Features.Hud; namespace FEZUG.Features { @@ -17,8 +18,8 @@ internal class LevelTimer : IFezugCommand, IFezugFeature { public string Name => "timer"; - public string HelpText => @"timer - creates a timer between two level entrances.\n - timer clear - clear the current timer."; + public string HelpText => "timer - creates a timer between two level entrances." + +"\ntimer clear - clear the current timer."; private bool enabled = false; @@ -37,6 +38,7 @@ internal class LevelTimer : IFezugCommand, IFezugFeature private GameTime currentTime = new GameTime(); private GameTime startTime = new GameTime(); + private HudPositioner Positioner; [ServiceDependency] public ILevelManager LevelManager { private get; set; } @@ -49,6 +51,8 @@ public LevelTimer() ServiceHelper.InjectServices(this); GameLevelManager.LevelChanged += OnLevelChange; + + Positioner = new HudPositioner("timer", "level timer", 0.5f, 0.0f); } public void Initialize() @@ -128,7 +132,7 @@ public void Update(GameTime gameTime) public void DrawHUD(GameTime gameTime) { - if (!active && timeHistory.Count == 0) return; + if (!enabled) return; var defaultTimerString = "00:00.000"; var timerString = defaultTimerString; @@ -137,9 +141,6 @@ public void DrawHUD(GameTime gameTime) var textScale = 3.0f; var textWidth = (int)(DrawingTools.DefaultFont.MeasureString(defaultTimerString).X * textScale); var textHeight = 45; - var viewportWidth = DrawingTools.GetViewport().Width; - var pad = 15; - var timerPos = new Vector2(viewportWidth - textWidth - 20, 20 - pad); if (active) { @@ -156,12 +157,17 @@ public void DrawHUD(GameTime gameTime) } } - DrawingTools.DrawRect(new Rectangle((int)timerPos.X - pad, (int)timerPos.Y, textWidth + 2 * pad, textHeight + 2 * pad), new Color(0, 0, 0, 128)); + var margin = 5; + var width = textWidth + 10; + var height = textHeight + 20; + var position = Positioner.GetPosition(width + 2 * margin, height + 2 * margin); + + DrawingTools.DrawRect(new Rectangle((int)(position.X + margin), (int)(position.Y + margin), width, height), new Color(10, 10, 10, 220)); for (var i = 0; i < timerString.Length; i++) { - var currentCharOffset = DrawingTools.DefaultFont.MeasureString(defaultTimerString.Substring(0,i)).X * textScale; - DrawingTools.DrawText(timerString[i].ToString(), timerPos + Vector2.UnitX * currentCharOffset, 0.0f, textScale, timerColor); + var currentCharOffset = DrawingTools.DefaultFont.MeasureString(defaultTimerString.Substring(0,i)).X * textScale + 12; + DrawingTools.DrawText(timerString[i].ToString(), position + Vector2.UnitX * currentCharOffset, 0.0f, textScale, timerColor); } }