the Ball class and adding the p attr that is an instance of Point.
Updating BallManager.save_to_file, Gzv.draw_ball to fit with the above
change.
The feature "Move ball" was implemented and to do so, the event
motion-notify-event of the Gzv.treeview was binded to a new
callback (Gzv.ball_motion), Gzv.button_pressed now discovers if
the click was inside an already existent ball and were added
two new attributes to Gzv class: last_y and move_ball.
+# -*- coding: utf-8; -*-
# gzv.py - an user interface to generate-zooming-video
#
# Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
# gzv.py - an user interface to generate-zooming-video
#
# Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
+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
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
self.radius = int(r)
self.name = name
def save_to_file(self, path):
target = open(path, 'w')
for i in self:
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):
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('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.
# making it possible to grab motion events when the mouse is
# over the widget.
self.setup_treeview()
self.new_ball = False
self.setup_treeview()
self.new_ball = False
# drawing stuff
self.start_x = -1
self.start_y = -1
self.last_x = -1
# drawing stuff
self.start_x = -1
self.start_y = -1
self.last_x = -1
self.radius = Ball.DEFAULT_WIDTH
def show(self):
self.radius = Ball.DEFAULT_WIDTH
def show(self):
if self.start_x < 0:
return False
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()
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.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
ctx.stroke()
def button_press(self, widget, event):
self.new_ball = True
+ self.last_x = event.x
+ self.last_y = event.y
+
+ 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.start_x = event.x
self.start_y = event.y
def button_release(self, widget, event):
def button_release(self, widget, event):
if event.button == 1:
self.finish_drawing()
if event.button == 1:
self.finish_drawing()
+ 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):
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
# reseting to the default coordenades
self.start_x = -1