forked from tstriker/hamster-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storing_input.py
executable file
·74 lines (53 loc) · 2.03 KB
/
storing_input.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
#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2010 Toms Bauģis <toms.baugis at gmail.com>
"""
Move the mouse across the screen to change the position of the rectangles.
The positions of the mouse are recorded into a list and played back every frame.
Between each frame, the newest value are added to the start of the list.
Ported from processing.js (http://processingjs.org/learning/basic/storinginput)
"""
import gtk
from lib import graphics
from lib.pytweener import Easing
import math
class Segment(object):
def __init__(self, x, y, color, width):
self.x = x
self.y = y
self.color = color
self.width = width
class Scene(graphics.Scene):
def __init__(self):
graphics.Scene.__init__(self)
self.segments = []
self.connect("on-mouse-move", self.on_mouse_move)
self.connect("on-enter-frame", self.on_enter_frame)
def on_mouse_move(self, widget, event):
x, y = event.x, event.y
segment = Segment(x, y, "#666666", 50)
self.tweener.add_tween(segment, easing = Easing.Cubic.ease_out, duration=1.5, width = 0)
self.segments.insert(0, segment)
def on_enter_frame(self, scene, context):
g = graphics.Graphics(context)
# on expose is called when we are ready to draw
for i, segment in reversed(list(enumerate(self.segments))):
if segment.width:
g.rectangle(segment.x - segment.width / 2.0,
segment.y - segment.width / 2.0,
segment.width,
segment.width, 3)
g.fill(segment.color, 0.5)
else:
del self.segments[i]
self.redraw()
class BasicWindow:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(600, 400)
window.connect("delete_event", lambda *args: gtk.main_quit())
window.add(Scene())
window.show_all()
if __name__ == "__main__":
example = BasicWindow()
gtk.main()