+# -*- coding: utf-8; -*-
# gzv.py - an user interface to generate-zooming-video
#
# Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
_ = lambda x:x
+class Point(object):
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ @staticmethod
+ def pythagorean(p1, p2):
+ return math.sqrt((p2.x - p1.x)**2 + (p2.y - p2.y)**2)
+
class Ball(object):
DEFAULT_WIDTH = 10
def __init__(self, x, y, r, name='', position=0, selected=False):
self.position = position
self.selected = selected
- self.x = int(x)
- self.y = int(y)
+ self.p = Point(int(x), int(y))
self.radius = int(r)
self.name = name
def save_to_file(self, path):
target = open(path, 'w')
for i in self:
- target.write('%d,%d %d %s\n' % (i.x, i.y, i.radius, i.name))
+ target.write('%d,%d %d %s\n' % (i.p.x, i.p.y, i.radius, i.name))
target.close()
class GladeLoader(object):
self.evtbox.connect('button-press-event', self.button_press)
self.evtbox.connect('button-release-event', self.button_release)
self.evtbox.connect('motion-notify-event', self.motion_notify)
+ self.evtbox.connect('motion-notify-event', self.ball_motion)
# making it possible to grab motion events when the mouse is
# over the widget.
self.setup_treeview()
self.new_ball = False
+ self.move_ball = None
# drawing stuff
self.start_x = -1
self.start_y = -1
self.last_x = -1
+ self.last_y = -1
self.radius = Ball.DEFAULT_WIDTH
def show(self):
if self.start_x < 0:
return False
- ball = Ball(self.start_x, self.start_y, self.radius)
- self.draw_ball(ball)
+ if self.new_ball:
+ ball = Ball(self.start_x, self.start_y, self.radius)
+ self.draw_ball(ball)
return False
def draw_ball(self, ball):
ctx = self.draw.window.cairo_create()
- ctx.arc(ball.x, ball.y, ball.radius, 0, 64*math.pi)
+ ctx.arc(ball.p.x, ball.p.y, ball.radius, 0, 64*math.pi)
ctx.set_source_rgba(0.0, 0.0, 0.5, 0.4)
ctx.fill()
if ball.selected:
ctx.set_source_rgba(0.0, 0.5, 0.0, 0.4)
ctx.set_line_width(5)
- ctx.arc(ball.x, ball.y, ball.radius+1, 0, 64*math.pi)
+ ctx.arc(ball.p.x, ball.p.y, ball.radius+1, 0, 64*math.pi)
ctx.stroke()
def button_press(self, widget, event):
self.new_ball = True
+ self.last_x = event.x
+ self.last_y = event.y
+
if event.button == 1:
+ for i in self.balls:
+ p1 = Point(event.x, event.y)
+ p2 = Point(i.p.x, i.p.y)
+ if Point.pythagorean(p1, p2) < i.radius:
+ self.last_x = event.x - i.p.x
+ self.last_y = event.y - i.p.y
+
+ self.new_ball = False
+ self.move_ball = i
+ break
+
self.start_x = event.x
self.start_y = event.y
- self.last_x = event.x
def button_release(self, widget, event):
- self.new_ball = False
+ self.move_ball = None
if event.button == 1:
self.finish_drawing()
self.last_x = event.x
+ def ball_motion(self, widget, event):
+ if not self.move_ball:
+ return
+
+ self.move_ball.p.x += (event.x - self.move_ball.p.x)
+ self.move_ball.p.y += (event.y - self.move_ball.p.y)
+
+ self.draw.queue_draw()
+
+ self.last_x = event.x - self.move_ball.p.x
+ self.last_y = event.y - self.move_ball.p.y
+
def finish_drawing(self):
- position = len(self.balls)
- ball = Ball(self.start_x, self.start_y, self.radius, '', position)
- self.balls.append(ball)
- self.model.append([position, ''])
- self.treeview.set_cursor(str(position), self.fpcolumn, True)
+ if self.new_ball:
+ position = len(self.balls)
+ ball = Ball(self.start_x, self.start_y, self.radius, '', position)
+ self.balls.append(ball)
+ self.model.append([position, ''])
+ self.treeview.set_cursor(str(position), self.fpcolumn, True)
+ self.new_ball = False
# reseting to the default coordenades
self.start_x = -1