From fc1e11e4ca9572161ec6fac6372960777f069461 Mon Sep 17 00:00:00 2001 From: Lincoln de Sousa Date: Tue, 12 Aug 2008 08:00:34 -0300 Subject: [PATCH] Adding the class Point, removing the attributes x and y from 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. --- gzv.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/gzv.py b/gzv.py index 752c117..6c0d4dc 100644 --- a/gzv.py +++ b/gzv.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8; -*- # gzv.py - an user interface to generate-zooming-video # # Copyright (C) 2008 Lincoln de Sousa @@ -22,14 +23,22 @@ from ConfigParser import ConfigParser _ = 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 @@ -40,7 +49,7 @@ class BallManager(list): 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): @@ -138,6 +147,7 @@ class Gzv(GladeLoader): 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. @@ -162,11 +172,13 @@ class Gzv(GladeLoader): 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): @@ -326,33 +338,47 @@ class Gzv(GladeLoader): 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() @@ -370,12 +396,26 @@ class Gzv(GladeLoader): 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 -- 2.20.1