# -*- coding: utf-8 -*- # view/menus/login_view.py from __future__ import annotations import tkinter as tk from resources.code_res.game_constants import ( BG_COLOR, FONT_COLOR_HEADING_SUB, FONT_COLOR_TITLE_MAIN, FONT_SIZE_BODY, FONT_SIZE_TITLE_LG, SUBTLE_BORDER, ) from resources.code_res.i18n import t from view.dialogs.audio_settings_dialog import AudioSettingsDialog from view.widgets.goth_dropdown import GothDropdown from view.widgets.goth_entry import GothEntry from view.widgets.image_button import ImageButton class LoginView: 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.profile_var: tk.StringVar | None = None self.new_name_var: tk.StringVar | None = None self._photo_refs: list = [] self._profiles: list[str] = [] self._dropdown: GothDropdown | None = None self._entry: GothEntry | None = None def show(self, profiles: list[str], preselect_name: str = ""): GothDropdown.close_any_open_popup() self.root.clear_window(apply_global_bg=True) self._photo_refs.clear() self._profiles = list(profiles) cv = self.root.bg_widget if not isinstance(cv, tk.Canvas): cv = tk.Canvas(self.root, bg=BG_COLOR, highlightthickness=0, bd=0) cv.place(relx=0, rely=0, relwidth=1, relheight=1) self._cv = cv btn_norm = self.assets.raw_btn_default btn_purp = self.assets.raw_btn_purple btn_fire = self.assets.raw_btn_fire or btn_purp # Obere rechte Steuerung (Sprache & Audio) ohne Kasten self._btn_lang = ImageButton( cv, t("btn_lang"), btn_norm, btn_purp, lambda: self.ctrl.toggle_language(), sound_mgr=self.sounds, width=116, height=36, style="amethyst" ) self._btn_audio = ImageButton( cv, t("btn_audio"), btn_norm, btn_purp, lambda: AudioSettingsDialog.open(self.root, self.assets, self.sounds), sound_mgr=self.sounds, width=102, height=36, style="amethyst" ) # Vorhandenes Profil wählen (GothDropdown + Feuer-Button) self._dropdown = None self._btn_login = None if self._profiles: initial_prof = preselect_name if preselect_name in self._profiles else self._profiles[0] self.profile_var = tk.StringVar(value=initial_prof) self._dropdown = GothDropdown( cv, self.assets, values=self._profiles, textvariable=self.profile_var, sound_mgr=self.sounds, width=270, height=42, ) self._btn_login = ImageButton( cv, t("login_btn"), btn_fire, btn_fire, lambda: self.ctrl.do_login(self.profile_var.get()), sound_mgr=self.sounds, width=168, height=42, style="fire" ) # Neues Seelen-Profil anlegen (GothEntry + Button) self.new_name_var = tk.StringVar() self._entry = GothEntry( cv, self.assets, width=270, height=42, textvariable=self.new_name_var, ) self._entry.bind_return(lambda _e: self._submit_new_profile()) new_btn_pil = btn_fire if not self._profiles else btn_norm new_btn_hov = btn_fire if not self._profiles else btn_purp new_btn_style = "fire" if not self._profiles else "amethyst" self._btn_create = ImageButton( cv, t("new_profile_btn"), new_btn_pil, new_btn_hov, self._submit_new_profile, sound_mgr=self.sounds, width=168, height=42, style=new_btn_style ) cv.bind("", self._on_canvas_resize, add="+") self.root.update_idletasks() w = max(800, cv.winfo_width()) h = max(600, cv.winfo_height()) self._draw_scene(w, h) if not self._profiles and self._entry is not None: self._entry.entry.focus_set() def _submit_new_profile(self): if self._entry is None: return name = self._entry.get().strip() if name: self.ctrl.do_create_profile(name) def _on_canvas_resize(self, event): if event.widget != self._cv: return GothDropdown.close_any_open_popup() w, h = max(1, event.width), max(1, event.height) if w >= 100 and h >= 100: self._draw_scene(w, h) def _draw_scene(self, w: int, h: int): cv = self._cv cv.delete("login_fg") self._photo_refs.clear() pad_right = max(56, int(w * 0.07)) top_y = max(52, int(h * 0.08)) # 1. Sprach- & Audio-Buttons oben rechts (frei innerhalb der Spinnweben) cv.create_window(w - pad_right, top_y, window=self._btn_audio, anchor="e", tags="login_fg") cv.create_window(w - pad_right - 110, top_y, window=self._btn_lang, anchor="e", tags="login_fg") cx = w // 2 compact = (h < 780) # 2. Panorama-Titelbanner oben mittig (ohne künstlichen Rahmen) banner_w = 460 if compact else 540 banner_h = 115 title_photo = self.assets.get_proportional_photo("title_login_banner", self.assets.raw_title, banner_w) box_h = (255 if self._profiles else 160) if compact else (280 if self._profiles else 175) total_block_h = banner_h + 24 + box_h start_y = max(top_y + 36, (h - total_block_h) // 2) if title_photo: self._photo_refs.append(title_photo) banner_cy = start_y + title_photo.height() // 2 cv.create_image(cx, banner_cy, image=title_photo, anchor="center", tags="login_fg") box_top = start_y + title_photo.height() + (18 if compact else 26) else: t_ph = self.assets.get_goth_text_photo(t("app_title"), FONT_SIZE_TITLE_LG, FONT_COLOR_TITLE_MAIN) self._photo_refs.append(t_ph) cv.create_image(cx, start_y + 40, image=t_ph, anchor="center", tags="login_fg") box_top = start_y + 92 # 3. Transparenter Zierrahmen box_big.png um die Anmelde-Elemente (ohne Hintergrund-Füllung!) box_w = 530 box_cy = box_top + box_h // 2 frame_photo = self.assets.get_box_photo("box_big", (box_w, box_h)) if frame_photo: self._photo_refs.append(frame_photo) cv.create_image(cx, box_cy, image=frame_photo, anchor="center", tags="login_fg") row_left = cx - 225 field_cx = row_left + 135 btn_cx = row_left + 270 + 14 + 84 cur_y = box_top + (34 if compact else 40) if self._profiles and self._dropdown is not None and self._btn_login is not None: lbl1 = self.assets.get_goth_text_photo( t("login_select_lbl"), size_px=FONT_SIZE_BODY, color=FONT_COLOR_HEADING_SUB, align="left" ) self._photo_refs.append(lbl1) cv.create_image(row_left, cur_y, image=lbl1, anchor="w", tags="login_fg") cur_y += 38 cv.create_window(field_cx, cur_y, window=self._dropdown, anchor="center", tags="login_fg") cv.create_window(btn_cx, cur_y, window=self._btn_login, anchor="center", tags="login_fg") sep_y = cur_y + (36 if compact else 42) cv.create_line(cx - 215, sep_y, cx + 215, sep_y, fill=SUBTLE_BORDER, width=1, tags="login_fg") cur_y = sep_y + (26 if compact else 30) lbl2 = self.assets.get_goth_text_photo( t("login_create_lbl"), size_px=FONT_SIZE_BODY, color=FONT_COLOR_HEADING_SUB, align="left" ) self._photo_refs.append(lbl2) cv.create_image(row_left, cur_y, image=lbl2, anchor="w", tags="login_fg") cur_y += 38 cv.create_window(field_cx, cur_y, window=self._entry, anchor="center", tags="login_fg") cv.create_window(btn_cx, cur_y, window=self._btn_create, anchor="center", tags="login_fg")