358 lines
17 KiB
Python
358 lines
17 KiB
Python
import tkinter as tk
|
|
from PIL import Image, ImageTk
|
|
from models.constants import CATEGORIES
|
|
from models.i18n import t
|
|
from views.theme import create_img_button, BG_COLOR, FG_COLOR, ACCENT_ORANGE
|
|
from views.resizing_bg import ResizingBG
|
|
|
|
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_text_lbl = None
|
|
self.overlay_anim_lbl = None
|
|
self.overlay_anim_step = 0
|
|
self.anim_job = None
|
|
|
|
def _create_styled_modal(self, title, width=420, height=260):
|
|
top = tk.Toplevel(self.root)
|
|
top.title(title)
|
|
top.configure(bg=BG_COLOR)
|
|
top.transient(self.root)
|
|
|
|
self.root.update_idletasks()
|
|
x = self.root.winfo_x() + (self.root.winfo_width() - width) // 2
|
|
y = self.root.winfo_y() + (self.root.winfo_height() - height) // 2
|
|
top.geometry(f"{width}x{height}+{x}+{y}")
|
|
|
|
def safe_grab():
|
|
if top.winfo_exists():
|
|
try:
|
|
top.wait_visibility()
|
|
top.grab_set()
|
|
except Exception:
|
|
pass
|
|
|
|
top.after(30, safe_grab)
|
|
return top
|
|
|
|
def show_error(self, title, msg):
|
|
top = self._create_styled_modal(title, 400, 200)
|
|
tk.Label(top, text=title, font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg="#FF4444").pack(pady=15)
|
|
tk.Label(top, text=msg, font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="center", wraplength=360).pack(pady=10)
|
|
create_img_button(top, t("close_btn"), self.assets.btn_orange, self.assets.btn_orange_active, top.destroy).pack(pady=15)
|
|
|
|
def show_info(self, title, msg):
|
|
top = self._create_styled_modal(title, 420, 260)
|
|
tk.Label(top, text=title, font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
|
tk.Label(top, text=msg, font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="center", wraplength=380).pack(pady=10)
|
|
create_img_button(top, t("close_btn"), self.assets.btn_orange, self.assets.btn_orange_active, top.destroy).pack(pady=15)
|
|
|
|
def show_stats_popup(self, stats):
|
|
top = self._create_styled_modal(t("stats_title"), 420, 310)
|
|
tk.Label(top, text=t("stats_title"), font=("Helvetica", 16, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
|
msg = t("stats_popup_text", stats[1], stats[2], stats[4], stats[5], stats[6], stats[8])
|
|
tk.Label(top, text=msg, font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="center").pack(pady=10)
|
|
create_img_button(top, t("close_btn"), self.assets.sel_inactive, self.assets.sel_active, top.destroy).pack(pady=15)
|
|
|
|
def show_game_over_dialog(self, scoreboard, human_won=True, on_close=None):
|
|
"""Zeigt das Spielende mit Unterüberschrift, win.gif / lose.gif und Rangliste an."""
|
|
go_frame = tk.Frame(self.root, bg=BG_COLOR, highlightbackground=ACCENT_ORANGE, highlightthickness=2)
|
|
go_frame.place(relx=0.33, rely=0.5, width=480, height=540, anchor="center")
|
|
go_frame.lift()
|
|
|
|
if self.assets.raw_bg:
|
|
bg = ResizingBG(go_frame, self.assets.raw_bg, bg=BG_COLOR)
|
|
bg.place(relx=0, rely=0, relwidth=1, relheight=1)
|
|
|
|
content = tk.Frame(go_frame, bg=BG_COLOR)
|
|
content.place(relx=0.5, rely=0.5, anchor="center")
|
|
|
|
# Hauptüberschrift
|
|
tk.Label(
|
|
content, text=t("game_over_title").upper(),
|
|
font=("Helvetica", 20, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE
|
|
).pack(pady=(6, 2))
|
|
|
|
# Neue Unterüberschrift: "GEWONNEN!!!" oder "Leider verloren...."
|
|
sub_text = t("game_over_win_sub") if human_won else t("game_over_lose_sub")
|
|
sub_color = "#00FF00" if human_won else "#FF4444"
|
|
tk.Label(
|
|
content, text=sub_text,
|
|
font=("Helvetica", 15, "bold"), bg=BG_COLOR, fg=sub_color
|
|
).pack(pady=(0, 6))
|
|
|
|
# Animiertes GIF (win.gif oder lose.gif)
|
|
frames = self.assets.win_frames if human_won else self.assets.lose_frames
|
|
gif_lbl = tk.Label(content, bg=BG_COLOR, bd=0)
|
|
gif_lbl.pack(pady=4)
|
|
|
|
anim_state = {"idx": 0, "job": None}
|
|
def animate_result_gif():
|
|
if go_frame.winfo_exists() and frames:
|
|
frame_img = frames[anim_state["idx"] % len(frames)]
|
|
gif_lbl.config(image=frame_img)
|
|
anim_state["idx"] += 1
|
|
anim_state["job"] = self.root.after(90, animate_result_gif)
|
|
|
|
if frames:
|
|
animate_result_gif()
|
|
|
|
tk.Label(
|
|
content, text=t("leaderboard"),
|
|
font=("Helvetica", 11, "bold"), bg=BG_COLOR, fg="#888"
|
|
).pack(pady=(4, 6))
|
|
|
|
board_frame = tk.Frame(content, bg=BG_COLOR, highlightbackground="#333", highlightthickness=1, padx=15, pady=8)
|
|
board_frame.pack(fill="x", pady=4)
|
|
|
|
for rank, (disp_name, score) in enumerate(scoreboard, 1):
|
|
row = tk.Frame(board_frame, bg=BG_COLOR)
|
|
row.pack(fill="x", pady=3)
|
|
color = "#FFD700" if rank == 1 else FG_COLOR
|
|
font_style = ("Helvetica", 11, "bold") if rank == 1 else ("Helvetica", 10)
|
|
|
|
tk.Label(row, text=f"{rank}. {disp_name}", font=font_style, bg=BG_COLOR, fg=color, anchor="w").pack(side="left", padx=(0, 16))
|
|
tk.Label(row, text=f"{score} {t('points_word')}", font=("Courier", 11, "bold"), bg=BG_COLOR, fg=color, anchor="e").pack(side="right")
|
|
|
|
def finish():
|
|
if anim_state["job"]:
|
|
try:
|
|
self.root.after_cancel(anim_state["job"])
|
|
except Exception:
|
|
pass
|
|
if go_frame.winfo_exists():
|
|
go_frame.destroy()
|
|
if on_close:
|
|
on_close()
|
|
|
|
create_img_button(content, t("close_btn"), self.assets.btn_orange, self.assets.btn_orange_active, finish).pack(pady=12)
|
|
|
|
if hasattr(self.root, "toast_chat") and self.root.toast_chat:
|
|
self.root.toast_chat.lift()
|
|
|
|
def show_help_dialog(self):
|
|
top = self._create_styled_modal(t("help_title"), 440, 360)
|
|
tk.Label(top, text=t("help_header"), font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
|
tk.Label(top, text=t("help_text"), font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="left", padx=20).pack(pady=10, anchor="w")
|
|
create_img_button(top, t("close_btn"), self.assets.sel_inactive, self.assets.sel_active, top.destroy).pack(pady=20)
|
|
|
|
def show_chat_log_dialog(self, history_list):
|
|
top = self._create_styled_modal(t("chat_log_title"), 540, 420)
|
|
tk.Label(
|
|
top, text=t("chat_log_title"),
|
|
font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE
|
|
).pack(pady=(12, 8))
|
|
|
|
log_frame = tk.Frame(top, bg="#16141F", highlightbackground="#38304C", highlightthickness=1)
|
|
log_frame.pack(fill="both", expand=True, padx=16, pady=6)
|
|
|
|
scrollbar = tk.Scrollbar(log_frame, bg="#222", troughcolor=BG_COLOR)
|
|
scrollbar.pack(side="right", fill="y")
|
|
|
|
text_widget = tk.Text(
|
|
log_frame, bg="#16141F", fg=FG_COLOR, font=("Helvetica", 10),
|
|
bd=0, highlightthickness=0, wrap="word", yscrollcommand=scrollbar.set,
|
|
padx=10, pady=8
|
|
)
|
|
text_widget.pack(side="left", fill="both", expand=True)
|
|
scrollbar.config(command=text_widget.yview)
|
|
|
|
text_widget.tag_configure("time", foreground="#777777", font=("Courier", 9))
|
|
text_widget.tag_configure("roll", foreground="#00E676", font=("Helvetica", 10, "bold"))
|
|
text_widget.tag_configure("taunt", foreground=ACCENT_ORANGE, font=("Helvetica", 10, "bold"))
|
|
text_widget.tag_configure("chat", foreground="#FF4444", font=("Helvetica", 10, "bold"))
|
|
text_widget.tag_configure("msg", foreground=FG_COLOR, font=("Helvetica", 10))
|
|
|
|
if not history_list:
|
|
text_widget.insert("end", t("chat_log_empty"), "msg")
|
|
else:
|
|
for item in history_list:
|
|
tag = "roll" if item.get("is_roll_log") else ("taunt" if item["color"] == ACCENT_ORANGE else "chat")
|
|
text_widget.insert("end", f"[{item['time']}] ", "time")
|
|
text_widget.insert("end", f"{item['sender']}: ", tag)
|
|
text_widget.insert("end", f"{item['text']}\n", "msg")
|
|
|
|
text_widget.see("end")
|
|
text_widget.config(state="disabled")
|
|
|
|
create_img_button(top, t("close_btn"), self.assets.sel_inactive, self.assets.sel_active, top.destroy).pack(pady=12)
|
|
|
|
def show_menu_confirm_dialog(self):
|
|
top = self._create_styled_modal(t("leave_title"), 400, 230)
|
|
tk.Label(top, text=t("leave_header"), font=("Helvetica", 13, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
|
tk.Label(top, text=t("leave_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 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, t("save_btn"), self.assets.sel_inactive, self.assets.sel_active, save_and_exit).pack(side="left", padx=5)
|
|
create_img_button(btn_frame, t("discard_btn"), self.assets.btn_orange, self.assets.btn_orange_active, discard_and_exit).pack(side="left", padx=5)
|
|
tk.Button(top, text=t("cancel_btn"), command=top.destroy, bg=BG_COLOR, fg="#888", bd=0, font=("Helvetica", 10)).pack(pady=5)
|
|
|
|
def show_achievement_banner(self, raw_img, fallback_title="ACHIEVED!", on_close=None):
|
|
"""Zeigt das Bonus- oder Yahtzee-Banner direkt zentriert im Hauptfenster an."""
|
|
banner_overlay = tk.Frame(self.root, bg=BG_COLOR, highlightbackground=ACCENT_ORANGE, highlightthickness=2)
|
|
banner_overlay.place(relx=0.5, rely=0.5, width=520, height=320, anchor="center")
|
|
banner_overlay.lift()
|
|
|
|
canvas = tk.Canvas(banner_overlay, width=500, height=300, bg=BG_COLOR, highlightthickness=0, bd=0)
|
|
canvas.pack(fill="both", expand=True, padx=8, pady=8)
|
|
|
|
if raw_img:
|
|
img_tk = ImageTk.PhotoImage(raw_img.resize((500, 300), Image.Resampling.LANCZOS))
|
|
canvas.image_ref = img_tk
|
|
canvas.create_image(250, 150, image=img_tk, anchor="center")
|
|
else:
|
|
# Schicker Fallback im Goth-Stil, falls das Bild nicht geladen wurde
|
|
tk.Label(
|
|
canvas, text=fallback_title, font=("Helvetica", 28, "bold"),
|
|
fg=ACCENT_ORANGE, bg=BG_COLOR
|
|
).place(relx=0.5, rely=0.42, anchor="center")
|
|
tk.Label(
|
|
canvas, text=t("achieved_fallback"), font=("Helvetica", 16, "bold"),
|
|
fg="#00FF00", bg=BG_COLOR
|
|
).place(relx=0.5, rely=0.65, anchor="center")
|
|
|
|
closed = [False]
|
|
def finish(event=None):
|
|
if not closed[0]:
|
|
closed[0] = True
|
|
if banner_overlay.winfo_exists():
|
|
banner_overlay.destroy()
|
|
if on_close:
|
|
on_close()
|
|
|
|
self.root.after(2500, finish)
|
|
banner_overlay.bind("<Button-1>", finish)
|
|
canvas.bind("<Button-1>", finish)
|
|
|
|
def ask_zero_score_confirm(self, cat_name, on_confirm):
|
|
top = self._create_styled_modal(t("zero_confirm_title"), 400, 210)
|
|
tk.Label(top, text=t("zero_confirm_header"), font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
|
tk.Label(
|
|
top, text=t("zero_confirm_msg", cat_name),
|
|
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, t("zero_confirm_yes"), self.assets.btn_orange, self.assets.btn_orange_active, confirm).pack(side="left", padx=5)
|
|
create_img_button(btn_frame, t("cancel_btn"), 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):
|
|
top = self._create_styled_modal(t("better_opt_title"), 420, 240)
|
|
tk.Label(top, text=t("better_opt_header"), font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
|
tk.Label(
|
|
top, text=t("better_opt_msg", current_cat_name, better_cat_name, better_pts),
|
|
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, t("better_opt_anyway"), self.assets.sel_inactive, self.assets.sel_active, confirm).pack(side="left", padx=5)
|
|
create_img_button(btn_frame, t("cancel_btn"), self.assets.btn_orange, self.assets.btn_orange_active, top.destroy).pack(side="left", padx=5)
|
|
|
|
def show_bot_details(self, bot_player):
|
|
top = self._create_styled_modal(t("bot_details_title", bot_player.display_name), 340, 490)
|
|
tk.Label(top, text=t("bot_scorecard_header", bot_player.display_name), font=("Helvetica", 11, "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=t("total_score", bot_player.total_score), font=("Helvetica", 12, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=10)
|
|
|
|
def show_centered_overlay(self, text_msg):
|
|
if self.overlay_frame and self.overlay_frame.winfo_exists():
|
|
if self.overlay_text_lbl and self.overlay_text_lbl.winfo_exists():
|
|
self.overlay_text_lbl.config(text=text_msg)
|
|
if hasattr(self.root.board, "toast_chat") and self.root.board.toast_chat:
|
|
self.root.board.toast_chat.lift()
|
|
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()
|
|
|
|
# Wichtig: Toast-Chat immer ÜBER das Bot-Overlay heben!
|
|
if hasattr(self.root.board, "toast_chat") and self.root.board.toast_chat:
|
|
self.root.board.toast_chat.lift()
|
|
|
|
def ask_roll_warning_confirm(self, header_text, body_text, on_confirm):
|
|
top = self._create_styled_modal(t("roll_warn_title"), 450, 260)
|
|
tk.Label(top, text=header_text, font=("Helvetica", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=15)
|
|
tk.Label(
|
|
top, text=body_text,
|
|
font=("Helvetica", 11), bg=BG_COLOR, fg=FG_COLOR, justify="center", wraplength=410
|
|
).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, t("roll_anyway_btn"), self.assets.sel_inactive, self.assets.sel_active, confirm).pack(side="left", padx=5)
|
|
create_img_button(btn_frame, t("hold_and_stay_btn"), self.assets.btn_orange, self.assets.btn_orange_active, top.destroy).pack(side="left", padx=5)
|
|
|
|
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)
|
|
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 self.anim_job:
|
|
try:
|
|
self.root.after_cancel(self.anim_job)
|
|
except Exception:
|
|
pass
|
|
self.anim_job = None
|
|
if self.overlay_frame and self.overlay_frame.winfo_exists():
|
|
self.overlay_frame.destroy()
|
|
self.overlay_frame = None
|