From dd960a67d5e39b351ebd879e1515d10c9790108a Mon Sep 17 00:00:00 2001 From: Danamir Date: Wed, 15 Nov 2023 23:41:44 +0100 Subject: [PATCH 1/2] Added eraser zone tool, key shortcut 'z' --- scripts/views/PygameView.py | 41 +++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/scripts/views/PygameView.py b/scripts/views/PygameView.py index 3b1081d..c0773a7 100644 --- a/scripts/views/PygameView.py +++ b/scripts/views/PygameView.py @@ -132,18 +132,22 @@ def __init__(self, img2img): pygame.draw.rect(self.canvas, (255, 255, 255), (0, 0, self.state.render["width"] * (1 if self.state.img2img else 2), self.state.render["height"])) # Set up the brush - self.brush_size = {1: 2, 2: 2, 'e': 10} + self.brush_size = {1: 2, 2: 2, 'e': 10, 'z': 2} self.brush_colors = { 1: (0, 0, 0), # Left mouse button color 2: (255, 255, 255), # Middle mouse button color 'e': (255, 255, 255), # Eraser color + 'z': (200, 200, 200), # Eraser zone color } self.brush_color = self.brush_colors[1] self.brush_pos = {1: None, 2: None, 'e': None} # type: dict[int|str, tuple[int, int]|None] + self.button_down = False self.prev_pos = None self.prev_pos2 = None self.shift_pos = None self.eraser_down = False + self.eraser_zone_down = False + self.eraser_zone_pos = [] self.render_wait = 0.5 if not self.state.img2img else 0.0 # wait time max between 2 draw before launching the render self.last_draw_time = time.time() self.last_render_bytes: io.BytesIO | None = None @@ -900,6 +904,8 @@ def main(self): self.running = False elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.FINGERDOWN: + self.button_down = True + # Handle brush stroke start and modifiers if event.type == pygame.FINGERDOWN: event.button = 1 @@ -909,6 +915,10 @@ def main(self): self.image_click = True # clicked on the image part if self.state.render["batch_size"] != 1 and len(self.state.render["batch_images"]): self.select_batch_image(event.pos) + elif self.eraser_zone_down: + self.eraser_zone_pos.append((max(self.state.render["width"], event.pos[0]), event.pos[1])) + if len(self.eraser_zone_pos) > 1: + pygame.draw.polygon(self.canvas, self.brush_colors['z'], (self.eraser_zone_pos[-2], self.eraser_zone_pos[-1]), self.brush_size['z']) else: self.need_redraw = True self.last_draw_time = time.time() @@ -937,13 +947,21 @@ def main(self): self.shift_pos = self.brush_pos[brush_key] elif event.type == pygame.MOUSEBUTTONUP or event.type == pygame.FINGERUP: + self.button_down = False + # Handle brush stoke end self.last_draw_time = time.time() + if self.state.render["quick_mode"]: + self.instant_render = True + if event.type == pygame.FINGERUP: event.button = 1 event.pos = self.finger_pos(event.x, event.y) + if self.eraser_zone_down: + self.need_redraw = True + if not self.image_click: self.need_redraw = True self.rendering = True @@ -967,7 +985,13 @@ def main(self): if event.type == pygame.FINGERMOTION: event.pos = self.finger_pos(event.x, event.y) - if not self.image_click: + if self.eraser_zone_down: + self.need_redraw = True + if self.button_down: + self.eraser_zone_pos.append((max(self.state.render["width"], event.pos[0]), event.pos[1])) + if len(self.eraser_zone_pos) > 1: + pygame.draw.polygon(self.canvas, self.brush_colors['z'], self.eraser_zone_pos[-2:], self.brush_size['z']) + elif not self.image_click: self.need_redraw = True for button, pos in self.brush_pos.items(): if pos is not None and button in self.brush_colors: @@ -1173,6 +1197,9 @@ def main(self): elif event.key == pygame.K_e: self.eraser_down = True + elif event.key == pygame.K_z: + self.eraser_zone_down = True + elif event.key == pygame.K_t: if self.shift_down: if self.render_wait == 2.0: @@ -1268,6 +1295,16 @@ def main(self): self.eraser_down = False self.brush_pos['e'] = None + if event.key == pygame.K_z: + self.eraser_zone_down = False + self.rendering = True + self.need_redraw = True + self.brush_pos['e'] = None + if len(self.eraser_zone_pos) > 2: + pygame.draw.polygon(self.canvas, self.brush_colors['e'], self.eraser_zone_pos, self.brush_size['z']) + pygame.draw.polygon(self.canvas, self.brush_colors['e'], self.eraser_zone_pos) + self.eraser_zone_pos = [] + elif event.key in (pygame.K_LSHIFT, pygame.K_RSHIFT): if self.rendering_key: self.rendering = True From 08a627257dd66f21d8983ec64f2fa7ec347aad15 Mon Sep 17 00:00:00 2001 From: Danamir Date: Wed, 15 Nov 2023 23:43:29 +0100 Subject: [PATCH 2/2] Eraser zone tool documentation --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b0c27b..a9b8162 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,11 @@ the canvas when the image is generated. ## Controls | Key / Mouse button | Control | -| ----------------------------- |----------------------------------------------------| +|-------------------------------|----------------------------------------------------| | Left button | Draw with the current brush size | | Middle button | Draw with a white color brush | | `e` + Left button | Eraser brush (bigger) | +| `z` + Left button | Draw zone to be erased | | Scroll up / down | Increase / decrease brush size | | `1` to `9` | Set brush size | | `backspace` | Erase the entire sketch |