the_goth_cardgame_collection/games/blackjack/views/round_result_dialog.py

322 lines
13 KiB
Python
Raw Normal View History

2026-11-14 04:25:51 +00:00
# -*- coding: utf-8 -*-
# games/blackjack/views/round_result_dialog.py
from __future__ import annotations
2026-11-14 04:25:51 +00:00
import tkinter as tk
from PIL import Image, ImageTk
2026-11-14 04:25:51 +00:00
from resources.code_res.game_constants import (
BG_COLOR,
FONT_COLOR_BODY_DEFAULT,
FONT_COLOR_BODY_MUTED,
FONT_COLOR_HEADING_SUB,
FONT_COLOR_STATUS_LOSS,
FONT_COLOR_STATUS_WIN,
FONT_COLOR_TITLE_MAIN,
FONT_SIZE_BODY,
FONT_SIZE_HEADING,
FONT_SIZE_SMALL_MIN,
FONT_SIZE_TITLE_LG,
OUTCOME_GIF_SIZE,
SUBTLE_BORDER,
2026-11-14 04:25:51 +00:00
)
from resources.code_res.i18n import t
from view.widgets.image_button import ImageButton
SHOWDOWN_MINI_CARD_SIZE = (48, 70)
2026-11-14 04:25:51 +00:00
class BlackjackResultDialog(tk.Canvas):
"""
Rahmenloses Gothic-Showdown-Overlay für Blackjack:
Hintergrund background_vertical.jpg + box_big.png, Gewinner-Sitze in box_gold_smal.png,
übrige Sitze in box_small_purple.png und kompakte Kartenfächer für 768p-Bildschirme.
"""
2026-11-14 04:25:51 +00:00
def __init__(
self,
parent,
assets,
sounds,
result_data: dict,
on_next_round,
on_back_to_menu,
):
hands_list = result_data.get("showdown_hands", [])
cols = min(3, max(1, len(hands_list)))
rows = (len(hands_list) + 2) // 3 if hands_list else 0
has_gif = result_data.get("player_won") is not None
extra_lines = len(result_data.get("other_gains", []))
dlg_w = max(560, min(830, cols * 250 + 64))
dlg_h = (
(116 if has_gif else 54)
+ (48 if result_data.get("reason_text") else 8)
+ (rows * 140 + 28 if rows else 0)
+ 92 + min(2, extra_lines) * 20
+ (24 if result_data.get("new_level_title") else 0)
+ 62
)
# Sicherstellen, dass das Dialogfenster auch auf 768p-Displays komplett sichtbar bleibt
try:
max_h = max(460, parent.winfo_toplevel().winfo_height() - 74)
dlg_h = min(dlg_h, max_h)
except Exception:
pass
2026-11-14 04:25:51 +00:00
super().__init__(
parent,
width=dlg_w,
height=dlg_h,
bg=BG_COLOR,
bd=0,
highlightthickness=0,
2026-11-14 04:25:51 +00:00
)
self.assets = assets
self.sounds = sounds
self.data = result_data
self.on_next_round = on_next_round
self.on_back_to_menu = on_back_to_menu
self._dlg_w = dlg_w
self._dlg_h = dlg_h
self._gif_job: str | None = None
self._gif_item: int | None = None
self._photo_refs: list = []
2026-11-14 04:25:51 +00:00
self.place(relx=0.5, rely=0.5, anchor="center")
self.tk.call("raise", self._w)
self._build_scene()
2026-11-14 04:25:51 +00:00
def _build_scene(self):
w, h = self._dlg_w, self._dlg_h
cx = w // 2
self._photo_refs.clear()
# 1. Hintergrund + äußerer Zierrahmen box_big.png
bg_src = self.assets.raw_bg_vertical or self.assets.raw_bg_horizontal or self.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")
2026-11-14 04:25:51 +00:00
outer_box = self.assets.get_box_photo("box_big", (w - 6, h - 6))
if outer_box:
self._photo_refs.append(outer_box)
self.create_image(cx, h // 2, image=outer_box, anchor="center")
cur_y = 20
# 2. Win/Lose GIF + Headline
p_won = self.data.get("player_won")
gif_key = "win_gif" if p_won is True else ("lose_gif" if p_won is False else "")
frames = self.assets.get_gif_frames(gif_key, size=(145, 80)) if gif_key else []
if frames:
self._gif_item = self.create_image(cx, cur_y + 40, image=frames[0], anchor="center")
cur_y += 84
if len(frames) > 1:
self._animate_gif(frames, 0)
hl_col = (
FONT_COLOR_TITLE_MAIN
if p_won is True
else (FONT_COLOR_BODY_MUTED if p_won is None else FONT_COLOR_STATUS_LOSS)
2026-11-14 04:25:51 +00:00
)
hl_ph = self.assets.get_goth_text_photo(
self.data["headline"], FONT_SIZE_TITLE_LG - 4, hl_col, max_width=w - 60
)
self._photo_refs.append(hl_ph)
self.create_image(cx, cur_y + hl_ph.height() // 2, image=hl_ph, anchor="center")
cur_y += hl_ph.height() + 6
2026-11-14 04:25:51 +00:00
# 3. Begründung im schmalen Gold-Doppelrahmen
reason_txt = self.data.get("reason_text", "")
if reason_txt:
r_ph = self.assets.get_goth_text_photo(
reason_txt, FONT_SIZE_SMALL_MIN + 1, FONT_COLOR_HEADING_SUB, max_width=w - 92
)
self._photo_refs.append(r_ph)
rb_h = max(36, r_ph.height() + 14)
rb_cy = cur_y + rb_h // 2
rb_box = self.assets.get_box_photo("box_gold_double", (w - 56, rb_h))
if rb_box:
self._photo_refs.append(rb_box)
self.create_image(cx, rb_cy, image=rb_box, anchor="center")
self.create_image(cx, rb_cy, image=r_ph, anchor="center")
cur_y += rb_h + 8
# 4. Showdown-Karten aller Teilnehmer
hands_list = self.data.get("showdown_hands", [])
if hands_list:
sec_ph = self.assets.get_goth_text_photo(
t("bj_showdown_section_title"), FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, align="left"
)
self._photo_refs.append(sec_ph)
self.create_image(32, cur_y + 10, image=sec_ph, anchor="w")
cur_y += 22
max_cols = 3
rows = (len(hands_list) + max_cols - 1) // max_cols
seat_w = min(242, (w - 64 - (max_cols - 1) * 10) // max(1, min(max_cols, len(hands_list))))
seat_h = 128 if rows > 1 else 136
win_box = self.assets.get_box_photo("box_gold_small", (seat_w, seat_h))
std_box = self.assets.get_box_photo("box_purple", (seat_w, seat_h))
if win_box:
self._photo_refs.append(win_box)
if std_box:
self._photo_refs.append(std_box)
for idx, entry in enumerate(hands_list):
r_i, c_i = divmod(idx, max_cols)
in_row = min(max_cols, len(hands_list) - r_i * max_cols)
row_total_w = in_row * seat_w + (in_row - 1) * 10
sx = (w - row_total_w) // 2 + c_i * (seat_w + 10) + seat_w // 2
sy = cur_y + r_i * (seat_h + 6) + seat_h // 2
is_win = entry.get("is_winner", False)
is_dead = entry.get("busted", False) or entry.get("folded", False)
frame_ph = win_box if is_win else std_box
if frame_ph:
self.create_image(sx, sy, image=frame_ph, anchor="center")
o_col = (
FONT_COLOR_TITLE_MAIN
if is_win
else (FONT_COLOR_HEADING_SUB if entry.get("is_dealer") else FONT_COLOR_BODY_DEFAULT)
)
o_ph = self.assets.get_goth_text_photo(
entry["owner_label"], FONT_SIZE_SMALL_MIN + 1, o_col, max_width=seat_w - 24
)
self._photo_refs.append(o_ph)
self.create_image(sx, sy - seat_h // 2 + 20, image=o_ph, anchor="center")
# Überlappender Mini-Kartenfächer
c_imgs = entry.get("card_images", [])
c_photos = []
for img_name in c_imgs:
ph = self.assets.get_card_photo(img_name, size=SHOWDOWN_MINI_CARD_SIZE)
if ph:
self._photo_refs.append(ph)
c_photos.append(ph)
if c_photos:
cw_c, ch_c = SHOWDOWN_MINI_CARD_SIZE
n_c = len(c_photos)
step = 24
if n_c > 1 and (cw_c + (n_c - 1) * step) > (seat_w - 28):
step = max(14, (seat_w - 28 - cw_c) // (n_c - 1))
fan_w = cw_c + (n_c - 1) * step if n_c > 1 else cw_c
start_cx = sx - fan_w // 2
card_top = sy - ch_c // 2 + 2
for ci, cph in enumerate(c_photos):
self.create_image(start_cx + ci * step, card_top, image=cph, anchor="nw")
rk_col = (
FONT_COLOR_STATUS_WIN
if is_win
else (FONT_COLOR_STATUS_LOSS if is_dead else FONT_COLOR_BODY_DEFAULT)
)
rk_ph = self.assets.get_goth_text_photo(
entry["rank_label"], FONT_SIZE_SMALL_MIN, rk_col, max_width=seat_w - 22
)
self._photo_refs.append(rk_ph)
self.create_image(sx, sy + seat_h // 2 - 18, image=rk_ph, anchor="center")
cur_y += rows * (seat_h + 6) + 6
# 5. Gold- & XP-Abrechnung
self.create_line(36, cur_y, w - 36, cur_y, fill=SUBTLE_BORDER, width=1)
cur_y += 10
2026-11-14 04:25:51 +00:00
p_net = self.data["player_net"]
if p_net > 0:
own_txt, own_col = t("bj_popup_your_gain", p_net), FONT_COLOR_STATUS_WIN
2026-11-14 04:25:51 +00:00
elif p_net < 0:
own_txt, own_col = t("bj_popup_your_loss", abs(p_net)), FONT_COLOR_STATUS_LOSS
2026-11-14 04:25:51 +00:00
else:
own_txt, own_col = t("bj_popup_your_even"), FONT_COLOR_BODY_DEFAULT
2026-11-14 04:25:51 +00:00
own_ph = self.assets.get_goth_text_photo(own_txt, FONT_SIZE_BODY + 2, own_col, max_width=w - 72)
self._photo_refs.append(own_ph)
self.create_image(cx, cur_y + own_ph.height() // 2, image=own_ph, anchor="center")
cur_y += own_ph.height() + 2
2026-11-14 04:25:51 +00:00
other_gains = self.data.get("other_gains", [])
if other_gains:
joined_others = " • ".join(other_gains)
oth_ph = self.assets.get_goth_text_photo(
joined_others, FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, max_width=w - 72
)
self._photo_refs.append(oth_ph)
self.create_image(cx, cur_y + oth_ph.height() // 2, image=oth_ph, anchor="center")
cur_y += oth_ph.height() + 2
2026-11-14 04:25:51 +00:00
xp_line = t("bj_popup_xp_earned", self.data["xp_gained"])
2026-11-14 04:25:51 +00:00
if self.data.get("new_level_title"):
xp_line = f"{xp_line} • {self.data['new_level_title']}"
xp_ph = self.assets.get_goth_text_photo(
xp_line, FONT_SIZE_BODY, FONT_COLOR_TITLE_MAIN if self.data.get("new_level_title") else FONT_COLOR_HEADING_SUB,
max_width=w - 72,
)
self._photo_refs.append(xp_ph)
self.create_image(cx, cur_y + xp_ph.height() // 2, image=xp_ph, anchor="center")
2026-11-14 04:25:51 +00:00
# 6. Untere Buttons
btn_norm = self.assets.raw_btn_default
btn_purp = self.assets.raw_btn_purple
btn_fire = self.assets.raw_btn_fire or btn_purp
2026-11-14 04:25:51 +00:00
btn_y = h - 34
can_next = self.data.get("can_continue", True)
can_menu = self.data.get("can_leave_menu", True)
2026-11-14 04:25:51 +00:00
if can_next and can_menu:
b_next = ImageButton(
self, t("bj_popup_next_round_btn"), btn_fire, btn_fire,
self._handle_next, sound_mgr=self.sounds, width=205, height=38, style="fire"
)
b_menu = ImageButton(
self, t("bj_popup_to_menu_btn"), btn_norm, btn_purp,
self._handle_menu, sound_mgr=self.sounds, width=175, height=38, style="amethyst"
)
self.create_window(42, btn_y, window=b_next, anchor="w")
self.create_window(w - 42, btn_y, window=b_menu, anchor="e")
elif can_next:
b_next = ImageButton(
self, t("bj_popup_next_round_btn"), btn_fire, btn_fire,
self._handle_next, sound_mgr=self.sounds, width=215, height=38, style="fire"
)
self.create_window(cx, btn_y, window=b_next, anchor="center")
elif can_menu:
b_menu = ImageButton(
self, t("bj_popup_to_menu_btn"), btn_fire, btn_fire,
self._handle_menu, sound_mgr=self.sounds, width=195, height=38, style="fire"
)
self.create_window(cx, btn_y, window=b_menu, anchor="center")
2026-11-14 04:25:51 +00:00
def _animate_gif(self, frames: list, idx: int):
if not self.winfo_exists() or self._gif_item is None:
2026-11-14 04:25:51 +00:00
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))
2026-11-14 04:25:51 +00:00
def close_dialog(self):
if self._gif_job:
try:
self.after_cancel(self._gif_job)
except Exception:
pass
self._gif_job = None
self._photo_refs.clear()
2026-11-14 04:25:51 +00:00
if self.winfo_exists():
self.destroy()
def _handle_next(self):
self.close_dialog()
self.on_next_round()
def _handle_menu(self):
self.close_dialog()
self.on_back_to_menu()