Pure Python gestures
Python gesture implementation on Github for those situations where you cannot or do not want to use the ObjC gestures.
Simple usage example:
import pygestures class MyTouchableView(pygestures.GestureView): def on_swipe(self, data): if data.direction in (data.UP, data.DOWN): print('I was swiped vertically')
Run the file as-is to play around with the gestures. (Green circles track your touches, crosshairs show the centroid, red circle reflects pan, pinch and rotation.)
In your subclass, implement any or all the methods below to handle gestures. All methods get an information object with attributes including:
state- one of BEGAN, CHANGED, ENDED
location- location of the touch, or the centroid of all touches, as a scene.Point
no_of_touches- use this if you want to filter for e.g. only taps with 2 fingers
on_swipe- data includes
direction, one of UP, DOWN, LEFT, RIGHT
on_pan- data includes
translation, the distance from the start of the gesture, as a scene.Point. For most purposes this is better than
location, as it does not jump around if you add more fingers.
on_pinch- data includes
on_rotate- data includes
rotationin degrees, negative for counterclockwise rotation
There are also
prev_rotation, if you need them.
If it is more convenient to you, you can inherit GestureMixin together with ui.View or some other custom view class. In that case, if you want to use e.g. rotate, you need to make sure you have set
multitouch_enabled = True.
Wow, this is very impressive. Sorry for that most likely stupid question: Is there a way to use this with a scene? I am trying to make a scene zoom+panable, but I did not manage to do it.
I wanted to use
def on_pinch(self, data): self.scale = data.scale
def on_pan(self, data): self.frame = (data.translation.x, data.translation.y, self.width, self.height)
inside my scene class, where I would have got
Maybe someone could provide me with a basic example for a zoom and panable scene?
Would be great, thanks in advance.
Panning and zooming is a bit more involved than just using the gestures, so in the same repo there is also zoompanscene.py, which you use like this:
from zoompanscene import * class SpaceScene(ZoomPanScene): def setup(self): super().setup() ship = SpriteNode('spc:PlayerShip1Orange') ship.position = self.size / 2 self.add_child(ship) run(SpaceScene())
I am just again overwhelmed by this new clean beautiful piece of API creation from @mikael ! Thank you so much for this...
Due to popular demand (I needed it) I added edge swipe gestures to the Python version. Implement some of these methods to use them:
Note: "left" means "toward left", or "from the right edge".
Also included in the same module a ZoomPanView implementation, which rotates as well! It has these constructor parameters (can be set as attributes, if you prefer):
Run the file to play with the demo, or implement something like this:
class ZoomPanDemo(ZoomPanView): def __init__(self, **kwargs): self.background_color='black' super().__init__( rotate=True, min_rotation=-45, max_rotation=45, **kwargs) self.crazy_text = ui.Label( text='Pan, zoom, rotate', text_color='white', alignment=ui.ALIGN_CENTER, flex='LTBR', ) self.crazy_text.size_to_fit() self.crazy_text.center = self.bounds.center() self.add_subview(self.crazy_text)