-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuzz.py
executable file
·217 lines (164 loc) · 6.45 KB
/
buzz.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2014 Toms Baugis <[email protected]>
"""A quick stab."""
import math
import random
from gi.repository import Gtk as gtk
from lib import graphics
from lib import layout
from lib.pytweener import Easing
class Rattle(graphics.Sprite):
def __init__(self, **kwargs):
graphics.Sprite.__init__(self, **kwargs)
self.connect("on-render", self.on_render)
self.snap_to_pixel = False
self._intensity = 0
self.shake_range = 5
self.easing = Easing.Sine
self.duration = 1.3
self.fill = "#ddd"
def buzz(self, sprite=None):
graphics.chain([
[self, {"_intensity": self.shake_range,
"duration": self.duration,
"easing": self.easing.ease_in,
"on_update": self._update_buzz}],
[self, {"_intensity": 0,
"duration": self.duration,
"easing": self.easing.ease_out,
"on_update": self._update_buzz,
"on_complete": self.buzz}]
])
def _update_buzz(self, sprite):
self.update_buzz()
def rattle(self):
pass
def on_render(self, sprite):
self.graphics.rectangle(-25, -25, 50, 50)
self.graphics.fill(self.fill)
class RattleRandomXY(Rattle):
def update_buzz(self):
shake_range = self._intensity
self.x = (1 - 2 * random.random()) * shake_range
self.y = (1 - 2 * random.random()) * shake_range
class RattleRandomAngle(Rattle):
def update_buzz(self):
shake_range = self._intensity
angle = random.randint(0, 360)
self.x = math.cos(math.radians(angle)) * shake_range
self.y = math.sin(math.radians(angle)) * shake_range
class RattleRandomAngleShadow(RattleRandomAngle):
def __init__(self, **kwargs):
RattleRandomAngle.__init__(self, **kwargs)
self.prev_x, self.prev_y = 0, 0
def update_buzz(self):
self.prev_x, self.prev_y = self.x, self.y
RattleRandomAngle.update_buzz(self)
def on_render(self, sprite):
self.graphics.rectangle(-25 - self.prev_x + self.x, -25 - self.prev_y + self.y, 50, 50)
self.graphics.fill(self.fill, 0.3)
self.graphics.rectangle(-25, -25, 50, 50)
self.graphics.fill(self.fill)
class RattleRandomAngle2Shadow(RattleRandomAngleShadow):
def on_render(self, sprite):
self.graphics.rectangle(-25 + self.prev_x - self.x, -25 + self.prev_y - self.y, 50, 50)
self.graphics.fill(self.fill, 0.3)
self.graphics.rectangle(-25 - self.prev_x + self.x, -25 - self.prev_y + self.y, 50, 50)
self.graphics.fill(self.fill, 0.3)
self.graphics.rectangle(-25, -25, 50, 50)
self.graphics.fill(self.fill)
class RattleWithEase(graphics.Sprite):
"""
Want to go from prev random to next random using animation.
It will be super-short steps, the intensity updated on completion of
each step
"""
def __init__(self, **kwargs):
graphics.Sprite.__init__(self, **kwargs)
self.connect("on-render", self.on_render)
self.snap_to_pixel = False
self._intensity = 0
self.shake_range = 4
self.easing = Easing.Sine
self.step_duration = 0.01
self.duration = 0.75
self.fill = "#ddd"
self.prev_x, self.prev_y = 0, 0
self.next_x, self.next_y = 0, 0
self.current_step = 0
def buzz(self):
self.buzz_in()
def buzz_in(self, sprite=None):
self.current_step += 1 / (self.duration / self.step_duration)
if self.current_step > 1:
self.current_step = 1
self.buzz_out()
return
shake_range = self.easing.ease_in(self.current_step) * self.shake_range
angle = random.randint(0, 360)
self.next_x = (1 - 2 * random.random()) * shake_range
self.next_y = (1 - 2 * random.random()) * shake_range
self.animate(x=self.next_x, y=self.next_y,
duration=self.step_duration,
easing=Easing.Linear.ease_in,
on_update=self.on_render,
on_complete=self.buzz_in)
def buzz_out(self, sprite=None):
self.current_step -= 1 / (self.duration / self.step_duration)
if self.current_step < 0:
self.current_step = 0
self.buzz_in()
return
shake_range = self.easing.ease_in(self.current_step) * self.shake_range
angle = random.randint(0, 360)
self.next_x = (1 - 2 * random.random()) * shake_range
self.next_y = (1 - 2 * random.random()) * shake_range
self.animate(x=self.next_x, y=self.next_y,
duration=self.step_duration,
easing=Easing.Linear.ease_in,
on_update=self.on_render,
on_complete=self.buzz_out)
def on_render(self, sprite):
self.graphics.rectangle(-25 + self.prev_x - self.x, -25 + self.prev_y - self.y, 50, 50)
self.graphics.fill(self.fill, 0.3)
self.graphics.rectangle(-25 - self.prev_x + self.x, -25 - self.prev_y + self.y, 50, 50)
self.graphics.fill(self.fill, 0.3)
self.graphics.rectangle(-25, -25, 50, 50)
self.graphics.fill(self.fill)
self.prev_x, self.prev_y = self.x, self.y
class Scene(graphics.Scene):
def __init__(self):
graphics.Scene.__init__(self, background_color="#333")
self.rattles = [
RattleRandomXY(),
RattleRandomAngle(),
RattleRandomAngleShadow(),
RattleRandomAngle2Shadow(),
RattleWithEase(),
]
box = layout.HBox()
self.add_child(box)
for rattle in self.rattles:
container = layout.VBox(
rattle,
fill=False
)
box.add_child(container)
self.rattling = False
self.connect("on-first-frame", self.on_first_frame)
def on_first_frame(self, scene, context):
for rattle in self.rattles:
rattle.buzz()
class BasicWindow:
def __init__(self):
window = gtk.Window()
window.set_default_size(600, 500)
window.connect("delete_event", lambda *args: gtk.main_quit())
window.add(Scene())
window.show_all()
if __name__ == '__main__':
window = BasicWindow()
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL) # gtk3 screws up ctrl+c
gtk.main()