mirror of https://github.com/kivy/kivy.git
button: add the button widget
This commit is contained in:
parent
c1f333ab88
commit
955d1758de
|
@ -306,6 +306,7 @@ class WindowBase(EventDispatcher):
|
|||
from kivy.core.gl import *
|
||||
from kivy.graphics import GraphicContext
|
||||
from kivy.lib.transformations import clip_matrix
|
||||
|
||||
context = GraphicContext.instance()
|
||||
context.set('projection_mat', clip_matrix(0, self.width, 0, self.height, -1, 1))
|
||||
|
||||
|
|
|
@ -63,6 +63,8 @@ class FactoryBase(object):
|
|||
|
||||
return cls
|
||||
|
||||
get = __getattr__
|
||||
|
||||
|
||||
#: Factory instance to use for getting new classes
|
||||
Factory = FactoryBase()
|
||||
|
|
|
@ -26,3 +26,4 @@ Factory.register('TouchFactory', module='kivy.input.factory')
|
|||
Factory.register('TouchShape', module='kivy.input.shape')
|
||||
Factory.register('TouchShapeRect', module='kivy.input.shape')
|
||||
Factory.register('Widget', module='kivy.uix.widget')
|
||||
Factory.register('Button', module='kivy.uix.button')
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
'''
|
||||
Button:
|
||||
'''
|
||||
|
||||
__all__ = ('Button', )
|
||||
|
||||
from kivy.uix.widget import Widget
|
||||
from kivy.c_ext.properties import OptionProperty
|
||||
|
||||
class Button(Widget):
|
||||
|
||||
state = OptionProperty(['normal', 'down'])
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(Button, self).__init__(**kwargs)
|
||||
self.register_event_type('on_press')
|
||||
self.register_event_type('on_release')
|
||||
|
||||
def on_touch_down(self, touch):
|
||||
if not self.collide_point(touch.x, touch.y):
|
||||
return False
|
||||
if self in touch.userdata:
|
||||
return False
|
||||
touch.grab(self)
|
||||
touch.userdata[self] = True
|
||||
self.state = 'down'
|
||||
self.dispatch('on_press')
|
||||
return True
|
||||
|
||||
def on_touch_move(self, touch):
|
||||
return self in touch.userdata
|
||||
|
||||
def on_touch_up(self, touch):
|
||||
if not self in touch.userdata:
|
||||
return False
|
||||
touch.ungrab(self)
|
||||
self.state = 'normal'
|
||||
self.dispatch('on_release')
|
||||
return True
|
||||
|
Loading…
Reference in New Issue