# -*- coding: utf-8 -*- # view/stats/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.db.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 from view.layout_mode import LayoutMode class StatsHubRenderer: """Rendert die kategorisierte Spiel-Auswahl für Statistiken (Desktop & Kompakt/Mobile).""" @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: is_mobile = LayoutMode.is_compact(w, h) compact = (h < 780) or is_mobile pad_x = LayoutMode.outer_pad_x(w, h, desktop_ratio=0.075, desktop_min=72) top_y = 34 if is_mobile else 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 - 2) if is_mobile else (FONT_SIZE_HEADING + (0 if compact else 2)), color=FONT_COLOR_TITLE_MAIN, max_width=max(160, w - 2 * pad_x - 280), ) photo_refs.append(hdr_ph) cv.create_image(w // 2, top_y, image=hdr_ph, anchor="center", tags="fg") sep_y = top_y + (30 if is_mobile else (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 (1-zeilig auf Desktop, 2x2-Raster auf schmalen 658px-Screens) total_tabs_w = sum(bw for _, bw in cat_btns) + max(0, len(cat_btns) - 1) * 10 if total_tabs_w <= (w - 2 * pad_x): cat_y = sep_y + (28 if compact else 36) 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 cat_bottom_y = cat_y + 20 else: r1, r2 = cat_btns[:2], cat_btns[2:] for r_i, row_btns in enumerate((r1, r2)): ry = sep_y + 24 + r_i * 44 rw_sum = sum(bw for _, bw in row_btns) + max(0, len(row_btns) - 1) * 10 tx = (w - rw_sum) // 2 for btn_wgt, bw in row_btns: cv.create_window(tx + bw // 2, ry, window=btn_wgt, anchor="center", tags="fg") tx += bw + 10 cat_bottom_y = sep_y + 24 + 44 + 20 active_cat_title = active_cat_desc = "" for cid, t_key, d_key in STAT_CATEGORIES: if cid == selected_cat: active_cat_title, active_cat_desc = t(t_key), t(d_key) break desc_top_y = cat_bottom_y + (12 if is_mobile else 18) 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_top_y, image=desc_ph, anchor="n", tags="fg") pid = profile["id"] games_in_cat: list[tuple[str, str, dict]] = [] for folder, title_key in STAT_GAME_GROUPS: if STAT_FOLDER_CATEGORY_MAP.get(folder, STAT_CAT_LUCK) == selected_cat: games_in_cat.append((folder, t(title_key), Database.get_game_progress(pid, folder))) area_top = desc_top_y + desc_ph.height() + (16 if is_mobile else 26) area_bot = h - (18 if is_mobile else 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=min(620, w - 2 * pad_x - 40), ) photo_refs.append(empty_ph) eb_frame = assets.get_box_photo("box_purple", (empty_ph.width() + 56, empty_ph.height() + 40)) 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(360, w - 2 * pad_x + 16) max_cols_fit = max(2, min(5, usable_w // 196)) 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 = 182 if tight else 202 gap_x = 14 if is_mobile else (18 if cols >= 5 else 22) col_step = min(tile_w + gap_x, usable_w // max(1, cols)) row_step = 204 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 cls._draw_game_tile( cv, assets, sounds, photo_refs, cx, cy, idx, folder, g_title, str(prog["title"]), 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 = (182, 194) 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 = 88 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 - (27 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) cv.tag_bind(hit_tag, "", _leave) cv.tag_bind(hit_tag, "", lambda _e: (sounds.play("click"), on_click()))