From 03b39c19b99bb95b6ed497020dc0989e0012a552 Mon Sep 17 00:00:00 2001 From: klemek Date: Thu, 26 Aug 2021 12:52:25 +0200 Subject: [PATCH] can zoom --- watchy-image-editor/main.py | 56 +++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/watchy-image-editor/main.py b/watchy-image-editor/main.py index 7c1a29d..d252469 100644 --- a/watchy-image-editor/main.py +++ b/watchy-image-editor/main.py @@ -7,7 +7,7 @@ import os.path from math import sqrt -DRAW_SCALE = 3 +INITIAL_DRAW_SCALE = 3 class BitmapError(Exception): @@ -281,6 +281,8 @@ class App(ttk.Frame): self.parent = parent self.current_file = None + self.draw_scale = INITIAL_DRAW_SCALE + self.explorer = self.make_explorer() self.canvas = self.make_canvas() self.menu_file, self.menu_edit = self.make_menus() @@ -290,6 +292,8 @@ class App(ttk.Frame): self.open_file(None) + self.pack(fill="both", expand=True) + @property def current_image(self) -> Optional[Image]: if self.explorer.focus() == "": @@ -398,6 +402,10 @@ class App(ttk.Frame): view = ttk.Frame(self, height=650, width=650) view.grid(column=1, row=0, sticky=(N, S, E, W)) + view.bind("", self.zoom_canvas) + view.bind("", self.zoom_canvas_up) + view.bind("", self.zoom_canvas_down) + canvas = Canvas(view, width=0, height=0, background="white") canvas.place(in_=view, anchor="c", relx=0.5, rely=0.5) canvas.bind("", self.click_canvas_b1) @@ -407,6 +415,10 @@ class App(ttk.Frame): canvas.bind("", self.click_canvas_b3) canvas.bind("", self.update) + canvas.bind("", self.zoom_canvas) + canvas.bind("", self.zoom_canvas_up) + canvas.bind("", self.zoom_canvas_down) + return canvas def update(self, *args, force: bool = False) -> None: @@ -442,8 +454,8 @@ class App(ttk.Frame): ) else: self.canvas.configure( - width=(image.width * DRAW_SCALE), - height=(image.height * DRAW_SCALE), + width=(image.width * self.draw_scale), + height=(image.height * self.draw_scale), background="white", ) self.canvas.delete("all") @@ -451,10 +463,10 @@ class App(ttk.Frame): for y in range(image.height): if image.get_pixel(x, y): self.canvas.create_rectangle( - x * DRAW_SCALE, - y * DRAW_SCALE, - (x + 1) * DRAW_SCALE, - (y + 1) * DRAW_SCALE, + x * self.draw_scale + 1, + y * self.draw_scale + 1, + (x + 1) * self.draw_scale + 1, + (y + 1) * self.draw_scale + 1, fill="black", outline="", ) @@ -503,18 +515,32 @@ class App(ttk.Frame): def click_canvas(self, value, event): if self.current_image is None: return - x = event.x // DRAW_SCALE - y = event.y // DRAW_SCALE + x = int(event.x / self.draw_scale) + y = int(event.y / self.draw_scale) self.current_image.set_pixel(x, y, value) self.canvas.create_rectangle( - x * DRAW_SCALE, - y * DRAW_SCALE, - (x + 1) * DRAW_SCALE, - (y + 1) * DRAW_SCALE, + x * self.draw_scale + 1, + y * self.draw_scale + 1, + (x + 1) * self.draw_scale + 1, + (y + 1) * self.draw_scale + 1, fill=("black" if value else "white"), outline="", ) + def zoom_canvas(self, event): + if event.delta > 0: + self.zoom_canvas_up() + else: + self.zoom_canvas_down() + + def zoom_canvas_up(self, event=None): + self.draw_scale *= 2 + self.update_canvas() + + def zoom_canvas_down(self, event=None): + self.draw_scale /= 2 + self.update_canvas() + def save_file(self, path: Optional[str] = None) -> None: if path == "": path = filedialog.asksaveasfilename() @@ -569,6 +595,8 @@ class App(ttk.Frame): if __name__ == "__main__": app = App(Tk()) - app.pack(fill="both", expand=True) + + # TODO remove + app.open_file("../watchfaces/tetris-2.0/tetris.h") app.mainloop() \ No newline at end of file