Forked from Kovak/gist:a0d7d429e00a655b9187137ae91da471
Created
April 20, 2017 12:28
-
-
Save tejastank/4fbbbf21f181b6c012ab1c9fad45222a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TouchRippleBehavior(object): | |
ripple_rad = NumericProperty(10) | |
ripple_pos = ListProperty([0, 0]) | |
ripple_color = ListProperty((0., 0., 0., 1.)) | |
ripple_duration_in = NumericProperty(.4) | |
ripple_duration_out = NumericProperty(.4) | |
fade_to_alpha = NumericProperty(.12) | |
ripple_scale = NumericProperty(4.0) | |
ripple_func_in = StringProperty('in_cubic') | |
ripple_func_out = StringProperty('out_quad') | |
add_before = BooleanProperty(False) | |
add_main = BooleanProperty(True) | |
def on_touch_down(self, touch): | |
if self in touch.ud: | |
self.anim_complete(self, self) | |
self.ripple_pos = ripple_pos = (touch.x, touch.y) | |
Animation.cancel_all(self, 'ripple_rad', 'ripple_color') | |
rc = self.ripple_color | |
ripple_rad = self.ripple_rad | |
self.ripple_color = [rc[0], rc[1], rc[2], .16] | |
anim = Animation( | |
ripple_rad=max(self.width, self.height) * self.ripple_scale, | |
t=self.ripple_func_in, | |
ripple_color=[rc[0], rc[1], rc[2], self.fade_to_alpha], | |
duration=self.ripple_duration_in) | |
anim.start(self) | |
with self.canvas.after: | |
StencilPush() | |
if self.canvas.has_before and self.add_before: | |
for c in self.canvas.before.children[:]: | |
if isinstance(c, VertexInstruction): | |
self.canvas.after.add(c) | |
if self.add_main: | |
for c in self.canvas.children[:]: | |
if self.canvas.has_after: | |
if c is self.canvas.after: | |
continue | |
if isinstance(c, VertexInstruction): | |
self.canvas.after.add(c) | |
with self.canvas.after: | |
StencilUse(op='equal') | |
self.col_instruction = Color(rgba=self.ripple_color) | |
self.ellipse = Ellipse(size=(ripple_rad, ripple_rad), | |
pos=(ripple_pos[0] - ripple_rad/2., | |
ripple_pos[1] - ripple_rad/2.)) | |
StencilUnUse() | |
StencilPop() | |
self.bind(ripple_color=self.set_color, ripple_pos=self.set_ellipse, | |
ripple_rad=self.set_ellipse) | |
return super(TouchRippleBehavior, self).on_touch_down(touch) | |
def set_ellipse(self, instance, value): | |
ellipse = self.ellipse | |
ripple_pos = self.ripple_pos | |
ripple_rad = self.ripple_rad | |
ellipse.size = (ripple_rad, ripple_rad) | |
ellipse.pos = (ripple_pos[0] - ripple_rad/2., | |
ripple_pos[1] - ripple_rad/2.) | |
def set_color(self, instance, value): | |
self.col_instruction.rgba = value | |
def on_touch_up(self, touch): | |
if self in touch.ud: | |
rc = 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) | |
anim.start(self) | |
return super(TouchRippleBehavior, self).on_touch_up(touch) | |
def anim_complete(self, anim, instance): | |
self.ripple_rad = 10 | |
self.canvas.after.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment