Пример #1
0
class PixelPerfectCollisionBehavior(object):
    collision_source = StringProperty(None, allownone=True)
    '''The collision source is the file to load the collision
    mask from. The collision mask should be a png or other image
    file that supports the alpha layer. Any transparent pixels
    will count as no collision. Any opaque pixels will trigger
    a collision. The mask is scaled to fit the widget.
        '''

    __collision_mask = ObjectProperty(None, allownone=True)
    '''The collision mask is used to detect the collision point against.
    If the collision mask is None then no collision will be possible.
        '''
    def __init__(self, **kwargs):
        super(PixelPerfectCollisionBehavior, self).__init__(**kwargs)

    def on_collision_source(self, instance, value):
        if value is None:
            return
        try:
            self.__collision_mask = CoreImage(value, keep_data=True)
        except:
            self.__collision_mask = None

    def collide_point(self, x, y):
        if self.__collision_mask is None:
            # print("No collision mask")
            return False
        if not super().collide_point(x, y):
            # print("Not inside the widget")
            return False
        try:
            # print(x, y)
            # print(self.x, self.y, self.right, self.top)
            wscale = (self.__collision_mask.width / self.width)
            hscale = (self.__collision_mask.height / self.height)
            # print(wscale, hscale)
            # print(x, self.x)
            # print(y, self.y)
            # print((x - self.x) * wscale, (self.height - (y - self.y)) * hscale)
            # print(self.__collision_mask.width, self.__collision_mask.height)
            color = self.__collision_mask.read_pixel(
                (x - self.x) * wscale, (self.height - (y - self.y)) * hscale)
            # print(color)
        except Exception as e:
            color = 0, 0, 0, 0
        if color[-1] > 0:
            print(self.x, self.y, 'collide')
            return True
Пример #2
0
    def __init__(self, **kwargs):
        super(ContentPopup, self).__init__(**kwargs)

        # Find theme and espace keywords
        themes = []
        espaces = []
        theme = ''
        espace = ''
        for group in self.app.db.keywords:

            if group['group'] == u'Thématique':
                for key in group['children']:
                    if key['id'] in self.item.keywords:
                        themes.append(key)
                        theme = key['name']
            elif group['group'] == u'Espace de la bibliothèque':
                for key in group['children']:
                    if key['id'] in self.item.keywords:
                        espaces.append(key)
                        espace = key['name']

        self.ids['theme'].text = theme.upper()
        self.ids['espace'].text = espace.upper()

        # Find main color in the image
        if self.item.filename is not None:
            cimg = CoreImage(self.item.filename, keep_data=True)
            r, g, b = 0, 0, 0
            count = 0
            for i in range(0, cimg.width, 5):
                for j in range(0, cimg.height, 5):
                    try:
                        color = cimg.read_pixel(i, j)
                        if len(color) > 2:
                            r += color[0]
                            g += color[1]
                            b += color[2]
                            count += 1

                    except IndexError as e:
                        print "Can't find color : ", e
                        r, g, b = 0
                        count = 1
            r = r / count
            g = g / count
            b = b / count
            self.color = (r, g, b, 1)
Пример #3
0
 def getPixelGreyValue(self, xy_pixels, imagePath):
     image = CoreImage(imagePath, keep_data = True)
     rgb = image.read_pixel(xy_pixels[0], xy_pixels[1])
     return (rgb[0]+rgb[1]+rgb[2])/3 #averaging method (see for more options: https://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/)
Пример #4
0
class Sheep(Scatter):
    image = ObjectProperty(None)
    canMove = BooleanProperty(False)
    idx_frame = NumericProperty(0.0)
    previous_position = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Sheep, self).__init__(**kwargs)
        self.image = CoreImage('images/maze.png', keep_data=True)
        self.previous_position = self.pos

    def on_touch_down(self, touch):
        if not self.canMove:
            return
        ret = super(Sheep, self).on_touch_down(touch)
        if not ret:
            return
        return True

    def on_touch_move(self, touch):
        if not self.canMove:
            return
        x, y = touch.pos
        if not self or not self.parent or not self.parent.maze:
            return
        mazeimg = self.parent.maze
        x -= mazeimg.x
        y -= mazeimg.y
        x = int(x)
        y = int(y)
        y = self.parent.maze.height - y
        try:
            color = self.image.read_pixel(x, y)
        except IndexError:
            return
        if color[-1] == 0:
            return
        if self.idx_frame == 10 :   

            if touch.x != self.previous_position[0]:
                angle = math.degrees(math.atan((self.previous_position[1]-touch.y)/(self.previous_position[0]-touch.x)))
                if self.previous_position[0] > touch.x :
                    angle += 180
               
            else:
                angle = 90 * math.copysign(1, self.rotation)
            self.rotation = angle 
            self.previous_position = touch.pos
            self.idx_frame=0
        else :
            self.idx_frame+=1
        #print angle

        # ret = super(Sheep, self).on_touch_move(touch)
        # return ret

        if abs(touch.x - self.center_x) < 50 and abs(touch.y - self.center_y) < 50:
            ret = super(Sheep, self).on_touch_move(touch)
            return ret
        return True

    def on_touch_up(self, touch):
        if not self.canMove:
            return
        if not touch.grab_current == self:
            return False
        ret = super(Sheep, self).on_touch_up(touch)
        return ret