From 453a5418c56429d224cfb8680e35efb3e4b5ff99 Mon Sep 17 00:00:00 2001 From: Lincoln de Sousa Date: Tue, 12 Aug 2008 17:36:32 -0300 Subject: [PATCH] fixing issues when the image does not fill the whole allocated area. Added methods: Gzv.ball_with_border, Gzv.point_without_border. Changed methods: Gzv.button_press, Gzv.draw_ball --- gzv.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/gzv.py b/gzv.py index ca94af9..6d591fc 100644 --- a/gzv.py +++ b/gzv.py @@ -344,16 +344,38 @@ class Gzv(GladeLoader): return False + def ball_width_border(self, ball): + iw, ih = self.draw.size_request() + w = self.draw.get_allocation().width + h = self.draw.get_allocation().height + + x = ((w / 2) - (iw / 2)) + ball.p.x + y = ((h / 2) - (ih / 2)) + ball.p.y + return Point(x, y) + + def point_without_border(self, point): + iw, ih = self.draw.size_request() + w = self.draw.get_allocation().width + h = self.draw.get_allocation().height + + x = point.x - ((w / 2) - (iw / 2)) + y = point.y - ((h / 2) - (ih / 2)) + return Point(x, y) + def draw_ball(self, ball): ctx = self.draw.window.cairo_create() - ctx.arc(ball.p.x, ball.p.y, ball.radius, 0, 64*math.pi) + ctx.arc(self.ball_width_border(ball).x, + self.ball_width_border(ball).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.p.x, ball.p.y, ball.radius+1, 0, 64*math.pi) + ctx.arc(self.ball_width_border(ball).x, + self.ball_width_border(ball).y, + ball.radius+1, 0, 64*math.pi) ctx.stroke() def button_press(self, widget, event): @@ -365,7 +387,7 @@ class Gzv(GladeLoader): if event.button == 1: for i in self.balls: p1 = Point(event.x, event.y) - p2 = Point(i.p.x, i.p.y) + p2 = self.ball_width_border(i) if Point.pythagorean(p1, p2) < i.radius: self.last_x = event.x - i.p.x self.last_y = event.y - i.p.y @@ -374,8 +396,8 @@ class Gzv(GladeLoader): self.move_ball = i break - self.start_x = event.x - self.start_y = event.y + self.start_x = self.point_without_border(event).x + self.start_y = self.point_without_border(event).y def button_release(self, widget, event): self.move_ball = None @@ -400,14 +422,11 @@ class Gzv(GladeLoader): 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.move_ball.p.x = self.point_without_border(event).x + self.move_ball.p.y = self.point_without_border(event).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): if self.new_ball: position = len(self.balls) -- 2.20.1