the_goth_cardgame_collection/view/menus/settings_cards.py

331 lines
11 KiB
Python
Raw Permalink Normal View History

# -*- 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 kollisionsfrei auf das SettingsView-Canvas."""
@staticmethod
def shorten_path(p_str: str, max_chars: int = 78) -> 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_stacked_labels(
cv: tk.Canvas,
x_left: int,
cy: int,
box_h: int,
lbl_ph,
desc_ph,
line_gap: int = -6,
) -> None:
"""Stapelt Titel und (1- bis 3-zeilige) Beschreibung sauber zentriert ohne Überlappung."""
lh = lbl_ph.height() if lbl_ph is not None else 26
dh = desc_ph.height() if desc_ph is not None else 24
total_h = lh + line_gap + dh
y0 = max(cy - box_h // 2 + 8, cy - total_h // 2)
if lbl_ph is not None:
cv.create_image(x_left, y0, image=lbl_ph, anchor="nw", tags="settings_fg")
if desc_ph is not None:
cv.create_image(
x_left, y0 + lh + line_gap, image=desc_ph, anchor="nw", tags="settings_fg"
)
@classmethod
def draw_lang_card(
cls,
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")
avail_txt_w = max(140, box_w - 220)
lbl_ph = assets.get_goth_text_photo(
t("settings_lang_title"),
FONT_SIZE_BODY + 2,
FONT_COLOR_TITLE_MAIN,
max_width=avail_txt_w,
align="left",
)
desc_ph = assets.get_goth_text_photo(
t("settings_lang_desc"),
FONT_SIZE_SMALL_MIN,
FONT_COLOR_BODY_MUTED,
max_width=avail_txt_w,
align="left",
)
photo_refs.extend([lbl_ph, desc_ph])
cls._draw_stacked_labels(cv, x_left, cy, box_h, lbl_ph, desc_ph)
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")
@classmethod
def draw_font_card(
cls,
cv: tk.Canvas,
assets,
photo_refs: list,
cx: int,
cy: int,
box_w: int,
box_h: int,
dd_font,
) -> 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")
dd_w = min(252, max(196, int(box_w * 0.44)))
if dd_font is not None and dd_font.winfo_exists():
if getattr(dd_font, "_width", dd_w) != dd_w:
dd_font._width = dd_w
dd_font.configure(width=dd_w)
dd_font._render_closed()
cv.create_window(x_right, cy, window=dd_font, anchor="e", tags="settings_fg")
avail_txt_w = max(130, box_w - dd_w - 64)
lbl_ph = assets.get_goth_text_photo(
t("settings_font_title"),
FONT_SIZE_BODY + 2,
FONT_COLOR_TITLE_MAIN,
max_width=avail_txt_w,
align="left",
)
desc_ph = assets.get_goth_text_photo(
t("settings_font_desc"),
FONT_SIZE_SMALL_MIN,
FONT_COLOR_BODY_MUTED,
max_width=avail_txt_w,
align="left",
)
photo_refs.extend([lbl_ph, desc_ph])
cls._draw_stacked_labels(cv, x_left, cy, box_h, lbl_ph, desc_ph)
@classmethod
def draw_switch_card(
cls,
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")
avail_txt_w = max(140, box_w - 158)
lbl_ph = assets.get_goth_text_photo(
title_txt,
FONT_SIZE_BODY + 2,
FONT_COLOR_TITLE_MAIN,
max_width=avail_txt_w,
align="left",
)
desc_ph = assets.get_goth_text_photo(
desc_txt,
FONT_SIZE_SMALL_MIN,
FONT_COLOR_BODY_MUTED,
max_width=avail_txt_w,
align="left",
)
photo_refs.extend([lbl_ph, desc_ph])
cls._draw_stacked_labels(cv, x_left, cy, box_h, lbl_ph, desc_ph)
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_top = cy - box_h // 2
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")
# Rechts 84px Platz für die Prozentzahl lassen, damit nichts überlappt
avail_txt_w = max(160, box_w - 132)
lbl_ph = assets.get_goth_text_photo(
label_txt,
FONT_SIZE_BODY + 2,
FONT_COLOR_TITLE_MAIN,
max_width=avail_txt_w,
align="left",
)
desc_ph = assets.get_goth_text_photo(
desc_txt,
max(14, FONT_SIZE_SMALL_MIN - 1),
FONT_COLOR_BODY_MUTED,
max_width=max(160, box_w - 56),
align="left",
)
photo_refs.extend([lbl_ph, desc_ph])
lbl_y = box_top + 10
desc_y = lbl_y + lbl_ph.height() - 8
cv.create_image(x_left, lbl_y, image=lbl_ph, anchor="nw", tags="settings_fg")
cv.create_image(x_left, desc_y, image=desc_ph, anchor="nw", tags="settings_fg")
pct_item = cv.create_image(
x_right, lbl_y + lbl_ph.height() // 2, anchor="e", tags="settings_fg"
)
track_y = cy + box_h // 2 - 19
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(48, box_w // 11)
)
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
)
avail_path_w = max(
200, (box_w - 486) if (horizontal_btns and box_w >= 860) else (box_w - 56)
)
path_ph = assets.get_goth_text_photo(
sub_txt, FONT_SIZE_SMALL_MIN, sub_col, max_width=avail_path_w, align="left"
)
photo_refs.extend([hdr_ph, path_ph])
if horizontal_btns and box_w >= 860:
cls._draw_stacked_labels(cv, x_left, cy, box_h, hdr_ph, path_ph)
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:
y0 = cy - box_h // 2 + 12
cv.create_image(x_left, y0, image=hdr_ph, anchor="nw", tags="settings_fg")
cv.create_image(
x_left, y0 + hdr_ph.height() - 6, image=path_ph, anchor="nw", tags="settings_fg"
)
if btn_load is not None and btn_load.winfo_exists():
cv.create_window(
x_left, cy + box_h // 2 - 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 + box_h // 2 - 26, window=btn_move, anchor="e", tags="settings_fg"
)