192 lines
6.8 KiB
Python
192 lines
6.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
# view/widgets/stats_board_widget.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_HEADING,
|
|
FONT_SIZE_SMALL_MIN,
|
|
STAT_FIELD_KEYS,
|
|
)
|
|
from resources.code_res.i18n import t
|
|
from view.widgets.goth_dropdown import GothDropdown
|
|
|
|
|
|
class StatsBoardWidget(tk.Canvas):
|
|
"""
|
|
Rahmenloses Gothic-Statistik-Board:
|
|
Schneidet seinen Hintergrund live aus dem Hauptfenster-Hintergrundbild aus,
|
|
rahmt die Tafel mit box_big.png und jede der 8 Kennzahlen mit box_small_purple.png.
|
|
"""
|
|
|
|
BOARD_HEIGHT = 238
|
|
|
|
def __init__(self, parent, title: str, filter_map: dict[str, int | None], fetch_callback):
|
|
super().__init__(
|
|
parent,
|
|
height=self.BOARD_HEIGHT,
|
|
bg=BG_COLOR,
|
|
bd=0,
|
|
highlightthickness=0,
|
|
)
|
|
top = self.winfo_toplevel()
|
|
self.assets = getattr(top, "assets", None)
|
|
ctrl = getattr(top, "ctrl", None)
|
|
self.sounds = getattr(ctrl, "sounds", None) if ctrl else None
|
|
|
|
self._title = str(title or "")
|
|
self.filter_map = dict(filter_map)
|
|
self.fetch_callback = fetch_callback
|
|
self._stats_data: dict = {}
|
|
|
|
self._bg_photo: ImageTk.PhotoImage | None = None
|
|
self._photo_refs: list[ImageTk.PhotoImage] = []
|
|
self._last_geom: tuple[int, int, int, int] = (0, 0, -1, -1)
|
|
|
|
first_key = next(iter(self.filter_map.keys()), "")
|
|
self.filter_var = tk.StringVar(value=first_key)
|
|
self.filter_cb = GothDropdown(
|
|
self,
|
|
self.assets,
|
|
values=list(self.filter_map.keys()),
|
|
textvariable=self.filter_var,
|
|
on_change=lambda _val: self.refresh(),
|
|
sound_mgr=self.sounds,
|
|
width=290,
|
|
height=38,
|
|
)
|
|
|
|
self.bind("<Configure>", self._on_configure)
|
|
self.refresh()
|
|
|
|
def set_title(self, new_title: str):
|
|
self._title = str(new_title or "")
|
|
if self.winfo_exists() and self.winfo_width() >= 120:
|
|
self._redraw_contents(self.winfo_width(), self.winfo_height())
|
|
|
|
def refresh(self):
|
|
vid = self.filter_map.get(self.filter_var.get())
|
|
self._stats_data = self.fetch_callback(vid) or {}
|
|
if self.winfo_exists() and self.winfo_width() >= 120:
|
|
self._redraw_contents(self.winfo_width(), self.winfo_height())
|
|
|
|
def _slice_global_bg(self, w: int, h: int):
|
|
if self.assets is None:
|
|
return
|
|
top = self.winfo_toplevel()
|
|
rw = max(w, top.winfo_width())
|
|
rh = max(h, top.winfo_height())
|
|
src = (
|
|
self.assets.raw_bg_vertical
|
|
if (rh > rw and self.assets.raw_bg_vertical is not None)
|
|
else (self.assets.raw_bg_horizontal or self.assets.raw_bg)
|
|
)
|
|
if src is None:
|
|
return
|
|
ox = max(0, min(rw - w, self.winfo_rootx() - top.winfo_rootx()))
|
|
oy = max(0, min(rh - h, self.winfo_rooty() - top.winfo_rooty()))
|
|
scaled = src.resize((rw, rh), Image.Resampling.BILINEAR)
|
|
patch = scaled.crop((ox, oy, ox + w, oy + h))
|
|
self._bg_photo = ImageTk.PhotoImage(patch)
|
|
self.delete("sb_bg")
|
|
self.create_image(0, 0, image=self._bg_photo, anchor="nw", tags="sb_bg")
|
|
self.tag_lower("sb_bg")
|
|
|
|
def _on_configure(self, event):
|
|
w, h = max(1, event.width), max(1, event.height)
|
|
if w < 120 or h < 80:
|
|
return
|
|
top = self.winfo_toplevel()
|
|
geom_key = (w, h, self.winfo_rootx() - top.winfo_rootx(), self.winfo_rooty() - top.winfo_rooty())
|
|
if geom_key != self._last_geom:
|
|
self._last_geom = geom_key
|
|
self._slice_global_bg(w, h)
|
|
self._redraw_contents(w, h)
|
|
|
|
def _redraw_contents(self, w: int, h: int):
|
|
self.delete("sb_fg")
|
|
self._photo_refs.clear()
|
|
|
|
if self.assets is None:
|
|
return
|
|
|
|
# Äußerer Zierrahmen box_big.png
|
|
outer_frame = self.assets.get_box_photo("box_big", (w - 4, h - 4))
|
|
if outer_frame:
|
|
self._photo_refs.append(outer_frame)
|
|
self.create_image(w // 2, h // 2, image=outer_frame, anchor="center", tags="sb_fg")
|
|
|
|
pad_x = 26
|
|
hdr_y = 34
|
|
|
|
# Titel links oben + GothDropdown rechts oben
|
|
title_ph = self.assets.get_goth_text_photo(
|
|
self._title,
|
|
size_px=FONT_SIZE_HEADING - 2,
|
|
color=FONT_COLOR_TITLE_MAIN,
|
|
max_width=max(160, w - 360),
|
|
align="left",
|
|
)
|
|
self._photo_refs.append(title_ph)
|
|
self.create_image(pad_x, hdr_y, image=title_ph, anchor="w", tags="sb_fg")
|
|
self.create_window(w - pad_x, hdr_y, window=self.filter_cb, anchor="e", tags="sb_fg")
|
|
|
|
# 2x4 Grid aus box_small_purple.png Kacheln
|
|
grid_top = 64
|
|
grid_h = h - grid_top - 18
|
|
cols = 4
|
|
rows = 2
|
|
gap_x = 12
|
|
gap_y = 10
|
|
cell_w = max(110, (w - 2 * pad_x - (cols - 1) * gap_x) // cols)
|
|
cell_h = max(56, (grid_h - (rows - 1) * gap_y) // rows)
|
|
|
|
cell_box_ph = self.assets.get_box_photo("box_purple", (cell_w, cell_h))
|
|
if cell_box_ph:
|
|
self._photo_refs.append(cell_box_ph)
|
|
|
|
for idx, (stat_key, i18n_key) in enumerate(STAT_FIELD_KEYS):
|
|
r, c = divmod(idx, cols)
|
|
cx = pad_x + c * (cell_w + gap_x) + cell_w // 2
|
|
cy = grid_top + r * (cell_h + gap_y) + cell_h // 2
|
|
|
|
if cell_box_ph:
|
|
self.create_image(cx, cy, image=cell_box_ph, anchor="center", tags="sb_fg")
|
|
|
|
lbl_ph = self.assets.get_goth_text_photo(
|
|
t(i18n_key),
|
|
size_px=FONT_SIZE_SMALL_MIN,
|
|
color=FONT_COLOR_BODY_MUTED,
|
|
max_width=cell_w - 24,
|
|
align="center",
|
|
)
|
|
val_str = str(self._stats_data.get(stat_key, "0"))
|
|
if stat_key == "won":
|
|
val_col = FONT_COLOR_STATUS_WIN
|
|
elif stat_key == "lost":
|
|
val_col = FONT_COLOR_STATUS_LOSS
|
|
elif stat_key in ("win_pct", "longest_streak"):
|
|
val_col = FONT_COLOR_HEADING_SUB
|
|
else:
|
|
val_col = FONT_COLOR_BODY_DEFAULT
|
|
|
|
val_ph = self.assets.get_goth_text_photo(
|
|
val_str,
|
|
size_px=FONT_SIZE_HEADING - 2,
|
|
color=val_col,
|
|
max_width=cell_w - 24,
|
|
align="center",
|
|
)
|
|
self._photo_refs.extend([lbl_ph, val_ph])
|
|
|
|
self.create_image(cx, cy - cell_h // 4 + 2, image=lbl_ph, anchor="center", tags="sb_fg")
|
|
self.create_image(cx, cy + cell_h // 5 + 2, image=val_ph, anchor="center", tags="sb_fg") |