586 lines
20 KiB
Python
586 lines
20 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# view/widgets/goth_dropdown.py
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import tkinter as tk
|
||
|
|
from PIL import Image, ImageDraw, ImageEnhance, ImageTk
|
||
|
|
|
||
|
|
from resources.code_res.game_constants import (
|
||
|
|
BG_COLOR,
|
||
|
|
DROPDOWN_ITEM_TEXT_HOVER,
|
||
|
|
DROPDOWN_ITEM_TEXT_NORMAL,
|
||
|
|
DROPDOWN_ITEM_TEXT_SELECTED,
|
||
|
|
DROPDOWN_TEXT_CLOSED,
|
||
|
|
FONT_COLOR_DISABLED,
|
||
|
|
FONT_LETTER_SPACING_PX,
|
||
|
|
FONT_OUTLINE_COLOR,
|
||
|
|
FONT_OUTLINE_THICKNESS_PX,
|
||
|
|
FONT_SHADOW_COLOR,
|
||
|
|
FONT_SHADOW_ENABLED,
|
||
|
|
FONT_SHADOW_OFFSET_SMALL,
|
||
|
|
FONT_SIZE_DROPDOWN,
|
||
|
|
FONT_SIZE_SMALL_MIN,
|
||
|
|
FONT_VERTICAL_OFFSET_PX,
|
||
|
|
UI_ACCENT_AMETHYST,
|
||
|
|
UI_ACCENT_GOLD,
|
||
|
|
)
|
||
|
|
from view.widgets.image_button import _clean_outer_black_matte, _get_old_london
|
||
|
|
|
||
|
|
_TRIM_CACHE: dict[tuple[int, int, int], Image.Image] = {}
|
||
|
|
|
||
|
|
|
||
|
|
def _trim_outer_black_bbox(pil_img: Image.Image, threshold: int = 38) -> Image.Image:
|
||
|
|
"""
|
||
|
|
Schneidet äußere schwarze Leerränder exakt bis zur eigentlichen Rahmenkante ab,
|
||
|
|
damit linkes Hauptstück und rechtes Button-Stück ohne Höhenversatz nahtlos aneinanderpassen.
|
||
|
|
"""
|
||
|
|
key = (id(pil_img), pil_img.width, pil_img.height)
|
||
|
|
if key in _TRIM_CACHE:
|
||
|
|
return _TRIM_CACHE[key]
|
||
|
|
|
||
|
|
clean = _clean_outer_black_matte(pil_img)
|
||
|
|
rgb = clean.convert("RGB")
|
||
|
|
w, h = rgb.size
|
||
|
|
px = rgb.load()
|
||
|
|
|
||
|
|
min_x, min_y = w, h
|
||
|
|
max_x, max_y = -1, -1
|
||
|
|
|
||
|
|
for y in range(h):
|
||
|
|
for x in range(w):
|
||
|
|
r, g, b = px[x, y]
|
||
|
|
if r > threshold or g > threshold or b > threshold:
|
||
|
|
if x < min_x:
|
||
|
|
min_x = x
|
||
|
|
if x > max_x:
|
||
|
|
max_x = x
|
||
|
|
if y < min_y:
|
||
|
|
min_y = y
|
||
|
|
if y > max_y:
|
||
|
|
max_y = y
|
||
|
|
|
||
|
|
if max_x <= min_x or max_y <= min_y:
|
||
|
|
_TRIM_CACHE[key] = clean
|
||
|
|
return clean
|
||
|
|
|
||
|
|
cropped = clean.crop((min_x, min_y, max_x + 1, max_y + 1))
|
||
|
|
_TRIM_CACHE[key] = cropped
|
||
|
|
return cropped
|
||
|
|
|
||
|
|
|
||
|
|
class GothDropdown(tk.Canvas):
|
||
|
|
"""
|
||
|
|
Zweiteiliges Gothic-Dropdown:
|
||
|
|
- Rechts: Button-Kappe (dropdown_closed_button / dropdown_box_hightlighted_button),
|
||
|
|
skaliert NUR über die Höhe im festen Original-Seitenverhältnis (wird niemals breitgezogen!).
|
||
|
|
- Links: Hauptkörper (dropdown_closed / dropdown_box_hightlighted),
|
||
|
|
hält die linke Zierecke scharf und streckt den Rest exakt bis zur Button-Kappe.
|
||
|
|
- Aufklapp-Liste: Nutzt ausschließlich dropdown_box.png (bringt Rahmen & Hintergrund mit),
|
||
|
|
Einträge komplett ohne Auswahl-Kasten/Hintergrund (nur Text-Highlight),
|
||
|
|
inkl. Scroll-Pfeilen oben/unten und Mausrad-Support.
|
||
|
|
"""
|
||
|
|
|
||
|
|
_active_popup_owner: GothDropdown | None = None
|
||
|
|
ARROW_ZONE_H = 18
|
||
|
|
|
||
|
|
def __init__(
|
||
|
|
self,
|
||
|
|
parent,
|
||
|
|
assets,
|
||
|
|
values: list[str] | tuple[str, ...] = (),
|
||
|
|
textvariable: tk.StringVar | None = None,
|
||
|
|
on_change=None,
|
||
|
|
sound_mgr=None,
|
||
|
|
width: int = 250,
|
||
|
|
height: int = 40,
|
||
|
|
font_size: int = FONT_SIZE_DROPDOWN,
|
||
|
|
max_visible_items: int = 7,
|
||
|
|
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,
|
||
|
|
cursor="hand2" if enabled else "arrow",
|
||
|
|
)
|
||
|
|
|
||
|
|
self.assets = assets
|
||
|
|
self.sounds = sound_mgr
|
||
|
|
self._on_change = on_change
|
||
|
|
self._width = max(110, int(width))
|
||
|
|
self._height = max(28, int(height))
|
||
|
|
self._font_px = max(FONT_SIZE_SMALL_MIN, min(int(font_size), int(self._height * 0.52)))
|
||
|
|
self._max_visible = max(3, int(max_visible_items))
|
||
|
|
self._values: list[str] = list(values)
|
||
|
|
self._var = textvariable if textvariable is not None else tk.StringVar()
|
||
|
|
self._enabled = bool(enabled)
|
||
|
|
self._hovered = False
|
||
|
|
|
||
|
|
self._popup: tk.Toplevel | None = None
|
||
|
|
self._closed_photo: ImageTk.PhotoImage | None = None
|
||
|
|
self._popup_refs: list[ImageTk.PhotoImage] = []
|
||
|
|
self._scroll_offset: int = 0
|
||
|
|
self._hover_idx: int = -1
|
||
|
|
self._hover_arrow: str | None = None # "up" | "down" | None
|
||
|
|
|
||
|
|
if not self._var.get() and self._values:
|
||
|
|
self._var.set(self._values[0])
|
||
|
|
|
||
|
|
self._var.trace_add("write", lambda *_: self._render_closed())
|
||
|
|
|
||
|
|
self.bind("<Enter>", self._on_enter)
|
||
|
|
self.bind("<Leave>", self._on_leave)
|
||
|
|
self.bind("<Button-1>", self._toggle_popup)
|
||
|
|
self.bind("<Destroy>", lambda _e: self.close_popup())
|
||
|
|
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
# --- Kompatibilitäts-Methoden ---
|
||
|
|
def get(self) -> str:
|
||
|
|
return self._var.get()
|
||
|
|
|
||
|
|
def set(self, value: str):
|
||
|
|
self._var.set(value)
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
def current(self, newindex: int | None = None) -> int:
|
||
|
|
if newindex is not None and 0 <= newindex < len(self._values):
|
||
|
|
self.set(self._values[newindex])
|
||
|
|
cur = self._var.get()
|
||
|
|
return self._values.index(cur) if cur in self._values else -1
|
||
|
|
|
||
|
|
def set_values(self, values: list[str] | tuple[str, ...], keep_current: bool = True):
|
||
|
|
self._values = list(values)
|
||
|
|
if not keep_current or self._var.get() not in self._values:
|
||
|
|
self._var.set(self._values[0] if self._values else "")
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
def set_enabled(self, enabled: bool):
|
||
|
|
self._enabled = bool(enabled)
|
||
|
|
if not self._enabled:
|
||
|
|
self.close_popup()
|
||
|
|
self.configure(cursor="hand2" if self._enabled else "arrow")
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
# --- Nahtloses Zusammensetzen aus gezerrtem Body + proportionalem Button ---
|
||
|
|
def _compose_bar_pil(self, is_highlighted: bool) -> tuple[Image.Image | None, int]:
|
||
|
|
if is_highlighted:
|
||
|
|
raw_body = getattr(self.assets, "raw_dropdown_open", None) or getattr(self.assets, "raw_dropdown_closed", None)
|
||
|
|
raw_btn = getattr(self.assets, "raw_dropdown_open_btn", None) or getattr(self.assets, "raw_dropdown_closed_btn", None)
|
||
|
|
else:
|
||
|
|
raw_body = getattr(self.assets, "raw_dropdown_closed", None)
|
||
|
|
raw_btn = getattr(self.assets, "raw_dropdown_closed_btn", None)
|
||
|
|
|
||
|
|
if raw_body is None:
|
||
|
|
raw_body = getattr(self.assets, "raw_btn_default", None)
|
||
|
|
if raw_body is None:
|
||
|
|
return None, 36
|
||
|
|
|
||
|
|
h = self._height
|
||
|
|
w = self._width
|
||
|
|
body_trim = _trim_outer_black_bbox(raw_body)
|
||
|
|
|
||
|
|
if raw_btn is None:
|
||
|
|
full = body_trim.resize((w, h), Image.Resampling.LANCZOS)
|
||
|
|
return full, max(34, h)
|
||
|
|
|
||
|
|
btn_trim = _trim_outer_black_bbox(raw_btn)
|
||
|
|
# Button-Breite strikt proportional zur Zielhöhe (niemals in die Breite ziehen!)
|
||
|
|
btn_aspect = btn_trim.width / max(1, btn_trim.height)
|
||
|
|
btn_w = max(24, min(w // 2, int(round(h * btn_aspect))))
|
||
|
|
body_w = max(24, w - btn_w)
|
||
|
|
|
||
|
|
btn_scaled = btn_trim.resize((btn_w, h), Image.Resampling.LANCZOS)
|
||
|
|
|
||
|
|
# Linke Zierecke des Hauptkörpers proportional halten, Rest bis zum Button strecken
|
||
|
|
left_cap_src_w = max(12, int(body_trim.height * 0.30))
|
||
|
|
left_cap_dst_w = min(body_w // 2, max(8, int(round(h * 0.30))))
|
||
|
|
stretch_dst_w = max(1, body_w - left_cap_dst_w)
|
||
|
|
|
||
|
|
left_cap = body_trim.crop((0, 0, left_cap_src_w, body_trim.height)).resize(
|
||
|
|
(left_cap_dst_w, h), Image.Resampling.LANCZOS
|
||
|
|
)
|
||
|
|
mid_stretch = body_trim.crop((left_cap_src_w, 0, body_trim.width, body_trim.height)).resize(
|
||
|
|
(stretch_dst_w, h), Image.Resampling.BILINEAR
|
||
|
|
)
|
||
|
|
|
||
|
|
combined = Image.new("RGBA", (w, h), (0, 0, 0, 0))
|
||
|
|
combined.paste(left_cap, (0, 0))
|
||
|
|
combined.paste(mid_stretch, (left_cap_dst_w, 0))
|
||
|
|
combined.paste(btn_scaled, (body_w, 0))
|
||
|
|
return combined, btn_w
|
||
|
|
|
||
|
|
def _draw_tracked_text_clipped(
|
||
|
|
self,
|
||
|
|
img: Image.Image,
|
||
|
|
text: str,
|
||
|
|
x_left: int,
|
||
|
|
x_right: int,
|
||
|
|
color: str,
|
||
|
|
font_px: int,
|
||
|
|
):
|
||
|
|
if not text:
|
||
|
|
return
|
||
|
|
draw = ImageDraw.Draw(img)
|
||
|
|
clean_txt = self.assets.sanitize_goth_case(str(text))
|
||
|
|
fnt = _get_old_london(font_px)
|
||
|
|
avail_w = max(20, x_right - x_left)
|
||
|
|
|
||
|
|
def _measure(s: str) -> float:
|
||
|
|
tot = 0.0
|
||
|
|
for i, ch in enumerate(s):
|
||
|
|
tot += draw.textlength(ch, font=fnt)
|
||
|
|
if i < len(s) - 1:
|
||
|
|
tot += FONT_LETTER_SPACING_PX
|
||
|
|
return tot
|
||
|
|
|
||
|
|
disp = clean_txt
|
||
|
|
if _measure(disp) > avail_w:
|
||
|
|
while len(disp) > 1 and _measure(disp + "...") > avail_w:
|
||
|
|
disp = disp[:-1]
|
||
|
|
disp = disp.rstrip() + "..."
|
||
|
|
|
||
|
|
try:
|
||
|
|
asc, desc = fnt.getmetrics() # type: ignore[attr-defined]
|
||
|
|
th = asc + desc
|
||
|
|
except Exception:
|
||
|
|
th = font_px
|
||
|
|
|
||
|
|
cy = (img.height - th) / 2.0 + FONT_VERTICAL_OFFSET_PX
|
||
|
|
cx = float(x_left)
|
||
|
|
sx, sy = FONT_SHADOW_OFFSET_SMALL
|
||
|
|
|
||
|
|
for ch in disp:
|
||
|
|
if FONT_SHADOW_ENABLED and self._enabled:
|
||
|
|
draw.text((cx + sx, cy + sy), ch, font=fnt, fill=FONT_SHADOW_COLOR)
|
||
|
|
if FONT_OUTLINE_THICKNESS_PX > 0 and self._enabled:
|
||
|
|
draw.text(
|
||
|
|
(cx, cy),
|
||
|
|
ch,
|
||
|
|
font=fnt,
|
||
|
|
fill=color,
|
||
|
|
stroke_width=FONT_OUTLINE_THICKNESS_PX,
|
||
|
|
stroke_fill=FONT_OUTLINE_COLOR,
|
||
|
|
)
|
||
|
|
else:
|
||
|
|
draw.text((cx, cy), ch, font=fnt, fill=color)
|
||
|
|
cx += draw.textlength(ch, font=fnt) + FONT_LETTER_SPACING_PX
|
||
|
|
|
||
|
|
def _render_closed(self):
|
||
|
|
if not self.winfo_exists():
|
||
|
|
return
|
||
|
|
self.delete("all")
|
||
|
|
|
||
|
|
is_open = (self._popup is not None and self._popup.winfo_exists())
|
||
|
|
use_highlight_art = self._enabled and (is_open or self._hovered)
|
||
|
|
|
||
|
|
bar_pil, btn_w = self._compose_bar_pil(is_highlighted=use_highlight_art)
|
||
|
|
if bar_pil is None:
|
||
|
|
return
|
||
|
|
|
||
|
|
if not self._enabled:
|
||
|
|
alpha = bar_pil.getchannel("A")
|
||
|
|
rgb = ImageEnhance.Color(bar_pil.convert("RGB")).enhance(0.10)
|
||
|
|
rgb = ImageEnhance.Brightness(rgb).enhance(0.58)
|
||
|
|
bar_pil = rgb.convert("RGBA")
|
||
|
|
bar_pil.putalpha(alpha)
|
||
|
|
|
||
|
|
txt_col = (
|
||
|
|
FONT_COLOR_DISABLED
|
||
|
|
if not self._enabled
|
||
|
|
else (DROPDOWN_ITEM_TEXT_HOVER if use_highlight_art else DROPDOWN_TEXT_CLOSED)
|
||
|
|
)
|
||
|
|
|
||
|
|
pad_left = max(14, int(self._height * 0.36))
|
||
|
|
text_right_limit = self._width - btn_w - 8
|
||
|
|
|
||
|
|
self._draw_tracked_text_clipped(
|
||
|
|
bar_pil,
|
||
|
|
self._var.get(),
|
||
|
|
x_left=pad_left,
|
||
|
|
x_right=text_right_limit,
|
||
|
|
color=txt_col,
|
||
|
|
font_px=self._font_px,
|
||
|
|
)
|
||
|
|
|
||
|
|
self._closed_photo = ImageTk.PhotoImage(bar_pil)
|
||
|
|
self.create_image(0, 0, image=self._closed_photo, anchor="nw")
|
||
|
|
|
||
|
|
def _on_enter(self, _e):
|
||
|
|
if self._enabled:
|
||
|
|
self._hovered = True
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
def _on_leave(self, _e):
|
||
|
|
self._hovered = False
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
# --- Aufklapp-Liste (dropdown_box.png ohne inneren Auswahl-Hintergrund/Rahmen) ---
|
||
|
|
@classmethod
|
||
|
|
def close_any_open_popup(cls):
|
||
|
|
if cls._active_popup_owner is not None:
|
||
|
|
cls._active_popup_owner.close_popup()
|
||
|
|
|
||
|
|
def close_popup(self):
|
||
|
|
if GothDropdown._active_popup_owner is self:
|
||
|
|
GothDropdown._active_popup_owner = None
|
||
|
|
if self._popup is not None:
|
||
|
|
try:
|
||
|
|
if self._popup.winfo_exists():
|
||
|
|
self._popup.destroy()
|
||
|
|
except Exception:
|
||
|
|
pass
|
||
|
|
self._popup = None
|
||
|
|
self._popup_refs.clear()
|
||
|
|
self._hover_arrow = None
|
||
|
|
if self.winfo_exists():
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
def _toggle_popup(self, _event=None):
|
||
|
|
if not self._enabled or not self._values:
|
||
|
|
return
|
||
|
|
if self._popup is not None and self._popup.winfo_exists():
|
||
|
|
self.close_popup()
|
||
|
|
return
|
||
|
|
|
||
|
|
GothDropdown.close_any_open_popup()
|
||
|
|
if self.sounds:
|
||
|
|
self.sounds.play("click")
|
||
|
|
self._open_popup()
|
||
|
|
|
||
|
|
def _open_popup(self):
|
||
|
|
GothDropdown._active_popup_owner = self
|
||
|
|
scrollable = len(self._values) > self._max_visible
|
||
|
|
vis_count = min(len(self._values), self._max_visible)
|
||
|
|
|
||
|
|
row_h = max(30, self._height - 6)
|
||
|
|
border_pad = 12
|
||
|
|
arrow_h = self.ARROW_ZONE_H if scrollable else 0
|
||
|
|
top_offset = border_pad + arrow_h
|
||
|
|
|
||
|
|
pop_w = self._width
|
||
|
|
pop_h = vis_count * row_h + 2 * top_offset
|
||
|
|
|
||
|
|
cur_idx = self.current()
|
||
|
|
if cur_idx >= vis_count:
|
||
|
|
self._scroll_offset = min(cur_idx - vis_count + 1, max(0, len(self._values) - vis_count))
|
||
|
|
else:
|
||
|
|
self._scroll_offset = 0
|
||
|
|
self._hover_idx = -1
|
||
|
|
self._hover_arrow = None
|
||
|
|
|
||
|
|
self.update_idletasks()
|
||
|
|
rx = self.winfo_rootx()
|
||
|
|
ry = self.winfo_rooty() + self._height + 2
|
||
|
|
screen_h = self.winfo_screenheight()
|
||
|
|
if ry + pop_h > screen_h - 24:
|
||
|
|
ry = max(8, self.winfo_rooty() - pop_h - 2)
|
||
|
|
|
||
|
|
pop = tk.Toplevel(self)
|
||
|
|
pop.wm_overrideredirect(True)
|
||
|
|
pop.attributes("-topmost", True)
|
||
|
|
pop.geometry(f"{pop_w}x{pop_h}+{rx}+{ry}")
|
||
|
|
pop.configure(bg=BG_COLOR)
|
||
|
|
self._popup = pop
|
||
|
|
|
||
|
|
cv = tk.Canvas(pop, width=pop_w, height=pop_h, bg=BG_COLOR, bd=0, highlightthickness=0, cursor="hand2")
|
||
|
|
cv.pack(fill="both", expand=True)
|
||
|
|
self._pop_cv = cv
|
||
|
|
self._pop_geom = (pop_w, pop_h, row_h, top_offset, vis_count, scrollable, border_pad)
|
||
|
|
|
||
|
|
self._redraw_popup_list()
|
||
|
|
self._render_closed()
|
||
|
|
|
||
|
|
cv.bind("<Motion>", self._on_popup_motion)
|
||
|
|
cv.bind("<Leave>", self._on_popup_leave)
|
||
|
|
cv.bind("<Button-1>", self._on_popup_click)
|
||
|
|
|
||
|
|
# Mausrad auf Windows/macOS (<MouseWheel>) + Linux/Crostini (<Button-4>/<Button-5>)
|
||
|
|
for target in (pop, cv):
|
||
|
|
target.bind("<MouseWheel>", self._on_popup_wheel)
|
||
|
|
target.bind("<Button-4>", lambda _e: self._scroll_by(-1))
|
||
|
|
target.bind("<Button-5>", lambda _e: self._scroll_by(1))
|
||
|
|
|
||
|
|
pop.bind("<Up>", lambda _e: self._scroll_by(-1))
|
||
|
|
pop.bind("<Down>", lambda _e: self._scroll_by(1))
|
||
|
|
pop.bind("<Escape>", lambda _e: self.close_popup())
|
||
|
|
pop.bind("<FocusOut>", lambda _e: self.close_popup())
|
||
|
|
pop.focus_force()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def _draw_scroll_arrow(
|
||
|
|
draw: ImageDraw.ImageDraw,
|
||
|
|
cx: int,
|
||
|
|
cy: int,
|
||
|
|
direction: str,
|
||
|
|
enabled: bool,
|
||
|
|
hovered: bool,
|
||
|
|
):
|
||
|
|
"""Zeichnet einen klaren generischen Pfeil (▲ / ▼) oben bzw. unten in die Liste."""
|
||
|
|
if not enabled:
|
||
|
|
fill_col = FONT_COLOR_DISABLED
|
||
|
|
outline_col = None
|
||
|
|
elif hovered:
|
||
|
|
fill_col = "#ffffff"
|
||
|
|
outline_col = UI_ACCENT_AMETHYST
|
||
|
|
else:
|
||
|
|
fill_col = UI_ACCENT_GOLD
|
||
|
|
outline_col = "#0d0b12"
|
||
|
|
|
||
|
|
half_w = 8
|
||
|
|
half_h = 5
|
||
|
|
if direction == "up":
|
||
|
|
pts = [(cx, cy - half_h), (cx - half_w, cy + half_h), (cx + half_w, cy + half_h)]
|
||
|
|
else:
|
||
|
|
pts = [(cx, cy + half_h), (cx - half_w, cy - half_h), (cx + half_w, cy - half_h)]
|
||
|
|
|
||
|
|
draw.polygon(pts, fill=fill_col, outline=outline_col)
|
||
|
|
|
||
|
|
def _redraw_popup_list(self):
|
||
|
|
if self._popup is None or not self._popup.winfo_exists():
|
||
|
|
return
|
||
|
|
cv = self._pop_cv
|
||
|
|
pop_w, pop_h, row_h, top_offset, vis_count, scrollable, border_pad = self._pop_geom
|
||
|
|
cv.delete("all")
|
||
|
|
self._popup_refs.clear()
|
||
|
|
|
||
|
|
# dropdown_box.png bringt Rahmen & Hintergrund bereits komplett mit
|
||
|
|
box_pil = self.assets.get_9slice_pil("dropdown_box", (pop_w, pop_h))
|
||
|
|
if box_pil is None:
|
||
|
|
box_pil = Image.new("RGBA", (pop_w, pop_h), (16, 13, 22, 255))
|
||
|
|
else:
|
||
|
|
box_pil = box_pil.copy()
|
||
|
|
|
||
|
|
cur_val = self._var.get()
|
||
|
|
|
||
|
|
# Einträge komplett ohne Extra-Rahmen und ohne Auswahl-Hintergrund rendern
|
||
|
|
for slot in range(vis_count):
|
||
|
|
val_idx = self._scroll_offset + slot
|
||
|
|
if val_idx >= len(self._values):
|
||
|
|
break
|
||
|
|
item_txt = self._values[val_idx]
|
||
|
|
y0 = top_offset + slot * row_h
|
||
|
|
|
||
|
|
is_hov = (val_idx == self._hover_idx)
|
||
|
|
is_sel = (item_txt == cur_val)
|
||
|
|
|
||
|
|
if is_hov:
|
||
|
|
t_col = DROPDOWN_ITEM_TEXT_HOVER
|
||
|
|
elif is_sel:
|
||
|
|
t_col = DROPDOWN_ITEM_TEXT_SELECTED
|
||
|
|
else:
|
||
|
|
t_col = DROPDOWN_ITEM_TEXT_NORMAL
|
||
|
|
|
||
|
|
row_img = Image.new("RGBA", (pop_w, row_h), (0, 0, 0, 0))
|
||
|
|
self._draw_tracked_text_clipped(
|
||
|
|
row_img,
|
||
|
|
item_txt,
|
||
|
|
x_left=18,
|
||
|
|
x_right=pop_w - 18,
|
||
|
|
color=t_col,
|
||
|
|
font_px=max(FONT_SIZE_SMALL_MIN, self._font_px - 1),
|
||
|
|
)
|
||
|
|
box_pil.alpha_composite(row_img, (0, y0))
|
||
|
|
|
||
|
|
# Generelle Scroll-Pfeile oben & unten einzeichnen, sobald gescrollt werden kann
|
||
|
|
if scrollable:
|
||
|
|
draw = ImageDraw.Draw(box_pil)
|
||
|
|
can_up = self._scroll_offset > 0
|
||
|
|
can_down = (self._scroll_offset + vis_count) < len(self._values)
|
||
|
|
up_cy = border_pad + self.ARROW_ZONE_H // 2
|
||
|
|
dn_cy = pop_h - border_pad - self.ARROW_ZONE_H // 2
|
||
|
|
|
||
|
|
self._draw_scroll_arrow(
|
||
|
|
draw, pop_w // 2, up_cy, "up",
|
||
|
|
enabled=can_up, hovered=(self._hover_arrow == "up"),
|
||
|
|
)
|
||
|
|
self._draw_scroll_arrow(
|
||
|
|
draw, pop_w // 2, dn_cy, "down",
|
||
|
|
enabled=can_down, hovered=(self._hover_arrow == "down"),
|
||
|
|
)
|
||
|
|
|
||
|
|
tk_pop = ImageTk.PhotoImage(box_pil)
|
||
|
|
self._popup_refs.append(tk_pop)
|
||
|
|
cv.create_image(0, 0, image=tk_pop, anchor="nw")
|
||
|
|
|
||
|
|
def _hit_arrow_zone(self, y: int) -> str | None:
|
||
|
|
_pop_w, pop_h, _row_h, top_offset, _vis_count, scrollable, _border_pad = self._pop_geom
|
||
|
|
if not scrollable:
|
||
|
|
return None
|
||
|
|
if 0 <= y < top_offset:
|
||
|
|
return "up"
|
||
|
|
if (pop_h - top_offset) < y <= pop_h:
|
||
|
|
return "down"
|
||
|
|
return None
|
||
|
|
|
||
|
|
def _slot_from_y(self, y: int) -> int:
|
||
|
|
_pop_w, _pop_h, row_h, top_offset, vis_count, _scrollable, _border_pad = self._pop_geom
|
||
|
|
rel = y - top_offset
|
||
|
|
if rel < 0:
|
||
|
|
return -1
|
||
|
|
slot = rel // row_h
|
||
|
|
if 0 <= slot < vis_count and (self._scroll_offset + slot) < len(self._values):
|
||
|
|
return self._scroll_offset + slot
|
||
|
|
return -1
|
||
|
|
|
||
|
|
def _on_popup_motion(self, event):
|
||
|
|
arr = self._hit_arrow_zone(event.y)
|
||
|
|
idx = -1 if arr is not None else self._slot_from_y(event.y)
|
||
|
|
if idx != self._hover_idx or arr != self._hover_arrow:
|
||
|
|
self._hover_idx = idx
|
||
|
|
self._hover_arrow = arr
|
||
|
|
self._redraw_popup_list()
|
||
|
|
|
||
|
|
def _on_popup_leave(self, _event):
|
||
|
|
if self._hover_idx != -1 or self._hover_arrow is not None:
|
||
|
|
self._hover_idx = -1
|
||
|
|
self._hover_arrow = None
|
||
|
|
self._redraw_popup_list()
|
||
|
|
|
||
|
|
def _on_popup_click(self, event):
|
||
|
|
arr = self._hit_arrow_zone(event.y)
|
||
|
|
if arr == "up":
|
||
|
|
self._scroll_by(-1)
|
||
|
|
return
|
||
|
|
if arr == "down":
|
||
|
|
self._scroll_by(1)
|
||
|
|
return
|
||
|
|
|
||
|
|
idx = self._slot_from_y(event.y)
|
||
|
|
if 0 <= idx < len(self._values):
|
||
|
|
chosen = self._values[idx]
|
||
|
|
changed = (chosen != self._var.get())
|
||
|
|
if self.sounds:
|
||
|
|
self.sounds.play("click")
|
||
|
|
self._var.set(chosen)
|
||
|
|
self.close_popup()
|
||
|
|
if changed and callable(self._on_change):
|
||
|
|
self._on_change(chosen)
|
||
|
|
else:
|
||
|
|
self.close_popup()
|
||
|
|
|
||
|
|
def _scroll_by(self, delta: int):
|
||
|
|
if self._popup is None or not self._popup.winfo_exists():
|
||
|
|
return
|
||
|
|
_pop_w, _pop_h, _row_h, _top_offset, vis_count, scrollable, _border_pad = self._pop_geom
|
||
|
|
if not scrollable:
|
||
|
|
return
|
||
|
|
max_off = max(0, len(self._values) - vis_count)
|
||
|
|
new_off = max(0, min(max_off, self._scroll_offset + delta))
|
||
|
|
if new_off != self._scroll_offset:
|
||
|
|
self._scroll_offset = new_off
|
||
|
|
self._redraw_popup_list()
|
||
|
|
|
||
|
|
def _on_popup_wheel(self, event):
|
||
|
|
if getattr(event, "delta", 0) == 0:
|
||
|
|
return
|
||
|
|
step = -1 if event.delta > 0 else 1
|
||
|
|
self._scroll_by(step)
|