Skip to content

@tbaugis /drag node arcs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.