# -*- coding: utf-8 -*- # view/menus/login_view.py from __future__ import annotations from pathlib import Path import tkinter as tk from tkinter import filedialog from models.database import Database from resources.code_res.game_constants import ( BG_COLOR, FONT_COLOR_BODY_MUTED, FONT_COLOR_HEADING_SUB, FONT_COLOR_STATUS_LOSS, FONT_COLOR_STATUS_WIN, FONT_COLOR_TITLE_MAIN, FONT_SIZE_BODY, FONT_SIZE_SMALL_MIN, FONT_SIZE_TITLE_LG, SUBTLE_BORDER, USER_DB_FILENAME, ) from resources.code_res.i18n import t from view.menus.online_login_panel import OnlineLoginPanel 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._login_mode: str = "local" self._first_db_dir: Path = Database.get_user_db_dir() self._status_msg: str = "" self._status_is_error: bool = False self._photo_refs: list = [] self._profiles: list[str] = [] self._cv: tk.Canvas | None = None self._dropdown: GothDropdown | None = None self._btn_login: ImageButton | None = None self._entry_new: GothEntry | None = None self._btn_create: ImageButton | None = None self._btn_pick_first_dir: ImageButton | None = None self._btn_load_existing_db: ImageButton | None = None self._online_panel: OnlineLoginPanel | None = None @staticmethod def _shorten_path(p_str: str, max_chars: int = 54) -> str: if len(p_str) <= max_chars: return p_str keep = max_chars - 5 left = keep // 2 - 2 return f"{p_str[:left]}...{p_str[-(keep - left):]}" def show( self, profiles: list[str], preselect_name: str = "", status_msg: str = "", is_error: bool = False, login_mode: str = "local", ): GothDropdown.close_any_open_popup() self.root.clear_window(apply_global_bg=True) self._photo_refs.clear() self._profiles = list(profiles) self._login_mode = "online" if login_mode == "online" else "local" self._status_msg = status_msg self._status_is_error = is_error self._first_db_dir = Database.get_user_db_dir() 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 self._create_widgets(preselect_name) cv.bind("", self._on_canvas_resize, add="+") self.root.update_idletasks() self._draw_scene(max(800, cv.winfo_width()), max(600, cv.winfo_height())) if self._login_mode == "local" and not self._profiles and self._entry_new is not None: self._entry_new.entry.focus_set() elif self._login_mode == "online" and self._online_panel is not None: self._online_panel.focus_default() def _create_widgets(self, preselect_name: str): cv = self._cv btn_norm, btn_purp = self.assets.raw_btn_default, self.assets.raw_btn_purple btn_fire = self.assets.raw_btn_fire or btn_purp self._btn_settings = ImageButton( cv, t("btn_settings"), btn_norm, btn_purp, lambda: self.ctrl.show_settings(on_back=lambda: self.ctrl.start(login_mode=self._login_mode)), sound_mgr=self.sounds, width=126, height=34, style="amethyst", ) is_loc = (self._login_mode == "local") self._btn_tab_local = ImageButton( cv, t("login_mode_local"), btn_fire if is_loc else btn_norm, btn_fire if is_loc else btn_purp, lambda: self._switch_login_mode("local"), sound_mgr=self.sounds, width=212, height=34, style="fire" if is_loc else "amethyst", ) self._btn_tab_online = ImageButton( cv, t("login_mode_online"), btn_fire if not is_loc else btn_norm, btn_fire if not is_loc else btn_purp, lambda: self._switch_login_mode("online"), sound_mgr=self.sounds, width=212, height=34, style="fire" if not is_loc else "amethyst", ) self._dropdown = 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=40, ) 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=40, style="fire", ) self.new_name_var = tk.StringVar() self._entry_new = GothEntry(cv, self.assets, width=270, height=40, textvariable=self.new_name_var) self._entry_new.bind_return(lambda _e: self._submit_new_profile()) new_pri = (not self._profiles) self._btn_create = ImageButton( cv, t("new_profile_btn"), btn_fire if new_pri else btn_norm, btn_fire if new_pri else btn_purp, self._submit_new_profile, sound_mgr=self.sounds, width=168, height=40, style="fire" if new_pri else "amethyst", ) self._btn_pick_first_dir = ImageButton( cv, t("db_choose_dir_btn"), btn_norm, btn_purp, self._choose_first_db_folder, sound_mgr=self.sounds, width=150, height=32, style="amethyst", ) self._btn_load_existing_db = ImageButton( cv, t("db_load_btn"), btn_norm, btn_purp, self._load_existing_db_from_login, sound_mgr=self.sounds, width=168, height=32, style="amethyst", ) self._online_panel = OnlineLoginPanel( cv, self.assets, self.sounds, on_submit=self._submit_online_login, on_switch_addr_mode=lambda _m: self.show( self._profiles, preselect_name=self.profile_var.get() if self.profile_var else "", login_mode="online" ), ) def _switch_login_mode(self, mode: str): if self._login_mode != mode: self.show(self._profiles, preselect_name=self.profile_var.get() if self.profile_var else "", login_mode=mode) def _choose_first_db_folder(self): chosen = filedialog.askdirectory( parent=self.root, initialdir=str(self._first_db_dir), title=t("db_move_dialog_title") ) if chosen: self._first_db_dir = Path(chosen).resolve() if self._cv and self._cv.winfo_exists(): self._draw_scene(max(800, self._cv.winfo_width()), max(600, self._cv.winfo_height())) def _load_existing_db_from_login(self): picked = filedialog.askopenfilename( parent=self.root, initialdir=str(Database.get_user_db_dir()), title=t("db_load_dialog_title"), filetypes=[("SQLite Database", "*.db *.sqlite *.sqlite3"), ("All Files", "*.*")], ) if not picked: return ok, msg = self.ctrl.load_user_data_from_file(picked) if not ok and self._cv and self._cv.winfo_exists(): self._status_msg, self._status_is_error = msg, True self._draw_scene(max(800, self._cv.winfo_width()), max(600, self._cv.winfo_height())) def _submit_new_profile(self): if self._entry_new is not None: name = self._entry_new.get().strip() if name: self.ctrl.do_create_profile(name, first_user_db_dir=str(self._first_db_dir) if not self._profiles else None) def _submit_online_login(self): if self._online_panel is None: return ok, err_msg = self.ctrl.do_online_login(**self._online_panel.get_credentials()) if not ok and self._cv and self._cv.winfo_exists(): self._status_msg, self._status_is_error = err_msg, True self._draw_scene(max(800, self._cv.winfo_width()), max(600, self._cv.winfo_height())) def _on_canvas_resize(self, event): if event.widget == self._cv and event.width >= 100 and event.height >= 100: GothDropdown.close_any_open_popup() self._draw_scene(event.width, event.height) def _hide_unused_widgets(self): for wgt in ( self._dropdown, self._btn_login, self._entry_new, self._btn_create, self._btn_pick_first_dir, self._btn_load_existing_db, ): if wgt is not None and wgt.winfo_exists(): wgt.place_forget() if self._online_panel is not None: self._online_panel.hide_all() def _draw_scene(self, w: int, h: int): cv = self._cv if cv is None or not cv.winfo_exists(): return cv.delete("login_fg") self._hide_unused_widgets() self._photo_refs.clear() pad_right, top_y = max(48, int(w * 0.06)), max(42, int(h * 0.065)) cv.create_window(w - pad_right, top_y, window=self._btn_settings, anchor="e", tags="login_fg") cx, compact = w // 2, (h < 790) box_h = ( (352 if compact else 372) if self._login_mode == "online" else ((304 if compact else 326) if self._profiles else (312 if compact else 332)) ) title_photo = self.assets.get_proportional_photo("title_login_banner", self.assets.raw_title, 430 if compact else 520) banner_h = title_photo.height() if title_photo else 76 start_y = max(top_y + 26, (h - (banner_h + (14 if compact else 22) + box_h)) // 2 - (10 if self._status_msg else 0)) if title_photo: self._photo_refs.append(title_photo) cv.create_image(cx, start_y + banner_h // 2, image=title_photo, anchor="center", tags="login_fg") box_top = start_y + banner_h + (14 if compact else 22) 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 + 34, image=t_ph, anchor="center", tags="login_fg") box_top = start_y + 78 box_w = 546 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_top + box_h // 2, image=frame_photo, anchor="center", tags="login_fg") tab_y = box_top + 38 cv.create_window(cx - 112, tab_y, window=self._btn_tab_local, anchor="center", tags="login_fg") cv.create_window(cx + 112, tab_y, window=self._btn_tab_online, anchor="center", tags="login_fg") cv.create_line(cx - 226, tab_y + 25, cx + 226, tab_y + 25, fill=SUBTLE_BORDER, width=1, tags="login_fg") if self._login_mode == "local": self._draw_local_section(cv, cx, tab_y + 46, compact) elif self._online_panel is not None: self._online_panel.draw(cx, tab_y + 44, compact, self._photo_refs) if self._status_msg: st_col = FONT_COLOR_STATUS_LOSS if self._status_is_error else FONT_COLOR_STATUS_WIN st_ph = self.assets.get_goth_text_photo(self._status_msg, FONT_SIZE_SMALL_MIN + 1, st_col, max_width=box_w + 80) self._photo_refs.append(st_ph) cv.create_image(cx, box_top + box_h + 20, image=st_ph, anchor="center", tags="login_fg") def _draw_local_section(self, cv: tk.Canvas, cx: int, start_y: int, compact: bool): row_left, field_cx, btn_cx = cx - 226, cx - 91, cx + 142 cur_y = start_y 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"), FONT_SIZE_BODY, 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 += 35 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 + (32 if compact else 38) cv.create_line(cx - 215, sep_y, cx + 215, sep_y, fill=SUBTLE_BORDER, width=1, tags="login_fg") cur_y = sep_y + (22 if compact else 26) else: db_lbl = self.assets.get_goth_text_photo(t("db_first_setup_lbl"), FONT_SIZE_SMALL_MIN + 1, FONT_COLOR_HEADING_SUB, align="left") preview = str((self._first_db_dir / USER_DB_FILENAME).resolve()) path_ph = self.assets.get_goth_text_photo(self._shorten_path(preview, 54), FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, align="left") self._photo_refs.extend([db_lbl, path_ph]) cv.create_image(row_left, cur_y, image=db_lbl, anchor="w", tags="login_fg") cur_y += 22 cv.create_image(row_left, cur_y, image=path_ph, anchor="w", tags="login_fg") cur_y += 30 cv.create_window(row_left + 75, cur_y, window=self._btn_pick_first_dir, anchor="center", tags="login_fg") cv.create_window(cx + 226 - 84, cur_y, window=self._btn_load_existing_db, anchor="center", tags="login_fg") sep_y = cur_y + 26 cv.create_line(cx - 215, sep_y, cx + 215, sep_y, fill=SUBTLE_BORDER, width=1, tags="login_fg") cur_y = sep_y + 22 lbl2 = self.assets.get_goth_text_photo(t("login_create_lbl"), FONT_SIZE_BODY, 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 += 35 cv.create_window(field_cx, cur_y, window=self._entry_new, anchor="center", tags="login_fg") cv.create_window(btn_cx, cur_y, window=self._btn_create, anchor="center", tags="login_fg")