the_goth_cardgame_collection/games/solitaire/base/solitaire_menu_chrome.py

111 lines
4.0 KiB
Python

# -*- coding: utf-8 -*-
# games/solitaire/base/solitaire_menu_chrome.py
from __future__ import annotations
import tkinter as tk
from PIL import Image, ImageTk
from resources.code_res.game_constants import (
BG_COLOR,
FONT_COLOR_BODY_DEFAULT,
FONT_COLOR_STATUS_LOSS,
FONT_COLOR_TITLE_MAIN,
FONT_SIZE_BODY,
FONT_SIZE_TITLE_LG,
)
from view.widgets.goth_dropdown import GothDropdown
from view.widgets.image_button import ImageButton
def create_menu_body_canvas(ctx, topbar_h: int = 56) -> tk.Canvas:
"""Erstellt ein vollflächiges Menü-Canvas unterhalb der GothTopbar, das background_*.jpg nahtlos zeigt."""
GothDropdown.close_any_open_popup()
cv = tk.Canvas(ctx.root, bg=BG_COLOR, bd=0, highlightthickness=0)
cv.place(x=0, y=topbar_h, relwidth=1.0, relheight=1.0, height=-topbar_h)
state = {"last": (0, 0), "bg_ph": None}
def _sync_bg(w: int, h: int):
if (w, h) == state["last"] or w < 80 or h < 80:
return
state["last"] = (w, h)
rw = max(w, ctx.root.winfo_width())
rh = max(h + topbar_h, ctx.root.winfo_height())
src = (
ctx.assets.raw_bg_vertical
if (rh > rw and ctx.assets.raw_bg_vertical is not None)
else (ctx.assets.raw_bg_horizontal or ctx.assets.raw_bg)
)
if src is None:
return
full_img = src.resize((rw, rh), Image.Resampling.LANCZOS)
cropped = full_img.crop((0, topbar_h, min(rw, w), min(rh, topbar_h + h)))
state["bg_ph"] = ImageTk.PhotoImage(cropped)
cv.delete("menu_bg")
cv.create_image(0, 0, image=state["bg_ph"], anchor="nw", tags="menu_bg")
cv.tag_lower("menu_bg")
cv._sync_bg = _sync_bg # type: ignore[attr-defined]
return cv
def open_goth_modal(
ctx,
title_txt: str,
body_txt: str,
buttons: list[tuple[str, str, object]],
is_danger: bool = False,
) -> tk.Canvas:
"""Rahmenloses Gothic-Modal auf background_vertical.jpg + box_big.png für Solitaire-Dialoge."""
GothDropdown.close_any_open_popup()
dlg_w = 560 if len(buttons) >= 3 else 490
dlg_h = 265
overlay = tk.Canvas(
ctx.root, width=dlg_w, height=dlg_h, bg=BG_COLOR, bd=0, highlightthickness=0
)
overlay.place(relx=0.5, rely=0.53, anchor="center")
refs: list = []
overlay._goth_refs = refs # type: ignore[attr-defined]
bg_src = ctx.assets.raw_bg_vertical or ctx.assets.raw_bg_horizontal or ctx.assets.raw_bg
if bg_src is not None:
bg_ph = ImageTk.PhotoImage(bg_src.resize((dlg_w, dlg_h), Image.Resampling.LANCZOS))
refs.append(bg_ph)
overlay.create_image(0, 0, image=bg_ph, anchor="nw")
box_ph = ctx.assets.get_box_photo("box_big", (dlg_w - 6, dlg_h - 6))
if box_ph:
refs.append(box_ph)
overlay.create_image(dlg_w // 2, dlg_h // 2, image=box_ph, anchor="center")
t_col = FONT_COLOR_STATUS_LOSS if is_danger else FONT_COLOR_TITLE_MAIN
t_ph = ctx.assets.get_goth_text_photo(title_txt, FONT_SIZE_TITLE_LG - 4, t_col, max_width=dlg_w - 60)
b_ph = ctx.assets.get_goth_text_photo(
body_txt, FONT_SIZE_BODY, FONT_COLOR_BODY_DEFAULT, max_width=dlg_w - 72, align="center"
)
refs.extend([t_ph, b_ph])
overlay.create_image(dlg_w // 2, 52, image=t_ph, anchor="center")
overlay.create_image(dlg_w // 2, 124, image=b_ph, anchor="center")
btn_norm = ctx.assets.raw_btn_default
btn_purp = ctx.assets.raw_btn_purple
btn_fire = ctx.assets.raw_btn_fire or btn_purp
bw = 160 if len(buttons) >= 3 else 182
gap = 10
total_w = len(buttons) * bw + max(0, len(buttons) - 1) * gap
cur_x = (dlg_w - total_w) // 2
for lbl, style, cmd in buttons:
p_n = btn_fire if style == "fire" else btn_norm
p_h = btn_fire if style == "fire" else btn_purp
b = ImageButton(
overlay, lbl, p_n, p_h, cmd,
sound_mgr=ctx.sounds, width=bw, height=38, style=style
)
overlay.create_window(cur_x + bw // 2, dlg_h - 46, window=b, anchor="center")
cur_x += bw + gap
return overlay