264 lines
11 KiB
Python
264 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
|
# view/menus/main_menu_view.py
|
|
import tkinter as tk
|
|
|
|
from models.database import Database
|
|
from resources.code_res.game_constants import (
|
|
ACCENT_GOLD, ACCENT_GOTH, BG_COLOR, BORDER_COLOR, CAT_LUCK,
|
|
CAT_ONLINE, ERROR_FG, FG_COLOR, GAME_CATEGORIES, MENU_CARD_SIZE,
|
|
MUTED_FG, PANEL_BG, SUBTLE_BORDER
|
|
)
|
|
from resources.code_res.i18n import t
|
|
from view.dialogs.audio_settings_dialog import AudioSettingsDialog
|
|
from view.widgets.image_button import ImageButton
|
|
|
|
|
|
class MainMenuView:
|
|
def __init__(self, root_view):
|
|
self.root = root_view
|
|
self.ctrl = root_view.ctrl
|
|
self.assets = root_view.assets
|
|
self.sounds = root_view.ctrl.sounds
|
|
self.selected_category: str = CAT_LUCK
|
|
|
|
def show(
|
|
self,
|
|
profile: dict,
|
|
available_games: list[dict],
|
|
devils_resume_only: bool = False,
|
|
category: str | None = None,
|
|
):
|
|
self.root.clear_window(apply_global_bg=True)
|
|
|
|
valid_cats = {c[0] for c in GAME_CATEGORIES}
|
|
if category in valid_cats:
|
|
self.selected_category = category
|
|
Database.save_setting("last_menu_category", category)
|
|
else:
|
|
saved_cat = Database.load_setting("last_menu_category", CAT_LUCK)
|
|
self.selected_category = saved_cat if saved_cat in valid_cats else CAT_LUCK
|
|
|
|
container = tk.Frame(
|
|
self.root, bg=BG_COLOR, padx=26, pady=14,
|
|
highlightthickness=1, highlightbackground=SUBTLE_BORDER
|
|
)
|
|
container.place(relx=0.5, rely=0.52, anchor="center", relwidth=0.92, relheight=0.88)
|
|
|
|
# 3-Spalten-Topbar mit zentriertem title.jpg Banner in Spalte 1
|
|
top_bar = tk.Frame(container, bg=BG_COLOR)
|
|
top_bar.pack(fill="x", pady=(0, 10))
|
|
top_bar.columnconfigure(0, weight=1)
|
|
top_bar.columnconfigure(1, weight=1)
|
|
top_bar.columnconfigure(2, weight=1)
|
|
|
|
left_btns = tk.Frame(top_bar, bg=BG_COLOR)
|
|
left_btns.grid(row=0, column=0, sticky="w")
|
|
|
|
if not devils_resume_only:
|
|
ImageButton(
|
|
left_btns, t("btn_switch_profile"), self.assets.raw_sel, self.assets.raw_sel_active,
|
|
self.ctrl.logout_to_login, sound_mgr=self.sounds, width=145, height=36, font=("Helvetica", 9, "bold")
|
|
).pack(side="left", padx=(0, 5))
|
|
|
|
ImageButton(
|
|
left_btns, t("btn_stats"), self.assets.raw_sel, self.assets.raw_sel_active,
|
|
self.ctrl.show_statistics, sound_mgr=self.sounds, width=110, height=36, font=("Helvetica", 9, "bold")
|
|
).pack(side="left", padx=(0, 5))
|
|
|
|
ImageButton(
|
|
left_btns, t("btn_lang"), self.assets.raw_sel, self.assets.raw_sel_active,
|
|
lambda: self.ctrl.toggle_language(), sound_mgr=self.sounds, width=105, height=36, font=("Helvetica", 9, "bold")
|
|
).pack(side="left", padx=(0, 5))
|
|
|
|
ImageButton(
|
|
left_btns, t("btn_audio"), self.assets.raw_sel, self.assets.raw_sel_active,
|
|
lambda: AudioSettingsDialog.open(self.root, self.assets, self.sounds),
|
|
sound_mgr=self.sounds, width=85, height=36, font=("Helvetica", 9, "bold")
|
|
).pack(side="left")
|
|
|
|
# Mitte: Proportionales Titel-Banner (320px breit -> ~80px hoch)
|
|
header_banner = self.assets.get_proportional_photo("title_main_header", self.assets.raw_title, 320)
|
|
if header_banner:
|
|
hdr_lbl = tk.Label(
|
|
top_bar, image=header_banner, bg=BG_COLOR, bd=0,
|
|
highlightthickness=1, highlightbackground=SUBTLE_BORDER
|
|
)
|
|
hdr_lbl.image = header_banner
|
|
hdr_lbl.grid(row=0, column=1, padx=12)
|
|
else:
|
|
tk.Label(
|
|
top_bar, text=t("header_title"),
|
|
font=("Georgia", 20, "bold"), fg=ACCENT_GOTH, bg=BG_COLOR
|
|
).grid(row=0, column=1, padx=12)
|
|
|
|
acc_box = tk.Frame(top_bar, bg=PANEL_BG, padx=16, pady=7, highlightthickness=1, highlightbackground=BORDER_COLOR)
|
|
acc_box.grid(row=0, column=2, sticky="e")
|
|
tk.Label(
|
|
acc_box,
|
|
text=t("main_acc_soul_only", profile["name"]),
|
|
font=("Georgia", 12, "bold"), fg=ACCENT_GOLD, bg=PANEL_BG
|
|
).pack()
|
|
|
|
tk.Frame(container, bg=SUBTLE_BORDER, height=1).pack(fill="x", pady=(0, 10))
|
|
|
|
cards_section = tk.Frame(container, bg=BG_COLOR)
|
|
cards_section.pack(fill="both", expand=True)
|
|
|
|
# Im aktiven Devil's Deal sind alle Kategorien gesperrt -> nur Resume!
|
|
if devils_resume_only:
|
|
pakt_panel = tk.Frame(
|
|
cards_section, bg=PANEL_BG, padx=36, pady=28,
|
|
highlightthickness=2, highlightbackground=ERROR_FG
|
|
)
|
|
pakt_panel.place(relx=0.5, rely=0.5, anchor="center")
|
|
|
|
tk.Label(
|
|
pakt_panel, text=t("devils_deal_resume_title"),
|
|
font=("Georgia", 18, "bold"), fg=ERROR_FG, bg=PANEL_BG
|
|
).pack(pady=(0, 8))
|
|
tk.Label(
|
|
pakt_panel, text=t("devils_deal_resume_desc"),
|
|
font=("Helvetica", 11), fg=MUTED_FG, bg=PANEL_BG, wraplength=520, justify="center"
|
|
).pack(pady=(0, 22))
|
|
|
|
resume_card = self._create_game_card(
|
|
pakt_panel, t("devils_deal_resume_card"), "", self.ctrl.resume_devils_deal, bg_col=PANEL_BG
|
|
)
|
|
resume_card.pack()
|
|
return
|
|
|
|
# --- FLACHE 4-KATEGORIEN-LEISTE ---
|
|
cat_bar = tk.Frame(
|
|
cards_section, bg=PANEL_BG, padx=16, pady=10,
|
|
highlightthickness=1, highlightbackground=BORDER_COLOR
|
|
)
|
|
cat_bar.pack(fill="x", pady=(0, 12))
|
|
|
|
tabs_wrap = tk.Frame(cat_bar, bg=PANEL_BG)
|
|
tabs_wrap.pack()
|
|
|
|
active_cat_title = ""
|
|
active_cat_desc = ""
|
|
for cat_id, title_key, desc_key in GAME_CATEGORIES:
|
|
c_title = t(title_key)
|
|
is_sel = (cat_id == self.selected_category)
|
|
if is_sel:
|
|
active_cat_title = c_title
|
|
active_cat_desc = t(desc_key)
|
|
|
|
pil_norm = self.assets.raw_btn if is_sel else self.assets.raw_sel
|
|
pil_hov = self.assets.raw_btn_active if is_sel else self.assets.raw_sel_active
|
|
btn_w = 205 if cat_id == "party_offline" else 155
|
|
|
|
ImageButton(
|
|
tabs_wrap, c_title, pil_norm, pil_hov,
|
|
lambda cid=cat_id: self.show(profile, available_games, devils_resume_only=False, category=cid),
|
|
sound_mgr=self.sounds, width=btn_w, height=38, font=("Helvetica", 10, "bold")
|
|
).pack(side="left", padx=6)
|
|
|
|
is_online_tab = (self.selected_category == CAT_ONLINE)
|
|
tk.Label(
|
|
cat_bar,
|
|
text=active_cat_desc,
|
|
font=("Helvetica", 10, "bold" if is_online_tab else "normal"),
|
|
fg=ERROR_FG if is_online_tab else MUTED_FG,
|
|
bg=PANEL_BG,
|
|
).pack(pady=(8, 2))
|
|
|
|
# --- GEFILTERTES SPIELKARTEN-GRID ---
|
|
filtered_games = [g for g in available_games if g.get("category", CAT_LUCK) == self.selected_category]
|
|
|
|
cards_grid = tk.Frame(cards_section, bg=BG_COLOR)
|
|
cards_grid.pack(expand=True)
|
|
|
|
if not filtered_games:
|
|
empty_box = tk.Frame(
|
|
cards_grid, bg=PANEL_BG, padx=32, pady=22,
|
|
highlightthickness=1, highlightbackground=ERROR_FG if is_online_tab else SUBTLE_BORDER
|
|
)
|
|
empty_box.pack(pady=30)
|
|
tk.Label(
|
|
empty_box,
|
|
text=t("no_games_in_category", active_cat_title),
|
|
font=("Georgia", 12, "bold"),
|
|
fg=ERROR_FG if is_online_tab else MUTED_FG,
|
|
bg=PANEL_BG,
|
|
).pack()
|
|
else:
|
|
for idx, game in enumerate(filtered_games):
|
|
prog = game.get("progress")
|
|
sub_txt = t("menu_card_rank_sub", prog["title"], prog["level"]) if prog else ""
|
|
card_widget = self._create_game_card(
|
|
cards_grid,
|
|
game["title"],
|
|
sub_txt,
|
|
lambda g=game: self.ctrl.launch_game(g),
|
|
is_online=is_online_tab,
|
|
)
|
|
card_widget.grid(row=idx // 4, column=idx % 4, padx=18, pady=10)
|
|
|
|
def _create_game_card(
|
|
self, parent, game_title: str, rank_subtitle: str, on_click, bg_col: str = BG_COLOR, is_online: bool = False
|
|
) -> tk.Canvas:
|
|
card_w, card_h = MENU_CARD_SIZE
|
|
cv = tk.Canvas(parent, width=card_w, height=card_h, bg=bg_col, highlightthickness=0, cursor="hand2")
|
|
|
|
base_outline = ERROR_FG if is_online else BORDER_COLOR
|
|
gem_color = ERROR_FG if is_online else ACCENT_GOTH
|
|
|
|
outer_id = cv.create_rectangle(
|
|
4, 4, card_w - 4, card_h - 4,
|
|
fill=PANEL_BG, outline=base_outline, width=2
|
|
)
|
|
inner_id = cv.create_rectangle(
|
|
12, 12, card_w - 12, card_h - 12,
|
|
fill="#16131c", outline=SUBTLE_BORDER, width=1
|
|
)
|
|
# "Helvetica" für Unicode-Ornamente (❖), damit OldLondon kein Kästchen zeichnet
|
|
top_gem = cv.create_text(card_w // 2, 28, text="†" if is_online else "❖", font=("Helvetica", 11, "bold"), fill=gem_color)
|
|
bot_gem = cv.create_text(card_w // 2, card_h - 28, text="†" if is_online else "❖", font=("Helvetica", 11, "bold"), fill=gem_color)
|
|
|
|
longest_word = max((len(w) for w in game_title.replace("\n", " ").split()), default=0)
|
|
title_size = 15 if longest_word >= 11 else 17
|
|
title_y = (card_h // 2 - 22) if rank_subtitle else (card_h // 2)
|
|
|
|
txt_id = cv.create_text(
|
|
card_w // 2, title_y,
|
|
text=game_title, width=card_w - 32, justify="center",
|
|
font=("Georgia", title_size, "bold"), fill=FG_COLOR
|
|
)
|
|
|
|
sub_id = None
|
|
if rank_subtitle:
|
|
cv.create_line(
|
|
36, card_h // 2 + 4, card_w - 36, card_h // 2 + 4,
|
|
fill=SUBTLE_BORDER, width=1
|
|
)
|
|
sub_id = cv.create_text(
|
|
card_w // 2, card_h // 2 + 34,
|
|
text=rank_subtitle, width=card_w - 30, justify="center",
|
|
font=("Helvetica", 9, "bold"), fill=gem_color
|
|
)
|
|
|
|
def _on_enter(_e):
|
|
cv.itemconfigure(outer_id, outline=ACCENT_GOLD)
|
|
cv.itemconfigure(inner_id, fill="#211c2a", outline=gem_color)
|
|
cv.itemconfigure(txt_id, fill=ACCENT_GOLD)
|
|
cv.itemconfigure(top_gem, fill=ACCENT_GOLD)
|
|
cv.itemconfigure(bot_gem, fill=ACCENT_GOLD)
|
|
if sub_id:
|
|
cv.itemconfigure(sub_id, fill="#ffffff")
|
|
|
|
def _on_leave(_e):
|
|
cv.itemconfigure(outer_id, outline=base_outline)
|
|
cv.itemconfigure(inner_id, fill="#16131c", outline=SUBTLE_BORDER)
|
|
cv.itemconfigure(txt_id, fill=FG_COLOR)
|
|
cv.itemconfigure(top_gem, fill=gem_color)
|
|
cv.itemconfigure(bot_gem, fill=gem_color)
|
|
if sub_id:
|
|
cv.itemconfigure(sub_id, fill=gem_color)
|
|
|
|
cv.bind("<Enter>", _on_enter)
|
|
cv.bind("<Leave>", _on_leave)
|
|
cv.bind("<Button-1>", lambda e: (self.sounds.play("click"), on_click()))
|
|
return cv |