本文整理汇总了Python中kivy.gesture.GestureDatabase.find方法的典型用法代码示例。如果您正苦于以下问题:Python GestureDatabase.find方法的具体用法?Python GestureDatabase.find怎么用?Python GestureDatabase.find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kivy.gesture.GestureDatabase
的用法示例。
在下文中一共展示了GestureDatabase.find方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TabPannel
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
class TabPannel(TabbedPanel):
def __init__(self, *args, **kwargs):
super(TabPannel, self).__init__(*args, **kwargs)
self.app = RfAttendance.get_running_app()
self.gdb = GestureDatabase()
self.left_to_right_gesture = self.gdb.str_to_gesture(left_to_right_gesture)
self.right_to_left_gesture = self.gdb.str_to_gesture(right_to_left_gesture)
self.gdb.add_gesture(self.left_to_right_gesture)
self.gdb.add_gesture(self.right_to_left_gesture)
def on_touch_down(self, touch):
touch.ud['line'] = Line(points=(touch.x, touch.y))
return super(TabPannel, self).on_touch_down(touch)
def on_touch_move(self, touch):
try:
touch.ud['line'].points += [touch.x, touch.y]
return super(TabbedPanel, self).on_touch_move(touch)
except KeyError:
pass
def on_touch_up(self, touch):
g = Gesture()
g.add_stroke(list(zip(
touch.ud['line'].points[::2],
touch.ud['line'].points[1::2]
)))
g.normalize()
g.name = ''
g2 = self.gdb.find(g, minscore=0.70)
if g2 and g2[1] == self.left_to_right_gesture:
self.switch_to_left()
elif g2 and g2[1] == self.right_to_left_gesture:
self.switch_to_right()
return super(TabPannel, self).on_touch_up(touch)
def switch_to_right(self):
current_tab_index = self.tab_list.index(self.current_tab)
if current_tab_index == 0:
return
try:
self.switch_to(self.tab_list[current_tab_index - 1])
except IndexError:
pass
def switch_to_left(self):
current_tab_index = self.tab_list.index(self.current_tab)
try:
self.switch_to(self.tab_list[current_tab_index + 1])
except IndexError:
pass
示例2: GestureListener
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
class GestureListener(Widget):
def __init__(self, **kwargs): # inicializace
self.app = kwargs['root']
self.register_event_type('on_swipe_right')
self.register_event_type('on_swipe_left')
super(GestureListener, self).__init__(**kwargs)
self.gdb = GestureDatabase()
self.GSTR_SWIPE_LEFT = \
self.gdb.str_to_gesture('eNqtls2O2jAURvd+EdgU+f7bL0C3lXiAikIEaKYQQabtvH3tC0ITQZtZeJXkU3ycfEdK7vzwcvj1vth1l+Ht3IWvt2Mfw3zbQ1jNjuuf3Sz0WE7LgcJlNbsM59NLdymXHOavvYT5U8jKbwu9VpSV9f3pcBzqslSX5X8s+1bvCj1cn6A+wntZAhiWcRHr9n/qJdVL0pQNjZUIkAzC5cf6/1B2qITdnbe7oRBSxEwSIXJCmkb5a4E9olDEVCBnUGAUsmlWclZuwULvDeGRVSr62BfjNAudRU1YXj1KE5Z3j9aE5d3jQ/dfyikrIEgmzMCRTSZh5OUTtIF5+0RtYF4/SRuY90/WBuYCqI0AdgH8TAAmyBaVc8oCZvkTMBfA1AbmAljawFwAWxuYC+DcBCYuQNoIEBcgbQSIC5BnAjjFmCiJMSOxpemvv7gAsTYwFyC5CUxdgEIbmAtQagNzAdpGgLoAbSNAXYA+E6AEEa38eVWTaflNTLHM+zdowvL6jZqwvH2TT7LqmLc5d93xPrSZ1qnNLMyXlqSsXxLnchh6S2H9GOZrGD+GKXpoNArBQ8mjED1kGIXkIego5BpqHu2exEMbMzWsr6+27w67/VAnUbuPlb8P22FfoxSWEPUaDqfX7rw+buoInLJPGDW+Vfy9P5+2b5sKymUfXmA0SGqmkAG0Dh+Lv5QfIpE=')
self.GSTR_SWIPE_RIGHT = \
self.gdb.str_to_gesture('eNq1l82O2jAQgO9+Ebhs5Pm3X4C9VuIBKgoRi3YLEWTb7tvXnmzbjVQKquRcDJOZb4Z8KLKXh+fDt7du31/G13MfHt/XIYblboCwXhw3X/tFGLB8LAuFy3pxGc+n5/5SvnJYvgwSln+FrD0tDFpRVuqH0+E41rJUy/KVsk81KwwwTVBHeCslgGEVuxhZWRNH4KQ55eTz/Kj3KaweYkcZDJMiCwmBhsuXzb+7sHeRsL/eYP+LzQaQsmSIlqLqHXT/6WAT/aHi0VKCyDFlhMyW9A8fwWI0Js7MSJZv45PjcyM8ugGEVnh0PLXCu1mU+/CRAIgSppwVE0q6LRddLlq7Bq4Xc7MG5IIJ2jVwxUTtGrhkaieZXDLdI7n8R9nUVCxnFJUot+lumHIbOrtehkZ0d8vUiO5iWRrR3So3sspulRtZFbcqjayKW5VGVsWtyn1WBYSIxZBztKh8m+5WxRrR3arkNnR1qwqN6G5VqRHdrWojq+pWtZFVdavayKq5VWtk1dyq3WdVY0IDMpn2t7fhLtXkf+H1mLA99/3x96bftO76zcJyBdrFsCIqy8dLwzhYCpuSkK4n5JqAaX4n8pX0FGu60sTjspQYeA/ALs+uDxk4TZFmZeSdNc6CPI0js6CEzfQMnvrD/mmsRx4NK5yPV95UJeP7YTc+1QQrCZpLfQmOp5f+vDlu68krpfqIoYbfVX0ezqfd69axZWjuQKJC3dYgkGp9D3c/Ab86orc=')
self.gdb.add_gesture(self.GSTR_SWIPE_LEFT)
self.gdb.add_gesture(self.GSTR_SWIPE_RIGHT)
def simplegesture(self, name, point_list):
# Pomocná funkce pro rozpoznávání gesta
g = Gesture()
g.add_stroke(point_list)
g.normalize()
g.name = name
return g
def on_touch_down(self, touch):
# Při doteku, spustí zaznamenávání
if self.collide_point(touch.x, touch.y):
touch.ud["line"] = Line(points=(touch.x, touch.y))
touch.grab(self)
print "< grabbed >"
def on_touch_move(self, touch):
# Při pohybu prstu, zaznamenává pohyb
if (touch.grab_current is self):
touch.ud["line"].points += [touch.x, touch.y]
def on_touch_up(self, touch):
# Při zvednutí prstu, porovnává s gestem v databázi
if (touch.grab_current is self):
g = self.simplegesture('', zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2]))
g2 = self.gdb.find(g, minscore=0.80)
if g2:
if g2[1] == self.GSTR_SWIPE_LEFT:
self.dispatch('on_swipe_left')
if g2[1] == self.GSTR_SWIPE_RIGHT:
self.dispatch('on_swipe_right')
touch.ungrab(self)
def on_swipe_left(self, *args):
# Událost přejetí prstu doleva
self.app.on_swipe_left()
def on_swipe_right(self, *args):
# Událost přejetí prstu doprava
self.app.on_swipe_right()
示例3: GestureBoard
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
class GestureBoard(FloatLayout):
"""
Our application main widget, derived from touchtracer example, use data
constructed from touches to match symboles loaded from my_gestures.
"""
edge_size = NumericProperty(0)
exists=BooleanProperty(False)
def __init__(self, *args, **kwargs):
super(GestureBoard, self).__init__()
self.gdb = GestureDatabase()
# add pre-recorded gestures to database
for square in squares:
self.gdb.add_gesture(square)
def on_touch_down(self, touch):
super(GestureBoard,self).on_touch_down(touch)
if self.collide_point(*touch.pos):
if App.get_running_app().config.getboolean('museotouch','validation') == True:
# start collecting points in touch.ud
# create a line to display the points
userdata = touch.ud
userdata['line'] = Line(points=(touch.x, touch.y))
return True
def on_touch_move(self, touch):
if self.collide_point(*touch.pos):
super(GestureBoard,self).on_touch_move(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):
super(GestureBoard,self).on_touch_up(touch)
# touch is over, display informations, and check if it matches some
# known gesture.
try :
g = simplegesture(
'',
list(zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2]))
)
self.edge_size = (self.stroke_length(list(zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2]))))/4
if self.edge_size < 150:
self.edge_size=150
# gestures to my_gestures.py
except :
return
# use database to find the more alike gesture, if any
g2 = self.gdb.find(g, minscore=0.9)
if g2:
for index,square in enumerate(squares) :
if (g2[1] == square):
if index in [0,1]:
square_pos=[touch.x,touch.y-self.edge_size]
elif index in [2,3]:
square_pos=[touch.x-self.edge_size,touch.y-self.edge_size]
elif index in [4,5]:
square_pos=[touch.x-self.edge_size,touch.y]
elif index in [6,7]:
square_pos=[touch.x,touch.y]
valid = Valid(pos=(0,0),size=[self.edge_size,self.edge_size],rotation=180,scale_min=0.5)
self.add_widget(valid)
Animation(pos=square_pos,d=.3,rotation=0,transition='out_sine').start(valid)
self.exists=True
break
def stroke_length(self,l):
distance = 0
for index, point in enumerate(l) :
if index < len(l)-1:
distance += Vector(point).distance(l[index+1])
return distance
示例4: Gesture
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
from kivy.gesture import Gesture, GestureDatabase
# Create a gesture
g = Gesture()
g.add_stroke(point_list=[(1, 1), (3, 4), (2, 1)])
g.normalize()
g.name = "triangle"
# Add it to database
gdb = GestureDatabase()
gdb.add_gesture(g)
# And for the next gesture, try to find a match!
g2 = Gesture()
g2.add_stroke(point_list=[(1, 1), (3, 4), (2, 1)])
g2.normalize()
print gdb.find(g2).name # will print "triangle"
示例5: GestureBoard
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
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()
示例6: GestureBox
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
#.........这里部分代码省略.........
return self._get_uid() in touch.ud
if touch.grab_current is not self:
return True
ud = touch.ud[self._get_uid()]
if ud['mode'] == 'unknown':
dx = abs(touch.ox - touch.x)
dy = abs(touch.oy - touch.y)
distance = sqrt(dx * dx + dy * dy)
# If we've moved more than the thershold distance inside the
# threshold time, treat as a gesture
if distance > self.gesture_distance:
Clock.unschedule(self._change_touch_mode)
ud['mode'] = 'gesture'
# Regardless of whether this is a known gesture or not, collect
# the motion point in case it becomes one.
try:
touch.ud['gesture_line'].points += [touch.x, touch.y]
except KeyError:
pass
return True
def on_touch_up(self, touch):
'''(internal) When the touch up occurs, we have to decide if a gesture
was attempted. If so, we fire the on_gesture event. In all other cases
we propogate the change to child widget.'''
if self._get_uid('cavoid') in touch.ud:
return
if self in [x() for x in touch.grab_list]:
touch.ungrab(self)
self._touch = None
ud = touch.ud[self._get_uid()]
if ud['mode'] == 'unknown':
Clock.unschedule(self._change_touch_mode)
super(GestureBox, self).on_touch_down(touch)
Clock.schedule_once(partial(self._do_touch_up, touch), .1)
else:
# A gesture was attempted. Did it match?
gesture = Gesture()
gesture.add_stroke(
zip(touch.ud['gesture_line'].points[::2],
touch.ud['gesture_line'].points[1::2]))
gesture.normalize()
match = self.gestures.find(gesture, minscore=0.70)
if match:
self.dispatch('on_gesture', match[1].name)
else:
# The gesture wasn't recognized; invoke a normal reaction
super(GestureBox, self).on_touch_down(touch)
Clock.schedule_once(partial(self._do_touch_up, touch), .1)
return True
else:
if self._touch is not touch and self.uid not in touch.ud:
super(GestureBox, self).on_touch_up(touch)
return self._get_uid() in touch.ud
def _do_touch_up(self, touch, *largs):
'''(internal) Simulate touch up events for anything that has grabbed the touch'''
super(GestureBox, self).on_touch_up(touch)
# don't forget about grab event!
for x in touch.grab_list[:]:
touch.grab_list.remove(x)
x = x()
if not x:
continue
touch.grab_current = x
super(GestureBox, self).on_touch_up(touch)
touch.grab_current = None
return True
def _change_touch_mode(self, *largs):
'''(internal) Simulate a touch down if we know the touch did not become
a gesture'''
if not self._touch:
return
uid = self._get_uid()
touch = self._touch
ud = touch.ud[uid]
if ud['mode'] == 'unknown':
touch.ungrab(self)
self._touch = None
touch.push()
touch.apply_transform_2d(self.to_widget)
touch.apply_transform_2d(self.to_parent)
super(GestureBox, self).on_touch_down(touch)
touch.pop()
return
def _get_uid(self, prefix='sv'):
return '{0}.{1}'.format(prefix, self.uid)
def on_gesture(self, gesture_name):
'''Called whenever a gesture has occured. This is a Kivy event. It
can be overridden in a subclass our bound to using :meth:`Widget.bind`.
'''
pass
示例7: AppView
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
class AppView(Scatter):
app = ObjectProperty(None)
texture_sidebar = ObjectProperty(None)
texture = ObjectProperty(None)
bar_width = NumericProperty(135)
bar_translation_min_distance = NumericProperty(250)
content = ObjectProperty(None)
def __init__(self, **kwargs):
super(AppView, self).__init__(**kwargs)
#internal variables
self.touches2 = {}
self.last_touch_id = -1
#self.translation_allowed = False
self.gesture_found = False
self.position_left = True
self.anim = False #currently animated
self.set_texture('style/bar/slider-fond.png')
#add gestures
self.gdb = GestureDatabase()
self.create_gesture( [(0,0),(10,0)] )
self.create_gesture( [(0,0),(-10,0)] )
'''
def add_widget(self, child):
if self.content:
print child
return self.content.add_widget(child)
return super(AppView, self).add_widget(child)
'''
def set_texture(self,path):
#from kivy.core.image import Image
tex = Image(path).texture
tex.wrap = 'repeat'
self.texture_sidebar = tex
#tex = Image('style/1.png').texture
if tex is not None:
tex.wrap = 'repeat'
self.texture = tex
def create_gesture(self,point_list):
# Create a gesture
g = Gesture()
g.add_stroke(point_list)
g.normalize()
# Add it to database
self.gdb.add_gesture(g)
def on_touch_down(self,touch):
id = touch.id
if touch.x < self.x + self.bar_width :
self.touches2[id] = touch.pos
self.last_touch_id = id
#if len(self._touches) <= 1 :
# return True
l = len(self.touches2)
if l > 1 :
#if several touches on the bar
self.set_texture('style/bar/slider-fond.png')
return super(AppView, self).on_touch_down(touch)
def on_touch_up(self,touch):
id = touch.id
if id in self.touches2.keys() and len(self.touches2) == 2:
#still one more touch on bar
#does the user want to translate the bar to the right or the left ?
origin = self.touches2[id]#.pos
current = touch.pos
dist = Vector(origin).distance( Vector(current) )
#print len(self.touches2), self.touches2[id], touch.pos, dist
#if touch.id in self.touches2.keys() :
if dist >= self.bar_translation_min_distance :
# try to find a gesture
g = Gesture()
g.add_stroke(point_list=[origin,current])
g.normalize()
gest = self.gdb.find(g)
try :
if gest[0] > 0.95 : #gesture found
if len(self.touches2) == 2: #no touch left on bar
d = current[0] - origin[0]
if d > 0:
self.move_bar_to_right()
else :
self.move_bar_to_left()
except :
self.move_back()
else : self.move_back()
if id in self.touches2.keys():
del self.touches2[id]
if len(self.touches2) <= 0 :
self.set_texture('style/bar/slider-fond.png')
return super(AppView, self).on_touch_up(touch)
#.........这里部分代码省略.........
示例8: GestureBoard
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
class GestureBoard(FloatLayout):
ponts = 0
def __init__(self, *args, **kwargs):
super(GestureBoard, self).__init__()
self.gdb = GestureDatabase()
self.error_sound = SoundLoader.load('error.mp3')
self.success_sound = SoundLoader.load('success.mp3')
#Gestures
for key, ges in gestures.items():
self.gdb.add_gesture(ges)
self.sort_letter()
def sort_letter(self):
self.letter = random.choice(gestures.keys())
letter_button = Button(
text=self.letter,
size_hint=(.1, .1),
pos_hint={'x':0, 'y':.9},
font_size=38,
line_height=1
)
self.add_widget(letter_button)
ponts_button = Button(
text=u"Pontos: %s" % self.ponts,
size_hint=(.3, .1),
pos_hint={'x':.7, 'y':.9}
)
self.add_widget(ponts_button)
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 = self.simplegesture(
'',
list(zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2]))
)
print("gesture representation:", self.gdb.gesture_to_str(g))
# use database to find the more alike gesture, if any
g2 = self.gdb.find(g, minscore=0.70)
if g2:
if self._get_key_by_gesture(g2[1]) == self.letter:
self.ponts += 10
if self.success_sound.status != 'stop':
self.success_sound.stop()
self.success_sound.play()
else:
if self.error_sound.status != 'stop':
self.error_sound.stop()
self.error_sound.play()
# 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()
self.sort_letter()
def _get_key_by_gesture(self, g):
for key, ges in gestures.items():
if g == ges: return key
def simplegesture(self, name, point_list):
"""
A simple helper function
"""
g = Gesture()
g.add_stroke(point_list)
g.normalize()
g.name = name
return g
示例9: GameMatrix
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
#.........这里部分代码省略.........
touch.ud["line"].points += [touch.x, touch.y]
self.box_size = self.get(0,0).size[0]
self.dpos = (touch.dpos[0]+self.dpos[0], touch.dpos[1]+self.dpos[1])
if abs(self.dpos[0]) >= self.box_size:
next_pos = deepcopy(self.shape[self.orientation])
for box in next_pos:
box[0] += int(round((0.5*self.dpos[0])/self.box_size,0))
minmax = self.getMinMax(next_pos)
if minmax['x'][0] < 0 or minmax['x'][1] > 11:
pass
elif len(self.colored.intersection(set(tuple(y) for y in next_pos))) != 0:
pass
else:
for box in self.shape[self.orientation]:
self.drop(box, anim=1)
for i in self.shape:
for box in i:
box[0] += int(round((0.5*self.dpos[0])/self.box_size,0))
for box in self.shape[self.orientation]:
self.mark(box, anim=1)
self.move = True
self.mLR = True
self.dpos = (0, self.dpos[1])
if -1*(self.dpos[1]) >= 1.05*self.box_size:
for i in range(abs(int(round(self.dpos[1]/self.box_size,0)*1))):
self._fall()
self.move = True
self.dpos = (self.dpos[0], 0)
def on_touch_up(self, touch):
# Uživatel zvedl prst z obrazovky
if touch.grab_current is self:
g = self.simplegesture('', zip(touch.ud['line'].points[::2], touch.ud['line'].points[1::2]))
g2 = self.gdb.find(g, minscore=0.80)
if g2 and (self.mLR == False) and (self.blockRotation == False):
if g2[1] == self.GSTR_DROP:
print "< GESTURE: Drop >"
for i in range(21-self.shape[self.orientation][-1][1]):
self._fall(animate=2)
self.mLR = False
else:
if (self.mLR == False) and (self.move == False) and (self.blockRotation == False):
if touch.x <= self.parent.size[0]/2.:
self.rotate(False) #left
else:
self.rotate(True) #right
self.mLR = False
if self.move == True:
self.move = False
touch.ungrab(self)
def dropAnim(self, fall):
# Animace padání bloku
if fall:
d = .1
else:
d= .2
boxes = []
for box in self.shape[self.orientation]:
if ((box[0] >= 0) and (box[0] <= 11)) and ((box[1] >= 0) and (box[1] <= 21)):
cur = self.get(box[0], box[1])
a = Box()
a.size = cur.size
a.pos = cur.pos
a.colored = cur.colored
a.size_hint = (None, None)
示例10: DrawingCanvas
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
class DrawingCanvas(StencilView):
def __init__(self, *args, **kwargs):
super(DrawingCanvas, self).__init__()
self.gdb = GestureDatabase()
self.line45 = self.gdb.str_to_gesture(line45_str)
self.circle = self.gdb.str_to_gesture(circle_str)
self.cross = self.gdb.str_to_gesture(cross_str)
self.line135 = self.line45.rotate(90)
self.line225 = self.line45.rotate(180)
self.line315 = self.line45.rotate(270)
self.gdb.add_gesture(self.line45)
self.gdb.add_gesture(self.line135)
self.gdb.add_gesture(self.line225)
self.gdb.add_gesture(self.line315)
self.gdb.add_gesture(self.circle)
self.gdb.add_gesture(self.cross)
self.links = []
def addLink(self,link):
self.links.append(link)
def removeLink(self,link):
self.remove_widget(link.widgetLink)
self.links.remove(link)
def getLink(self,element1, element2):
for child in self.links:
if ( child.element1 == element1 and child.element2 == element2 ) or ( child.element1 == element2 and child.element2 == element1 ):
return child
return None
def getLinksForOneElement(self,element):
tempLinks = []
for child in self.links:
if ( child.element1 == element or child.element2 == element ):
tempLinks.append(child)
return tempLinks
def activate(self):
self.bind(on_touch_down=self.down,
on_touch_move=self.move,
on_touch_up=self.up)
def deactivate(self):
self.unbind(on_touch_down=self.down,
on_touch_move=self.move,
on_touch_up=self.up)
def down(self, ds, touch):
if self.collide_point(*touch.pos):
self.points = [touch.pos]
self.ix = self.fx = touch.x
self.iy = self.fy = touch.y
return True
def move(self, ds, touch):
if self.collide_point(*touch.pos):
self.points += [touch.pos]
self.min_and_max(touch.x, touch.y)
return True
def up(self, ds, touch):
if self.collide_point(*touch.pos):
self.points += [touch.pos]
self.min_and_max(touch.x, touch.y)
gesture = self.gesturize()
recognized = self.gdb.find(gesture, minscore=0.50)
if recognized:
self.discriminate(recognized)
return True
def gesturize(self):
gesture = Gesture()
gesture.add_stroke(self.points)
gesture.normalize()
return gesture
def min_and_max(self, x, y):
self.ix = min(self.ix, x)
self.iy = min(self.iy, y)
self.fx = max(self.fx, x)
self.fy = max(self.fy, y)
def discriminate(self, recognized):
if recognized[1] == self.cross:
self.add_stickman()
if recognized[1] == self.circle:
self.add_circle()
if recognized[1] == self.line45:
self.add_line(self.ix,self.iy,self.fx,self.fy)
if recognized[1] == self.line135:
self.add_line(self.ix,self.fy,self.fx,self.iy)
if recognized[1] == self.line225:
self.add_line(self.fx,self.fy,self.ix,self.iy)
if recognized[1] == self.line315:
self.add_line(self.fx,self.iy,self.ix,self.fy)
def add_circle(self):
cx = (self.ix + self.fx)/2.0
cy = (self.iy + self.fy)/2.0
self.tool_box.tool_circle.widgetize(self, cx, cy, self.fx, self.fy)
def add_line(self,ix,iy,fx,fy):
self.tool_box.tool_line.widgetize(self,ix,iy,fx,fy)
#.........这里部分代码省略.........
示例11: DrawingSpace
# 需要导入模块: from kivy.gesture import GestureDatabase [as 别名]
# 或者: from kivy.gesture.GestureDatabase import find [as 别名]
class DrawingSpace(StencilView):
def __init__(self, *args, **kwargs):
super(DrawingSpace, self).__init__()
self.gdb = GestureDatabase()
self.line45 = self.gdb.str_to_gesture(line45_str)
self.circle = self.gdb.str_to_gesture(circle_str)
self.cross = self.gdb.str_to_gesture(cross_str)
self.line135 = self.line45.rotate(90)
self.line225 = self.line45.rotate(180)
self.line315 = self.line45.rotate(270)
self.gdb.add_gesture(self.line45)
self.gdb.add_gesture(self.line135)
self.gdb.add_gesture(self.line225)
self.gdb.add_gesture(self.line315)
self.gdb.add_gesture(self.circle)
self.gdb.add_gesture(self.cross)
def activate(self):
self.bind(on_touch_down=self.down, on_touch_move=self.move, on_touch_up=self.up)
def deactivate(self):
self.unbind(on_touch_down=self.down, on_touch_move=self.move, on_touch_up=self.up)
def down(self, ds, touch):
if self.collide_point(*touch.pos):
self.points = [touch.pos]
self.ix = self.fx = touch.x
self.iy = self.fy = touch.y
return True
def move(self, ds, touch):
if self.collide_point(*touch.pos):
self.points += [touch.pos]
self.min_and_max(touch.x, touch.y)
return True
def up(self, ds, touch):
if self.collide_point(*touch.pos):
self.points += [touch.pos]
self.min_and_max(touch.x, touch.y)
gesture = self.gesturize()
recognized = self.gdb.find(gesture, minscore=0.50)
if recognized:
self.discriminate(recognized)
return True
def gesturize(self):
gesture = Gesture()
gesture.add_stroke(self.points)
gesture.normalize()
return gesture
def min_and_max(self, x, y):
self.ix = min(self.ix, x)
self.iy = min(self.iy, y)
self.fx = max(self.fx, x)
self.fy = max(self.fy, y)
def discriminate(self, recognized):
if recognized[1] == self.cross:
self.add_stickman()
if recognized[1] == self.circle:
self.add_circle()
if recognized[1] == self.line45:
self.add_line(self.ix, self.iy, self.fx, self.fy)
if recognized[1] == self.line135:
self.add_line(self.ix, self.fy, self.fx, self.iy)
if recognized[1] == self.line225:
self.add_line(self.fx, self.fy, self.ix, self.iy)
if recognized[1] == self.line315:
self.add_line(self.fx, self.iy, self.ix, self.fy)
def add_circle(self):
cx = (self.ix + self.fx) / 2.0
cy = (self.iy + self.fy) / 2.0
self.tool_box.tool_circle.widgetize(self, cx, cy, self.fx, self.fy)
def add_line(self, ix, iy, fx, fy):
self.tool_box.tool_line.widgetize(self, ix, iy, fx, fy)
def add_stickman(self):
cx = (self.ix + self.fx) / 2.0
cy = (self.iy + self.fy) / 2.0
self.tool_box.tool_stickman.draw(self, cx, cy)
def on_children(self, instance, value):
self.status_bar.counter = len(self.children)