the_goth_cardgame_collection/view/widgets/menu_body_canvas.py

67 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
# view/widgets/menu_body_canvas.py
from __future__ import annotations
import tkinter as tk
from PIL import Image, ImageTk
from resources.code_res.game_constants import BG_COLOR
from view.widgets.goth_dropdown import GothDropdown
class MenuBodyCanvas(tk.Canvas):
"""
Vollflächiges Menü-Canvas unterhalb der GothTopbar:
Dockt per pack() strikt UNTER der Topbar an (überdeckt sie niemals) und
holt sich seinen Hintergrund-Ausschnitt aus dem geteilten AssetManager-Cache.
"""
def __init__(self, ctx, topbar_h: int = 0):
GothDropdown.close_any_open_popup()
super().__init__(ctx.root, bg=BG_COLOR, bd=0, highlightthickness=0)
self.ctx = ctx
self._last_key: tuple[int, int, int, int, int] = (0, 0, -1, 0, 0)
self._bg_photo: ImageTk.PhotoImage | None = None
self.pack(fill="both", expand=True)
# Sofortiges Vorzeichnen mit bekannter Root-Größe -> 0 Frames Leerlauf!
rw = ctx.root.winfo_width()
rh = ctx.root.winfo_height()
top_h = max(84, int(rh * 0.135))
if rw >= 200 and rh > top_h + 80:
self._bg_photo = ctx.assets.get_bg_patch_photo(rw, rh, 0, top_h, rw, rh - top_h)
if self._bg_photo is not None:
self._last_key = (rw, rh - top_h, top_h, rw, rh)
self.create_image(0, 0, image=self._bg_photo, anchor="nw", tags="menu_bg")
self.tag_lower("menu_bg")
def sync_bg(self, w: int, h: int):
if w < 80 or h < 80:
return
rw = max(w, self.ctx.root.winfo_width())
rh = max(h, self.ctx.root.winfo_height())
oy = max(0, min(rh - h, self.winfo_rooty() - self.ctx.root.winfo_rooty()))
key = (w, h, oy, rw, rh)
if key == self._last_key:
return
self._last_key = key
if hasattr(self.ctx.assets, "get_bg_patch_photo"):
self._bg_photo = self.ctx.assets.get_bg_patch_photo(rw, rh, 0, oy, w, h)
else:
src = (
self.ctx.assets.raw_bg_vertical
if (rh > rw and self.ctx.assets.raw_bg_vertical is not None)
else (self.ctx.assets.raw_bg_horizontal or self.ctx.assets.raw_bg)
)
if src is None:
return
full_img = src.resize((rw, rh), Image.Resampling.BILINEAR)
self._bg_photo = ImageTk.PhotoImage(full_img.crop((0, oy, min(rw, w), min(rh, oy + h))))
if self._bg_photo is None:
return
self.delete("menu_bg")
self.create_image(0, 0, image=self._bg_photo, anchor="nw", tags="menu_bg")
self.tag_lower("menu_bg")