Skip to content

Commit

Permalink
overhaul hud
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzyhau committed Feb 26, 2023
1 parent 19492dd commit 9332a97
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
48 changes: 48 additions & 0 deletions Features/Hud/HudPositioner.cs
Original file line number Diff line number Diff line change
@@ -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
);
}
}
}
22 changes: 16 additions & 6 deletions Features/TextHud.cs → Features/Hud/TextHud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Text;
using System.Threading.Tasks;

namespace FEZUG.Features
namespace FEZUG.Features.Hud
{
public class TextHud : IFezugFeature
{
Expand All @@ -23,6 +23,8 @@ public class TextHud : IFezugFeature
private float lastWidth;
private TimeSpan lastWidthUpdateTime;

private HudPositioner Positioner;

[ServiceDependency]
public IPlayerManager PlayerManager { private get; set; }

Expand All @@ -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)
Expand Down Expand Up @@ -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));
}

}
Expand Down
24 changes: 15 additions & 9 deletions Features/LevelTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using FEZUG.Features.Hud;

namespace FEZUG.Features
{
internal class LevelTimer : IFezugCommand, IFezugFeature
{
public string Name => "timer";

public string HelpText => @"timer <start_level> <end_level> - creates a timer between two level entrances.\n
timer clear - clear the current timer.";
public string HelpText => "timer <start_level> <end_level> - creates a timer between two level entrances."
+"\ntimer clear - clear the current timer.";

private bool enabled = false;

Expand All @@ -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; }
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 9332a97

Please sign in to comment.