/usr/share/kivy-examples/gestures/gesture_board.py is in python-kivy-examples 1.9.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
from kivy.gesture import Gesture, GestureDatabase
from my_gestures import cross, circle, check, square
def simplegesture(name, point_list):
"""
A simple helper function
"""
g = Gesture()
g.add_stroke(point_list)
g.normalize()
g.name = name
return g
class GestureBoard(FloatLayout):
"""
Our application main widget, derived from touchtracer example, use data
constructed from touches to match symboles loaded from my_gestures.
"""
def __init__(self, *args, **kwargs):
super(GestureBoard, self).__init__()
self.gdb = GestureDatabase()
# add pre-recorded gestures to database
self.gdb.add_gesture(cross)
self.gdb.add_gesture(check)
self.gdb.add_gesture(circle)
self.gdb.add_gesture(square)
def on_touch_down(self, touch):
# start collecting points in touch.ud
# create a line to display the points
userdata = touch.ud
with self.canvas:
Color(1, 1, 0)
d = 30.
Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
userdata['line'] = Line(points=(touch.x, touch.y))
return True
def on_touch_move(self, touch):
# store points of the touch movement
try:
touch.ud['line'].points += [touch.x, touch.y]
return True
except (KeyError) as e:
pass
def on_touch_up(self, touch):
# touch is over, display informations, and check if it matches some
# known gesture.
g = simplegesture('', list(zip(touch.ud['line'].points[::2],
touch.ud['line'].points[1::2])))
# gestures to my_gestures.py
print("gesture representation:", self.gdb.gesture_to_str(g))
# print match scores between all known gestures
print("cross:", g.get_score(cross))
print("check:", g.get_score(check))
print("circle:", g.get_score(circle))
print("square:", g.get_score(square))
# use database to find the more alike gesture, if any
g2 = self.gdb.find(g, minscore=0.70)
print(g2)
if g2:
if g2[1] == circle:
print("circle")
if g2[1] == square:
print("square")
if g2[1] == check:
print("check")
if g2[1] == cross:
print("cross")
# erase the lines on the screen, this is a bit quick&dirty, since we
# can have another touch event on the way...
self.canvas.clear()
class DemoGesture(App):
def build(self):
return GestureBoard()
if __name__ == '__main__':
DemoGesture().run()
|