visualization.Window(): better boundary box handling
This commit is contained in:
parent
ed0fe0dafc
commit
2d7d339bf0
@ -15,17 +15,16 @@ class Window:
|
||||
self.__canvas.bind("<ButtonPress-1>", self._scroll_start)
|
||||
self.__canvas.bind("<B1-Motion>", self._scroll_move)
|
||||
|
||||
self.__boundary_box = [0, 0, 0, 0]
|
||||
self.__boundary_box = [None, None, None, None]
|
||||
|
||||
def _update_boundaries(self, coord: Coordinate | tuple[int, int]) -> None:
|
||||
if coord[0] < self.__boundary_box[0]:
|
||||
if self.__boundary_box[0] is None or coord[0] < self.__boundary_box[0]:
|
||||
self.__boundary_box[0] = coord[0]
|
||||
elif coord[0] > self.__boundary_box[2]:
|
||||
if self.__boundary_box[2] is None or coord[0] > self.__boundary_box[2]:
|
||||
self.__boundary_box[2] = coord[0]
|
||||
|
||||
if coord[1] < self.__boundary_box[1]:
|
||||
if self.__boundary_box[1] is None or coord[1] < self.__boundary_box[1]:
|
||||
self.__boundary_box[1] = coord[1]
|
||||
elif coord[1] > self.__boundary_box[3]:
|
||||
if self.__boundary_box[3] is None or coord[1] > self.__boundary_box[3]:
|
||||
self.__boundary_box[3] = coord[1]
|
||||
|
||||
def _zoom(self, event: tk.Event) -> None:
|
||||
@ -38,18 +37,20 @@ class Window:
|
||||
def _scroll_move(self, event: tk.Event) -> None:
|
||||
self.__canvas.scan_dragto(event.x, event.y, gain=1)
|
||||
|
||||
def clear(self) -> None:
|
||||
def clear(self, keep_boundaries: bool = False) -> None:
|
||||
self.__canvas.delete(tk.ALL)
|
||||
if not keep_boundaries:
|
||||
self.__boundary_box = [None, None, None, None]
|
||||
|
||||
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, padding: int = 10) -> None:
|
||||
if self.__boundary_box[0] < 0:
|
||||
def realign(self, padding: int = 1) -> None:
|
||||
if self.__boundary_box[0] is not None and self.__boundary_box[0] < 0:
|
||||
self.__canvas.move(tk.ALL, abs(self.__boundary_box[0]) + padding, 0)
|
||||
if self.__boundary_box[1] < 0:
|
||||
if self.__boundary_box[1] is not None and self.__boundary_box[1] < 0:
|
||||
self.__canvas.move(tk.ALL, 0, abs(self.__boundary_box[1]) + padding)
|
||||
dim_x = self.__boundary_box[2] - self.__boundary_box[0] + 2 * padding
|
||||
dim_y = self.__boundary_box[3] - self.__boundary_box[1] + 2 * padding
|
||||
|
||||
Loading…
Reference in New Issue
Block a user