-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathripplebehavior.py
More file actions
169 lines (144 loc) · 6.66 KB
/
Copy pathripplebehavior.py
File metadata and controls
169 lines (144 loc) · 6.66 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
from kivy.properties import ListProperty, NumericProperty, StringProperty, \
BooleanProperty
from kivy.animation import Animation
from kivy.graphics import Color, Ellipse, StencilPush, StencilPop, \
StencilUse, StencilUnUse, Rectangle
class CommonRipple(object):
ripple_rad = NumericProperty()
ripple_rad_default = NumericProperty(1)
ripple_post = ListProperty()
ripple_color = ListProperty()
ripple_alpha = NumericProperty(.5)
ripple_scale = NumericProperty(None)
ripple_duration_in_fast = NumericProperty(.3)
# FIXME: These speeds should be calculated based on widget size in dp
ripple_duration_in_slow = NumericProperty(2)
ripple_duration_out = NumericProperty(.3)
ripple_func_in = StringProperty('out_quad')
ripple_func_out = StringProperty('out_quad')
doing_ripple = BooleanProperty(False)
finishing_ripple = BooleanProperty(False)
fading_out = BooleanProperty(False)
def on_touch_down(self, touch):
if touch.is_mouse_scrolling:
return False
if not self.collide_point(touch.x, touch.y):
return False
if not self.disabled:
if self.doing_ripple:
Animation.cancel_all(self, 'ripple_rad', 'ripple_color',
'rect_color')
self.anim_complete()
self.ripple_rad = self.ripple_rad_default
self.ripple_pos = (touch.x, touch.y)
if self.ripple_color != []:
pass
elif hasattr(self, 'theme_cls'):
self.ripple_color = self.theme_cls.ripple_color
else:
# If no theme, set Grey 300
self.ripple_color = [0.8784313725490196, 0.8784313725490196,
0.8784313725490196, self.ripple_alpha]
self.ripple_color[3] = self.ripple_alpha
self.lay_canvas_instructions()
self.finish_rad = max(self.width, self.height) * self.ripple_scale
self.start_ripple()
return super(CommonRipple, self).on_touch_down(touch)
def lay_canvas_instructions(self):
raise NotImplementedError
def on_touch_move(self, touch, *args):
if not self.collide_point(touch.x, touch.y):
if not self.finishing_ripple and self.doing_ripple:
self.finish_ripple()
return super(CommonRipple, self).on_touch_move(touch, *args)
def on_touch_up(self, touch):
if self.collide_point(touch.x, touch.y) and self.doing_ripple:
self.finish_ripple()
return super(CommonRipple, self).on_touch_up(touch)
def start_ripple(self):
if not self.doing_ripple:
anim = Animation(
ripple_rad=self.finish_rad,
t='linear',
duration=self.ripple_duration_in_slow)
anim.bind(on_complete=self.fade_out)
self.doing_ripple = True
anim.start(self)
def _set_ellipse(self, instance, value):
self.ellipse.size = (self.ripple_rad, self.ripple_rad)
# Adjust ellipse pos here
def _set_color(self, instance, value):
self.col_instruction.a = value[3]
def finish_ripple(self):
if self.doing_ripple and not self.finishing_ripple:
Animation.cancel_all(self, 'ripple_rad')
anim = Animation(ripple_rad=self.finish_rad,
t=self.ripple_func_in,
duration=self.ripple_duration_in_fast)
anim.bind(on_complete=self.fade_out)
self.finishing_ripple = True
anim.start(self)
def fade_out(self, *args):
rc = self.ripple_color
if not self.fading_out:
Animation.cancel_all(self, 'ripple_color')
anim = Animation(ripple_color=[rc[0], rc[1], rc[2], 0.],
t=self.ripple_func_out,
duration=self.ripple_duration_out)
anim.bind(on_complete=self.anim_complete)
self.fading_out = True
anim.start(self)
def anim_complete(self, *args):
self.doing_ripple = False
self.finishing_ripple = False
self.fading_out = False
self.canvas.after.clear()
class RectangularRippleBehavior(CommonRipple):
ripple_scale = NumericProperty(2.75)
def lay_canvas_instructions(self):
with self.canvas.after:
StencilPush()
Rectangle(pos=self.pos, size=self.size)
StencilUse()
self.col_instruction = Color(rgba=self.ripple_color)
self.ellipse = \
Ellipse(size=(self.ripple_rad, self.ripple_rad),
pos=(self.ripple_pos[0] - self.ripple_rad / 2.,
self.ripple_pos[1] - self.ripple_rad / 2.))
StencilUnUse()
Rectangle(pos=self.pos, size=self.size)
StencilPop()
self.bind(ripple_color=self._set_color,
ripple_rad=self._set_ellipse)
def _set_ellipse(self, instance, value):
super(RectangularRippleBehavior, self)._set_ellipse(instance, value)
self.ellipse.pos = (self.ripple_pos[0] - self.ripple_rad / 2.,
self.ripple_pos[1] - self.ripple_rad / 2.)
class CircularRippleBehavior(CommonRipple):
ripple_scale = NumericProperty(1)
def lay_canvas_instructions(self):
with self.canvas.after:
StencilPush()
self.stencil = Ellipse(size=(self.width * self.ripple_scale,
self.height * self.ripple_scale),
pos=(self.center_x - (
self.width * self.ripple_scale) / 2,
self.center_y - (
self.height * self.ripple_scale) / 2))
StencilUse()
self.col_instruction = Color(rgba=self.ripple_color)
self.ellipse = Ellipse(size=(self.ripple_rad, self.ripple_rad),
pos=(self.center_x - self.ripple_rad / 2.,
self.center_y - self.ripple_rad / 2.))
StencilUnUse()
Ellipse(pos=self.pos, size=self.size)
StencilPop()
self.bind(ripple_color=self._set_color,
ripple_rad=self._set_ellipse)
def _set_ellipse(self, instance, value):
super(CircularRippleBehavior, self)._set_ellipse(instance, value)
if self.ellipse.size[0] > self.width * .6 and not self.fading_out:
self.fade_out()
self.ellipse.pos = (self.center_x - self.ripple_rad / 2.,
self.center_y - self.ripple_rad / 2.)