162 lines
5.2 KiB
Python
162 lines
5.2 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# view/widgets/goth_entry.py
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import tkinter as tk
|
||
|
|
from PIL import Image, ImageTk
|
||
|
|
|
||
|
|
from resources.code_res.game_constants import (
|
||
|
|
BG_COLOR,
|
||
|
|
FONT_COLOR_DISABLED,
|
||
|
|
FONT_FAMILY_PRIMARY,
|
||
|
|
FONT_SIZE_INPUT,
|
||
|
|
INPUT_BG,
|
||
|
|
INPUT_CARET_COLOR,
|
||
|
|
INPUT_SELECTION_BG,
|
||
|
|
INPUT_TEXT_PLACEHOLDER,
|
||
|
|
INPUT_TEXT_VALUE,
|
||
|
|
)
|
||
|
|
from view.widgets.image_button import _clean_outer_black_matte
|
||
|
|
|
||
|
|
|
||
|
|
class GothEntry(tk.Canvas):
|
||
|
|
"""
|
||
|
|
Rahmenloses Gothic-Texteingabefeld auf Basis von input.png:
|
||
|
|
Kein künstlicher Tkinter-Rahmen, goldener Cursor, Amethyst-Selektion & Platzhalter-Support.
|
||
|
|
"""
|
||
|
|
|
||
|
|
def __init__(
|
||
|
|
self,
|
||
|
|
parent,
|
||
|
|
assets,
|
||
|
|
width: int = 240,
|
||
|
|
height: int = 42,
|
||
|
|
textvariable: tk.StringVar | None = None,
|
||
|
|
placeholder: str = "",
|
||
|
|
font_size: int = FONT_SIZE_INPUT,
|
||
|
|
enabled: bool = True,
|
||
|
|
):
|
||
|
|
try:
|
||
|
|
parent_bg = parent.cget("bg")
|
||
|
|
except Exception:
|
||
|
|
parent_bg = BG_COLOR
|
||
|
|
|
||
|
|
super().__init__(
|
||
|
|
parent,
|
||
|
|
width=width,
|
||
|
|
height=height,
|
||
|
|
bg=parent_bg,
|
||
|
|
bd=0,
|
||
|
|
highlightthickness=0,
|
||
|
|
)
|
||
|
|
|
||
|
|
self.assets = assets
|
||
|
|
self._width = max(80, int(width))
|
||
|
|
self._height = max(30, int(height))
|
||
|
|
self._placeholder = placeholder
|
||
|
|
self._placeholder_active = False
|
||
|
|
self._enabled = bool(enabled)
|
||
|
|
self._var = textvariable if textvariable is not None else tk.StringVar()
|
||
|
|
|
||
|
|
self._bg_photo: ImageTk.PhotoImage | None = None
|
||
|
|
inner_fill = INPUT_BG
|
||
|
|
|
||
|
|
if getattr(self.assets, "raw_input", None) is not None:
|
||
|
|
clean_inp = _clean_outer_black_matte(self.assets.raw_input)
|
||
|
|
scaled = clean_inp.resize((self._width, self._height), Image.Resampling.LANCZOS)
|
||
|
|
# Exakte Innenfarbe aus der Grafikmitte abgreifen, damit das eingebettete Entry unsichtbar verschmilzt
|
||
|
|
cx, cy = self._width // 2, self._height // 2
|
||
|
|
r, g, b, a = scaled.getpixel((cx, cy))
|
||
|
|
if a > 200:
|
||
|
|
inner_fill = f"#{r:02x}{g:02x}{b:02x}"
|
||
|
|
self._bg_photo = ImageTk.PhotoImage(scaled)
|
||
|
|
self.create_image(0, 0, image=self._bg_photo, anchor="nw")
|
||
|
|
|
||
|
|
self._inner_bg = inner_fill
|
||
|
|
pad_x = max(16, int(self._width * 0.08))
|
||
|
|
eff_font_px = min(font_size, max(14, int(self._height * 0.46)))
|
||
|
|
|
||
|
|
self.entry = tk.Entry(
|
||
|
|
self,
|
||
|
|
textvariable=self._var,
|
||
|
|
font=(FONT_FAMILY_PRIMARY, eff_font_px),
|
||
|
|
fg=INPUT_TEXT_VALUE,
|
||
|
|
disabledforeground=FONT_COLOR_DISABLED,
|
||
|
|
disabledbackground=inner_fill,
|
||
|
|
readonlybackground=inner_fill,
|
||
|
|
bg=inner_fill,
|
||
|
|
insertbackground=INPUT_CARET_COLOR,
|
||
|
|
insertwidth=2,
|
||
|
|
selectbackground=INPUT_SELECTION_BG,
|
||
|
|
selectforeground="#ffffff",
|
||
|
|
bd=0,
|
||
|
|
relief="flat",
|
||
|
|
highlightthickness=0,
|
||
|
|
)
|
||
|
|
|
||
|
|
self.create_window(
|
||
|
|
self._width // 2,
|
||
|
|
self._height // 2,
|
||
|
|
window=self.entry,
|
||
|
|
width=max(40, self._width - 2 * pad_x),
|
||
|
|
height=max(18, self._height - 14),
|
||
|
|
anchor="center",
|
||
|
|
)
|
||
|
|
|
||
|
|
self.entry.bind("<FocusIn>", self._on_focus_in)
|
||
|
|
self.entry.bind("<FocusOut>", self._on_focus_out)
|
||
|
|
self.bind("<Button-1>", lambda _e: self.entry.focus_set() if self._enabled else None)
|
||
|
|
|
||
|
|
if not self._var.get() and self._placeholder:
|
||
|
|
self._show_placeholder()
|
||
|
|
self.set_enabled(self._enabled)
|
||
|
|
|
||
|
|
def _show_placeholder(self):
|
||
|
|
if self._placeholder and not self._var.get():
|
||
|
|
self._placeholder_active = True
|
||
|
|
self.entry.configure(fg=INPUT_TEXT_PLACEHOLDER)
|
||
|
|
self._var.set(self._placeholder)
|
||
|
|
|
||
|
|
def _clear_placeholder(self):
|
||
|
|
if self._placeholder_active:
|
||
|
|
self._placeholder_active = False
|
||
|
|
self._var.set("")
|
||
|
|
self.entry.configure(fg=INPUT_TEXT_VALUE)
|
||
|
|
|
||
|
|
def _on_focus_in(self, _event):
|
||
|
|
if self._enabled:
|
||
|
|
self._clear_placeholder()
|
||
|
|
|
||
|
|
def _on_focus_out(self, _event):
|
||
|
|
if not self._var.get().strip():
|
||
|
|
self._var.set("")
|
||
|
|
self._show_placeholder()
|
||
|
|
|
||
|
|
def get(self) -> str:
|
||
|
|
if self._placeholder_active:
|
||
|
|
return ""
|
||
|
|
return self._var.get()
|
||
|
|
|
||
|
|
def set(self, value: str):
|
||
|
|
self._placeholder_active = False
|
||
|
|
self._var.set(value)
|
||
|
|
if not value and self._placeholder and self.focus_get() != self.entry:
|
||
|
|
self._show_placeholder()
|
||
|
|
else:
|
||
|
|
self.entry.configure(fg=INPUT_TEXT_VALUE if self._enabled else FONT_COLOR_DISABLED)
|
||
|
|
|
||
|
|
def delete(self, first, last=None):
|
||
|
|
self._clear_placeholder()
|
||
|
|
self.entry.delete(first, last)
|
||
|
|
|
||
|
|
def insert(self, index, string: str):
|
||
|
|
self._clear_placeholder()
|
||
|
|
self.entry.insert(index, string)
|
||
|
|
|
||
|
|
def set_enabled(self, enabled: bool):
|
||
|
|
self._enabled = bool(enabled)
|
||
|
|
self.entry.configure(state="normal" if self._enabled else "disabled")
|
||
|
|
self.configure(cursor="xterm" if self._enabled else "arrow")
|
||
|
|
|
||
|
|
def bind_return(self, callback):
|
||
|
|
self.entry.bind("<Return>", callback)
|