# -*- coding: utf-8 -*- # view/menus/settings_cards.py from __future__ import annotations import tkinter as tk from models.database import Database from resources.code_res.game_constants import ( FONT_COLOR_BODY_DEFAULT, FONT_COLOR_BODY_MUTED, FONT_COLOR_STATUS_LOSS, FONT_COLOR_STATUS_WIN, FONT_COLOR_TITLE_MAIN, FONT_SIZE_BODY, FONT_SIZE_SMALL_MIN, SUBTLE_BORDER, UI_ACCENT_AMETHYST, UI_ACCENT_GOLD, ) from resources.code_res.i18n import t class SettingsCardsRenderer: """Zeichnet die einzelnen Einstellungs-Rahmenboxen auf das SettingsView-Canvas.""" @staticmethod def shorten_path(p_str: str, max_chars: int = 72) -> str: if len(p_str) <= max_chars: return p_str keep = max_chars - 5 left = keep // 2 - 2 right = keep - left return f"{p_str[:left]}...{p_str[-right:]}" @staticmethod def draw_lang_card( cv: tk.Canvas, assets, photo_refs: list, cx: int, cy: int, box_w: int, box_h: int, btn_lang: tk.Widget | None, ) -> None: x_left = cx - box_w // 2 + 24 x_right = cx + box_w // 2 - 22 box_ph = assets.get_box_photo("box_purple", (box_w, box_h)) if box_ph: photo_refs.append(box_ph) cv.create_image(cx, cy, image=box_ph, anchor="center", tags="settings_fg") lbl_ph = assets.get_goth_text_photo( t("settings_lang_title"), FONT_SIZE_BODY + 2, FONT_COLOR_BODY_DEFAULT, align="left" ) desc_ph = assets.get_goth_text_photo( t("settings_lang_desc"), FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, max_width=max(120, box_w - 210), align="left", ) photo_refs.extend([lbl_ph, desc_ph]) cv.create_image(x_left, cy - 12, image=lbl_ph, anchor="w", tags="settings_fg") cv.create_image(x_left, cy + 13, image=desc_ph, anchor="w", tags="settings_fg") if btn_lang is not None and btn_lang.winfo_exists(): cv.create_window(x_right, cy, window=btn_lang, anchor="e", tags="settings_fg") @staticmethod def draw_switch_card( cv: tk.Canvas, assets, photo_refs: list, cx: int, cy: int, box_w: int, box_h: int, title_txt: str, desc_txt: str, switch_wgt: tk.Widget | None, ) -> None: x_left = cx - box_w // 2 + 24 x_right = cx + box_w // 2 - 22 box_ph = assets.get_box_photo("box_purple", (box_w, box_h)) if box_ph: photo_refs.append(box_ph) cv.create_image(cx, cy, image=box_ph, anchor="center", tags="settings_fg") lbl_ph = assets.get_goth_text_photo( title_txt, FONT_SIZE_BODY + 1, FONT_COLOR_BODY_DEFAULT, max_width=max(120, box_w - 156), align="left", ) desc_ph = assets.get_goth_text_photo( desc_txt, FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, max_width=max(120, box_w - 156), align="left", ) photo_refs.extend([lbl_ph, desc_ph]) cv.create_image(x_left, cy - 13, image=lbl_ph, anchor="w", tags="settings_fg") cv.create_image(x_left, cy + 13, image=desc_ph, anchor="w", tags="settings_fg") if switch_wgt is not None and switch_wgt.winfo_exists(): cv.create_window(x_right, cy, window=switch_wgt, anchor="e", tags="settings_fg") @staticmethod def draw_slider_card( cv: tk.Canvas, assets, photo_refs: list, sliders_dict: dict, key: str, cx: int, cy: int, box_w: int, box_h: int, label_txt: str, desc_txt: str, initial_vol: float, on_change, ) -> None: x_left = cx - box_w // 2 + 24 x_right = cx + box_w // 2 - 24 box_ph = assets.get_box_photo("box_purple", (box_w, box_h)) if box_ph: photo_refs.append(box_ph) cv.create_image(cx, cy, image=box_ph, anchor="center", tags="settings_fg") top_off = max(24, box_h // 4 + 4) lbl_ph = assets.get_goth_text_photo( label_txt, FONT_SIZE_BODY + 2, FONT_COLOR_BODY_DEFAULT, max_width=max(140, box_w - 130), align="left", ) desc_ph = assets.get_goth_text_photo( desc_txt, FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, max_width=max(140, box_w - 48), align="left", ) photo_refs.extend([lbl_ph, desc_ph]) cv.create_image(x_left, cy - top_off, image=lbl_ph, anchor="w", tags="settings_fg") cv.create_image(x_left, cy - 2, image=desc_ph, anchor="w", tags="settings_fg") pct_item = cv.create_image(x_right, cy - top_off, anchor="e", tags="settings_fg") track_y = cy + max(24, box_h // 4 + 2) track_x1, track_x2 = x_left + 6, x_right - 6 cv.create_rectangle( track_x1, track_y - 5, track_x2, track_y + 5, fill="#09070e", outline=SUBTLE_BORDER, width=1, tags="settings_fg", ) fill_id = cv.create_rectangle( track_x1, track_y - 4, track_x1, track_y + 4, fill=UI_ACCENT_AMETHYST, outline="", tags="settings_fg", ) knob_id = cv.create_polygon( 0, 0, 0, 0, 0, 0, 0, 0, fill=UI_ACCENT_GOLD, outline="#0d0b12", width=1, tags="settings_fg", ) sliders_dict[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, } @classmethod def draw_user_data_card( cls, cv: tk.Canvas, assets, photo_refs: list, cx: int, cy: int, box_w: int, box_h: int, feedback_txt: str, feedback_err: bool, btn_load: tk.Widget | None, btn_move: tk.Widget | None, horizontal_btns: bool = True, ) -> None: x_left = cx - box_w // 2 + 26 x_right = cx + box_w // 2 - 26 box_ph = assets.get_box_photo("box_gold_double", (box_w, box_h)) if box_ph: photo_refs.append(box_ph) cv.create_image(cx, cy, image=box_ph, anchor="center", tags="settings_fg") hdr_ph = assets.get_goth_text_photo( t("db_settings_title"), FONT_SIZE_BODY + 2, FONT_COLOR_TITLE_MAIN, align="left" ) cur_db_str = cls.shorten_path(str(Database.get_user_db_path()), max_chars=max(42, box_w // 12)) sub_txt = feedback_txt if feedback_txt else t("db_Active_path_lbl", cur_db_str) sub_col = ( (FONT_COLOR_STATUS_LOSS if feedback_err else FONT_COLOR_STATUS_WIN) if feedback_txt else FONT_COLOR_BODY_DEFAULT ) path_ph = assets.get_goth_text_photo( sub_txt, FONT_SIZE_SMALL_MIN + 1, sub_col, max_width=max(180, (box_w - 490) if (horizontal_btns and box_w >= 860) else (box_w - 56)), align="left", ) photo_refs.extend([hdr_ph, path_ph]) if horizontal_btns and box_w >= 860: cv.create_image(x_left, cy - 14, image=hdr_ph, anchor="w", tags="settings_fg") cv.create_image(x_left, cy + 14, image=path_ph, anchor="w", tags="settings_fg") if btn_move is not None and btn_move.winfo_exists(): cv.create_window(x_right, cy, window=btn_move, anchor="e", tags="settings_fg") if btn_load is not None and btn_load.winfo_exists(): cv.create_window(x_right - 220, cy, window=btn_load, anchor="e", tags="settings_fg") else: cv.create_image(x_left, cy - 30, image=hdr_ph, anchor="w", tags="settings_fg") cv.create_image(x_left, cy - 6, image=path_ph, anchor="w", tags="settings_fg") if btn_load is not None and btn_load.winfo_exists(): cv.create_window(x_left, cy + 26, window=btn_load, anchor="w", tags="settings_fg") if btn_move is not None and btn_move.winfo_exists(): cv.create_window(x_right, cy + 26, window=btn_move, anchor="e", tags="settings_fg")