From 146a5fcfd00e014494ef4dd8017c5d2e194ba466 Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Sun, 20 Apr 2014 14:12:26 +0100 Subject: [PATCH] Added a second effectwidget example --- examples/widgets/effectwidget2.py | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/widgets/effectwidget2.py diff --git a/examples/widgets/effectwidget2.py b/examples/widgets/effectwidget2.py new file mode 100644 index 000000000..f929251c2 --- /dev/null +++ b/examples/widgets/effectwidget2.py @@ -0,0 +1,52 @@ +''' +This is an example of creating your own effect by writing a glsl string. +''' + +from kivy.base import runTouchApp +from kivy.lang import Builder +from kivy.uix.effectwidget import EffectWidget, EffectBase + + +# The effect string is glsl code defining an effect function. +effect_string = ''' +vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords) +{ + // Note that time is a uniform variable that is automatically + // provided to all effects. + float red = color.x * abs(sin(time*2.0)); + float green = color.y; // No change + float blue = color.z * (1.0 - abs(sin(time*2.0))); + return vec4(red, green, blue, color.w); +} +''' + + +class NewEffect(EffectBase): + def __init__(self, *args, **kwargs): + super(NewEffect, self).__init__(*args, **kwargs) + self.glsl = effect_string + + +class DemoEffect(EffectWidget): + def __init__(self, *args, **kwargs): + self.effect_reference = NewEffect() + super(DemoEffect, self).__init__(*args, **kwargs) + + +widget = Builder.load_string(''' +DemoEffect: + effects: [self.effect_reference] if checkbox.active else [] + orientation: 'vertical' + Button: + text: 'Some text so you can see what happens.' + BoxLayout: + size_hint_y: None + height: dp(50) + Label: + text: 'Enable effect?' + CheckBox: + id: checkbox + active: True +''') + +runTouchApp(widget)