From 17d18798889a354781d58334001a39e7e4bb87e2 Mon Sep 17 00:00:00 2001 From: Danamir Date: Thu, 16 Nov 2023 04:05:37 +0100 Subject: [PATCH 1/2] Added save/keep zone tool, key shortcut 's' --- scripts/views/PygameView.py | 79 ++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/scripts/views/PygameView.py b/scripts/views/PygameView.py index c0773a7..c1002d7 100644 --- a/scripts/views/PygameView.py +++ b/scripts/views/PygameView.py @@ -132,12 +132,13 @@ 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, 'z': 2} + self.brush_size = {1: 2, 2: 2, 'e': 10, 'z': 2, 's': 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 + 's': (200, 200, 255), # Save 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] @@ -146,8 +147,9 @@ def __init__(self, img2img): self.prev_pos2 = None self.shift_pos = None self.eraser_down = False - self.eraser_zone_down = False - self.eraser_zone_pos = [] + self.erase_zone_down = False + self.save_zone_down = False + self.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 @@ -911,14 +913,22 @@ def main(self): event.button = 1 event.pos = self.finger_pos(event.x, event.y) + valid_brush_pos = (max(self.state.render["width"], event.pos[0]), event.pos[1]) + if event.pos[0] < self.state.render["width"]: 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']) + elif self.erase_zone_down: + if not self.zone_pos or self.zone_pos[-1] != valid_brush_pos: + self.zone_pos.append(valid_brush_pos) + if len(self.zone_pos) > 1: + pygame.draw.polygon(self.canvas, self.brush_colors['z'], (self.zone_pos[-2], self.zone_pos[-1]), self.brush_size['z']) + elif self.save_zone_down: + if not self.zone_pos or self.zone_pos[-1] != valid_brush_pos: + self.zone_pos.append(valid_brush_pos) + if len(self.zone_pos) > 1: + pygame.draw.polygon(self.canvas, self.brush_colors['s'], (self.zone_pos[-2], self.zone_pos[-1]), self.brush_size['s']) else: self.need_redraw = True self.last_draw_time = time.time() @@ -959,7 +969,7 @@ def main(self): event.button = 1 event.pos = self.finger_pos(event.x, event.y) - if self.eraser_zone_down: + if self.erase_zone_down or self.save_zone_down: self.need_redraw = True if not self.image_click: @@ -985,12 +995,21 @@ def main(self): if event.type == pygame.FINGERMOTION: event.pos = self.finger_pos(event.x, event.y) - if self.eraser_zone_down: + valid_brush_pos = (max(self.state.render["width"], event.pos[0]), event.pos[1]) + if self.erase_zone_down: + self.need_redraw = True + if self.button_down: + if not self.zone_pos or self.zone_pos[-1] != valid_brush_pos: + self.zone_pos.append(valid_brush_pos) + if len(self.zone_pos) > 1: + pygame.draw.polygon(self.canvas, self.brush_colors['z'], self.zone_pos[-2:], self.brush_size['z']) + elif self.save_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']) + if not self.zone_pos or self.zone_pos[-1] != valid_brush_pos: + self.zone_pos.append(valid_brush_pos) + if len(self.zone_pos) > 1: + pygame.draw.polygon(self.canvas, self.brush_colors['s'], self.zone_pos[-2:], self.brush_size['s']) elif not self.image_click: self.need_redraw = True for button, pos in self.brush_pos.items(): @@ -1193,12 +1212,14 @@ def main(self): samplers = self.state.samplers["list"] self.state.samplers["sampler"] = samplers[(samplers.index(self.state.samplers["sampler"]) + 1) % len(samplers)] self.osd(text=f"Sampler: {self.state['samplers']['sampler']}") + else: + self.save_zone_down = True elif event.key == pygame.K_e: self.eraser_down = True elif event.key == pygame.K_z: - self.eraser_zone_down = True + self.erase_zone_down = True elif event.key == pygame.K_t: if self.shift_down: @@ -1295,22 +1316,34 @@ 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 self.rendering_key = False self.shift_pos = None + elif event.key in (pygame.K_z, pygame.K_s): + self.rendering = True + self.need_redraw = True + self.brush_pos['e'] = None + if len(self.zone_pos) > 2: + if self.save_zone_down: + self.zone_pos.insert(0, (self.state.render["width"], 0)) + # self.zone_pos.insert(1, (self.zone_pos[1][0], 0)) + self.zone_pos.append(self.zone_pos[1]) + self.zone_pos.append(self.zone_pos[0]) + self.zone_pos.append((self.state.render["width"]*2, 0)) + self.zone_pos.append((self.state.render["width"]*2, self.state.render["height"])) + self.zone_pos.append((self.state.render["width"], self.state.render["height"])) + + pygame.draw.polygon(self.canvas, self.brush_colors['e'], self.zone_pos, self.brush_size['z']) + pygame.draw.polygon(self.canvas, self.brush_colors['e'], self.zone_pos) + + self.zone_pos = [] + + self.erase_zone_down = False + self.save_zone_down = False + elif event.key in (pygame.K_c,): # Remove OSD always-on text self.need_redraw = True From 759f33308b040601a2f74217b03e523d3a43331a Mon Sep 17 00:00:00 2001 From: Danamir Date: Thu, 16 Nov 2023 04:06:04 +0100 Subject: [PATCH 2/2] Save/keep zone tool documentation --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a9b8162..1e41ecd 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ the canvas when the image is generated. | Middle button | Draw with a white color brush | | `e` + Left button | Eraser brush (bigger) | | `z` + Left button | Draw zone to be erased | +| `s` + Left button | Draw zone to be kept | | Scroll up / down | Increase / decrease brush size | | `1` to `9` | Set brush size | | `backspace` | Erase the entire sketch |