File: effectwidget3_advanced.py

package info (click to toggle)
kivy 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,316 kB
  • sloc: python: 80,678; ansic: 5,326; javascript: 780; objc: 725; lisp: 195; sh: 173; makefile: 150
file content (62 lines) | stat: -rw-r--r-- 1,699 bytes parent folder | download | duplicates (2)
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
'''
This example demonstrates creating and using an AdvancedEffectBase. In
this case, we use it to efficiently pass the touch coordinates into the shader.
'''

from kivy.base import runTouchApp
from kivy.properties import ListProperty
from kivy.lang import Builder
from kivy.uix.effectwidget import EffectWidget, AdvancedEffectBase


effect_string = '''
uniform vec2 touch;

vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
{
    vec2 distance = 0.025*(coords - touch);
    float dist_mag = (distance.x*distance.x + distance.y*distance.y);
    vec3 multiplier = vec3(abs(sin(dist_mag - time)));
    return vec4(multiplier * color.xyz, 1.0);
}
'''


class TouchEffect(AdvancedEffectBase):
    touch = ListProperty([0.0, 0.0])

    def __init__(self, *args, **kwargs):
        super(TouchEffect, self).__init__(*args, **kwargs)
        self.glsl = effect_string

        self.uniforms = {'touch': [0.0, 0.0]}

    def on_touch(self, *args, **kwargs):
        self.uniforms['touch'] = [float(i) for i in self.touch]


class TouchWidget(EffectWidget):
    def __init__(self, *args, **kwargs):
        super(TouchWidget, self).__init__(*args, **kwargs)
        self.effect = TouchEffect()
        self.effects = [self.effect]

    def on_touch_down(self, touch):
        super(TouchWidget, self).on_touch_down(touch)
        self.on_touch_move(touch)

    def on_touch_move(self, touch):
        self.effect.touch = touch.pos


root = Builder.load_string('''
TouchWidget:
    Button:
        text: 'Some text!'
    Image:
        source: 'data/logo/kivy-icon-512.png'
        fit_mode: "fill"
''')


runTouchApp(root)