the_goth_cardgame_collection/games/solitaire/base/solitaire_modal_dialog.py

118 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
# games/solitaire/base/solitaire_modal_dialog.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
from view.widgets.modal_veil import ModalVeil
class SolitaireModalDialog(tk.Canvas):
"""
Rahmenloses Gothic-Modal (ohne eingebettetes GIF), das über allen UI-Elementen
schwebt, das Spielfeld dahinter abdunkelt und alle übrigen Buttons sperrt.
"""
def __init__(
self,
ctx,
title_txt: str,
body_txt: str,
buttons: list[tuple[str, str, object, bool] | tuple[str, str, object]],
is_danger: bool = False,
gif_key: str = "", # abwärtskompatibel ignoriert (Animation läuft vorab)
):
GothDropdown.close_any_open_popup()
_ = gif_key
dlg_w = 590 if len(buttons) >= 3 else 500
dlg_h = 268
super().__init__(
ctx.root,
width=dlg_w,
height=dlg_h,
bg=BG_COLOR,
bd=0,
highlightthickness=0,
)
self.ctx = ctx
self._dlg_w = dlg_w
self._dlg_h = dlg_h
self._photo_refs: list[ImageTk.PhotoImage] = []
self.place(relx=0.5, rely=0.5, anchor="center")
self._build_scene(title_txt, body_txt, buttons, is_danger)
self._veil = ModalVeil(ctx.root, ctx.assets, modal_widget=self)
def _build_scene(
self,
title_txt: str,
body_txt: str,
buttons: list,
is_danger: bool,
):
w, h = self._dlg_w, self._dlg_h
assets = self.ctx.assets
bg_src = assets.raw_bg_vertical or assets.raw_bg_horizontal or assets.raw_bg
if bg_src is not None:
bg_ph = ImageTk.PhotoImage(bg_src.resize((w, h), Image.Resampling.LANCZOS))
self._photo_refs.append(bg_ph)
self.create_image(0, 0, image=bg_ph, anchor="nw")
box_ph = assets.get_box_photo("box_big", (w - 6, h - 6))
if box_ph:
self._photo_refs.append(box_ph)
self.create_image(w // 2, h // 2, image=box_ph, anchor="center")
cur_y = 38
t_col = FONT_COLOR_STATUS_LOSS if is_danger else FONT_COLOR_TITLE_MAIN
clean_body = str(body_txt or "").replace("•", "-")
t_ph = assets.get_goth_text_photo(title_txt, FONT_SIZE_TITLE_LG - 4, t_col, max_width=w - 64)
b_ph = assets.get_goth_text_photo(
clean_body, FONT_SIZE_BODY, FONT_COLOR_BODY_DEFAULT, max_width=w - 76, align="center"
)
self._photo_refs.extend([t_ph, b_ph])
self.create_image(w // 2, cur_y + t_ph.height() // 2, image=t_ph, anchor="center")
cur_y += t_ph.height() + 14
self.create_image(w // 2, cur_y + b_ph.height() // 2, image=b_ph, anchor="center")
btn_norm = assets.raw_btn_default
btn_purp = assets.raw_btn_purple
btn_fire = assets.raw_btn_fire or btn_purp
req_w = 152 if len(buttons) >= 3 else 176
gap = 10
created_btns: list[ImageButton] = []
for entry in buttons:
if len(entry) == 4:
lbl, style, cmd, enabled = entry
else:
lbl, style, cmd = entry
enabled = True
p_n = btn_fire if style == "fire" else btn_norm
p_h = btn_fire if style == "fire" else btn_purp
btn = ImageButton(
self, lbl, p_n, p_h, cmd,
sound_mgr=self.ctx.sounds, width=req_w, height=36,
enabled=bool(enabled), style=style,
)
created_btns.append(btn)
total_w = sum(b.pixel_width for b in created_btns) + max(0, len(created_btns) - 1) * gap
cur_x = (w - total_w) // 2
for btn in created_btns:
bw = btn.pixel_width
self.create_window(cur_x + bw // 2, h - 44, window=btn, anchor="center")
cur_x += bw + gap