233 lines
10 KiB
Python
233 lines
10 KiB
Python
|
|
import tkinter as tk
|
||
|
|
from tkinter import messagebox
|
||
|
|
from PIL import Image, ImageTk
|
||
|
|
from models.constants import CATEGORIES
|
||
|
|
from models.i18n import t
|
||
|
|
from views.widgets import ResizingBG, create_img_button, BG_COLOR, FG_COLOR, ACCENT_ORANGE
|
||
|
|
|
||
|
|
class DialogManager:
|
||
|
|
def __init__(self, root_view):
|
||
|
|
self.root = root_view
|
||
|
|
self.ctrl = root_view.ctrl
|
||
|
|
self.assets = root_view.assets
|
||
|
|
self.overlay_frame = None
|
||
|
|
self.overlay_bg = None
|
||
|
|
self.overlay_anim_lbl = None
|
||
|
|
self.overlay_anim_step = 0
|
||
|
|
|
||
|
|
def show_error(self, title, msg):
|
||
|
|
messagebox.showerror(title, msg)
|
||
|
|
|
||
|
|
def show_info(self, title, msg):
|
||
|
|
messagebox.showinfo(title, msg)
|
||
|
|
|
||
|
|
def show_stats_popup(self, stats):
|
||
|
|
messagebox.showinfo(
|
||
|
|
"Statistiken",
|
||
|
|
f"GEGEN ECHTE GEGNER:\nSiege: {stats[1]} | Niederlagen: {stats[2]}\nHöchste Punkte: {stats[4]}\n\n"
|
||
|
|
f"GEGEN COMPUTER:\nSiege: {stats[5]} | Niederlagen: {stats[6]}\nHöchste Punkte: {stats[8]}"
|
||
|
|
)
|
||
|
|
|
||
|
|
def show_help_dialog(self):
|
||
|
|
top = tk.Toplevel(self.root)
|
||
|
|
top.title("Hilfe & Shortcuts")
|
||
|
|
top.geometry("420x380")
|
||
|
|
top.configure(bg=BG_COLOR)
|
||
|
|
|
||
|
|
tk.Label(top, text="Spiel-Hilfe & Shortcuts", font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
||
|
|
|
||
|
|
help_text = (
|
||
|
|
"• Leertaste: Würfeln\n"
|
||
|
|
"• Tasten 1-5: Würfel halten / freigeben\n"
|
||
|
|
"• Oberer Teil (1-6): Erreiche 63+ Punkte für +35 Bonus!\n"
|
||
|
|
"• Yahtzee: 5 gleiche Würfel (+50 Punkte / Joker)\n"
|
||
|
|
"• Klicke auf 'Details' bei Gegnern, um deren Karte zu sehen."
|
||
|
|
)
|
||
|
|
tk.Label(top, text=help_text, font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="left", padx=20).pack(pady=10, anchor="w")
|
||
|
|
|
||
|
|
create_img_button(top, "Schließen", self.assets.sel_inactive, self.assets.sel_active, top.destroy).pack(pady=20)
|
||
|
|
|
||
|
|
def show_menu_confirm_dialog(self):
|
||
|
|
top = tk.Toplevel(self.root)
|
||
|
|
top.title("Zum Hauptmenü")
|
||
|
|
top.geometry("380x220")
|
||
|
|
top.configure(bg=BG_COLOR)
|
||
|
|
|
||
|
|
tk.Label(top, text="Spielstand verlassen", font=("Helvetica", 13, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
||
|
|
tk.Label(top, text="Möchtest du den aktuellen Fortschritt\nvorher speichern oder verwerfen?", font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="center").pack(pady=5)
|
||
|
|
|
||
|
|
btn_frame = tk.Frame(top, bg=BG_COLOR)
|
||
|
|
btn_frame.pack(pady=20)
|
||
|
|
|
||
|
|
def save_and_exit():
|
||
|
|
top.destroy()
|
||
|
|
self.ctrl.save_and_quit()
|
||
|
|
|
||
|
|
def discard_and_exit():
|
||
|
|
top.destroy()
|
||
|
|
self.ctrl.discard_and_return_menu()
|
||
|
|
|
||
|
|
create_img_button(btn_frame, "Speichern", self.assets.sel_inactive, self.assets.sel_active, save_and_exit).pack(side="left", padx=5)
|
||
|
|
create_img_button(btn_frame, "Verwerfen", self.assets.btn_orange, self.assets.btn_orange_active, discard_and_exit).pack(side="left", padx=5)
|
||
|
|
tk.Button(top, text="Abbrechen", command=top.destroy, bg=BG_COLOR, fg="#888", bd=0, font=("Helvetica", 10)).pack(pady=5)
|
||
|
|
|
||
|
|
def show_achievement_banner(self, raw_img, on_close=None):
|
||
|
|
"""Zeigt das Bonus- oder Yahtzee-Erfolgsbild als Overlay und ruft danach on_close auf."""
|
||
|
|
top = tk.Toplevel(self.root)
|
||
|
|
top.overrideredirect(True)
|
||
|
|
top.geometry("500x300")
|
||
|
|
top.configure(bg=BG_COLOR)
|
||
|
|
top.attributes("-topmost", True)
|
||
|
|
|
||
|
|
self.root.update_idletasks()
|
||
|
|
x = self.root.winfo_x() + (self.root.winfo_width() - 500) // 2
|
||
|
|
y = self.root.winfo_y() + (self.root.winfo_height() - 300) // 2
|
||
|
|
top.geometry(f"+{x}+{y}")
|
||
|
|
|
||
|
|
canvas = tk.Canvas(top, width=500, height=300, bg=BG_COLOR, highlightthickness=0)
|
||
|
|
canvas.pack(fill="both", expand=True)
|
||
|
|
|
||
|
|
if raw_img:
|
||
|
|
img_tk = ImageTk.PhotoImage(raw_img.resize((500, 300), Image.Resampling.LANCZOS))
|
||
|
|
canvas.image_ref = img_tk
|
||
|
|
canvas.create_image(0, 0, image=img_tk, anchor="nw")
|
||
|
|
else:
|
||
|
|
tk.Label(canvas, text="ERREICHT!", font=("Helvetica", 28, "bold"), fg=ACCENT_ORANGE, bg=BG_COLOR).place(relx=0.5, rely=0.5, anchor="center")
|
||
|
|
|
||
|
|
closed = [False]
|
||
|
|
def finish():
|
||
|
|
if not closed[0]:
|
||
|
|
closed[0] = True
|
||
|
|
if top.winfo_exists():
|
||
|
|
top.destroy()
|
||
|
|
if on_close:
|
||
|
|
on_close()
|
||
|
|
|
||
|
|
top.after(2500, finish)
|
||
|
|
|
||
|
|
def ask_zero_score_confirm(self, cat_name, on_confirm):
|
||
|
|
"""Sicherheitsabfrage, wenn eine gewählte Kategorie 0 Punkte bringt."""
|
||
|
|
top = tk.Toplevel(self.root)
|
||
|
|
top.title("0 Punkte bestätigen")
|
||
|
|
top.geometry("400x210")
|
||
|
|
top.configure(bg=BG_COLOR)
|
||
|
|
top.transient(self.root)
|
||
|
|
top.grab_set()
|
||
|
|
|
||
|
|
self.root.update_idletasks()
|
||
|
|
x = self.root.winfo_x() + (self.root.winfo_width() - 400) // 2
|
||
|
|
y = self.root.winfo_y() + (self.root.winfo_height() - 210) // 2
|
||
|
|
top.geometry(f"+{x}+{y}")
|
||
|
|
|
||
|
|
tk.Label(top, text="Kategorie streichen?", font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
||
|
|
tk.Label(
|
||
|
|
top, text=f"Möchtest du für '{cat_name}' wirklich\n0 Punkte eintragen?",
|
||
|
|
font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="center"
|
||
|
|
).pack(pady=5)
|
||
|
|
|
||
|
|
btn_frame = tk.Frame(top, bg=BG_COLOR)
|
||
|
|
btn_frame.pack(pady=20)
|
||
|
|
|
||
|
|
def confirm():
|
||
|
|
top.destroy()
|
||
|
|
on_confirm()
|
||
|
|
|
||
|
|
create_img_button(btn_frame, "Ja, 0 Punkte", self.assets.btn_orange, self.assets.btn_orange_active, confirm).pack(side="left", padx=5)
|
||
|
|
create_img_button(btn_frame, "Abbrechen", self.assets.sel_inactive, self.assets.sel_active, top.destroy).pack(side="left", padx=5)
|
||
|
|
|
||
|
|
def ask_better_option_confirm(self, current_cat_name, better_cat_name, better_pts, on_confirm):
|
||
|
|
"""Warn-Dialog, wenn eine deutlich bessere Kategorie verfügbar ist."""
|
||
|
|
top = tk.Toplevel(self.root)
|
||
|
|
top.title("Bessere Option verfügbar!")
|
||
|
|
top.geometry("420x240")
|
||
|
|
top.configure(bg=BG_COLOR)
|
||
|
|
top.transient(self.root)
|
||
|
|
top.grab_set()
|
||
|
|
|
||
|
|
self.root.update_idletasks()
|
||
|
|
x = self.root.winfo_x() + (self.root.winfo_width() - 420) // 2
|
||
|
|
y = self.root.winfo_y() + (self.root.winfo_height() - 240) // 2
|
||
|
|
top.geometry(f"+{x}+{y}")
|
||
|
|
|
||
|
|
tk.Label(top, text="Bessere Option gefunden!", font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
||
|
|
|
||
|
|
msg = (
|
||
|
|
f"Du möchtest '{current_cat_name}' wählen,\n"
|
||
|
|
f"aber du hast hierfür eine deutlich bessere Option:\n\n"
|
||
|
|
f"👉 {better_cat_name} ({better_pts} Punkte)!"
|
||
|
|
)
|
||
|
|
tk.Label(top, text=msg, font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="center").pack(pady=5)
|
||
|
|
|
||
|
|
btn_frame = tk.Frame(top, bg=BG_COLOR)
|
||
|
|
btn_frame.pack(pady=20)
|
||
|
|
|
||
|
|
def confirm():
|
||
|
|
top.destroy()
|
||
|
|
on_confirm()
|
||
|
|
|
||
|
|
create_img_button(btn_frame, "Trotzdem eintragen", self.assets.sel_inactive, self.assets.sel_active, confirm).pack(side="left", padx=5)
|
||
|
|
create_img_button(btn_frame, "Abbrechen", self.assets.btn_orange, self.assets.btn_orange_active, top.destroy).pack(side="left", padx=5)
|
||
|
|
|
||
|
|
def show_bot_details(self, bot_player):
|
||
|
|
top = tk.Toplevel(self.root)
|
||
|
|
top.title(f"Details: {bot_player.name}")
|
||
|
|
top.geometry("300x480")
|
||
|
|
top.configure(bg=BG_COLOR)
|
||
|
|
|
||
|
|
tk.Label(top, text=f"Wertungskarte: {bot_player.name}", font=("Helvetica", 12, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=10)
|
||
|
|
grid = tk.Frame(top, bg=BG_COLOR)
|
||
|
|
grid.pack(padx=20, pady=10, fill="both", expand=True)
|
||
|
|
|
||
|
|
for idx, (cat_key, cat_name_key) in enumerate(CATEGORIES):
|
||
|
|
tk.Label(grid, text=t(cat_name_key), font=("Helvetica", 10), bg=BG_COLOR, fg=FG_COLOR).grid(row=idx, column=0, sticky="w", pady=2)
|
||
|
|
val = bot_player.scores[cat_key]
|
||
|
|
val_str = str(val) if val is not None else "-"
|
||
|
|
tk.Label(grid, text=val_str, font=("Courier", 10, "bold"), bg=BG_COLOR, fg="#00FF00" if val is not None else "#555").grid(row=idx, column=1, sticky="e", padx=20)
|
||
|
|
|
||
|
|
tk.Label(top, text=f"GESAMT: {bot_player.total_score}", font=("Helvetica", 12, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=10)
|
||
|
|
|
||
|
|
def show_centered_overlay(self, text_msg):
|
||
|
|
# Wenn das Overlay bereits da ist, nur den Text ändern (Null Flackern!)
|
||
|
|
if self.overlay_frame and self.overlay_frame.winfo_exists():
|
||
|
|
if hasattr(self, 'overlay_text_lbl') and self.overlay_text_lbl.winfo_exists():
|
||
|
|
self.overlay_text_lbl.config(text=text_msg)
|
||
|
|
return
|
||
|
|
|
||
|
|
self.overlay_frame = tk.Frame(self.root, bg=BG_COLOR)
|
||
|
|
self.overlay_frame.place(relx=0, rely=0, relwidth=1, relheight=1)
|
||
|
|
|
||
|
|
if self.assets.raw_bg:
|
||
|
|
self.overlay_bg = ResizingBG(self.overlay_frame, self.assets.raw_bg, bg=BG_COLOR)
|
||
|
|
self.overlay_bg.place(relx=0, rely=0, relwidth=1, relheight=1)
|
||
|
|
|
||
|
|
content = tk.Frame(self.overlay_frame, bg=BG_COLOR)
|
||
|
|
content.place(relx=0.5, rely=0.5, anchor="center")
|
||
|
|
|
||
|
|
self.overlay_text_lbl = tk.Label(content, text=text_msg, font=("Helvetica", 20, "bold"), fg=ACCENT_ORANGE, bg=BG_COLOR, wraplength=700)
|
||
|
|
self.overlay_text_lbl.pack(pady=30)
|
||
|
|
|
||
|
|
self.overlay_anim_lbl = tk.Label(content, bg=BG_COLOR, bd=0)
|
||
|
|
self.overlay_anim_lbl.pack(pady=20)
|
||
|
|
|
||
|
|
self.overlay_anim_step = 0
|
||
|
|
self._animate_overlay_gif()
|
||
|
|
|
||
|
|
def _animate_overlay_gif(self):
|
||
|
|
if self.overlay_frame and self.overlay_frame.winfo_exists():
|
||
|
|
if self.assets.anim_cache_large:
|
||
|
|
idx = self.overlay_anim_step % len(self.assets.anim_cache_large)
|
||
|
|
# Direktes Setzen des gecachten PhotoImage
|
||
|
|
self.overlay_anim_lbl.config(image=self.assets.anim_cache_large[idx])
|
||
|
|
self.overlay_anim_step += 1
|
||
|
|
self.anim_job = self.root.after(80, self._animate_overlay_gif)
|
||
|
|
|
||
|
|
def hide_centered_overlay(self):
|
||
|
|
if hasattr(self, 'anim_job'):
|
||
|
|
try:
|
||
|
|
self.root.after_cancel(self.anim_job)
|
||
|
|
except Exception:
|
||
|
|
pass
|
||
|
|
if self.overlay_frame and self.overlay_frame.winfo_exists():
|
||
|
|
self.overlay_frame.destroy()
|
||
|
|
self.overlay_frame = None
|