Compare commits

..

2 commits

Author SHA1 Message Date
Ruben van de Ven
79853af1c4 describe keyboard controls 2024-11-05 18:35:03 +01:00
Ruben van de Ven
2c71d4a679 Handle keyboard events to move corners 2024-11-05 18:33:11 +01:00
3 changed files with 51 additions and 3 deletions

View file

@ -10,7 +10,7 @@ pip install pyglet-cornerpin
## Usage ## Usage
Create instance for a window, and register the event handlers for dragging the pins. Create instance for a window, and register the event handlers for dragging the pins. A `on_key_release` is also registered to handle keyboard controls to move the pins.
```python ```python
@ -46,3 +46,5 @@ pins = PygletCornerPin(window, corners)
``` ```
Run [pattern.py](examples/pattern.py) in the examples folder for a demo. Run [pattern.py](examples/pattern.py) in the examples folder for a demo.
To use the keyboard, select a pin with number keys 1-4, the use the arrow keys (optionally with ctrl/shift modifier) to move the handle.

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])