Files
GladiatorIdle/Mono/Arena/SettingsOverlay.cs

151 lines
8.3 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using GladiatorIdle.Gfx;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using D = GladiatorIdle.Gfx.Draw;
namespace GladiatorIdle.Arena
{
/// <summary>
/// Einstellungen über jedem Bildschirm: Lautstärken (Musik, Effekte, Erzählstimme), Ton an/aus,
/// Vollbild, Sprache, Erzählstimme, Effekte reduzieren. Jede Änderung wird sofort gespeichert.
/// </summary>
internal sealed class SettingsOverlay
{
readonly Ui _ui = new();
readonly Action _toggleFullscreen;
public bool Open { get; set; }
public SettingsOverlay(Action toggleFullscreen) => _toggleFullscreen = toggleFullscreen;
/// Bildrate geändert: Grafik neu einstellen (VSync, feste Rate).
public Action ApplyGraphics = () => { };
/// Einführung: null = kein Spielstand, true = schon gesehen; ReplayTutorial schaltet sie wieder ein.
public Func<bool?> Tutorial = () => null;
public Action ReplayTutorial = () => { };
public void Update(Input input, Matrix ui)
{
_ui.Begin(input, ui);
if (input.Pressed(Microsoft.Xna.Framework.Input.Keys.Escape)) { Open = false; input.PrevKeys = input.Keys; }
}
/// Text ohne vorangestelltes Symbol (die Schrift hat keine Emoji).
static string Plain(string s) => new string(s.SkipWhile(c => !char.IsLetter(c)).ToArray());
static void Save()
{
AppSettings.Save();
AppSettings.ApplyVolumes();
}
public void Draw(SpriteBatch sb, float scale, Vector2 offset)
{
// weich einblenden: Hintergrund dunkelt ab, Tafel wächst leicht aus der Mitte
if (!_wasOpen) { _openedAt = Environment.TickCount64; _wasOpen = true; }
float k = MathHelper.Clamp((Environment.TickCount64 - _openedAt) / 220f, 0, 1);
k = 1 - (1 - k) * (1 - k);
var ui = Matrix.CreateTranslation(-Camera.W / 2f, -Camera.H / 2f, 0) * Matrix.CreateScale(0.94f + 0.06f * k)
* Matrix.CreateTranslation(Camera.W / 2f, Camera.H / 2f, 0) * Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
var flat = Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, flat);
D.Rect(sb, new RectangleF(0, 0, Camera.W, Camera.H), Color.Black * (0.65f * k));
sb.End();
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
// breite, flache Tafel in der Bildmitte: links Ton, rechts Anzeige, unten Sprache und Zurück
const float W = 1560, H = 650;
var r = new RectangleF((Camera.W - W) / 2, (Camera.H - H) / 2, W, H);
Ui.Panel(sb, r, D.Gold, 1f);
D.Text(sb, Assets.Serif, 38, L.t("main.settings").ToUpperInvariant(), new Vector2(r.Center.X, r.Y + 52), D.Gold, Align.Center);
D.Rect(sb, new RectangleF(r.Center.X - 160, r.Y + 84, 320, 2), D.Gold * 0.5f);
float colW = (W - 150) / 2, xl = r.X + 50, xr = r.X + 100 + colW, top = r.Y + 130;
D.Rect(sb, new RectangleF(r.Center.X - 1, top - 10, 2, 360), D.Border * 0.8f);
// links: Ton
float y = top;
D.Text(sb, Assets.SansBold, 18, L.t("settings.sec.audio").ToUpperInvariant(), new Vector2(xl, y), D.Dim);
y += 46;
Volume(sb, xl, y, colW, L.t("settings.row.music"), () => AppSettings.MusicVolume, v => AppSettings.MusicVolume = v); y += 54;
Volume(sb, xl, y, colW, L.t("settings.row.sfx"), () => AppSettings.SfxVolume, v => AppSettings.SfxVolume = v); y += 54;
Volume(sb, xl, y, colW, L.t("settings.vol.voice"), () => AppSettings.VoiceVolume, v => AppSettings.VoiceVolume = v); y += 54;
Toggle(sb, xl, y, colW, Plain(L.t(AppSettings.Muted ? "settings.muted" : "settings.sound.on")), !AppSettings.Muted,
() => { AppSettings.Muted = !AppSettings.Muted; if (!AppSettings.Muted) SoundManager.StartAmbient(); });
y += 54;
Toggle(sb, xl, y, colW, L.t("settings.narration"), AppSettings.Narration, () => AppSettings.Narration = !AppSettings.Narration);
y += 54;
Toggle(sb, xl, y, colW, L.t("settings.namecalls"), AppSettings.NameCalls, () => AppSettings.NameCalls = !AppSettings.NameCalls);
// rechts: Anzeige
y = top;
D.Text(sb, Assets.SansBold, 18, L.t("settings.sec.display").ToUpperInvariant(), new Vector2(xr, y), D.Dim);
y += 46;
Toggle(sb, xr, y, colW, L.t("settings.fullscreen"), AppSettings.Fullscreen, _toggleFullscreen);
y += 54;
Toggle(sb, xr, y, colW, L.t("settings.reducemotion"), AppSettings.ReduceMotion, () => AppSettings.ReduceMotion = !AppSettings.ReduceMotion);
y += 54;
Toggle(sb, xr, y, colW, L.t("settings.antialias"), AppSettings.AntiAlias, () => AppSettings.AntiAlias = !AppSettings.AntiAlias);
y += 54;
Toggle(sb, xr, y, colW, L.t("settings.bloom"), AppSettings.Bloom, () => AppSettings.Bloom = !AppSettings.Bloom);
y += 54;
D.Text(sb, Assets.Sans, 22, L.t("settings.framerate"), new Vector2(xr, y), D.Sand);
var rates = new[] { "settings.fps.vsync", "settings.fps.120", "settings.fps.max" };
for (int i = 0; i < rates.Length; i++)
if (_ui.Button(sb, new RectangleF(xr + colW - 3 * 132 + i * 132, y - 20, 124, 40), L.t(rates[i]), selected: AppSettings.FrameLimit == i, size: 17)
&& AppSettings.FrameLimit != i)
{ AppSettings.FrameLimit = i; Save(); ApplyGraphics(); }
y += 54;
D.Text(sb, Assets.Sans, 22, L.t("settings.row.language"), new Vector2(xr, y), D.Sand);
var langs = new[] { (L.Lang.De, "Deutsch"), (L.Lang.En, "English"), (L.Lang.Es, "Español") }; // i18n-ok
for (int i = 0; i < langs.Length; i++)
{
var (lang, name) = langs[i];
if (_ui.Button(sb, new RectangleF(xr + colW - 3 * 132 + i * 132, y - 20, 124, 40), name, selected: L.Current == lang, size: 17))
if (L.Current != lang && DevShot.Lang == null) L.Set(lang);
}
// unten: Einführung, Zurück, Hinweis
float by = r.Bottom - 110;
D.Rect(sb, new RectangleF(r.X + 50, by - 24, W - 100, 2), D.Border * 0.8f);
if (Tutorial() is bool seen)
{
D.Text(sb, Assets.Sans, 20, L.t("settings.tutorial"), new Vector2(xl, by + 32), D.Sand);
if (_ui.Button(sb, new RectangleF(xl + 260, by + 10, 280, 44), L.t(seen ? "settings.tutorial.replay" : "settings.tutorial.next"), seen, size: 17))
ReplayTutorial();
}
if (_ui.Button(sb, new RectangleF(r.Right - 50 - 300, by + 2, 300, 60), L.t("select.back"), selected: true, size: 24))
Open = false;
D.Text(sb, Assets.Sans, 14, $"{Credits.COPYRIGHT} · {Credits.MAIL}", new Vector2(r.Center.X, r.Bottom - 22), D.Dim, Align.Center); // i18n-ok
sb.End();
if (!Open) _wasOpen = false;
}
long _openedAt;
bool _wasOpen;
void Volume(SpriteBatch sb, float x, float y, float w, string label, Func<int> get, Action<int> set)
{
D.Text(sb, Assets.Sans, 22, label, new Vector2(x, y), D.Sand);
var steps = AppSettings.VolumeSteps;
for (int i = 0; i < steps.Length; i++)
{
int v = steps[i];
var r = new RectangleF(x + w - steps.Length * 70 + i * 70, y - 20, 64, 40);
if (_ui.Button(sb, r, v.ToString(NumberFormat.Culture), selected: get() == v, size: 17)) { set(v); Save(); SoundManager.PlayClick(); }
}
}
void Toggle(SpriteBatch sb, float x, float y, float w, string label, bool on, Action flip)
{
D.Text(sb, Assets.Sans, 22, label, new Vector2(x, y), D.Sand);
if (_ui.Button(sb, new RectangleF(x + w - 150, y - 20, 150, 40), L.t(on ? "settings.on" : "settings.off"), selected: on, size: 18))
{
flip();
Save();
}
}
}
}