# -*- coding: utf-8 -*- # games/solitaire/base/solitaire_bottom_bar.py from __future__ import annotations import tkinter as tk from PIL import Image, ImageTk import resources.code_res.game_constants as gc from resources.code_res.game_constants import ( BG_COLOR, FONT_COLOR_HEADING_SUB, FONT_SIZE_SMALL_MIN, UI_ACCENT_AMETHYST, ) from view.widgets.image_button import ImageButton BG_SAFE_SIDE_RATIO = getattr(gc, "BG_SAFE_SIDE_RATIO", 0.088) BG_SAFE_BOTTOM_RATIO = getattr(gc, "BG_SAFE_BOTTOM_RATIO", 0.095) class SolitaireBottomBar(tk.Canvas): """ Gerahmte HUD- & Aktionsleiste innerhalb der unteren Safe-Area von background.jpg: Sitzt oberhalb des unteren Dornenrahmens, lässt die Spinnweben frei und hält den sekündlichen Timer aus dem globalen Text-Cache fern. """ CONTENT_H = 48 def __init__(self, parent, ctx, hud_var: tk.StringVar, status_var: tk.StringVar): root_h = max(720, ctx.root.winfo_height()) self._bottom_inset = max(54, int(root_h * BG_SAFE_BOTTOM_RATIO)) init_h = self.CONTENT_H + self._bottom_inset super().__init__( parent, height=init_h, bg=BG_COLOR, bd=0, highlightthickness=0, ) self.ctx = ctx self.assets = ctx.assets self.hud_var = hud_var self.status_var = status_var self._buttons: list[ImageButton] = [] self._bg_ph: ImageTk.PhotoImage | None = None self._frame_ph: ImageTk.PhotoImage | None = None self._txt_refs: list[ImageTk.PhotoImage] = [] self._btn_left_limit: int = 500 self.hud_var.trace_add("write", lambda *_: self.refresh_texts()) self.status_var.trace_add("write", lambda *_: self.refresh_texts()) self.bind("", self._on_configure) def set_buttons(self, buttons: list[ImageButton]): self._buttons = list(buttons) self.relayout() self.after(20, self.relayout) def relayout(self): if not self.winfo_exists(): return w = self.winfo_width() h = self.winfo_height() if w >= 120 and h >= 40: self._slice_bg(w, h) self._layout(w, h) def _slice_bg(self, w: int, h: int): root = self.ctx.root rw = max(w, root.winfo_width()) rh = max(h, root.winfo_height()) ox = max(0, min(rw - w, self.winfo_rootx() - root.winfo_rootx())) oy = max(0, min(rh - h, self.winfo_rooty() - root.winfo_rooty())) if hasattr(self.assets, "get_bg_patch_photo"): self._bg_ph = self.assets.get_bg_patch_photo(rw, rh, ox, oy, w, h) else: 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 scaled = src.resize((rw, rh), Image.Resampling.BILINEAR) self._bg_ph = ImageTk.PhotoImage(scaled.crop((ox, oy, ox + w, oy + h))) if self._bg_ph is None: return self.delete("bb_bg") self.create_image(0, 0, image=self._bg_ph, anchor="nw", tags="bb_bg") self.tag_lower("bb_bg") def _on_configure(self, event): if event.widget is not self: return w, h = max(1, event.width), max(1, event.height) if w < 120: return root_h = max(600, self.ctx.root.winfo_height()) new_bot = max(52, int(root_h * BG_SAFE_BOTTOM_RATIO)) if new_bot != self._bottom_inset: self._bottom_inset = new_bot h = self.CONTENT_H + self._bottom_inset self.configure(height=h) self._slice_bg(w, h) self._layout(w, h) def refresh_texts(self): if self.winfo_exists() and self.winfo_width() >= 120: self._draw_texts(self.winfo_width(), self.winfo_height()) def _layout(self, w: int, h: int): self.delete("bb_frame") side_inset = max(92, int(w * BG_SAFE_SIDE_RATIO)) frame_w = max(240, w - 2 * side_inset) cy = self.CONTENT_H // 2 try: self._frame_ph = self.assets.get_box_photo( "box_gold_double", (frame_w, self.CONTENT_H - 2), border_scale=0.85 ) except TypeError: self._frame_ph = self.assets.get_box_photo("box_gold_double", (frame_w, self.CONTENT_H - 2)) if self._frame_ph: self.create_image(w // 2, cy, image=self._frame_ph, anchor="center", tags="bb_frame") rx = w - side_inset - 16 for btn in reversed(self._buttons): bw = btn.pixel_width bh = btn.pixel_height btn.place(x=rx - bw, y=cy - bh // 2, width=bw, height=bh) btn.lift() rx -= (bw + 6) self._btn_left_limit = rx self._draw_texts(w, h) def _draw_texts(self, w: int, _h: int): self.delete("bb_txt") self._txt_refs.clear() side_inset = max(92, int(w * BG_SAFE_SIDE_RATIO)) cy = self.CONTENT_H // 2 lx = side_inset + 18 right_limit = self._btn_left_limit - 12 raw_hud = (self.hud_var.get() or "").replace("•", "-") parts = [p.strip() for p in raw_hud.split("|") if p.strip()] for i, seg in enumerate(parts): txt = seg if i == 0 else f"- {seg}" # Zeit-Segment nicht im Dauer-Cache verewigen, statische Segmente schon if ":" in seg and any(ch.isdigit() for ch in seg): try: pil_seg = self.assets.render_goth_text_pil( txt, size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left", uncached=True ) except TypeError: pil_seg = self.assets.render_goth_text_pil( txt, size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left" ) seg_ph = ImageTk.PhotoImage(pil_seg) else: seg_ph = self.assets.get_goth_text_photo( txt, size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left" ) self._txt_refs.append(seg_ph) self.create_image(lx, cy, image=seg_ph, anchor="w", tags="bb_txt") lx += seg_ph.width() + 12 st_str = (self.status_var.get() or "").replace("•", "-") if st_str and (right_limit - lx) >= 80: st_ph = self.assets.get_goth_text_photo( st_str, size_px=FONT_SIZE_SMALL_MIN, color=UI_ACCENT_AMETHYST, max_width=right_limit - lx, align="left", ) self._txt_refs.append(st_ph) self.create_image(lx, cy, image=st_ph, anchor="w", tags="bb_txt")