the_goth_cardgame_collection/games/poker/views/poker_hands_overlay.py

178 lines
6.0 KiB
Python

# -*- coding: utf-8 -*-
# games/poker/views/poker_hands_overlay.py
from __future__ import annotations
import tkinter as tk
from PIL import Image, ImageTk
from resources.code_res.game_constants import (
ACCENT_GOLD,
BG_COLOR,
FG_COLOR,
FONT_COLOR_BODY_MUTED,
FONT_COLOR_TITLE_MAIN,
FONT_SIZE_HEADING,
RED_SUITS,
SUIT_SYMBOLS,
)
from resources.code_res.i18n import t
from view.widgets.image_button import ImageButton
HAND_REFERENCE_EXAMPLES = (
(1, 9, (("A", "S"), ("K", "S"), ("Q", "S"), ("J", "S"), ("10", "S"))),
(2, 8, (("J", "S"), ("10", "S"), ("9", "S"), ("8", "S"), ("7", "S"))),
(3, 7, (("K", "H"), ("K", "C"), ("K", "D"), ("K", "S"), None)),
(4, 6, (("A", "H"), ("A", "C"), ("A", "D"), ("9", "S"), ("9", "H"))),
(5, 5, (("K", "S"), ("J", "S"), ("8", "S"), ("7", "S"), ("5", "S"))),
(6, 4, (("J", "H"), ("10", "C"), ("9", "D"), ("8", "S"), ("7", "H"))),
(7, 3, (("K", "H"), ("K", "C"), ("K", "D"), None, None)),
(8, 2, (("A", "H"), ("A", "C"), ("9", "D"), ("9", "S"), None)),
(9, 1, (("K", "H"), ("K", "C"), None, None, None)),
(10, 0, (("A", "H"), None, None, None, None)),
)
class PokerHandsOverlay(tk.Canvas):
"""Neutrale Kartenreferenz-Übersicht der 10 Poker-Hände (ohne Spoiler der eigenen Hand)."""
OVL_W = 800
OVL_H = 468
def __init__(self, parent, ctx, on_close):
super().__init__(
parent,
width=self.OVL_W,
height=self.OVL_H,
bg=BG_COLOR,
bd=0,
highlightthickness=0,
)
self.ctx = ctx
self.assets = ctx.assets
self.on_close = on_close
self._photo_refs: list = []
self.place(relx=0.5, rely=0.5, anchor="center")
tk.Widget.tkraise(self)
self._build_content()
def _draw_mini_cards(self, x0: int, y0: int, cards_spec: tuple):
cw, ch, gap = 22, 26, 3
for i, spec in enumerate(cards_spec):
cx = x0 + i * (cw + gap)
if spec is None:
self.create_rectangle(
cx, y0, cx + cw, y0 + ch,
fill="#261321", outline="#4e2942", width=1,
)
else:
rank, suit = spec
fg_col = "#cf3848" if suit in RED_SUITS else "#181421"
sym = SUIT_SYMBOLS.get(suit, suit)
self.create_rectangle(
cx, y0, cx + cw, y0 + ch,
fill="#ede8f5", outline="#8e82a6", width=1,
)
self.create_text(
cx + cw // 2, y0 + 8,
text=rank, font=("Helvetica", 7, "bold"), fill=fg_col,
)
self.create_text(
cx + cw // 2, y0 + 18,
text=sym, font=("Helvetica", 8, "bold"), fill=fg_col,
)
def _build_content(self):
w, h = self.OVL_W, self.OVL_H
bg_src = self.assets.raw_bg_horizontal or self.assets.raw_bg_vertical or self.assets.raw_bg
if bg_src is not None:
bg_ph = ImageTk.PhotoImage(bg_src.resize((w, h), Image.Resampling.BILINEAR))
self._photo_refs.append(bg_ph)
self.create_image(0, 0, image=bg_ph, anchor="nw")
outer = self.assets.get_box_photo("box_big", (w - 6, h - 6))
if outer:
self._photo_refs.append(outer)
self.create_image(w // 2, h // 2, image=outer, anchor="center")
hdr_ph = self.assets.get_goth_text_photo(
t("pk_help_header"),
size_px=FONT_SIZE_HEADING,
color=FONT_COLOR_TITLE_MAIN,
align="center",
)
self._photo_refs.append(hdr_ph)
self.create_image(w // 2, 32, image=hdr_ph, anchor="center")
self.create_text(
w // 2,
60,
text=t("pk_help_general_info"),
font=("Helvetica", 9),
fill=FONT_COLOR_BODY_MUTED,
width=w - 80,
justify="center",
anchor="center",
)
col_w = (w - 76) // 2
row_h = 62
start_y = 86
cell_box_purp = self.assets.get_box_photo("box_purple", (col_w, row_h - 4))
if cell_box_purp:
self._photo_refs.append(cell_box_purp)
for rank_no, cat_id, cards_spec in HAND_REFERENCE_EXAMPLES:
col_idx = 0 if rank_no <= 5 else 1
row_idx = (rank_no - 1) % 5
cx = 30 + col_idx * (col_w + 16) + col_w // 2
cy = start_y + row_idx * row_h + (row_h - 4) // 2
left_edge = cx - col_w // 2
if cell_box_purp:
self.create_image(cx, cy, image=cell_box_purp, anchor="center")
self._draw_mini_cards(left_edge + 14, cy - 13, cards_spec)
raw_name = t(f"pk_hand_{cat_id}").replace("★ ", "").replace(" ★", "")
short_name = raw_name.split(" (")[0]
name_col = ACCENT_GOLD if rank_no == 1 else FG_COLOR
tip_raw = t(f"pk_help_hand_{cat_id}")
tip_short = tip_raw.split(":", 1)[-1].strip() if ":" in tip_raw else tip_raw
txt_x = left_edge + 142
txt_max_w = col_w - 154
self.create_text(
txt_x,
cy - 12,
text=f"{rank_no}. {short_name}",
font=("Helvetica", 9, "bold"),
fill=name_col,
anchor="w",
)
self.create_text(
txt_x,
cy + 9,
text=tip_short,
font=("Helvetica", 8),
fill=FONT_COLOR_BODY_MUTED,
width=txt_max_w,
anchor="w",
)
btn_fire = self.assets.raw_btn_fire or self.assets.raw_btn_purple
close_btn = ImageButton(
self,
t("pk_btn_close_help"),
btn_fire,
btn_fire,
self.on_close,
sound_mgr=self.ctx.sounds,
width=156,
height=34,
style="fire",
)
self.create_window(w // 2, h - 30, window=close_btn, anchor="center")