the_goth_cardgame_collection/games/solitaire/base/solitaire_modal_dialog.py

144 lines
5.0 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
class SolitaireModalDialog(tk.Canvas):
"""
Rahmenloses Gothic-Modal auf background_vertical.jpg + box_big.png
für Wiederaufnahme-, Aufgeben-, Deadlock- und Sieg-Dialoge (inkl. win.gif) in Solitaire.
"""
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 = "",
):
GothDropdown.close_any_open_popup()
frames = ctx.assets.get_gif_frames(gif_key, size=(150, 84)) if gif_key else []
dlg_w = 590 if len(buttons) >= 3 else 500
dlg_h = 355 if frames else 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._gif_job: str | None = None
self._gif_item: int | None = None
self.place(relx=0.5, rely=0.52, anchor="center")
self.bind("<Destroy>", self._on_destroy)
self._build_scene(title_txt, body_txt, buttons, is_danger, frames)
def _on_destroy(self, event=None):
if event is not None and event.widget is not self:
return
if self._gif_job is not None:
try:
self.after_cancel(self._gif_job)
except Exception:
pass
self._gif_job = None
def _animate_gif(self, frames: list, idx: int):
if not self.winfo_exists() or self._gif_item is None:
return
self.itemconfigure(self._gif_item, image=frames[idx % len(frames)])
self._gif_job = self.after(90, self._animate_gif, frames, (idx + 1) % len(frames))
def _build_scene(
self,
title_txt: str,
body_txt: str,
buttons: list,
is_danger: bool,
frames: list,
):
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 = 26
if frames:
self._gif_item = self.create_image(w // 2, cur_y + 42, image=frames[0], anchor="center")
cur_y += 92
if len(frames) > 1:
self._animate_gif(frames, 0)
else:
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