the_goth_cardgame_collection/view/menus/login_view.py

573 lines
23 KiB
Python
Raw Normal View History

2026-11-14 04:25:51 +00:00
# -*- coding: utf-8 -*-
# view/menus/login_view.py
from __future__ import annotations
from pathlib import Path
2026-11-14 04:25:51 +00:00
import tkinter as tk
from tkinter import filedialog
from models.database import Database
from models.db_locator import DbLocator
2026-11-14 04:25:51 +00:00
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,
2026-11-14 04:25:51 +00:00
)
from resources.code_res.i18n import t
from view.widgets.goth_dropdown import GothDropdown
from view.widgets.goth_entry import GothEntry
2026-11-14 04:25:51 +00:00
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._online_url_var: tk.StringVar | None = None
self._online_ip_var: tk.StringVar | None = None
self._online_port_var: tk.StringVar | None = None
self._online_user_var: tk.StringVar | None = None
self._online_pass_var: tk.StringVar | None = None
self._login_mode: str = "local" # 'local' | 'online'
self._addr_mode: str = "url" # 'url' | 'ip'
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
2026-11-14 04:25:51 +00:00
self._dropdown: GothDropdown | None = None
self._entry_new: GothEntry | None = None
self._entry_url: GothEntry | None = None
self._entry_ip: GothEntry | None = None
self._entry_port: GothEntry | None = None
self._entry_online_user: GothEntry | None = None
self._entry_online_pass: GothEntry | None = None
@staticmethod
def _shorten_path(p_str: str, max_chars: int = 48) -> str:
if len(p_str) <= max_chars:
return p_str
keep = max_chars - 5
left = keep // 2 - 2
right = keep - left
return f"{p_str[:left]}...{p_str[-right:]}"
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()
2026-11-14 04:25:51 +00:00
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()
online_prefs = DbLocator.load_online_prefs()
self._addr_mode = "ip" if online_prefs.get("addr_mode") == "ip" else "url"
2026-11-14 04:25:51 +00:00
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
2026-11-14 04:25:51 +00:00
self._create_widgets(preselect_name, online_prefs)
cv.bind("<Configure>", 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 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._entry_online_user is not None:
if not self._online_user_var.get().strip():
self._entry_online_user.entry.focus_set()
elif self._entry_online_pass is not None:
self._entry_online_pass.entry.focus_set()
def _create_widgets(self, preselect_name: str, online_prefs: dict):
cv = self._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
2026-11-14 04:25:51 +00:00
self._btn_audio = 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",
2026-11-14 04:25:51 +00:00
)
# Modus-Tabs: [Lokales Profil] vs. [Online-Account]
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",
)
# --- LOKALE ELEMENTE ---
self._dropdown = None
self._btn_login = None
if self._profiles:
initial_prof = preselect_name if preselect_name in self._profiles else self._profiles[0]
2026-11-14 04:25:51 +00:00
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",
2026-11-14 04:25:51 +00:00
)
self.new_name_var = tk.StringVar()
self._entry_new = GothEntry(
cv, self.assets, width=270, height=40, textvariable=self.new_name_var
2026-11-14 04:25:51 +00:00
)
self._entry_new.bind_return(lambda _e: self._submit_new_profile())
2026-11-14 04:25:51 +00:00
new_is_primary = (not self._profiles)
self._btn_create = ImageButton(
cv,
t("new_profile_btn"),
btn_fire if new_is_primary else btn_norm,
btn_fire if new_is_primary else btn_purp,
self._submit_new_profile,
sound_mgr=self.sounds,
width=168,
height=40,
style="fire" if new_is_primary else "amethyst",
)
# Erst-Start DB-Ordnerwahl & Vorhandene DB laden (wenn noch kein lokales Profil existiert)
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",
)
# --- ONLINE ELEMENTE (Einzelaccount: URL inkl. Port ODER IP + Port) ---
is_url_mode = (self._addr_mode == "url")
self._btn_addr_url = ImageButton(
cv,
t("online_addr_url"),
btn_fire if is_url_mode else btn_norm,
btn_fire if is_url_mode else btn_purp,
lambda: self._switch_addr_mode("url"),
sound_mgr=self.sounds,
width=186,
height=30,
style="fire" if is_url_mode else "amethyst",
)
self._btn_addr_ip = ImageButton(
cv,
t("online_addr_ip"),
btn_fire if not is_url_mode else btn_norm,
btn_fire if not is_url_mode else btn_purp,
lambda: self._switch_addr_mode("ip"),
sound_mgr=self.sounds,
width=186,
height=30,
style="fire" if not is_url_mode else "amethyst",
)
self._online_url_var = tk.StringVar(value=online_prefs.get("url", "https://"))
self._online_ip_var = tk.StringVar(value=online_prefs.get("ip", "127.0.0.1"))
self._online_port_var = tk.StringVar(value=online_prefs.get("port", "8443"))
self._online_user_var = tk.StringVar(value=online_prefs.get("username", ""))
self._online_pass_var = tk.StringVar(value="")
self._entry_url = GothEntry(
cv, self.assets, width=452, height=38, textvariable=self._online_url_var
)
self._entry_ip = GothEntry(
cv, self.assets, width=308, height=38, textvariable=self._online_ip_var
)
self._entry_port = GothEntry(
cv, self.assets, width=130, height=38, textvariable=self._online_port_var
)
self._entry_online_user = GothEntry(
cv, self.assets, width=220, height=38, textvariable=self._online_user_var
)
self._entry_online_pass = GothEntry(
cv, self.assets, width=218, height=38, textvariable=self._online_pass_var
)
try:
self._entry_online_pass.entry.configure(show="•")
except Exception:
pass
for ent in (
self._entry_url,
self._entry_ip,
self._entry_port,
self._entry_online_user,
self._entry_online_pass,
):
ent.bind_return(lambda _e: self._submit_online_login())
self._btn_online_connect = ImageButton(
cv,
t("online_connect_btn"),
btn_fire,
btn_fire,
self._submit_online_login,
sound_mgr=self.sounds,
width=210,
height=40,
style="fire",
)
def _switch_login_mode(self, mode: str):
if self._login_mode == mode:
return
self._status_msg = ""
self.show(
self._profiles,
preselect_name=self.profile_var.get() if self.profile_var else "",
login_mode=mode,
)
def _switch_addr_mode(self, addr_mode: str):
if self._addr_mode == addr_mode:
return
DbLocator.save_online_prefs(
addr_mode,
self._online_url_var.get() if self._online_url_var else "",
self._online_ip_var.get() if self._online_ip_var else "",
self._online_port_var.get() if self._online_port_var else "8443",
self._online_user_var.get() if self._online_user_var else "",
)
self._addr_mode = addr_mode
self._status_msg = ""
self.show(
self._profiles,
preselect_name=self.profile_var.get() if self.profile_var else "",
login_mode="online",
)
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 is not None 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:
self._status_msg = msg
self._status_is_error = True
if self._cv is not None and self._cv.winfo_exists():
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 None:
return
name = self._entry_new.get().strip()
if not name:
return
first_dir = str(self._first_db_dir) if not self._profiles else None
self.ctrl.do_create_profile(name, first_user_db_dir=first_dir)
def _submit_online_login(self):
url_s = self._entry_url.get().strip() if self._entry_url else ""
ip_s = self._entry_ip.get().strip() if self._entry_ip else ""
port_s = self._entry_port.get().strip() if self._entry_port else ""
user_s = self._entry_online_user.get().strip() if self._entry_online_user else ""
pass_s = self._entry_online_pass.get() if self._entry_online_pass else ""
ok, err_msg = self.ctrl.do_online_login(
addr_mode=self._addr_mode,
raw_url=url_s,
raw_ip=ip_s,
raw_port=port_s,
username=user_s,
password=pass_s,
)
if not ok:
self._status_msg = err_msg
self._status_is_error = True
if self._cv is not None and self._cv.winfo_exists():
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:
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 _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,
self._btn_addr_url,
self._btn_addr_ip,
self._entry_url,
self._entry_ip,
self._entry_port,
self._entry_online_user,
self._entry_online_pass,
self._btn_online_connect,
):
if wgt is not None and wgt.winfo_exists():
wgt.place_forget()
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 = max(48, int(w * 0.06))
top_y = max(42, int(h * 0.065))
cv.create_window(w - pad_right, top_y, window=self._btn_audio, anchor="e", tags="login_fg")
cx = w // 2
compact = (h < 790)
if self._login_mode == "online":
box_h = 352 if compact else 372
elif self._profiles:
box_h = 304 if compact else 326
else:
# Erst-Erstellung inkl. DB-Speicherort-Auswahl
box_h = 312 if compact else 332
banner_w = 430 if compact else 520
title_photo = self.assets.get_proportional_photo("title_login_banner", self.assets.raw_title, banner_w)
banner_h = title_photo.height() if title_photo else 76
total_block_h = banner_h + (14 if compact else 22) + box_h
start_y = max(top_y + 26, (h - total_block_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
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")
# Modus-Schalter oben im Rahmen
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)
else:
self._draw_online_section(cv, cx, tab_y + 44, compact)
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,
size_px=FONT_SIZE_SMALL_MIN + 1,
color=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 = cx - 226
field_cx = row_left + 135
btn_cx = row_left + 270 + 14 + 84
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"), 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 += 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:
# Allererstes Profil: Speicherort für user_data.db direkt wählbar!
db_lbl = self.assets.get_goth_text_photo(
t("db_first_setup_lbl"),
size_px=FONT_SIZE_SMALL_MIN + 1,
color=FONT_COLOR_HEADING_SUB,
align="left",
)
target_preview = str((self._first_db_dir / USER_DB_FILENAME).resolve())
path_ph = self.assets.get_goth_text_photo(
self._shorten_path(target_preview, 54),
size_px=FONT_SIZE_SMALL_MIN,
color=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"), 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 += 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")
def _draw_online_section(self, cv: tk.Canvas, cx: int, start_y: int, compact: bool):
row_left = cx - 226
cur_y = start_y + 6
# Sub-Switch: URL (inkl. Port) vs. IP + Port
cv.create_window(cx - 98, cur_y, window=self._btn_addr_url, anchor="center", tags="login_fg")
cv.create_window(cx + 98, cur_y, window=self._btn_addr_ip, anchor="center", tags="login_fg")
cur_y += 34
if self._addr_mode == "url":
u_lbl = self.assets.get_goth_text_photo(
t("online_url_lbl"), size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left"
)
self._photo_refs.append(u_lbl)
cv.create_image(row_left, cur_y, image=u_lbl, anchor="w", tags="login_fg")
cur_y += 30
cv.create_window(cx, cur_y, window=self._entry_url, anchor="center", tags="login_fg")
else:
ip_lbl = self.assets.get_goth_text_photo(
t("online_ip_lbl"), size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left"
)
pt_lbl = self.assets.get_goth_text_photo(
t("online_port_lbl"), size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left"
)
self._photo_refs.extend([ip_lbl, pt_lbl])
port_left = row_left + 322
cv.create_image(row_left, cur_y, image=ip_lbl, anchor="w", tags="login_fg")
cv.create_image(port_left, cur_y, image=pt_lbl, anchor="w", tags="login_fg")
cur_y += 30
cv.create_window(row_left + 154, cur_y, window=self._entry_ip, anchor="center", tags="login_fg")
cv.create_window(port_left + 65, cur_y, window=self._entry_port, anchor="center", tags="login_fg")
cur_y += 38 if not compact else 34
usr_lbl = self.assets.get_goth_text_photo(
t("online_user_lbl"), size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left"
)
pwd_lbl = self.assets.get_goth_text_photo(
t("online_pass_lbl"), size_px=FONT_SIZE_SMALL_MIN + 1, color=FONT_COLOR_HEADING_SUB, align="left"
)
self._photo_refs.extend([usr_lbl, pwd_lbl])
pwd_left = row_left + 234
cv.create_image(row_left, cur_y, image=usr_lbl, anchor="w", tags="login_fg")
cv.create_image(pwd_left, cur_y, image=pwd_lbl, anchor="w", tags="login_fg")
cur_y += 30
cv.create_window(row_left + 110, cur_y, window=self._entry_online_user, anchor="center", tags="login_fg")
cv.create_window(pwd_left + 109, cur_y, window=self._entry_online_pass, anchor="center", tags="login_fg")
cur_y += 46 if not compact else 42
cv.create_window(cx, cur_y, window=self._btn_online_connect, anchor="center", tags="login_fg")