1 GestureBox
panthertrader edited this page 2017-05-04 15:07:55 +02:00

A Sample how to use Gestures in Kivy

from kivy.app import App
from kivy.event import EventDispatcher
from kivy.lang import Builder
from kivy.gesture import Gesture, GestureDatabase
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget

from kivy.graphics.vertex_instructions import Line

# execute gesturebox of kivy examples for more gestures:
#/usr/share/kivy-examples/gestures

gesture_strings={
 "left_to_right_line":'eNq1l0tu2zAQhve8SLypwXlxOBdwtwVygMJJBMdIawu20za373CSpg9UljbMIoqpnx8pfsPIXO2f9t9e1rvhfHk+Denj23XMafUwQrq9OWy/DjdpRP/TL5TOtzfny+n4NJz9I6fVl1HS6r+Q24ilsTSUev/xuD9cWrfautlEt08tlUZ4nUGbwot3AUybD3mdM2UoYoACtQqJtQn9aAGKALOxYrYilA3Mb99tr4/DMY6kXRuijZCdTRVLZkLOGUzSefcLD363vP/21ll+PD7oMj6RZPNHBCn+bKbz+Bp464THsICwDI9qQspScq6Z2Zd2lo/Bp278sIsL7QIjVhTh7DUkVhesT9hF7cYPvWi9+BR+Cbrxwy8t9JuLAhiI72v2jYzzu4vCL0k3fvgl7cYPv7TEr98EluotkCWrmu+DWT6HX4Zu/PDLtIiPQn9WEM+XD4dell74sMvaCx9y2TrhJdwK9MKHWumlVkKt9FIroVZ6qZVQK8vUEoIWU3/5EjPZ/K4qYbZAJ3qILcvEsgoVFl8VLVisztPDa5FO9NBatBM9rBbrQ9ewqtCJHlZ1mVUBIEAvHPD/8prn31MaVnWZVZEs/lUtExfLMfN2aLg/DcPh/QigpZ0BVNNqgyDrnDZewH65jFrTtjXqa2NpY/3+KS1hkcDpRM2RkDydgEiUKwxsCT9FTCcoEoTTCY6EXBlFWoLpykxLJASmExoJq//emojHAsu1B4sFFpwe0nyBX70+Dvvd48WNGqQN/J0U31Ae+b5/uDy2hJcOZae1xsvxy3DaHu7b4dKibKA1v5Xf5/F0fHi+Dy57r7UXGvqJjyv6qUv8tXG+W/8EE0/L+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qMM7gGHw9w2GCGyzgQcN9uly7oUb0IHSe6aam40N4KwasaYW3godWeBFrZ7HgK7p+EC9m7WwW6uofg4tYy03gIFph1mqw+knp8BhetAK0wotWwFZ40QrUCi9igRvhnah1s1rNodg38ewxvKh10Aovat2oNiIw1O0xuph11IguYh23oaN4RdOILloRGtHFKk5WDULJZl/QtS/pbO7DRSpSG7g4RW4C96LUT0qtNiWdy38nXS06cdzKd/Hi1E9ObVV6YBjxiQ6O7NQ08324KPWTUtA1fGbHe0LBNv4/1kWMemrCFqGeW7BJfNLkE8DDrc2S4FjCLd23SWKTJpsQSnhRt63rpvtkUUmTSmerbUifo9O7wO7SdafpyT5eTHy0J1LLtdOw0mqNfsVlC14NPQW1TQmXEt7VCUoBLgKBPgaCLgLG8I2EKRPA8RA7bdnp0/YvGqQEFAlrMQ9zZafzN4ZhkQA9zuZzJ9bVuZhwlBOUE0GGeZ87x9Xhj8OCyYm8PEZW2I2zcV4Sg3V1Piaszom8JIZkmNPvgzFhywTC+wpiAqpEXh92VWe+tngP2+ZN8tQdD09D3B6citGpmNj7+7gfnlInRRUu9w3nl+6yPe3SK2d8nEh33dQ9bt7v/eW8f90JiaOIFWkKhuMflneGCdPr3uovChLZTw=='
}

gestures = GestureDatabase()
for name, gesture_string in gesture_strings.items():
    gesture = gestures.str_to_gesture(gesture_string)
    gesture.name = name
    gestures.add_gesture(gesture)


root = Builder.load_string('''
FloatLayout:
    
''')

class GestureBox(Widget):
    def __init__(self,**kwargs):
        for name,gst_string in gesture_strings.items():
            self.register_event_type("on_{}".format(name))

        super(GestureBox,self).__init__(**kwargs)

    def on_left_to_right_line(self):
        print("left to right gesture occured" )
        pass

    def on_right_to_left_line(self):
        print("right to left occured")
        pass

    def on_top_to_bottom_line(self):
        pass

    def on_bottom_to_top_line(self):
        pass

    def on_touch_down(self, touch):
        touch.ud['gesture_line'] = Line(points=(touch.x, touch.y))
        super (GestureBox, self).on_touch_down(touch)

    def on_touch_move(self, touch):
        touch.ud['gesture_line'].points += [touch.x, touch.y]
        super(GestureBox, self).on_touch_move(touch)

    def on_touch_up(self, touch):
        if "gesture_line" in touch.ud:
            gesture = Gesture()
            gesture.add_stroke(zip(touch.ud['gesture_line'].points[::2],
                touch.ud['gesture_line'].points[1::2]))
            gesture.normalize()
            match = gestures.find(gesture, minscore=0.9)
            if match:
                self.dispatch('on_{}'.format(match[1].name))
            else:
                pass
                #print("No gesture recognized")
        super(GestureBox, self).on_touch_up(touch)


# compare the gestures

class MainApp(App):

    def build(self):
        return GestureBox()

if __name__ == '__main__':
    MainApp().run()