Skip to content

Instantly share code, notes, and snippets.

@me2beats
Last active August 12, 2019 15:42
Show Gist options
  • Save me2beats/df539de5f6694ccf86ab615c47898ec6 to your computer and use it in GitHub Desktop.
Save me2beats/df539de5f6694ccf86ab615c47898ec6 to your computer and use it in GitHub Desktop.
anim system first steps
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.properties import StringProperty, ObjectProperty
from kivy.animation import Animation
from functools import partial
KV = """
MyLabel
<MyLabel>
pos_hint: {'center': (0.5,0.5)}
size_hint: 0.5, 0.5
text: '123456'
anim: 'GrowAnim'
canvas.before:
Color:
rgba: 0.4,0.3,0.5,1
Rectangle:
pos: self.pos
size: self.size
"""
class AnimBase():
def __init__(self, **kwargs):
self.widget = kwargs.get('widget', False)
#if not self.widget: return
self.binds = {}
def set_anim(self):
widget = self.widget
self.prepare_anim()
self.set_properties()
self.set_binds()
def prepare_anim(self):
Animation.stop_all(self.widget)
def set_properties(self):
pass
def set_binds(self):
widget = self.widget
cls = self.__class__
for event_name, anim_props in self.binds.items():
widget.bind(**{event_name: partial( self.animate, widget, anim_props)} )
def animate(self, widget, anim_props, *_):
Animation(**anim_props).start(widget)
#TODO:
# +&
# create_properties (if don't exist)
# create_events (if don't exist)
# create_widgets (if don't exist)
# unbind?
class GrowAnim(AnimBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
#actually this is not grow anim, just an example
self.binds = {'on_touch_down': {'x':400, 't':'out_elastic', 'd':.4}}
def set_scale_y():
pass
def set_scale_x():
pass
class AnimBhv():
anim = StringProperty()
def on_anim(self, inst, anim_name):
anim_cls = globals()[anim_name]
anim_inst = anim_cls (widget = self)
anim_inst.set_anim()
def test(self, event_name, method):
self.bind(**{event_name: method})
class MyLabel(AnimBhv, Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class MyApp(App):
def build(self):
return Builder.load_string(KV)
MyApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment