Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kivy: How to make widget behave like overflow:hidden

Tags:

python

kivy

I want to create GridLayout in Kivy with many rectangle buttons with some custom images with different size. To do so, I want to scale image (the way I wan to do this is shown below) and then crop/hide the parts that overflow the widget borders. The resizing works nice, but I can not figure out how to hide/crop that parts which are outside the rectangle. For example: if widget have dimensions of 10px x 10px and my image is 100px x 200px I will rezise it to 10px x 20px and center it in widget, but the 5px below and abowe will be visible. I do not want that. :) Cloud somebody help me with this problem?

class PlaceIcon(Widget):
    def __init__(self,image_path, **kwargs):
        super(PlaceIcon, self).__init__(**kwargs)
        self.bind(size=self.adjust_size)
        self.image = Image(source=image_path)
        self.image_path = image_path  

    def adjust_size(self,*args):
        (a,b) = self.image.texture.size
        (x,y) = self.size
        (x1,y1) = self.pos
        if x > y:
            scale = x/a
        else:
            scale = y/b
        x1 -= (scale*a-x)/2
        y1 -= (scale*b-y)/2
        with self.canvas:
            self.canvas.clear()
            self.background = Rectangle(texture=self.image.texture, pos=(x1,y1), size=(scale*a,scale*b))
like image 831
T.Z. Avatar asked May 23 '13 20:05

T.Z.


1 Answers

I would say to look at Texture.get_region: http://kivy.org/docs/api-kivy.graphics.texture.html#kivy.graphics.texture.Texture.get_region

So you can use only the part of the texture you want to display and passing as if it was an entirely different texture.

Another way would be to use StencilView, but i think here the get_region is better.

like image 124
Tshirtman Avatar answered Nov 15 '22 10:11

Tshirtman