89 lines
3.7 KiB
Python
89 lines
3.7 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
import tkinter as tk
|
||
|
|
from resources.code_res.game_constants import (
|
||
|
|
ACCENT_GOLD, ACCENT_GOTH, BG_COLOR, FG_COLOR,
|
||
|
|
MODAL_BG, MUTED_FG, PANEL_BG, SUBTLE_BORDER
|
||
|
|
)
|
||
|
|
from resources.code_res.i18n import t
|
||
|
|
from view.widgets.image_button import ImageButton
|
||
|
|
|
||
|
|
|
||
|
|
class AudioSettingsDialog(tk.Frame):
|
||
|
|
@classmethod
|
||
|
|
def open(cls, root_window: tk.Tk, assets, sound_mgr):
|
||
|
|
existing = getattr(root_window, "_audio_overlay", None)
|
||
|
|
if existing and existing.winfo_exists():
|
||
|
|
existing.lift()
|
||
|
|
return existing
|
||
|
|
dlg = cls(root_window, assets, sound_mgr)
|
||
|
|
root_window._audio_overlay = dlg
|
||
|
|
return dlg
|
||
|
|
|
||
|
|
def __init__(self, root_window: tk.Tk, assets, sound_mgr):
|
||
|
|
super().__init__(
|
||
|
|
root_window, bg=MODAL_BG, highlightthickness=2,
|
||
|
|
highlightbackground=ACCENT_GOTH, padx=30, pady=24
|
||
|
|
)
|
||
|
|
self.root_window = root_window
|
||
|
|
self.assets = assets
|
||
|
|
self.sound_mgr = sound_mgr
|
||
|
|
self.place(relx=0.5, rely=0.5, anchor="center")
|
||
|
|
self._build()
|
||
|
|
|
||
|
|
def _build(self):
|
||
|
|
tk.Label(self, text=t("audio_title"), font=("Georgia", 17, "bold"), fg=ACCENT_GOTH, bg=MODAL_BG).pack(pady=(0, 6))
|
||
|
|
tk.Label(self, text=t("audio_subtitle"), font=("Helvetica", 9), fg=MUTED_FG, bg=MODAL_BG).pack(pady=(0, 18))
|
||
|
|
|
||
|
|
self._build_slider_row(
|
||
|
|
label_txt=t("audio_ambiance_label"),
|
||
|
|
desc_txt=t("audio_ambiance_desc"),
|
||
|
|
initial_vol=self.sound_mgr.ambiance_volume,
|
||
|
|
on_change=lambda v: self.sound_mgr.set_ambiance_volume(v, persist=True),
|
||
|
|
)
|
||
|
|
self._build_slider_row(
|
||
|
|
label_txt=t("audio_sfx_label"),
|
||
|
|
desc_txt=t("audio_sfx_desc"),
|
||
|
|
initial_vol=self.sound_mgr.menu_volume,
|
||
|
|
on_change=lambda v: self.sound_mgr.set_menu_volume(v, persist=True),
|
||
|
|
)
|
||
|
|
|
||
|
|
btn_row = tk.Frame(self, bg=MODAL_BG)
|
||
|
|
btn_row.pack(fill="x", pady=(16, 0))
|
||
|
|
|
||
|
|
ImageButton(
|
||
|
|
btn_row, t("audio_test_btn"), self.assets.raw_sel, self.assets.raw_sel_active,
|
||
|
|
lambda: self.sound_mgr.play("deal"), sound_mgr=None, width=150, height=38, font=("Helvetica", 10, "bold")
|
||
|
|
).pack(side="left")
|
||
|
|
|
||
|
|
ImageButton(
|
||
|
|
btn_row, t("audio_done_btn"), self.assets.raw_btn, self.assets.raw_btn_active,
|
||
|
|
self.close, sound_mgr=self.sound_mgr, width=150, height=38, font=("Helvetica", 10, "bold")
|
||
|
|
).pack(side="right")
|
||
|
|
|
||
|
|
def _build_slider_row(self, label_txt: str, desc_txt: str, initial_vol: float, on_change):
|
||
|
|
row = tk.Frame(self, bg=PANEL_BG, padx=16, pady=12, highlightthickness=1, highlightbackground=SUBTLE_BORDER)
|
||
|
|
row.pack(fill="x", pady=6)
|
||
|
|
|
||
|
|
top = tk.Frame(row, bg=PANEL_BG)
|
||
|
|
top.pack(fill="x")
|
||
|
|
tk.Label(top, text=label_txt, font=("Helvetica", 11, "bold"), fg=FG_COLOR, bg=PANEL_BG).pack(side="left")
|
||
|
|
pct_lbl = tk.Label(top, text=f"{int(round(initial_vol * 100))} %", font=("Helvetica", 11, "bold"), fg=ACCENT_GOLD, bg=PANEL_BG)
|
||
|
|
pct_lbl.pack(side="right")
|
||
|
|
|
||
|
|
tk.Label(row, text=desc_txt, font=("Helvetica", 8), fg=MUTED_FG, bg=PANEL_BG).pack(anchor="w", pady=(2, 6))
|
||
|
|
|
||
|
|
def _handle(val_str):
|
||
|
|
pct_lbl.configure(text=f"{int(round(float(val_str)))} %")
|
||
|
|
on_change(float(val_str) / 100.0)
|
||
|
|
|
||
|
|
scale = tk.Scale(
|
||
|
|
row, from_=0, to=100, orient="horizontal", showvalue=False,
|
||
|
|
length=360, bg=PANEL_BG, fg=FG_COLOR, troughcolor=BG_COLOR,
|
||
|
|
activebackground=ACCENT_GOTH, highlightthickness=0, bd=0, command=_handle
|
||
|
|
)
|
||
|
|
scale.set(int(round(initial_vol * 100)))
|
||
|
|
scale.pack(fill="x")
|
||
|
|
|
||
|
|
def close(self):
|
||
|
|
self.destroy()
|
||
|
|
self.root_window._audio_overlay = None
|