# -*- coding: utf-8 -*- # view/dialogs/audio_settings_dialog.py from __future__ import annotations import tkinter as tk from PIL import Image, ImageTk from games.solitaire.base.solitaire_tap_prefs import ( is_single_tap_to_move_enabled, set_single_tap_to_move_enabled, ) from resources.code_res.game_constants import ( BG_COLOR, FONT_COLOR_BODY_DEFAULT, FONT_COLOR_BODY_MUTED, FONT_COLOR_HEADING_SUB, FONT_COLOR_TITLE_MAIN, FONT_SIZE_BODY, FONT_SIZE_HEADING, FONT_SIZE_SMALL_MIN, FONT_SIZE_TITLE_LG, SUBTLE_BORDER, UI_ACCENT_AMETHYST, UI_ACCENT_GOLD, ) from resources.code_res.i18n import t from view.widgets.goth_switch import GothSwitch from view.widgets.image_button import ( ImageButton, is_button_hover_help_enabled, set_button_hover_help_enabled, ) def is_german_active() -> bool: lang_btn = t("btn_lang").upper() return ("SPRACHE" in lang_btn) or lang_btn.endswith("DE") class AudioSettingsDialog(tk.Canvas): """ Rahmenloses Gothic-Einstellungs-Overlay (768p-kompatibel): - Sprache wechseln (DE / EN) - Animierter Switch: 'Hilfetexte bei Buttons anzeigen' / 'Show Help Texts on Button Hover' - Animierter Switch: 'Einfacher Klick zum Schieben' / 'Single tap to move' - Zwei Audio-Schieberegler (Ambiance & Soundeffekte) """ DLG_W = 500 DLG_H = 656 @classmethod def open( cls, root_window: tk.Tk, assets, sound_mgr, on_toggle_lang=None, ): existing = getattr(root_window, "_audio_overlay", None) if existing and existing.winfo_exists(): existing.on_toggle_lang = on_toggle_lang or existing.on_toggle_lang tk.Widget.tkraise(existing) return existing dlg = cls(root_window, assets, sound_mgr, on_toggle_lang=on_toggle_lang) root_window._audio_overlay = dlg return dlg def __init__( self, root_window: tk.Tk, assets, sound_mgr, on_toggle_lang=None, ): super().__init__( root_window, width=self.DLG_W, height=self.DLG_H, bg=BG_COLOR, bd=0, highlightthickness=0, ) self.root_window = root_window self.assets = assets self.sound_mgr = sound_mgr self.on_toggle_lang = on_toggle_lang or self._resolve_fallback_lang_toggler() self._bg_scaled_pil: Image.Image | None = None self._photo_refs: list[ImageTk.PhotoImage] = [] self._pct_photos: dict[str, ImageTk.PhotoImage] = {} self._dragging_key: str | None = None self._sliders: dict[str, dict] = {} self.place(relx=0.5, rely=0.5, anchor="center") tk.Widget.tkraise(self) self._build_scene() def _resolve_fallback_lang_toggler(self): ctrl = getattr(self.root_window, "_app_controller", None) if ctrl is not None and hasattr(ctrl, "toggle_language"): return lambda: ctrl.toggle_language(None) return None def _crop_dialog_bg_patch(self, cx: int, cy: int, pw: int, ph: int) -> ImageTk.PhotoImage | None: if self._bg_scaled_pil is None: return None x1 = max(0, cx - pw // 2) y1 = max(0, cy - ph // 2) x2 = min(self.DLG_W, x1 + pw) y2 = min(self.DLG_H, y1 + ph) if x2 <= x1 or y2 <= y1: return None return ImageTk.PhotoImage(self._bg_scaled_pil.crop((x1, y1, x2, y2))) def _build_scene(self): self.delete("all") self._photo_refs.clear() self._sliders.clear() w, h = self.DLG_W, self.DLG_H # 1. Hochformat-Hintergrund + box_big.png Zierrahmen bg_src = self.assets.raw_bg_vertical or self.assets.raw_bg_horizontal or self.assets.raw_bg if bg_src is not None: self._bg_scaled_pil = bg_src.resize((w, h), Image.Resampling.LANCZOS) bg_ph = ImageTk.PhotoImage(self._bg_scaled_pil) self._photo_refs.append(bg_ph) self.create_image(0, 0, image=bg_ph, anchor="nw") outer_box = self.assets.get_box_photo("box_big", (w - 6, h - 6)) if outer_box: self._photo_refs.append(outer_box) self.create_image(w // 2, h // 2, image=outer_box, anchor="center") # 2. Titel & Untertitel title_ph = self.assets.get_goth_text_photo( t("settings_title"), size_px=FONT_SIZE_TITLE_LG - 2, color=FONT_COLOR_TITLE_MAIN, max_width=w - 60, ) sub_ph = self.assets.get_goth_text_photo( t("settings_subtitle"), size_px=FONT_SIZE_SMALL_MIN, color=FONT_COLOR_BODY_MUTED, max_width=w - 70, ) self._photo_refs.extend([title_ph, sub_ph]) self.create_image(w // 2, 36, image=title_ph, anchor="center") self.create_image(w // 2, 64, image=sub_ph, anchor="center") # 3. Sprach-Box self._create_language_box(cy=118) # 4. Zwei Bedienhilfe-Switches (Hover-Hilfetexte & Einfacher Klick zum Schieben) self._create_switch_box( cy=196, title_txt=t("settings_hover_help_title"), desc_txt=t("settings_hover_help_desc"), initial_state=is_button_hover_help_enabled(), on_toggle=lambda state: set_button_hover_help_enabled(state, persist=True), ) self._create_switch_box( cy=274, title_txt=t("settings_single_tap_title"), desc_txt=t("settings_single_tap_desc"), initial_state=is_single_tap_to_move_enabled(), on_toggle=lambda state: set_single_tap_to_move_enabled(state, persist=True), ) # 5. Zwei Audio-Slider-Boxen self._create_goth_slider( key="amb", cy=372, 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._create_goth_slider( key="sfx", cy=482, 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), ) # 6. Untere Aktions-Buttons btn_norm = self.assets.raw_btn_default btn_purp = self.assets.raw_btn_purple btn_fire = self.assets.raw_btn_fire or btn_purp btn_test = ImageButton( self, t("audio_test_btn"), btn_norm, btn_purp, lambda: self.sound_mgr.play("deal"), sound_mgr=None, width=168, height=40, style="amethyst", ) btn_done = ImageButton( self, t("audio_done_btn"), btn_fire, btn_fire, self.close, sound_mgr=self.sound_mgr, width=168, height=40, style="fire", ) self.create_window(42, h - 44, window=btn_test, anchor="w") self.create_window(w - 42, h - 44, window=btn_done, anchor="e") self.bind("", self._on_mouse_down) self.bind("", self._on_mouse_drag) self.bind("", lambda _e: setattr(self, "_dragging_key", None)) def _create_language_box(self, cy: int): box_w, box_h = self.DLG_W - 72, 68 cx = self.DLG_W // 2 x_left = cx - box_w // 2 + 22 x_right = cx + box_w // 2 - 20 sec_box = self.assets.get_box_photo("box_purple", (box_w, box_h)) if sec_box: self._photo_refs.append(sec_box) self.create_image(cx, cy, image=sec_box, anchor="center") lbl_ph = self.assets.get_goth_text_photo( t("settings_lang_title"), size_px=FONT_SIZE_HEADING - 4, color=FONT_COLOR_BODY_DEFAULT, align="left", ) desc_ph = self.assets.get_goth_text_photo( t("settings_lang_desc"), size_px=FONT_SIZE_SMALL_MIN, color=FONT_COLOR_BODY_MUTED, max_width=box_w - 180, align="left", ) self._photo_refs.extend([lbl_ph, desc_ph]) self.create_image(x_left, cy - 11, image=lbl_ph, anchor="w") self.create_image(x_left, cy + 12, image=desc_ph, anchor="w") btn_norm = self.assets.raw_btn_default btn_purp = self.assets.raw_btn_purple btn_lang = ImageButton( self, t("settings_lang_btn_toggle"), btn_norm, btn_purp, self._handle_language_toggle, sound_mgr=self.sound_mgr, width=142, height=34, enabled=callable(self.on_toggle_lang), style="amethyst", ) self.create_window(x_right, cy, window=btn_lang, anchor="e") def _create_switch_box( self, cy: int, title_txt: str, desc_txt: str, initial_state: bool, on_toggle, ): box_w, box_h = self.DLG_W - 72, 70 cx = self.DLG_W // 2 x_left = cx - box_w // 2 + 22 x_right = cx + box_w // 2 - 20 sec_box = self.assets.get_box_photo("box_purple", (box_w, box_h)) if sec_box: self._photo_refs.append(sec_box) self.create_image(cx, cy, image=sec_box, anchor="center") lbl_ph = self.assets.get_goth_text_photo( title_txt, size_px=FONT_SIZE_BODY + 1, color=FONT_COLOR_BODY_DEFAULT, max_width=box_w - 144, align="left", ) desc_ph = self.assets.get_goth_text_photo( desc_txt, size_px=FONT_SIZE_SMALL_MIN, color=FONT_COLOR_BODY_MUTED, max_width=box_w - 144, align="left", ) self._photo_refs.extend([lbl_ph, desc_ph]) self.create_image(x_left, cy - 11, image=lbl_ph, anchor="w") self.create_image(x_left, cy + 13, image=desc_ph, anchor="w") sw_w, sw_h = 92, 38 sw_cx = x_right - sw_w // 2 patch_ph = self._crop_dialog_bg_patch(sw_cx, cy, sw_w, sw_h) if patch_ph is not None: self._photo_refs.append(patch_ph) switch_wgt = GothSwitch( self, value=initial_state, on_change=on_toggle, sound_mgr=self.sound_mgr, width=sw_w, height=sw_h, bg_photo=patch_ph, ) self.create_window(x_right, cy, window=switch_wgt, anchor="e") def _handle_language_toggle(self): if not callable(self.on_toggle_lang): return root = self.root_window assets = self.assets snd = self.sound_mgr toggle_cb = self.on_toggle_lang self.close() toggle_cb() if root.winfo_exists(): root.after( 15, lambda: AudioSettingsDialog.open( root, assets, snd, on_toggle_lang=toggle_cb ), ) def _create_goth_slider( self, key: str, cy: int, label_txt: str, desc_txt: str, initial_vol: float, on_change, ): box_w, box_h = self.DLG_W - 72, 98 cx = self.DLG_W // 2 x_left = cx - box_w // 2 + 22 x_right = cx + box_w // 2 - 22 sec_box = self.assets.get_box_photo("box_purple", (box_w, box_h)) if sec_box: self._photo_refs.append(sec_box) self.create_image(cx, cy, image=sec_box, anchor="center") lbl_ph = self.assets.get_goth_text_photo( label_txt, size_px=FONT_SIZE_HEADING - 4, color=FONT_COLOR_BODY_DEFAULT, align="left", ) desc_ph = self.assets.get_goth_text_photo( desc_txt, size_px=FONT_SIZE_SMALL_MIN, color=FONT_COLOR_BODY_MUTED, max_width=box_w - 44, align="left", ) self._photo_refs.extend([lbl_ph, desc_ph]) self.create_image(x_left, cy - 26, image=lbl_ph, anchor="w") self.create_image(x_left, cy - 3, image=desc_ph, anchor="w") pct_item = self.create_image(x_right, cy - 26, anchor="e") track_y = cy + 24 track_x1 = x_left + 4 track_x2 = x_right - 4 self.create_rectangle( track_x1, track_y - 4, track_x2, track_y + 4, fill="#09070e", outline=SUBTLE_BORDER, width=1, ) fill_id = self.create_rectangle( track_x1, track_y - 3, track_x1, track_y + 3, fill=UI_ACCENT_AMETHYST, outline="", ) knob_id = self.create_polygon( 0, 0, 0, 0, 0, 0, 0, 0, fill=UI_ACCENT_GOLD, outline="#0d0b12", width=1, ) self._sliders[key] = { "x1": track_x1, "x2": track_x2, "y": track_y, "fill_id": fill_id, "knob_id": knob_id, "pct_item": pct_item, "val": max(0.0, min(1.0, float(initial_vol))), "on_change": on_change, } self._update_slider_visual(key, self._sliders[key]["val"], notify=False) def _update_slider_visual(self, key: str, val: float, notify: bool = True): s = self._sliders[key] val = max(0.0, min(1.0, val)) s["val"] = val kx = s["x1"] + (s["x2"] - s["x1"]) * val ky = s["y"] self.coords(s["fill_id"], s["x1"] + 1, ky - 3, kx, ky + 3) r = 9 self.coords(s["knob_id"], kx, ky - r, kx + r, ky, kx, ky + r, kx - r, ky) pct_txt = f"{int(round(val * 100))} %" pct_ph = self.assets.get_goth_text_photo( pct_txt, size_px=FONT_SIZE_BODY + 2, color=FONT_COLOR_HEADING_SUB, align="right", ) self._pct_photos[key] = pct_ph self.itemconfigure(s["pct_item"], image=pct_ph) if notify: s["on_change"](val) def _on_mouse_down(self, event): for key, s in self._sliders.items(): if (s["x1"] - 12) <= event.x <= (s["x2"] + 12) and abs(event.y - s["y"]) <= 18: self._dragging_key = key ratio = (event.x - s["x1"]) / max(1.0, float(s["x2"] - s["x1"])) self._update_slider_visual(key, ratio, notify=True) break def _on_mouse_drag(self, event): if self._dragging_key and self._dragging_key in self._sliders: s = self._sliders[self._dragging_key] ratio = (event.x - s["x1"]) / max(1.0, float(s["x2"] - s["x1"])) self._update_slider_visual(self._dragging_key, ratio, notify=True) def close(self): if self.winfo_exists(): self.destroy() if getattr(self.root_window, "_audio_overlay", None) is self: self.root_window._audio_overlay = None SettingsDialog = AudioSettingsDialog