291 lines
12 KiB
Python
291 lines
12 KiB
Python
import tkinter as tk
|
|
import random
|
|
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
|
|
from views.image_die import ImageDie
|
|
from views.toast_chat import ToastChat
|
|
|
|
class BoardView:
|
|
def __init__(self, root_view):
|
|
self.root = root_view
|
|
self.ctrl = root_view.ctrl
|
|
self.assets = root_view.assets
|
|
|
|
self.bg_l = None
|
|
self.bg_r = None
|
|
self.game_title_img = None
|
|
self.game_logo_img = None
|
|
|
|
self.turn_lbl = None
|
|
self.dice_wrapper = None
|
|
self.dice_widgets = []
|
|
self.btn_roll = None
|
|
self.score_buttons = {}
|
|
self.score_labels = {}
|
|
self.bonus_lbl = None
|
|
self.total_lbl = None
|
|
self.bot_total_labels = {}
|
|
self.anim_count = 0
|
|
|
|
def init_ui(self):
|
|
self.root.clear_window(apply_global_bg=False)
|
|
self.root.bind("<space>", lambda e: self.ctrl.roll_dice())
|
|
for i in range(1, 6):
|
|
self.root.bind(str(i), lambda e, idx=i-1: self.ctrl.toggle_die(idx))
|
|
|
|
content_frame = tk.Frame(self.root, bg=BG_COLOR)
|
|
content_frame.pack(fill="both", expand=True, padx=20, pady=10)
|
|
|
|
top_frame = tk.Frame(content_frame, bg=BG_COLOR)
|
|
top_frame.pack(fill="x", pady=5)
|
|
self.turn_lbl = tk.Label(top_frame, text="", font=("Helvetica", 16, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE)
|
|
self.turn_lbl.pack(side="left")
|
|
|
|
right_frame = tk.Frame(top_frame, bg=BG_COLOR)
|
|
right_frame.pack(side="right")
|
|
|
|
# Nur im lokalen Einzelspieler-Modus "Speichern & Beenden" anzeigen
|
|
if not getattr(self.ctrl, "is_multiplayer", False):
|
|
create_img_button(
|
|
right_frame, t("btn_save_quit"),
|
|
self.assets.sel_inactive, self.assets.sel_active,
|
|
self.ctrl.save_and_quit
|
|
).pack(side="right", padx=5)
|
|
|
|
create_img_button(
|
|
right_frame, t("btn_menu"),
|
|
self.assets.sel_inactive, self.assets.sel_active,
|
|
lambda: self.root.dialogs.show_menu_confirm_dialog()
|
|
).pack(side="right", padx=5)
|
|
|
|
create_img_button(
|
|
right_frame, t("btn_help"),
|
|
self.assets.sel_inactive, self.assets.sel_active,
|
|
lambda: self.root.dialogs.show_help_dialog()
|
|
).pack(side="right", padx=5)
|
|
|
|
create_img_button(
|
|
right_frame, t("btn_chat_log"),
|
|
self.assets.sel_inactive, self.assets.sel_active,
|
|
self.root.show_chat_log_dialog
|
|
).pack(side="right", padx=5)
|
|
|
|
main_split = tk.Frame(content_frame, bg=BG_COLOR)
|
|
main_split.pack(fill="both", expand=True, pady=10)
|
|
|
|
self._build_left_panel(main_split)
|
|
self._build_right_panel(main_split)
|
|
self.toast_chat = ToastChat(content_frame)
|
|
# Ganz am Ende von init_ui() in views/board_view.py:
|
|
self.toast_chat = ToastChat(self.root)
|
|
|
|
def _build_left_panel(self, parent):
|
|
self.dice_wrapper = tk.Frame(parent, bg=BG_COLOR)
|
|
self.dice_wrapper.pack(side="left", fill="both", expand=True, padx=5)
|
|
|
|
if self.assets.raw_bg:
|
|
self.bg_l = ResizingBG(self.dice_wrapper, self.assets.raw_bg, bg=BG_COLOR)
|
|
self.bg_l.place(relx=0, rely=0, relwidth=1, relheight=1)
|
|
|
|
dice_inner = tk.Frame(self.dice_wrapper, bg=BG_COLOR)
|
|
dice_inner.place(relx=0.5, rely=0.5, anchor="center")
|
|
|
|
tk.Label(dice_inner, text=t("your_dice"), font=("Helvetica", 16, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=10)
|
|
|
|
dice_row = tk.Frame(dice_inner, bg=BG_COLOR)
|
|
dice_row.pack(pady=15)
|
|
|
|
self.dice_widgets = []
|
|
for i in range(5):
|
|
die = ImageDie(dice_row, self.assets, command=lambda idx=i: self.ctrl.toggle_die(idx))
|
|
die.grid(row=0, column=i, padx=3)
|
|
self.dice_widgets.append(die)
|
|
|
|
if self.assets.raw_logo:
|
|
self.game_logo_img = ImageTk.PhotoImage(self.assets.raw_logo.resize((180, 180), Image.Resampling.LANCZOS))
|
|
tk.Label(dice_inner, image=self.game_logo_img, bg=BG_COLOR, bd=0).pack(pady=10)
|
|
|
|
self.btn_roll = create_img_button(dice_inner, t("btn_roll"), self.assets.btn_orange, self.assets.btn_orange_active, self.ctrl.roll_dice)
|
|
self.btn_roll.pack(pady=10)
|
|
self.dice_wrapper.bind("<Configure>", self._on_dice_wrapper_resize)
|
|
|
|
def _build_right_panel(self, parent):
|
|
score_wrapper = tk.Frame(parent, bg=BG_COLOR)
|
|
score_wrapper.pack(side="right", fill="both", expand=True, padx=5)
|
|
|
|
if self.assets.raw_bg:
|
|
self.bg_r = ResizingBG(score_wrapper, self.assets.raw_bg, bg=BG_COLOR)
|
|
self.bg_r.place(relx=0, rely=0, relwidth=1, relheight=1)
|
|
|
|
score_inner = tk.Frame(score_wrapper, bg=BG_COLOR)
|
|
score_inner.place(relx=0.5, rely=0.52, anchor="center")
|
|
|
|
tk.Label(score_inner, text=t("your_scorecard"), font=("Helvetica", 12, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE).pack(pady=(0, 3))
|
|
grid_frame = tk.Frame(score_inner, bg=BG_COLOR)
|
|
grid_frame.pack(fill="x", pady=1)
|
|
|
|
upper_frame = tk.Frame(grid_frame, bg=BG_COLOR)
|
|
upper_frame.grid(row=0, column=0, sticky="n", padx=4)
|
|
lower_frame = tk.Frame(grid_frame, bg=BG_COLOR)
|
|
lower_frame.grid(row=0, column=1, sticky="n", padx=4)
|
|
|
|
self.score_buttons = {}
|
|
self.score_labels = {}
|
|
|
|
for row, i in enumerate(range(6)):
|
|
cat_key, cat_name_key = CATEGORIES[i]
|
|
btn = create_img_button(
|
|
upper_frame, t(cat_name_key), self.assets.sel_inactive, self.assets.sel_active,
|
|
command=lambda k=cat_key: self.ctrl.score_category(k),
|
|
min_px_width=125, height=27, font_size=10
|
|
)
|
|
btn.grid(row=row, column=0, pady=1, sticky="w")
|
|
self.score_buttons[cat_key] = btn
|
|
lbl = tk.Label(upper_frame, text="-", font=("Courier", 11, "bold"), bg=BG_COLOR, fg="#555", width=4)
|
|
lbl.grid(row=row, column=1, padx=3)
|
|
self.score_labels[cat_key] = lbl
|
|
|
|
tk.Frame(upper_frame, bg="#444", height=1).grid(row=6, column=0, columnspan=2, sticky="ew", pady=2)
|
|
self.bonus_lbl = tk.Label(upper_frame, text=t("bonus_progress", 0), font=("Helvetica", 9, "bold"), bg=BG_COLOR, fg="#888")
|
|
self.bonus_lbl.grid(row=7, column=0, columnspan=2)
|
|
|
|
for row, i in enumerate(range(6, 13)):
|
|
cat_key, cat_name_key = CATEGORIES[i]
|
|
btn = create_img_button(
|
|
lower_frame, t(cat_name_key), self.assets.sel_inactive, self.assets.sel_active,
|
|
command=lambda k=cat_key: self.ctrl.score_category(k),
|
|
min_px_width=125, height=27, font_size=10
|
|
)
|
|
btn.grid(row=row, column=0, pady=1, sticky="w")
|
|
self.score_buttons[cat_key] = btn
|
|
lbl = tk.Label(lower_frame, text="-", font=("Courier", 11, "bold"), bg=BG_COLOR, fg="#555", width=4)
|
|
lbl.grid(row=row, column=1, padx=3)
|
|
self.score_labels[cat_key] = lbl
|
|
|
|
tk.Frame(score_inner, bg="#444", height=1).pack(fill="x", pady=4)
|
|
self.total_lbl = tk.Label(score_inner, text=t("total_score", 0), font=("Courier", 14, "bold"), bg=BG_COLOR, fg=ACCENT_ORANGE)
|
|
self.total_lbl.pack(pady=0)
|
|
|
|
if len(self.ctrl.model.players) > 1:
|
|
bots_frame = tk.Frame(score_inner, bg=BG_COLOR, highlightbackground="#333", highlightthickness=1)
|
|
bots_frame.pack(fill="x", pady=(4, 0))
|
|
tk.Label(bots_frame, text=t("opponents_overview"), font=("Helvetica", 10, "bold"), bg=BG_COLOR, fg=FG_COLOR).pack(pady=(2, 1))
|
|
self.bot_total_labels = {}
|
|
for p in self.ctrl.model.players[1:]:
|
|
row_f = tk.Frame(bots_frame, bg=BG_COLOR)
|
|
row_f.pack(fill="x", padx=6, pady=1)
|
|
lbl = tk.Label(row_f, text=f"{p.display_name}: 0", font=("Helvetica", 9), bg=BG_COLOR, fg=FG_COLOR, anchor="w")
|
|
lbl.pack(side="left", padx=(0, 6))
|
|
self.bot_total_labels[p.name] = lbl
|
|
btn = create_img_button(
|
|
row_f, t("details_btn"), self.assets.sel_inactive, self.assets.sel_active,
|
|
command=lambda p=p: self.root.dialogs.show_bot_details(p),
|
|
min_px_width=70, height=23, font_size=9
|
|
)
|
|
btn.pack(side="right")
|
|
|
|
def _on_dice_wrapper_resize(self, event):
|
|
if event.width < 400:
|
|
new_size = max(40, (event.width - 100) // 5)
|
|
for die in self.dice_widgets:
|
|
if die.current_size != new_size:
|
|
die.resize_die(new_size)
|
|
else:
|
|
for die in self.dice_widgets:
|
|
if die.current_size != 70:
|
|
die.resize_die(70)
|
|
|
|
def set_turn_label(self, text):
|
|
self.turn_lbl.config(text=text)
|
|
|
|
def get_held_dice(self):
|
|
return [die.held for die in self.dice_widgets]
|
|
|
|
def toggle_die_visual(self, idx):
|
|
self.dice_widgets[idx].toggle_hold()
|
|
|
|
def reset_dice_visuals(self):
|
|
for die in self.dice_widgets:
|
|
die.held = False
|
|
die.update_image()
|
|
die.config(bg=BG_COLOR)
|
|
|
|
def disable_input(self):
|
|
self.btn_roll.config(state="disabled")
|
|
for btn in self.score_buttons.values():
|
|
btn.config(state="disabled")
|
|
|
|
def enable_input(self):
|
|
self.btn_roll.config(state="normal")
|
|
|
|
def update_dice_ui(self):
|
|
for i, val in enumerate(self.ctrl.model.dice):
|
|
self.dice_widgets[i].set_value(val)
|
|
if self.ctrl.model.rolls_left > 0:
|
|
self.btn_roll.config(text=t("btn_roll_count", self.ctrl.model.rolls_left), state="normal")
|
|
else:
|
|
self.btn_roll.config(text=t("btn_no_rolls_gui"), state="disabled")
|
|
|
|
def update_scoreboard(self):
|
|
human = self.ctrl.model.players[0]
|
|
active_p = self.ctrl.model.players[self.ctrl.current_player_idx]
|
|
is_human_turn = not active_p.is_bot
|
|
|
|
is_yz = len(set(self.ctrl.model.dice)) == 1
|
|
has_joker = is_yz and human.scores["Y"] == 50
|
|
upper_sum = 0
|
|
|
|
for cat_key, _ in CATEGORIES:
|
|
val = human.scores[cat_key]
|
|
lbl = self.score_labels[cat_key]
|
|
btn = self.score_buttons[cat_key]
|
|
|
|
if val is not None and cat_key in ["1", "2", "3", "4", "5", "6"]:
|
|
upper_sum += val
|
|
|
|
if val is not None:
|
|
lbl.config(text=str(val), fg="#00FF00")
|
|
btn.config(state="disabled")
|
|
else:
|
|
if is_human_turn:
|
|
pot = self.ctrl.model.calculate_score(cat_key, self.ctrl.model.dice, has_joker)
|
|
lbl.config(text=f"({pot})", fg=ACCENT_ORANGE)
|
|
btn.config(state="normal")
|
|
else:
|
|
lbl.config(text="-", fg="#555")
|
|
btn.config(state="disabled")
|
|
|
|
if upper_sum >= 63:
|
|
self.bonus_lbl.config(text=t("bonus_reached"), fg="#00FF00")
|
|
else:
|
|
self.bonus_lbl.config(text=t("bonus_progress", upper_sum), fg="#888")
|
|
|
|
self.total_lbl.config(text=t("total_score", human.total_score))
|
|
for p in self.ctrl.model.players[1:]:
|
|
if p.name in self.bot_total_labels:
|
|
self.bot_total_labels[p.name].config(text=f"{p.display_name}: {p.total_score}")
|
|
|
|
def trigger_roll_animation(self, is_bot=False, callback=None):
|
|
self.btn_roll.config(state="disabled")
|
|
self.anim_count = 0
|
|
def anim_step():
|
|
self.anim_count += 1
|
|
for die in self.dice_widgets:
|
|
if not die.held:
|
|
if self.assets.raw_anim_frames:
|
|
die.show_anim_frame(self.anim_count)
|
|
else:
|
|
die.set_value(random.randint(1, 6))
|
|
if self.anim_count < 15:
|
|
self.root.after(50, anim_step)
|
|
else:
|
|
if callback:
|
|
callback()
|
|
anim_step()
|
|
|
|
def add_chat_message(self, sender, text, is_self_taunt=False):
|
|
if hasattr(self, "toast_chat") and self.toast_chat:
|
|
self.toast_chat.add_message(sender, text, is_self_taunt) |