231 lines
8.6 KiB
Python
231 lines
8.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# view/stats_hub_renderer.py
|
|
from __future__ import annotations
|
|
|
|
import tkinter as tk
|
|
|
|
from games.solitaire.base.solitaire_theme import get_game_icon_photo
|
|
from models.database import Database
|
|
from resources.code_res.game_constants import (
|
|
FONT_COLOR_BODY_DEFAULT,
|
|
FONT_COLOR_BODY_MUTED,
|
|
FONT_COLOR_HEADING_SUB,
|
|
FONT_COLOR_TITLE_MAIN,
|
|
FONT_SIZE_BODY,
|
|
FONT_SIZE_HEADING,
|
|
FONT_SIZE_SMALL_MIN,
|
|
FONT_SIZE_TITLE_LG,
|
|
STAT_CATEGORIES,
|
|
STAT_CAT_LUCK,
|
|
STAT_FOLDER_CATEGORY_MAP,
|
|
STAT_GAME_GROUPS,
|
|
SUBTLE_BORDER,
|
|
UI_ACCENT_AMETHYST,
|
|
)
|
|
from resources.code_res.i18n import t
|
|
|
|
|
|
class StatsHubRenderer:
|
|
"""Rendert die kategorisierte Spiel-Auswahl als Einstiegspunkt für Statistiken."""
|
|
|
|
@classmethod
|
|
def draw_hub(
|
|
cls,
|
|
cv: tk.Canvas,
|
|
assets,
|
|
sounds,
|
|
profile: dict,
|
|
selected_cat: str,
|
|
cat_btns: list[tuple[tk.Widget, int]],
|
|
back_btn: tk.Widget,
|
|
settings_btn: tk.Widget | None,
|
|
on_select_game,
|
|
photo_refs: list,
|
|
w: int,
|
|
h: int,
|
|
) -> None:
|
|
compact = (h < 780)
|
|
pad_x = max(72, int(w * 0.075))
|
|
top_y = max(52, int(h * 0.088))
|
|
|
|
cv.create_window(pad_x, top_y, window=back_btn, anchor="w", tags="fg")
|
|
if settings_btn is not None:
|
|
cv.create_window(w - pad_x, top_y, window=settings_btn, anchor="e", tags="fg")
|
|
|
|
hdr_ph = assets.get_goth_text_photo(
|
|
t("stats_overall_summary", profile.get("name", "")),
|
|
size_px=FONT_SIZE_HEADING + (0 if compact else 2),
|
|
color=FONT_COLOR_TITLE_MAIN,
|
|
max_width=max(220, w - 480),
|
|
)
|
|
photo_refs.append(hdr_ph)
|
|
cv.create_image(w // 2, top_y, image=hdr_ph, anchor="center", tags="fg")
|
|
|
|
sep_y = top_y + (34 if compact else 42)
|
|
cv.create_line(pad_x, sep_y, w - pad_x, sep_y, fill=SUBTLE_BORDER, width=1, tags="fg")
|
|
|
|
# Kategorie-Leiste analog zum Hauptmenü
|
|
cat_y = sep_y + (30 if compact else 36)
|
|
total_tabs_w = sum(bw for _, bw in cat_btns) + max(0, len(cat_btns) - 1) * 10
|
|
tab_x = (w - total_tabs_w) // 2
|
|
for btn_wgt, bw in cat_btns:
|
|
cv.create_window(tab_x + bw // 2, cat_y, window=btn_wgt, anchor="center", tags="fg")
|
|
tab_x += bw + 10
|
|
|
|
active_cat_title = ""
|
|
active_cat_desc = ""
|
|
for cid, t_key, d_key in STAT_CATEGORIES:
|
|
if cid == selected_cat:
|
|
active_cat_title = t(t_key)
|
|
active_cat_desc = t(d_key)
|
|
break
|
|
|
|
desc_y = cat_y + (46 if compact else 54)
|
|
desc_ph = assets.get_goth_text_photo(
|
|
active_cat_desc,
|
|
size_px=FONT_SIZE_BODY,
|
|
color=FONT_COLOR_BODY_MUTED,
|
|
max_width=min(940, w - 2 * pad_x),
|
|
align="center",
|
|
)
|
|
photo_refs.append(desc_ph)
|
|
cv.create_image(w // 2, desc_y, image=desc_ph, anchor="center", tags="fg")
|
|
|
|
pid = profile["id"]
|
|
games_in_cat: list[tuple[str, str, dict]] = []
|
|
for folder, title_key in STAT_GAME_GROUPS:
|
|
g_cat = STAT_FOLDER_CATEGORY_MAP.get(folder, STAT_CAT_LUCK)
|
|
if g_cat == selected_cat:
|
|
prog = Database.get_game_progress(pid, folder)
|
|
games_in_cat.append((folder, t(title_key), prog))
|
|
|
|
area_top = desc_y + desc_ph.height() // 2 + (26 if compact else 34)
|
|
area_bot = h - max(52, int(h * 0.085))
|
|
mid_y = (area_top + area_bot) // 2
|
|
|
|
if not games_in_cat:
|
|
empty_ph = assets.get_goth_text_photo(
|
|
t("no_games_in_category", active_cat_title),
|
|
size_px=FONT_SIZE_HEADING - 2,
|
|
color=FONT_COLOR_BODY_MUTED,
|
|
max_width=620,
|
|
)
|
|
photo_refs.append(empty_ph)
|
|
eb_frame = assets.get_box_photo("box_purple", (empty_ph.width() + 68, empty_ph.height() + 44))
|
|
if eb_frame:
|
|
photo_refs.append(eb_frame)
|
|
cv.create_image(w // 2, mid_y, image=eb_frame, anchor="center", tags="fg")
|
|
cv.create_image(w // 2, mid_y, image=empty_ph, anchor="center", tags="fg")
|
|
return
|
|
|
|
usable_w = max(420, w - 2 * pad_x + 24)
|
|
max_cols_fit = max(2, min(5, usable_w // 202))
|
|
cols = min(max_cols_fit, len(games_in_cat))
|
|
rows = (len(games_in_cat) + cols - 1) // cols
|
|
tight = compact or rows > 1 or cols >= 5
|
|
|
|
tile_w = 184 if tight else 202
|
|
gap_x = 18 if cols >= 5 else 22
|
|
col_step = min(tile_w + gap_x, usable_w // max(1, cols))
|
|
row_step = 206 if tight else 234
|
|
start_y = mid_y - ((rows - 1) * row_step) // 2
|
|
|
|
for idx, (folder, g_title, prog) in enumerate(games_in_cat):
|
|
r, c = idx // cols, idx % cols
|
|
in_row = min(cols, len(games_in_cat) - r * cols)
|
|
cx = (w // 2) - ((in_row - 1) * col_step) // 2 + c * col_step
|
|
cy = start_y + r * row_step
|
|
sub_txt = str(prog["title"])
|
|
cls._draw_game_tile(
|
|
cv, assets, sounds, photo_refs,
|
|
cx, cy, idx, folder, g_title, sub_txt,
|
|
on_click=lambda f=folder: on_select_game(f),
|
|
compact=tight,
|
|
)
|
|
|
|
@staticmethod
|
|
def _draw_game_tile(
|
|
cv: tk.Canvas,
|
|
assets,
|
|
sounds,
|
|
photo_refs: list,
|
|
cx: int,
|
|
cy: int,
|
|
idx: int,
|
|
folder: str,
|
|
game_title: str,
|
|
rank_sub: str,
|
|
on_click,
|
|
compact: bool,
|
|
) -> None:
|
|
hit_tag = f"stat_tile_{idx}"
|
|
tags = ("fg", hit_tag)
|
|
|
|
cw_n, ch_n = (184, 196) if compact else (202, 214)
|
|
cw_h, ch_h = cw_n + 14, ch_n + 16
|
|
fn = assets.get_box_photo("box_purple", (cw_n, ch_n))
|
|
fh = assets.get_box_photo("box_gold_small", (cw_h, ch_h))
|
|
if fn:
|
|
photo_refs.append(fn)
|
|
if fh:
|
|
photo_refs.append(fh)
|
|
|
|
b_px = 90 if compact else 102
|
|
h_px = b_px + 12
|
|
ic_n = get_game_icon_photo(assets.res, folder, size_px=b_px)
|
|
ic_h = get_game_icon_photo(assets.res, folder, size_px=h_px)
|
|
if ic_n is None:
|
|
ic_n = assets.get_goth_text_photo("*", FONT_SIZE_TITLE_LG - 4, UI_ACCENT_AMETHYST)
|
|
ic_h = assets.get_goth_text_photo("*", FONT_SIZE_TITLE_LG + 2, FONT_COLOR_TITLE_MAIN)
|
|
photo_refs.extend([ic_n, ic_h])
|
|
|
|
tp_n = (FONT_SIZE_HEADING - 3) if compact else (FONT_SIZE_HEADING - 1)
|
|
t_n = assets.get_goth_text_photo(game_title, tp_n, FONT_COLOR_BODY_DEFAULT, max_width=cw_n - 24)
|
|
t_h = assets.get_goth_text_photo(game_title, tp_n + 3, FONT_COLOR_TITLE_MAIN, max_width=cw_h - 22)
|
|
s_n = assets.get_goth_text_photo(rank_sub, FONT_SIZE_SMALL_MIN, FONT_COLOR_HEADING_SUB, max_width=cw_n - 24)
|
|
s_h = assets.get_goth_text_photo(rank_sub, FONT_SIZE_SMALL_MIN + 2, "#ffffff", max_width=cw_h - 22)
|
|
photo_refs.extend([t_n, t_h, s_n, s_h])
|
|
|
|
iy_n = cy - (28 if compact else 32)
|
|
iy_h = iy_n - 3
|
|
ty_n = iy_n + b_px // 2 + (19 if compact else 21)
|
|
ty_h = iy_h + h_px // 2 + (20 if compact else 22)
|
|
sy_n = ty_n + (24 if compact else 26)
|
|
sy_h = ty_h + (26 if compact else 28)
|
|
|
|
f_id = cv.create_image(cx, cy, image=fn, anchor="center", tags=tags) if fn else None
|
|
i_id = cv.create_image(cx, iy_n, image=ic_n, anchor="center", tags=tags)
|
|
t_id = cv.create_image(cx, ty_n, image=t_n, anchor="center", tags=tags)
|
|
s_id = cv.create_image(cx, sy_n, image=s_n, anchor="center", tags=tags)
|
|
|
|
hit = cv.create_rectangle(
|
|
cx - cw_h // 2, cy - ch_h // 2, cx + cw_h // 2, cy + ch_h // 2,
|
|
fill="", outline="", width=0, tags=tags,
|
|
)
|
|
cv.tag_raise(hit)
|
|
|
|
def _enter(_e):
|
|
cv.configure(cursor="hand2")
|
|
if f_id and fh:
|
|
cv.itemconfigure(f_id, image=fh)
|
|
cv.coords(i_id, cx, iy_h)
|
|
cv.itemconfigure(i_id, image=ic_h)
|
|
cv.coords(t_id, cx, ty_h)
|
|
cv.itemconfigure(t_id, image=t_h)
|
|
cv.coords(s_id, cx, sy_h)
|
|
cv.itemconfigure(s_id, image=s_h)
|
|
|
|
def _leave(_e):
|
|
cv.configure(cursor="")
|
|
if f_id and fn:
|
|
cv.itemconfigure(f_id, image=fn)
|
|
cv.coords(i_id, cx, iy_n)
|
|
cv.itemconfigure(i_id, image=ic_n)
|
|
cv.coords(t_id, cx, ty_n)
|
|
cv.itemconfigure(t_id, image=t_n)
|
|
cv.coords(s_id, cx, sy_n)
|
|
cv.itemconfigure(s_id, image=s_n)
|
|
|
|
cv.tag_bind(hit_tag, "<Enter>", _enter)
|
|
cv.tag_bind(hit_tag, "<Leave>", _leave)
|
|
cv.tag_bind(hit_tag, "<Button-1>", lambda _e: (sounds.play("click"), on_click())) |