117 lines
4.6 KiB
Python
117 lines
4.6 KiB
Python
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
|
|
|
|
self.raw_btn_orange = None
|
|
self.raw_btn_orange_active = None
|
|
self.raw_sel_inactive = None
|
|
self.raw_sel_active = None
|
|
self._btn_cache = {}
|
|
|
|
self.btn_orange = None
|
|
self.btn_orange_active = None
|
|
self.sel_active = None
|
|
self.sel_inactive = None
|
|
|
|
self.bonus_achieved_img = None
|
|
self.yahtzee_achieved_img = None
|
|
|
|
self.raw_dice = {}
|
|
self.raw_anim_frames = []
|
|
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
|
|
|
|
# 1. Rohe Bilder laden (für dynamisches Skalieren)
|
|
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"])
|
|
|
|
# 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"])
|
|
|
|
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)
|
|
|
|
# 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"
|
|
])
|
|
|
|
# 4. Rohe Würfel für dynamische Größen
|
|
for i in range(1, 7):
|
|
self.raw_dice[i] = load_raw(f"ressources/dices/default/dice{i}.png")
|
|
|
|
# 5. Animations-GIF laden und alle Frames cachen
|
|
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}")
|
|
|
|
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] |