/usr/share/doc/python-elements/examples/demo11a_cairo_images.py is in python-elements 0.13+svn20090823.230+dfsg-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | import sys
sys.path.insert(0, "..") # if Elements has yet to be installed
import elements
from elements.locals import *
import pygtk
pygtk.require('2.0')
import gtk
import cairo
import gobject
import time
# just found this, need to take a look:
# ---> http://cairographics.org/threaded_animation_with_cairo/
# ----> http://www.tortall.net/mu/wiki/PyGTKCairoTutorial
#class myScreen(gtk.DrawingArea):
# # Draw in response to an expose-event
# __gsignals__ = { "expose-event": "override" }
# is there a possible problem with the timers and threading?
# i'm not exactly sure how these GTK timers work. could require some looking into.
# but i guess i won't worry about that for now since it doesn't seem like it's crashing *cough*
class myWindow(object):
draw_poly = False
points = [] #
mousex = 0 #
mousey = 0 #
fps_all = [] # array of the "most recent fps" -- how long each draw call took
fps_count=50 # how many samples to hold
fps =0 # the actual approximate fps
def contact_add(self, c):
#print "Any Contact"
pass
def contact_add_ball(self, c):
print "Contact with Ball"
def contact_add_poly(self, c):
print "Polygon Contact"
def destroy(self, widget, data=None):
print "Quitting..."
gtk.main_quit()
def keydown(self, widget, event):
keystr = gtk.gdk.keyval_name(event.keyval)
if keystr == 'Escape':
gtk.main_quit()
#elif
#else:
# print gtk.gdk.keyval_name(event.keyval)
def keyup(self, widget, event):
pass
def mousemove(self, widget, event):
self.mousex, self.mousey = event.x, event.y
if self.draw_poly:
pos = (event.x, event.y)
self.points.append( pos )
def mousedown(self, widget, event):
pos = (event.x, event.y)
print pos
#return
if event.button == 3:
self.world.add.triangle(pos, sidelength=50)
elif event.button == 1:
# Start/Stop Wall-Drawing
if not self.draw_poly:
self.draw_poly = True
self.points = []
self.points.append(pos)
def mouseup(self, widget, event):
self.mousex, self.mousey = event.x, event.y
pos = (event.x, event.y)
if event.button == 1 and self.draw_poly:
# Create Polygon
self.draw_poly = False
self.points.append(pos)
if len(self.points) > 2:
body, self.points = self.world.add.complexPoly(self.points)
else:
self.world.add.rect(pos, width=80, height=30)
def __init__(self, size = (640, 480)):
print "Initializing the window..."
# Create a new window
self.window = win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.set_title("Demo 11 - Using GTK+/Cairo")
# Connect the destroy function with the destruction of the window
win.set_default_size(size[0], size[1])
# Add the drawing area to the window
da = gtk.DrawingArea()
win.add(da)
win.connect("destroy", self.destroy)
win.connect("key_press_event", self.keydown)
win.connect("key_release_event", self.keyup)
da.set_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK |
gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK )
da.connect("button_press_event", self.mousedown)
da.connect("button_release_event", self.mouseup)
win.connect("motion_notify_event", self.mousemove)
# show the window
win.show_all()
# Create the Physical Space Class
self.world = world = elements.Elements(screen_size=size, renderer="cairo")
#self.world.set_inputAxisOrigin(left=True, top=True)
self.world.renderer.set_drawing_area(da)
world.callbacks.add(CALLBACK_DRAWING_END, self.custom_draw)
b1 = world.add.ball((211, 101), 50)
b2 = world.add.ball((200, 100), 50)
# Add contact callbacks
world.callbacks.add(CALLBACK_CONTACT_ADD, self.contact_add)
world.callbacks.add(CALLBACK_CONTACT_ADD, self.contact_add_ball, [b1])
# Add the ground
world.add.ground()
gobject.timeout_add(1000/60, self.physics_update)
gobject.timeout_add(1000/60, self.draw_update)
self.world.renderer.set_circle_image("smiley.png")
# self.world.renderer.set_box_image("boxy.png")
def custom_draw(self):
r = self.world.renderer
r.draw_circle((255,0,0), (self.mousex, self.mousey), 1)
if self.points:
r.draw_lines((1,0,0), False, self.points)
for p in self.points:
r.draw_circle((0.1,0.1,0.1), p, 5)
r.draw_text("FPS: %d" % self.fps, center=(50,50))
def physics_update(self):
self.world.update()
return True
def draw_update(self):
start = time.clock()
ret = self.world.draw()
elapsed = time.clock() - start
if elapsed == 0: return ret
self.fps_all.append(1 / elapsed)
if len(self.fps_all) > self.fps_count:
del self.fps_all[0]
self.fps = 0
for i in self.fps_all:
self.fps += i
self.fps /= self.fps_count
return ret
def main(self):
gtk.main()
def create_test_images(filenames):
pi = 3.14159
w, h = 100, 100
r = w/2
sf = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
# svg writing:
#sf = cairo.SVGSurface(file(filenames[0] + '.svg', "w"), w, h)
c = cairo.Context(sf)
# big yellow circle
c.set_source_rgb(1,1,0.25)
c.translate(w/2, h/2)
c.arc(0, 0, r, 0, 2 * pi)
c.fill()
# mouth
c.set_source_rgb(0,0,0)
c.arc_negative(0, 0, w/4, 11.5/8 * 2 * pi, 8.5/8 * 2 * pi)
c.stroke()
# eyes
c.arc(-w/8,-h/8, r/8, 0, 2 * pi)
c.fill()
c.arc( w/8,-h/8, r/8, 0, 2 * pi)
c.fill()
sf.write_to_png(filenames[0])
# svg writing:
#sf.finish()
print "Wrote smiley to", filenames[0]
w, h = 500, 100
border = w/5
sf = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
c = cairo.Context(sf)
# big yellow circle
c.set_source_rgb(0,0,1)
#c.translate(w/2, h/2)
c.rectangle(0, 0, w, h)
c.fill()
c.set_source_rgba(1,1,1,0.5)
c.rectangle(border, border, w-2*border, h-2*border)
c.stroke()
sf.write_to_png(filenames[1])
print "Wrote test image to", filenames[1]
# exit(0)
# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
create_test_images( ["smiley.png", "boxy.png"])
hello = myWindow()
hello.main()
|