diff options
Diffstat (limited to 'TurtleArt/sprites.py')
-rw-r--r-- | TurtleArt/sprites.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/TurtleArt/sprites.py b/TurtleArt/sprites.py index fdf0105..c400d88 100644 --- a/TurtleArt/sprites.py +++ b/TurtleArt/sprites.py @@ -83,6 +83,7 @@ import pango import pangocairo import cairo + class Sprites: ''' A class for the list of sprites and everything they share in common ''' @@ -130,12 +131,12 @@ class Sprites: if spr in self.list: self.list.remove(spr) - def find_sprite(self, pos): + def find_sprite(self, pos, region=False): ''' Search based on (x, y) position. Return the 'top/first' one. ''' list = self.list[:] list.reverse() for spr in list: - if spr.hit(pos): + if spr.hit(pos, readpixel=not region): return spr return None @@ -350,7 +351,7 @@ class Sprite: if len(self.labels) > 0: self.draw_label(cr) - def hit(self, pos): + def hit(self, pos, readpixel=False): ''' Is (x, y) on top of the sprite? ''' x, y = pos if x < self.rect.x: @@ -361,6 +362,12 @@ class Sprite: return False if y > self.rect.y + self.rect.height: return False + if readpixel: + r, g, b, a = self.get_pixel(pos) + if r == g == b == a == 0: + return False + if a == -1: + return False return self._sprites.find_in_list(self) def draw_label(self, cr): @@ -396,14 +403,14 @@ class Sprite: x = int(self.rect.x + self._margins[0] + (my_width - w) / 2) elif self._horiz_align[i] == 'left': x = int(self.rect.x + self._margins[0]) - else: # right + else: # right x = int(self.rect.x + self.rect.width - w - self._margins[2]) h = pl.get_size()[1] / pango.SCALE if self._vert_align[i] == "middle": y = int(self.rect.y + self._margins[1] + (my_height - h) / 2) elif self._vert_align[i] == "top": y = int(self.rect.y + self._margins[1]) - else: # bottom + else: # bottom y = int(self.rect.y + self.rect.height - h - self._margins[3]) cr.save() cr.translate(x, y) @@ -448,16 +455,13 @@ class Sprite: if x < 0 or x > (self.rect.width - 1) or \ y < 0 or y > (self.rect.height - 1): return(-1, -1, -1, -1) - - # create a new 1x1 cairo surface - cs = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1); + # Create a new 1x1 cairo surface. + cs = cairo.ImageSurface(cairo.FORMAT_RGB24, 1, 1) cr = cairo.Context(cs) cr.set_source_surface(self.cached_surfaces[i], -x, -y) - cr.rectangle(0,0,1,1) + cr.rectangle(0, 0, 1, 1) cr.set_operator(cairo.OPERATOR_SOURCE) cr.fill() - cs.flush() # ensure all writing is done - # Read the pixel - pixels = cs.get_data() + cs.flush() # Ensure all the writing is done. + pixels = cs.get_data() # Read the pixel. return (ord(pixels[2]), ord(pixels[1]), ord(pixels[0]), 0) - |