From 2c71d4a6790f63c8992226b62c624ffd91ccf631 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Tue, 5 Nov 2024 18:33:11 +0100 Subject: [PATCH] Handle keyboard events to move corners --- examples/pattern.py | 2 +- pyglet_cornerpin/cornerpin.py | 48 ++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/examples/pattern.py b/examples/pattern.py index d32db6b..dbdddc2 100644 --- a/examples/pattern.py +++ b/examples/pattern.py @@ -1,5 +1,5 @@ import pyglet -from pyglet_corner_pin import PygletCornerPin +from pyglet_cornerpin import PygletCornerPin window = pyglet.window.Window() diff --git a/pyglet_cornerpin/cornerpin.py b/pyglet_cornerpin/cornerpin.py index 81b3166..8daec18 100644 --- a/pyglet_cornerpin/cornerpin.py +++ b/pyglet_cornerpin/cornerpin.py @@ -96,6 +96,7 @@ class PygletCornerPin(pyglet.event.EventDispatcher): self.handles = [ pyglet.shapes.Arc(c[0],c[1],20, thickness=2, color=(0,0,255,255), batch=self.batch) for c in self.corners ] + self.current_corner = None def update_handles(self): for i, c in enumerate(self.corners): @@ -148,7 +149,52 @@ class PygletCornerPin(pyglet.event.EventDispatcher): self.corners[self.dragging][0] = x self.corners[self.dragging][1] = y self.update_view() - + + def on_key_release(self, symbol: int, modifiers: int): + if symbol not in [ + pyglet.window.key._1, pyglet.window.key._2, pyglet.window.key._3, pyglet.window.key._4, + pyglet.window.key.ESCAPE, + pyglet.window.key.UP, pyglet.window.key.DOWN, pyglet.window.key.LEFT, pyglet.window.key.RIGHT, + ]: + return pyglet.event.EVENT_UNHANDLED + + if symbol == pyglet.window.key._1: + self.current_corner = 2 + if symbol == pyglet.window.key._2: + self.current_corner = 3 + if symbol == pyglet.window.key._3: + self.current_corner = 0 + if symbol == pyglet.window.key._4: + self.current_corner = 1 + if symbol == pyglet.window.key.ESCAPE: + self.current_corner = None + + for i, h in enumerate(self.handles): + h.color = (255,0,0,255) if i == self.current_corner else (0,0,255,255) + + if self.current_corner is None: + return + + + if symbol not in [pyglet.window.key.UP,pyglet.window.key.DOWN, pyglet.window.key.LEFT, pyglet.window.key.RIGHT]: + return + + base = 2 + if modifiers & pyglet.window.key.MOD_SHIFT: + base += 10 + if modifiers & pyglet.window.key.MOD_CTRL: + base *= 2 + + if symbol == pyglet.window.key.RIGHT: + self.corners[self.current_corner][0] += base + if symbol == pyglet.window.key.LEFT: + self.corners[self.current_corner][0] -= base + if symbol == pyglet.window.key.UP: + self.corners[self.current_corner][1] += base + if symbol == pyglet.window.key.DOWN: + self.corners[self.current_corner][1] -= base + + self.update_view() def update_view(self): self.window.view = self.transform2d_matrix(*[x for c in self.corners for x in c])