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
{
///
/// Einstellungen über jedem Bildschirm: Lautstärken (Musik, Effekte, Erzählstimme), Ton an/aus,
/// Vollbild, Sprache, Erzählstimme, Effekte reduzieren. Jede Änderung wird sofort gespeichert.
///
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 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)
{
var ui = Matrix.CreateScale(scale) * Matrix.CreateTranslation(offset.X, offset.Y, 0);
sb.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, ui);
D.Rect(sb, new RectangleF(0, 0, Camera.W, Camera.H), Color.Black * 0.6f);
var r = new RectangleF(Camera.W / 2 - 420, 24, 840, 1032);
Ui.Panel(sb, r, D.Gold, 0.97f);
float x = r.X + 50, y = r.Y + 60, w = r.Width - 100;
D.Text(sb, Assets.Serif, 40, L.t("main.settings").ToUpperInvariant(), new Vector2(r.Center.X, y), D.Gold, Align.Center);
// Audio
y += 70;
D.Text(sb, Assets.SansBold, 18, L.t("settings.sec.audio"), new Vector2(x, y), D.Dim);
y += 40;
Volume(sb, x, y, w, L.t("settings.row.music"), () => AppSettings.MusicVolume, v => AppSettings.MusicVolume = v); y += 56;
Volume(sb, x, y, w, L.t("settings.row.sfx"), () => AppSettings.SfxVolume, v => AppSettings.SfxVolume = v); y += 56;
Volume(sb, x, y, w, L.t("settings.vol.voice"), () => AppSettings.VoiceVolume, v => AppSettings.VoiceVolume = v); y += 56;
Toggle(sb, x, y, w, Plain(L.t(AppSettings.Muted ? "settings.muted" : "settings.sound.on")), !AppSettings.Muted,
() => { AppSettings.Muted = !AppSettings.Muted; if (!AppSettings.Muted) SoundManager.StartAmbient(); });
y += 56;
Toggle(sb, x, y, w, L.t("settings.narration"), AppSettings.Narration, () => AppSettings.Narration = !AppSettings.Narration);
// Anzeige
y += 80;
D.Text(sb, Assets.SansBold, 18, L.t("settings.sec.display"), new Vector2(x, y), D.Dim);
y += 40;
Toggle(sb, x, y, w, L.t("settings.fullscreen"), AppSettings.Fullscreen, _toggleFullscreen);
y += 56;
Toggle(sb, x, y, w, L.t("settings.reducemotion"), AppSettings.ReduceMotion, () => AppSettings.ReduceMotion = !AppSettings.ReduceMotion);
y += 56;
Toggle(sb, x, y, w, L.t("settings.antialias"), AppSettings.AntiAlias, () => AppSettings.AntiAlias = !AppSettings.AntiAlias);
y += 56;
Toggle(sb, x, y, w, L.t("settings.bloom"), AppSettings.Bloom, () => AppSettings.Bloom = !AppSettings.Bloom);
y += 56;
D.Text(sb, Assets.Sans, 22, L.t("settings.framerate"), new Vector2(x, 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(x + w - 3 * 150 - 20 + i * 160, y - 22, 150, 44), L.t(rates[i]), selected: AppSettings.FrameLimit == i, size: 18)
&& AppSettings.FrameLimit != i)
{ AppSettings.FrameLimit = i; Save(); ApplyGraphics(); }
y += 56;
D.Text(sb, Assets.Sans, 22, L.t("settings.row.language"), new Vector2(x, 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(x + w - 3 * 150 - 20 + i * 160, y - 22, 150, 44), name, selected: L.Current == lang, size: 18))
if (L.Current != lang && DevShot.Lang == null) L.Set(lang);
}
y += 70;
if (Tutorial() is bool seen)
{
D.Text(sb, Assets.Sans, 22, L.t("settings.tutorial"), new Vector2(x, y), D.Sand);
if (_ui.Button(sb, new RectangleF(x + w - 300, y - 22, 300, 44), L.t(seen ? "settings.tutorial.replay" : "settings.tutorial.next"), seen, size: 18))
ReplayTutorial();
}
if (_ui.Button(sb, new RectangleF(r.Center.X - 160, r.Bottom - 100, 320, 64), L.t("select.back"), selected: true, size: 26))
Open = false;
D.Text(sb, Assets.Sans, 15, $"{Credits.COPYRIGHT} · {Credits.MAIL}", new Vector2(r.Center.X, r.Bottom - 20), D.Dim, Align.Center); // i18n-ok
sb.End();
}
void Volume(SpriteBatch sb, float x, float y, float w, string label, Func get, Action 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 * 86 + i * 86, y - 20, 78, 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 - 170, y - 20, 170, 40), L.t(on ? "settings.on" : "settings.off"), selected: on, size: 18))
{
flip();
Save();
}
}
}
}