# -*- coding: utf-8 -*- # view/widgets/casino_result_dialog_base.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_BODY_MUTED, FONT_COLOR_HEADING_SUB, FONT_COLOR_STATUS_LOSS, FONT_COLOR_STATUS_WIN, FONT_COLOR_TITLE_MAIN, FONT_SIZE_BODY, FONT_SIZE_SMALL_MIN, FONT_SIZE_TITLE_LG, SUBTLE_BORDER, ) from resources.code_res.i18n import t from view.widgets.goth_dropdown import GothDropdown from view.widgets.image_button import ImageButton from view.widgets.modal_veil import ModalVeil class CasinoResultDialogBase(tk.Canvas): """ Gemeinsame Gothic-Showdown-Modal-Basis (tk.Canvas + box_big + ModalVeil) für Blackjack, Poker und künftige Mehrspieler-Kartentische. """ def __init__( self, parent, assets, sounds, result_data: dict, dlg_w: int, dlg_h: int, on_next_round, on_back_to_menu, ): GothDropdown.close_any_open_popup() top_root = parent.winfo_toplevel() try: max_h = max(420, top_root.winfo_height() - 56) dlg_h = min(dlg_h, max_h) except Exception: pass super().__init__( top_root, width=dlg_w, height=dlg_h, bg=BG_COLOR, bd=0, highlightthickness=0, ) 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._photo_refs: list = [] self.place(relx=0.5, rely=0.5, anchor="center") self._build_scene() self._veil = ModalVeil(top_root, assets, modal_widget=self) def _draw_showdown_middle(self, w: int, cx: int, cur_y: int) -> int: raise NotImplementedError def _build_scene(self): w, h = self._dlg_w, self._dlg_h cx = w // 2 self._photo_refs.clear() 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") 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 = 26 p_won = self.data.get("player_won") 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) ) 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() + 8 cur_y = self._draw_showdown_middle(w, cx, cur_y) self.create_line(36, cur_y, w - 36, cur_y, fill=SUBTLE_BORDER, width=1) cur_y += 10 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 elif p_net < 0: own_txt, own_col = t("bj_popup_your_loss", abs(p_net)), FONT_COLOR_STATUS_LOSS else: own_txt, own_col = t("bj_popup_your_even"), FONT_COLOR_BODY_DEFAULT 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 other_gains = self.data.get("other_gains", []) if other_gains: oth_ph = self.assets.get_goth_text_photo( " | ".join(other_gains), 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 xp_line = t("bj_popup_xp_earned", self.data["xp_gained"]) 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") btn_norm = self.assets.raw_btn_default btn_purp = self.assets.raw_btn_purple btn_fire = self.assets.raw_btn_fire or btn_purp btn_y = h - 34 can_next = self.data.get("can_continue", True) can_menu = self.data.get("can_leave_menu", True) 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") def close_dialog(self): self._veil.release() self._photo_refs.clear() 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()