Skip to content
Snippets Groups Projects
Commit b9c5b6e3 authored by Taddeüs Kroes's avatar Taddeüs Kroes
Browse files

First attempt to make cairo rectangle draggable.

parent 878291b4
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,14 @@ class Rectangle(mt.RectangularWidget): ...@@ -19,6 +19,14 @@ class Rectangle(mt.RectangularWidget):
self.scale = 1 self.scale = 1
self.color = color 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): def scale(self, angle):
self.scale *= scale self.scale *= scale
...@@ -27,7 +35,7 @@ class Rectangle(mt.RectangularWidget): ...@@ -27,7 +35,7 @@ class Rectangle(mt.RectangularWidget):
def get_transformation_matrix(self): def get_transformation_matrix(self):
t = cairo.Matrix() t = cairo.Matrix()
t.translate(*self.get_position()) t.translate(*self)
t.rotate(self.angle) t.rotate(self.angle)
t.scale(self.scale, self.scale) t.scale(self.scale, self.scale)
return t return t
...@@ -48,8 +56,6 @@ def create_context_window(w, h, callback): ...@@ -48,8 +56,6 @@ def create_context_window(w, h, callback):
def create_context(area, event): def create_context(area, event):
"""Add Cairo context to GTK window and draw initial state.""" """Add Cairo context to GTK window and draw initial state."""
global cr global cr
#if cr:
# return
cr = area.window.cairo_create() cr = area.window.cairo_create()
draw() draw()
...@@ -78,24 +84,26 @@ def create_context_window(w, h, callback): ...@@ -78,24 +84,26 @@ def create_context_window(w, h, callback):
root = mt.RectangularWidget(0, 0, w, h) root = mt.RectangularWidget(0, 0, w, h)
# GTK window # GTK window
win = gtk.Window() global window
win.set_title('Cairo test') window = gtk.Window()
win.connect('destroy', quit) window.set_title('Cairo test')
win.connect('key-press-event', handle_key) window.connect('destroy', quit)
win.connect('configure-event', move_window) window.connect('key-press-event', handle_key)
win.connect('show', callback) window.connect('configure-event', move_window)
window.connect('show', callback)
# Drawing area, needed by cairo context for drawing # Drawing area, needed by cairo context for drawing
area = gtk.DrawingArea() area = gtk.DrawingArea()
area.set_size_request(w, h) area.set_size_request(w, h)
area.connect('expose-event', create_context) area.connect('expose-event', create_context)
win.add(area) window.add(area)
area.show() area.show()
win.show() window.show()
def draw(): def draw(*args):
print 'draw'
# Background # Background
cr.rectangle(0, 0, *root.get_size()) cr.rectangle(0, 0, *root.get_size())
cr.set_source_rgb(0, 1, 0) cr.set_source_rgb(0, 1, 0)
...@@ -103,13 +111,17 @@ def draw(): ...@@ -103,13 +111,17 @@ def draw():
# Drawable objects (use save and restore to allow transformations) # Drawable objects (use save and restore to allow transformations)
for obj in draw_objects: for obj in draw_objects:
#cr.save() cr.save()
obj.draw(cr) obj.draw(cr)
#cr.restore() cr.restore()
def refresh():
window.queue_draw()
# Initialization # Initialization
cr = root = None window = cr = root = None
draw_objects = [] draw_objects = []
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment