2026-11-02 18:32:36 +00:00
|
|
|
import tkinter as tk
|
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
|
from models.constants import CATEGORIES
|
|
|
|
|
from models.i18n import t
|
2026-11-02 21:43:42 +00:00
|
|
|
from views.theme import create_img_button, BG_COLOR, FG_COLOR, ACCENT_ORANGE
|
|
|
|
|
from views.resizing_bg import ResizingBG
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
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
|
2026-11-02 21:43:42 +00:00
|
|
|
self.overlay_text_lbl = None
|
2026-11-02 18:32:36 +00:00
|
|
|
self.overlay_anim_lbl = None
|
|
|
|
|
self.overlay_anim_step = 0
|
2026-11-02 21:43:42 +00:00
|
|
|
self.anim_job = None
|
|
|
|
|
|
|
|
|
|
def _create_styled_modal(self, title, width=420, height=260):
|
|
|
|
|
top = tk.Toplevel(self.root)
|
|
|
|
|
top.title(title)
|
|
|
|
|
top.geometry(f"{width}x{height}")
|
|
|
|
|
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() - width) // 2
|
|
|
|
|
y = self.root.winfo_y() + (self.root.winfo_height() - height) // 2
|
|
|
|
|
top.geometry(f"+{x}+{y}")
|
|
|
|
|
return top
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def show_error(self, title, msg):
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def show_info(self, title, msg):
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def show_stats_popup(self, stats):
|
2026-11-02 21:43:42 +00:00
|
|
|
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, on_close=None):
|
|
|
|
|
height = 260 + len(scoreboard) * 38
|
|
|
|
|
top = self._create_styled_modal(t("game_over_title"), 480, height)
|
|
|
|
|
|
|
|
|
|
if self.assets.raw_bg:
|
|
|
|
|
bg = ResizingBG(top, self.assets.raw_bg, bg=BG_COLOR)
|
|
|
|
|
bg.place(relx=0, rely=0, relwidth=1, relheight=1)
|
|
|
|
|
|
|
|
|
|
content = tk.Frame(top, bg=BG_COLOR)
|
|
|
|
|
content.place(relx=0.5, rely=0.5, anchor="center")
|
|
|
|
|
|
|
|
|
|
tk.Label(content, text=t("game_over_title").upper(), font=("Helvetica", 20, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=(10, 5))
|
|
|
|
|
tk.Label(content, text=t("leaderboard"), font=("Helvetica", 12, "bold"), bg=BG_COLOR, fg="#888").pack(pady=(0, 15))
|
|
|
|
|
|
|
|
|
|
board_frame = tk.Frame(content, bg=BG_COLOR, highlightbackground="#333", highlightthickness=1, padx=15, pady=10)
|
|
|
|
|
board_frame.pack(fill="x", pady=5)
|
|
|
|
|
|
|
|
|
|
for rank, (disp_name, score) in enumerate(scoreboard, 1):
|
|
|
|
|
row = tk.Frame(board_frame, bg=BG_COLOR)
|
|
|
|
|
row.pack(fill="x", pady=4)
|
|
|
|
|
color = "#FFD700" if rank == 1 else FG_COLOR
|
|
|
|
|
font_style = ("Helvetica", 12, "bold") if rank == 1 else ("Helvetica", 11)
|
|
|
|
|
|
|
|
|
|
tk.Label(row, text=f"{rank}. {disp_name}", font=font_style, bg=BG_COLOR, fg=color, anchor="w").pack(side="left", padx=(0, 20))
|
|
|
|
|
tk.Label(row, text=f"{score} {t('points_word')}", font=("Courier", 12, "bold"), bg=BG_COLOR, fg=color, anchor="e").pack(side="right")
|
|
|
|
|
|
|
|
|
|
def finish():
|
|
|
|
|
top.destroy()
|
|
|
|
|
if on_close:
|
|
|
|
|
on_close()
|
|
|
|
|
|
|
|
|
|
top.protocol("WM_DELETE_WINDOW", finish)
|
|
|
|
|
create_img_button(content, t("close_btn"), self.assets.btn_orange, self.assets.btn_orange_active, finish).pack(pady=20)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def show_help_dialog(self):
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def show_menu_confirm_dialog(self):
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def show_achievement_banner(self, raw_img, on_close=None):
|
|
|
|
|
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}")
|
|
|
|
|
|
2026-11-02 21:43:42 +00:00
|
|
|
canvas = tk.Canvas(top, width=500, height=300, bg=BG_COLOR, highlightthickness=0, bd=0)
|
2026-11-02 18:32:36 +00:00
|
|
|
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:
|
2026-11-02 21:43:42 +00:00
|
|
|
tk.Label(canvas, text=t("achieved_fallback"), font=("Helvetica", 28, "bold"), fg=ACCENT_ORANGE, bg=BG_COLOR).place(relx=0.5, rely=0.5, anchor="center")
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
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)
|
2026-11-02 21:43:42 +00:00
|
|
|
canvas.bind("<Button-1>", lambda e: finish())
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def ask_zero_score_confirm(self, cat_name, on_confirm):
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
tk.Label(
|
2026-11-02 21:43:42 +00:00
|
|
|
top, text=t("zero_confirm_msg", cat_name),
|
2026-11-02 18:32:36 +00:00
|
|
|
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()
|
|
|
|
|
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
|
|
|
|
|
2026-11-02 18:32:36 +00:00
|
|
|
def ask_better_option_confirm(self, current_cat_name, better_cat_name, better_pts, on_confirm):
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
btn_frame = tk.Frame(top, bg=BG_COLOR)
|
|
|
|
|
btn_frame.pack(pady=20)
|
|
|
|
|
|
|
|
|
|
def confirm():
|
|
|
|
|
top.destroy()
|
|
|
|
|
on_confirm()
|
|
|
|
|
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
|
|
|
|
|
2026-11-02 18:32:36 +00:00
|
|
|
def show_bot_details(self, bot_player):
|
2026-11-02 21:43:42 +00:00
|
|
|
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)
|
2026-11-02 18:32:36 +00:00
|
|
|
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)
|
|
|
|
|
|
2026-11-02 21:43:42 +00:00
|
|
|
tk.Label(top, text=t("total_score", bot_player.total_score), font=("Helvetica", 12, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=10)
|
2026-11-02 18:32:36 +00:00
|
|
|
|
|
|
|
|
def show_centered_overlay(self, text_msg):
|
|
|
|
|
if self.overlay_frame and self.overlay_frame.winfo_exists():
|
2026-11-02 21:43:42 +00:00
|
|
|
if self.overlay_text_lbl and self.overlay_text_lbl.winfo_exists():
|
2026-11-02 18:32:36 +00:00
|
|
|
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)
|
|
|
|
|
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):
|
2026-11-02 21:43:42 +00:00
|
|
|
if self.anim_job:
|
2026-11-02 18:32:36 +00:00
|
|
|
try:
|
|
|
|
|
self.root.after_cancel(self.anim_job)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
2026-11-02 21:43:42 +00:00
|
|
|
self.anim_job = None
|
2026-11-02 18:32:36 +00:00
|
|
|
if self.overlay_frame and self.overlay_frame.winfo_exists():
|
|
|
|
|
self.overlay_frame.destroy()
|
2026-11-02 21:43:42 +00:00
|
|
|
self.overlay_frame = None
|