Handle keyboard events to move corners

This commit is contained in:
Ruben van de Ven 2024-11-05 18:33:11 +01:00
parent c8814887ad
commit 2c71d4a679
2 changed files with 48 additions and 2 deletions

View file

@ -1,5 +1,5 @@
import pyglet import pyglet
from pyglet_corner_pin import PygletCornerPin from pyglet_cornerpin import PygletCornerPin
window = pyglet.window.Window() window = pyglet.window.Window()

View file

@ -96,6 +96,7 @@ class PygletCornerPin(pyglet.event.EventDispatcher):
self.handles = [ 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 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): def update_handles(self):
for i, c in enumerate(self.corners): 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][0] = x
self.corners[self.dragging][1] = y self.corners[self.dragging][1] = y
self.update_view() 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): def update_view(self):
self.window.view = self.transform2d_matrix(*[x for c in self.corners for x in c]) self.window.view = self.transform2d_matrix(*[x for c in self.corners for x in c])