public
Last active

  • Download Gist
drag node arcs
Python
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
#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2010 Toms Bauń£is <toms.baugis at gmail.com>
 
"""an example how to connect points by dragging instead of clicking"""
 
 
import gtk
from lib import graphics
 
class Node(graphics.Sprite):
def __init__(self, x, y):
graphics.Sprite.__init__(self, x, y, interactive = True)
self.graphics.set_color("#999")
self.graphics.rectangle(-5,-5, 10, 10, 3)
self.graphics.fill()
 
class Scene(graphics.Scene):
def __init__(self):
graphics.Scene.__init__(self)
self.connect("on-click", self.on_click)
self.connect("on-mouse-down", self.on_mouse_down)
self.connect("on-mouse-up", self.on_mouse_up)
self.connect("on-enter-frame", self.on_enter_frame)
self.connections = []
 
self.arc_start = None
 
def on_click(self, scene, event, sprite):
if not sprite:
self.add_child(Node(event.x, event.y))
self.redraw()
 
def on_mouse_down(self, scene, event):
self.arc_start = self.arc_start or self.sprite_at_position(event.x, event.y)
 
def on_mouse_up(self, scene):
x, y, mods = self.get_pointer()
sprite = self.sprite_at_position(x, y)
if self.arc_start and self.arc_start != sprite:
self.connections.append((self.arc_start, sprite))
self.arc_start = None
 
self.redraw()
 
def on_enter_frame(self, scene, context):
g = graphics.Graphics(context)
for start, end in self.connections:
g.move_to(start.x, start.y)
g.line_to(end.x, end.y)
 
g.stroke("#000")
 
class BasicWindow:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(600, 500)
window.connect("delete_event", lambda *args: gtk.main_quit())
window.add(Scene())
window.show_all()
 
example = BasicWindow()
gtk.main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.