Skip to content

Commit

Permalink
Merge pull request #87 from houseofsecrets/develop
Browse files Browse the repository at this point in the history
Eraser zone tool
  • Loading branch information
Danamir authored Nov 15, 2023
2 parents fe42cf7 + 08a6272 commit eac29a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
41 changes: 39 additions & 2 deletions scripts/views/PygameView.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit eac29a6

Please sign in to comment.