49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
|
|
import tkinter as tk
|
||
|
|
from PIL import Image, ImageTk
|
||
|
|
from views.theme import BG_COLOR
|
||
|
|
|
||
|
|
class ImageDie(tk.Label):
|
||
|
|
"""Ein Würfel-Widget, das seine Größe dynamisch anpassen kann."""
|
||
|
|
def __init__(self, master, assets, command=None, **kwargs):
|
||
|
|
super().__init__(master, bg=BG_COLOR, bd=0, highlightthickness=0, cursor="hand2", **kwargs)
|
||
|
|
self.assets = assets
|
||
|
|
self.value = 1
|
||
|
|
self.held = False
|
||
|
|
self.command = command
|
||
|
|
self.current_size = 70
|
||
|
|
self.image_ref = None
|
||
|
|
self.bind("<Button-1>", lambda e: self.on_click())
|
||
|
|
self.update_image()
|
||
|
|
|
||
|
|
def on_click(self):
|
||
|
|
if self.command:
|
||
|
|
self.command()
|
||
|
|
|
||
|
|
def toggle_hold(self):
|
||
|
|
self.held = not self.held
|
||
|
|
self.config(bg="#8B0000" if self.held else BG_COLOR)
|
||
|
|
|
||
|
|
def set_value(self, val):
|
||
|
|
self.value = val
|
||
|
|
self.update_image()
|
||
|
|
|
||
|
|
def update_image(self):
|
||
|
|
if self.assets and self.value in self.assets.raw_dice and self.assets.raw_dice[self.value]:
|
||
|
|
raw = self.assets.raw_dice[self.value]
|
||
|
|
img = ImageTk.PhotoImage(raw.resize((self.current_size, self.current_size), Image.Resampling.LANCZOS))
|
||
|
|
self.image_ref = img
|
||
|
|
self.config(image=self.image_ref, text="")
|
||
|
|
else:
|
||
|
|
self.config(text=f"[{self.value}]", font=("Helvetica", 24), fg="white")
|
||
|
|
|
||
|
|
def show_anim_frame(self, frame_idx):
|
||
|
|
if self.assets and self.assets.raw_anim_frames:
|
||
|
|
idx = frame_idx % len(self.assets.raw_anim_frames)
|
||
|
|
raw = self.assets.raw_anim_frames[idx]
|
||
|
|
img = ImageTk.PhotoImage(raw.resize((self.current_size, self.current_size), Image.Resampling.LANCZOS))
|
||
|
|
self.image_ref = img
|
||
|
|
self.config(image=self.image_ref)
|
||
|
|
|
||
|
|
def resize_die(self, new_size):
|
||
|
|
self.current_size = new_size
|
||
|
|
self.update_image()
|