Last active
June 16, 2022 01:30
-
-
Save tshirtman/08dab32d4accf925ec9241288b6f696b to your computer and use it in GitHub Desktop.
This file contains 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
from kivy.clock import Clock | |
from kivy.app import App | |
from kivy.animation import Animation | |
from kivy.uix.widget import Widget | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.lang import Builder | |
from kivy.core.window import Window | |
from kivy.graphics import RenderContext | |
from kivy.uix.modalview import ModalView | |
from kivy.properties import ListProperty, NumericProperty, ObjectProperty, ColorProperty | |
from kivy.uix.effectwidget import EffectWidget, HorizontalBlurEffect, VerticalBlurEffect | |
shader = ''' | |
$HEADER$ | |
uniform vec2 resolution; | |
uniform float radius; | |
void main(void) { | |
// need radius to be an int to iterate on it in the loops | |
int int_radius = int(radius); | |
gl_FragColor = vec4(0.); | |
if (int_radius > 0) { | |
for (int dx = -int_radius; dx < int_radius; dx++) | |
for (int dy = -int_radius; dy < int_radius; dy++) | |
gl_FragColor += texture2D(texture0, tex_coord0 + vec2(float(dx), float(dy)) / resolution); | |
gl_FragColor /= float(4 * int_radius * int_radius); | |
} else { | |
gl_FragColor = texture2D(texture0, tex_coord0); | |
} | |
} | |
''' | |
kv = """ | |
#:import Factory kivy.factory.Factory | |
<BlurredModalView>: | |
size_hint: .5, .5 | |
background_color: "#22222288" | |
overlay_color: "#11111122" | |
canvas.before: | |
Color: | |
rgba: self.overlay_color | |
Rectangle: | |
size: app.root.size | |
Color: | |
rgba: self.background_color | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
Label: | |
text: "This is in the modal" | |
FloatLayout: | |
Blur: | |
id: blur | |
blur_radius: 0 | |
Image: | |
source: "image.jpg" | |
BoxLayout: | |
size_hint: None, None | |
pos_hint: {'center': (.5, .5)} | |
orientation: 'vertical' | |
size: self.minimum_size | |
Button: | |
size_hint: None, None | |
text: 'press me' | |
on_release: Factory.BlurredModalView(target=blur).open() | |
Label: | |
text: 'test' | |
size_hint: None, None | |
""" | |
class Blur(EffectWidget): | |
blur_radius = NumericProperty(0) | |
blur_target_radius = NumericProperty(5) | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
self._hblur = HorizontalBlurEffect(size=0) | |
self._vblur = VerticalBlurEffect(size=0) | |
self.effects = [ | |
self._hblur, | |
self._vblur, | |
] | |
def on_blur_radius(self, *args): | |
self._hblur.size = self._vblur = self.blur_radius | |
def blur(self): | |
Animation.cancel_all(self, "blur_radius") | |
Animation(blur_radius=self.blur_target_radius, d=1).start(self) | |
def unblur(self): | |
Animation.cancel_all(self, "blur_radius") | |
Animation(blur_radius=0, d=.1).start(self) | |
class BlurredModalView(ModalView): | |
background_color = ColorProperty([0, 0, 0, 0]) | |
overlay_color = ColorProperty([0, 0, 0, 0]) | |
target = ObjectProperty() | |
def on_open(self): | |
super().on_open() | |
if self.target: | |
self.target.blur() | |
def on_dismiss(self): | |
super().on_dismiss() | |
if self.target: | |
self.target.unblur() | |
class BluredApp(App): | |
def build(self): | |
return Builder.load_string(kv) | |
if __name__ == '__main__': | |
BluredApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment