yahtzee-py/views/asset_manager.py

117 lines
4.6 KiB
Python
Raw Normal View History

2026-11-02 18:32:36 +00:00
import os
import sys
try:
from PIL import Image, ImageTk, ImageSequence
HAS_PIL = True
except ImportError:
HAS_PIL = False
print("WARNUNG: Pillow (PIL) nicht gefunden!")
class AssetManager:
"""Lädt alle Bilder und hält die Referenzen dauerhaft im Speicher."""
def __init__(self):
self.raw_bg = None
self.raw_title = None
self.raw_logo = None
2026-11-02 21:43:42 +00:00
self.raw_btn_orange = None
self.raw_btn_orange_active = None
self.raw_sel_inactive = None
self.raw_sel_active = None
self._btn_cache = {}
2026-11-02 18:32:36 +00:00
self.btn_orange = None
self.btn_orange_active = None
self.sel_active = None
self.sel_inactive = None
2026-11-02 21:43:42 +00:00
self.bonus_achieved_img = None
self.yahtzee_achieved_img = None
2026-11-02 18:32:36 +00:00
self.raw_dice = {}
2026-11-02 21:43:42 +00:00
self.raw_anim_frames = []
2026-11-02 18:32:36 +00:00
self.anim_cache_large = []
if not HAS_PIL:
return
def get_full_path(rel_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, rel_path)
def load_raw(paths):
if isinstance(paths, str):
paths = [paths]
for p in paths:
full_path = get_full_path(p)
if os.path.exists(full_path):
return Image.open(full_path).convert("RGBA")
return None
2026-11-02 21:43:42 +00:00
# 1. Rohe Bilder laden (für dynamisches Skalieren)
2026-11-02 18:32:36 +00:00
self.raw_bg = load_raw(["ressources/background.jpeg", "ressources/background.jpg", "ressources/background.png"])
self.raw_title = load_raw(["ressources/title1.png"])
self.raw_logo = load_raw(["ressources/logo.jpg", "ressources/logo.png"])
2026-11-02 21:43:42 +00:00
# 2. Rohe UI-Buttons laden + Standard-Instanzen erzeugen
self.raw_btn_orange = load_raw(["ressources/button1.png", "ressources/button1.jpeg"])
self.raw_btn_orange_active = load_raw(["ressources/button1_active.png"])
self.raw_sel_inactive = load_raw(["ressources/selection_inactive.png"])
self.raw_sel_active = load_raw(["ressources/selection_active.png"])
2026-11-02 18:32:36 +00:00
2026-11-02 21:43:42 +00:00
self.btn_orange, self.btn_orange_active = self.get_btn_pair("orange", 200, 50)
self.sel_inactive, self.sel_active = self.get_btn_pair("purple", 150, 35)
2026-11-02 18:32:36 +00:00
2026-11-02 21:43:42 +00:00
# 3. Erfolgs-Banner laden
self.bonus_achieved_img = load_raw([
"ressources/bonus_achived.jpg",
"ressources/bonus_achieved.jpg",
"ressources/bonus_achieved.png"
])
self.yahtzee_achieved_img = load_raw([
"ressources/yahtzee_achived.jpg",
"ressources/yahtzee_achieved.jpg",
"ressources/yahtzee_achieved.png"
])
2026-11-02 18:32:36 +00:00
2026-11-02 21:43:42 +00:00
# 4. Rohe Würfel für dynamische Größen
2026-11-02 18:32:36 +00:00
for i in range(1, 7):
self.raw_dice[i] = load_raw(f"ressources/dices/default/dice{i}.png")
2026-11-02 21:43:42 +00:00
# 5. Animations-GIF laden und alle Frames cachen
2026-11-02 18:32:36 +00:00
gif_paths = ["ressources/dice.gif", "ressources/dices/dice.gif", "ressources/dice_2.gif"]
anim_full_path = None
for gp in gif_paths:
full = get_full_path(gp)
if os.path.exists(full):
anim_full_path = full
break
if anim_full_path:
try:
gif = Image.open(anim_full_path)
for frame in ImageSequence.Iterator(gif):
f_rgba = frame.copy().convert("RGBA")
self.raw_anim_frames.append(f_rgba)
self.anim_cache_large.append(ImageTk.PhotoImage(f_rgba.resize((150, 150), Image.Resampling.LANCZOS)))
except Exception as e:
print(f"Fehler beim Laden des GIFs: {e}")
2026-11-02 21:43:42 +00:00
def get_btn_pair(self, style="purple", width=150, height=35):
"""Gibt ein exakt auf (width, height) skaliertes Bildpaar (normal, hover) zurück."""
if not HAS_PIL:
return None, None
key = (style, int(width), int(height))
if key not in self._btn_cache:
if style == "orange":
r_norm, r_act = self.raw_btn_orange, self.raw_btn_orange_active
else:
r_norm, r_act = self.raw_sel_inactive, self.raw_sel_active
img_n = ImageTk.PhotoImage(r_norm.resize((int(width), int(height)), Image.Resampling.LANCZOS)) if r_norm else None
img_a = ImageTk.PhotoImage(r_act.resize((int(width), int(height)), Image.Resampling.LANCZOS)) if r_act else img_n
self._btn_cache[key] = (img_n, img_a)
return self._btn_cache[key]