From 0aedd1c612ba9bc422370d75cb265808ea034fb6 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Thu, 28 Dec 2023 06:09:16 +0100 Subject: [PATCH] NEW: visualization.Window(): simple Tk visualization class, can just draw some lines for now, but with working realignment, zooming and panning --- requirements.txt | 1 - src/tools/visualization.py | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/tools/visualization.py diff --git a/requirements.txt b/requirements.txt index 4a2f3a2..be6a773 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,5 @@ bs4~=0.0.1 beautifulsoup4~=4.12.2 fishhook~=0.2.9 -pygame~=2.5.2 requests~=2.31.0 tqdm~=4.66 \ No newline at end of file diff --git a/src/tools/visualization.py b/src/tools/visualization.py new file mode 100644 index 0000000..223b495 --- /dev/null +++ b/src/tools/visualization.py @@ -0,0 +1,59 @@ +from __future__ import annotations +import tkinter as tk +from .coordinate import Line, Coordinate + + +class Window: + def __init__(self, width: int = 1280, height: int = 1024, bg_color: str = "black", fg_color: str = "white"): + self.width, self.height = width, height + self.bg_color, self.fg_color = bg_color, fg_color + + self.__tk_root = tk.Tk() + self.__canvas = tk.Canvas(master=self.__tk_root, width=width, height=height, bg=bg_color) + self.__canvas.pack(fill=tk.BOTH, expand=tk.YES) + self.__canvas.bind("", self._zoom) + self.__canvas.bind("", self._scroll_start) + self.__canvas.bind("", self._scroll_move) + + self.__boundary_box = [0, 0, 0, 0] + + def _update_boundaries(self, coord: Coordinate | tuple[int, int]) -> None: + if coord[0] < self.__boundary_box[0]: + self.__boundary_box[0] = coord[0] + elif coord[0] > self.__boundary_box[2]: + self.__boundary_box[2] = coord[0] + + if coord[1] < self.__boundary_box[1]: + self.__boundary_box[1] = coord[1] + elif coord[1] > self.__boundary_box[3]: + self.__boundary_box[3] = coord[1] + + def _zoom(self, event: tk.Event) -> None: + amount = 0.95 if event.delta < 0 else 1.05 + self.__canvas.scale(tk.ALL, 0, 0, amount, amount) + + def _scroll_start(self, event: tk.Event) -> None: + self.__canvas.scan_mark(event.x, event.y) + + def _scroll_move(self, event: tk.Event) -> None: + self.__canvas.scan_dragto(event.x, event.y, gain=1) + + def clear(self) -> None: + self.__canvas.delete(tk.ALL) + + def draw_line(self, line: Line) -> None: + self.__canvas.create_line(line.start.x, line.start.y, line.end.x, line.end.y, fill=self.fg_color, width=1) + self._update_boundaries(line.start) + self._update_boundaries(line.end) + + def realign(self) -> None: + print(self.__boundary_box) + if self.__boundary_box[0] < 0: + self.__canvas.move(tk.ALL, abs(self.__boundary_box[0]) + 10, 0) + if self.__boundary_box[1] < 0: + self.__canvas.move(tk.ALL, 0, abs(self.__boundary_box[1]) + 10) + self.__canvas.update() + + def done(self) -> None: + self.realign() + self.__canvas.mainloop()