-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_button.py
100 lines (76 loc) · 2.95 KB
/
custom_button.py
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
97
98
99
100
__all__ = ('AnimatedButton')
from kivy.factory import Factory
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.graphics import *
from kivy.properties import StringProperty, OptionProperty,\
ObjectProperty, BooleanProperty
class AnimatedButton(Label):
state = OptionProperty('normal', options=('normal', 'down'))
allow_stretch = BooleanProperty(True)
keep_ratio = BooleanProperty(False)
border = ObjectProperty(None)
anim_delay = ObjectProperty(None)
background_normal = StringProperty('data/images/button.png')
texture_background = ObjectProperty(None)
background_down = StringProperty('data/images/button_pressed.png')
def __init__(self, **kwargs):
super(AnimatedButton, self).__init__(**kwargs)
self.register_event_type('on_press')
self.register_event_type('on_release')
#borderImage.border by default is ...
self.border = (16, 16, 16, 16)
#Image to display depending on state
self.img = Image(source = self.background_normal,
allow_stretch = self.allow_stretch,
keep_ratio = self.keep_ratio, mipmap = True)
#reset animation if anim_delay is changed
def anim_reset(*l):
self.img.anim_delay = self.anim_delay
self.bind(anim_delay = anim_reset)
self.anim_delay = .1
#update self.texture when image.texture changes
self.img.bind(texture = self.on_tex_changed)
self.on_tex_changed()
#update image source when background image is changed
def background_changed(*l):
self.img.source = self.background_normal
self.anim_delay = .1
self.bind(background_normal = background_changed)
def on_tex_changed(self, *largs):
self.texture_background = self.img.texture
def _do_press(self):
self.state = 'down'
def _do_release(self):
self.state = 'normal'
def on_touch_down(self, touch):
if not self.collide_point(touch.x, touch.y):
return False
if self in touch.ud:
return False
touch.grab(self)
touch.ud[self] = True
_animdelay = self.img.anim_delay
self.img.source = self.background_down
self.img.anim_delay = _animdelay
self._do_press()
self.dispatch('on_press')
return True
def on_touch_move(self, touch):
return self in touch.ud
def on_touch_up(self, touch):
if touch.grab_current is not self:
return
assert(self in touch.ud)
touch.ungrab(self)
_animdelay = self.img._coreimage.anim_delay
self.img.source = self.background_normal
self.anim_delay = _animdelay
self._do_release()
self.dispatch('on_release')
return True
def on_press(self):
pass
def on_release(self):
pass
Factory.register('AnimatedButton', cls=AnimatedButton)