using Godot; using RailEnterprise.Client.App; using static RailEnterprise.Client.App.Localization; namespace RailEnterprise.Client.Audio; /// /// Pannello «Audio» sopra a tutto: un cursore per bus (generale, musica, ambiente, effetti, /// interfaccia) e il muto generale. I cambi si sentono subito; quando il pannello esce dall'albero /// (anche chiudendo la finestra del gioco) si salvano in user://settings.cfg. Esc o un clic /// sinistro fuori chiudono. /// public partial class AudioSettingsPanel : Control { private readonly AudioManager _audio; private readonly List<(HSlider Slider, Label Value)> _rows = []; private readonly CheckBox _mute = new() { Text = T("Muto") }; private static readonly string[] Captions = ["Volume generale", "Musica", "Ambiente", "Effetti", "Interfaccia"]; public AudioSettingsPanel(AudioManager audio) => _audio = audio; public override void _Ready() { SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect); UiTheme.Apply(this); var shade = new ColorRect { Color = new Color(0, 0, 0, 0.5f), MouseFilter = MouseFilterEnum.Stop }; shade.SetAnchorsPreset(LayoutPreset.FullRect); shade.GuiInput += e => { // solo il clic sinistro: la rotella e il tasto destro arrivano anch'essi come pressioni if (e is InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.Left }) Close(); }; AddChild(shade); var center = new CenterContainer { MouseFilter = MouseFilterEnum.Ignore }; center.SetAnchorsPreset(LayoutPreset.FullRect); AddChild(center); var panel = new PanelContainer { CustomMinimumSize = new Vector2(440, 0) }; center.AddChild(panel); var col = new VBoxContainer(); col.AddThemeConstantOverride("separation", 12); panel.AddChild(col); col.AddChild(UiTheme.TitleLabel(T("Audio"), 34)); for (int i = 0; i < Captions.Length; i++) { int bus = i; var row = new HBoxContainer(); row.AddThemeConstantOverride("separation", 12); var caption = UiTheme.Text(T(Captions[i]), 14, Palette.TextSecondary); caption.CustomMinimumSize = new Vector2(150, 0); row.AddChild(caption); var slider = new HSlider { MinValue = 0, MaxValue = 1, Step = 0.05, SizeFlagsHorizontal = SizeFlags.ExpandFill, SizeFlagsVertical = SizeFlags.ShrinkCenter, Value = _audio.Settings.Volumes[i], }; var value = UiTheme.Text("", 14, bold: true); value.CustomMinimumSize = new Vector2(48, 0); value.HorizontalAlignment = HorizontalAlignment.Right; slider.ValueChanged += v => _audio.SetVolume(bus, (float)v); row.AddChild(slider); row.AddChild(value); col.AddChild(row); _rows.Add((slider, value)); } _mute.ButtonPressed = _audio.Settings.Muted; _mute.Toggled += on => _audio.SetMuted(on); col.AddChild(_mute); var hint = UiTheme.Text(T("Il tasto M attiva e toglie il muto durante la partita."), 13, Palette.TextSecondary); hint.AutowrapMode = TextServer.AutowrapMode.WordSmart; col.AddChild(hint); var close = UiTheme.Primary(T("Chiudi")); close.Pressed += Close; col.AddChild(close); _audio.SettingsChanged += Refresh; Refresh(); } public override void _ExitTree() { _audio.SettingsChanged -= Refresh; _audio.SaveSettings(); } private void Refresh() { for (int i = 0; i < _rows.Count; i++) { _rows[i].Slider.SetValueNoSignal(_audio.Settings.Volumes[i]); _rows[i].Value.Text = $"{Math.Round(_audio.Settings.Volumes[i] * 100):0} %"; } _mute.SetPressedNoSignal(_audio.Settings.Muted); } public override void _Input(InputEvent e) { if (e is InputEventKey { Pressed: true, Keycode: Key.Escape }) { Close(); GetViewport().SetInputAsHandled(); } } private void Close() { if (IsQueuedForDeletion()) return; QueueFree(); } }