d6d67fe67dd5d6313ff0956513b56a9e4c8f9468
[experiments/cairo-gtk.git] / cairo-gtk-animation.py
1 #!/usr/bin/env python
2
3 # An example of an animation done with cairo and Gtk+
4 #
5 # Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 # References:
21 # http://wiki.laptop.org/go/PyGTK/Smooth_Animation_with_PyGTK
22
23 import gi
24
25 gi.require_version('Gtk', '3.0')
26 from gi.repository import Gtk, GObject, Gio
27
28 import math
29 import colorsys
30
31
32 class AnimatedCanvas(Gtk.DrawingArea):
33     def __init__(self,  width, height, *args, **kwargs):
34         super(AnimatedCanvas, self).__init__(*args, **kwargs)
35
36         self.width = width
37         self.height = height
38
39         self.x_offset = 2
40         self.y_offset = 3
41         self.radius = 10
42         self.border = 4
43
44         self.x = self.radius + self.border
45         self.y = self.radius + self.border
46
47         self.set_size_request(width, height)
48         self.connect('draw', self.do_draw_cb)
49         self.connect('configure-event', self.do_configure_event_cb)
50
51         fps = 30
52         GObject.timeout_add(1000 / fps, self.update)
53
54     def update(self):
55         if self.x - self.radius - self.border < 0 or \
56            self.x + self.radius + self.border > self.width:
57             self.x_offset *= -1
58
59         if self.y - self.radius - self.border < 0 or \
60            self.y + self.radius + self.border > self.height:
61             self.y_offset *= -1
62
63         self.x += self.x_offset
64         self.y += self.y_offset
65
66         self.queue_draw()
67         return True
68
69     def do_configure_event_cb(self, widget, event):
70         self.width = event.width
71         self.height = event.height
72
73     def do_draw_cb(self, widget, cr):
74
75         cr.rectangle(0, 0, self.width, self.height)
76         cr.set_source_rgb(1.0, 0.5, 0.5)
77         cr.fill()
78
79         cr.arc(self.x, self.y, self.radius, 0, 2 * math.pi)
80
81         cr.set_line_width(self.border)
82
83         _h = self.x / float(self.width)
84         _l = 0.5
85         _s = self.y / float(self.height)
86         _r, _g, _b = colorsys.hls_to_rgb(_h, _l, _s)
87         cr.set_source_rgb(_r, _g, _b)
88         cr.fill_preserve()
89
90         cr.set_source_rgb(0, 0, 0)
91         cr.stroke()
92
93
94 class AnimatedApp(Gtk.Application):
95     def __init__(self, title, width, height, *args, **kwargs):
96         super(AnimatedApp, self).__init__(*args, **kwargs)
97         self.connect("activate", self.on_activate)
98         self.title = title
99         self.width = width
100         self.height = height
101
102     def on_activate(self, data=None):
103         window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
104         window.set_title(self.title)
105
106         canvas = AnimatedCanvas(self.width, self.height)
107         window.add(canvas)
108         window.show_all()
109         self.add_window(window)
110
111
112 if __name__ == "__main__":
113     app = AnimatedApp(
114         "AnimatedApp", 640, 480,
115         application_id="apps.test.animatedapp",
116         flags=Gio.ApplicationFlags.FLAGS_NONE)
117     app.run(None)