Files
GladiatorIdle/Mono/Gfx/Draw.cs

98 lines
4.9 KiB
C#
Raw Normal View History

using System;
using FontStashSharp;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GladiatorIdle.Gfx
{
internal enum Align { Left, Center, Right }
/// <summary>
/// Zeichenhilfen in Design-Pixeln (1920×1080). Schrift wird in Bildschirmgröße gerastert
/// (<see cref="UiScale"/>) und verkleinert gezeichnet — scharf auch in 4K.
/// </summary>
internal static class Draw
{
public static float UiScale = 1f;
public static readonly Color Gold = new(236, 190, 84), Sand = new(222, 196, 150), Dim = new(150, 128, 98),
Red = new(200, 64, 52), Green = new(110, 190, 90), Blue = new(110, 160, 220), Purple = new(170, 130, 220),
Panel = new(24, 18, 12), Border = new(110, 82, 46);
public static Color A(Color c, float alpha) => c * MathHelper.Clamp(alpha, 0, 1);
public static void Rect(SpriteBatch sb, RectangleF r, Color c) =>
sb.Draw(Assets.Pixel, new Vector2(r.X, r.Y), null, c, 0, Vector2.Zero, new Vector2(r.Width, r.Height), SpriteEffects.None, 0);
public static void Frame(SpriteBatch sb, RectangleF r, Color c, float t = 2)
{
Rect(sb, new RectangleF(r.X, r.Y, r.Width, t), c);
Rect(sb, new RectangleF(r.X, r.Bottom - t, r.Width, t), c);
Rect(sb, new RectangleF(r.X, r.Y, t, r.Height), c);
Rect(sb, new RectangleF(r.Right - t, r.Y, t, r.Height), c);
}
public static void Glow(SpriteBatch sb, Vector2 center, Vector2 size, Color c) =>
sb.Draw(Assets.Soft, center, null, c, 0, new Vector2(64, 64), size / 128f, SpriteEffects.None, 0);
public static Vector2 Measure(FontSystem fs, float px, string text) =>
fs.GetFont(px * UiScale).MeasureString(text) / UiScale;
/// Text an <paramref name="pos"/> (y = Mitte der Zeile), mit weichem Schatten.
public static void Text(SpriteBatch sb, FontSystem fs, float px, string text, Vector2 pos, Color c,
Align align = Align.Left, float scale = 1f, bool shadow = true)
{
// feste Rastergröße (ganze Pixel), Animation nur über die Zeichen-Skalierung —
// sonst entstehen jedes Bild neue Glyphen
var font = fs.GetFont(MathF.Max(4, MathF.Round(px * UiScale)));
var size = font.MeasureString(text) / UiScale * scale;
float x = align switch { Align.Center => pos.X - size.X / 2, Align.Right => pos.X - size.X, _ => pos.X };
var p = new Vector2(x, pos.Y - size.Y / 2);
var s = new Vector2(scale / UiScale);
if (shadow) font.DrawText(sb, text, p + new Vector2(2, 3) * scale, Color.Black * (c.A / 255f * 0.6f), scale: s);
font.DrawText(sb, text, p, c, scale: s);
}
/// Text mit Zeilenumbruch an Wortgrenzen innerhalb von <paramref name="r"/> (oben links beginnend).
public static void TextWrap(SpriteBatch sb, FontSystem fs, float px, string text, RectangleF r, Color c)
{
float y = r.Y + px * 0.6f, lineH = px * 1.3f;
string line = "";
foreach (var word in text.Split(' '))
{
string next = line.Length == 0 ? word : line + " " + word;
if (line.Length > 0 && Measure(fs, px, next).X > r.Width)
{
if (y > r.Bottom) return;
Text(sb, fs, px, line, new Vector2(r.X, y), c);
y += lineH; line = word;
}
else line = next;
}
if (line.Length > 0 && y <= r.Bottom + lineH) Text(sb, fs, px, line, new Vector2(r.X, y), c);
}
/// Balken mit verzögertem Rest (weißer Abschnitt zeigt den gerade verlorenen Teil).
public static void Bar(SpriteBatch sb, RectangleF r, float value, float lag, Color c, bool fromRight = false)
{
Rect(sb, r, new Color(12, 8, 5) * 0.85f);
value = MathHelper.Clamp(value, 0, 1); lag = MathHelper.Clamp(lag, value, 1);
RectangleF Part(float a, float b) => fromRight
? new RectangleF(r.Right - 3 - (r.Width - 6) * b, r.Y + 3, (r.Width - 6) * (b - a), r.Height - 6)
: new RectangleF(r.X + 3 + (r.Width - 6) * a, r.Y + 3, (r.Width - 6) * (b - a), r.Height - 6);
Rect(sb, Part(value, lag), new Color(255, 240, 220));
Rect(sb, Part(0, value), c);
Rect(sb, new RectangleF(r.X + 3, r.Y + 3, r.Width - 6, (r.Height - 6) * 0.35f), Color.White * 0.12f);
Frame(sb, r, Border);
}
}
internal readonly record struct RectangleF(float X, float Y, float Width, float Height)
{
public float Right => X + Width;
public float Bottom => Y + Height;
public Vector2 Center => new(X + Width / 2, Y + Height / 2);
public bool Contains(Vector2 p) => p.X >= X && p.X < Right && p.Y >= Y && p.Y < Bottom;
}
}