the_goth_cardgame_collection/games/blackjack/views/round_result_dialog.py

131 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
# games/blackjack/views/round_result_dialog.py
import tkinter as tk
from resources.code_res.game_constants import (
ACCENT_GOLD, ACCENT_GOTH, CELL_BG, ERROR_FG,
FG_COLOR, MODAL_BG, MUTED_FG, SUBTLE_BORDER
)
from resources.code_res.i18n import t
from view.widgets.image_button import ImageButton
class BlackjackResultDialog(tk.Frame):
def __init__(
self,
parent,
assets,
sounds,
result_data: dict,
on_next_round,
on_back_to_menu,
):
super().__init__(
parent, bg=MODAL_BG, padx=32, pady=24,
highlightthickness=2, highlightbackground=ACCENT_GOTH
)
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._gif_job = None
self.place(relx=0.5, rely=0.5, anchor="center")
self.lift()
self._build()
def _build(self):
self.gif_lbl = tk.Label(self, bg=MODAL_BG, bd=0)
self.gif_lbl.pack(pady=(0, 6))
if self.data.get("player_won") is True:
self._start_gif("win_gif")
elif self.data.get("player_won") is False:
self._start_gif("lose_gif")
headline_color = ACCENT_GOLD if self.data.get("player_won") else (
MUTED_FG if self.data.get("player_won") is None else ERROR_FG
)
tk.Label(
self, text=self.data["headline"],
font=("Georgia", 18, "bold"), fg=headline_color, bg=MODAL_BG,
).pack(pady=(2, 12))
info_box = tk.Frame(
self, bg=CELL_BG, padx=22, pady=14,
highlightthickness=1, highlightbackground=SUBTLE_BORDER
)
info_box.pack(fill="x", pady=(0, 14))
p_net = self.data["player_net"]
if p_net > 0:
own_txt, own_col = t("bj_popup_your_gain", p_net), "#7be08a"
elif p_net < 0:
own_txt, own_col = t("bj_popup_your_loss", abs(p_net)), ERROR_FG
else:
own_txt, own_col = t("bj_popup_your_even"), FG_COLOR
tk.Label(info_box, text=own_txt, font=("Helvetica", 12, "bold"), fg=own_col, bg=CELL_BG).pack(pady=3)
for extra_line in self.data.get("other_gains", []):
tk.Label(info_box, text=extra_line, font=("Helvetica", 11), fg=MUTED_FG, bg=CELL_BG).pack(pady=2)
tk.Frame(info_box, bg=SUBTLE_BORDER, height=1).pack(fill="x", pady=10)
tk.Label(
info_box, text=t("bj_popup_xp_earned", self.data["xp_gained"]),
font=("Helvetica", 12, "bold"), fg=ACCENT_GOTH, bg=CELL_BG,
).pack(pady=2)
if self.data.get("new_level_title"):
tk.Label(
info_box, text=self.data["new_level_title"],
font=("Georgia", 11, "bold"), fg=ACCENT_GOLD, bg=CELL_BG,
).pack(pady=(4, 0))
btn_row = tk.Frame(self, bg=MODAL_BG)
btn_row.pack(fill="x", pady=(6, 0))
if self.data.get("can_continue", True):
ImageButton(
btn_row, t("bj_popup_next_round_btn"),
self.assets.raw_btn, self.assets.raw_btn_active,
self._handle_next, sound_mgr=self.sounds, width=195, height=40, font=("Helvetica", 10, "bold")
).pack(side="left", padx=(0, 10))
if self.data.get("can_leave_menu", True):
ImageButton(
btn_row, t("bj_popup_to_menu_btn"),
self.assets.raw_sel, self.assets.raw_sel_active,
self._handle_menu, sound_mgr=self.sounds, width=165, height=40, font=("Helvetica", 10, "bold")
).pack(side="right")
def _start_gif(self, key: str):
frames = self.assets.get_gif_frames(key)
if not frames:
return
def _step(idx=0):
if not self.gif_lbl.winfo_exists():
return
self.gif_lbl.configure(image=frames[idx])
self._gif_job = self.after(90, _step, (idx + 1) % len(frames))
_step(0)
def close_dialog(self):
if self._gif_job:
try:
self.after_cancel(self._gif_job)
except Exception:
pass
self._gif_job = None
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()