|
|
@@ -19,6 +19,14 @@ class Rectangle(mt.RectangularWidget):
|
|
|
self.scale = 1
|
|
|
self.color = color
|
|
|
|
|
|
+ self.on_drag(self.move)
|
|
|
+
|
|
|
+ def move(self, g):
|
|
|
+ print 'drag rectangle', g.get_translation()
|
|
|
+ self.translate(*g.get_translation())
|
|
|
+ print self
|
|
|
+ refresh()
|
|
|
+
|
|
|
def scale(self, angle):
|
|
|
self.scale *= scale
|
|
|
|
|
|
@@ -27,7 +35,7 @@ class Rectangle(mt.RectangularWidget):
|
|
|
|
|
|
def get_transformation_matrix(self):
|
|
|
t = cairo.Matrix()
|
|
|
- t.translate(*self.get_position())
|
|
|
+ t.translate(*self)
|
|
|
t.rotate(self.angle)
|
|
|
t.scale(self.scale, self.scale)
|
|
|
return t
|
|
|
@@ -48,8 +56,6 @@ def create_context_window(w, h, callback):
|
|
|
def create_context(area, event):
|
|
|
"""Add Cairo context to GTK window and draw initial state."""
|
|
|
global cr
|
|
|
- #if cr:
|
|
|
- # return
|
|
|
cr = area.window.cairo_create()
|
|
|
draw()
|
|
|
|
|
|
@@ -78,24 +84,26 @@ def create_context_window(w, h, callback):
|
|
|
root = mt.RectangularWidget(0, 0, w, h)
|
|
|
|
|
|
# GTK window
|
|
|
- win = gtk.Window()
|
|
|
- win.set_title('Cairo test')
|
|
|
- win.connect('destroy', quit)
|
|
|
- win.connect('key-press-event', handle_key)
|
|
|
- win.connect('configure-event', move_window)
|
|
|
- win.connect('show', callback)
|
|
|
+ global window
|
|
|
+ window = gtk.Window()
|
|
|
+ window.set_title('Cairo test')
|
|
|
+ window.connect('destroy', quit)
|
|
|
+ window.connect('key-press-event', handle_key)
|
|
|
+ window.connect('configure-event', move_window)
|
|
|
+ window.connect('show', callback)
|
|
|
|
|
|
# Drawing area, needed by cairo context for drawing
|
|
|
area = gtk.DrawingArea()
|
|
|
area.set_size_request(w, h)
|
|
|
area.connect('expose-event', create_context)
|
|
|
|
|
|
- win.add(area)
|
|
|
+ window.add(area)
|
|
|
area.show()
|
|
|
- win.show()
|
|
|
+ window.show()
|
|
|
|
|
|
|
|
|
-def draw():
|
|
|
+def draw(*args):
|
|
|
+ print 'draw'
|
|
|
# Background
|
|
|
cr.rectangle(0, 0, *root.get_size())
|
|
|
cr.set_source_rgb(0, 1, 0)
|
|
|
@@ -103,13 +111,17 @@ def draw():
|
|
|
|
|
|
# Drawable objects (use save and restore to allow transformations)
|
|
|
for obj in draw_objects:
|
|
|
- #cr.save()
|
|
|
+ cr.save()
|
|
|
obj.draw(cr)
|
|
|
- #cr.restore()
|
|
|
+ cr.restore()
|
|
|
+
|
|
|
+
|
|
|
+def refresh():
|
|
|
+ window.queue_draw()
|
|
|
|
|
|
|
|
|
# Initialization
|
|
|
-cr = root = None
|
|
|
+window = cr = root = None
|
|
|
draw_objects = []
|
|
|
|
|
|
|