Search code examples
kivyandroid-relativelayout

KIVY collide_point RelativeLayout vs StencilView


I started learning KIVY from Kivy – Interactive Applications and Games in Python - Second Edition by Roberto Ulloa". I was trying to understand collide_point with a sample program.

In RelativeLayout with the below sample code, I am able to identify when mouse gets clicked on horizontal line. But if I use StencilView, I am unable to catch touch event.

Any help please

from kivy.app import App
from kivy.graphics import Line
from kivy.uix.scatter import Scatter
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.stencilview import StencilView


class MyPaintWidget(Scatter):

    def __init__(self, **kwargs) :
        self.selected = None
        self.touched = False
        super(MyPaintWidget, self).__init__(**kwargs)

    def create_figure(self,  **kwargs):
        (ix,iy) = self.to_local(*self.pos)
        (fx,fy) = self.to_local(self.pos[0] + self.size[0], self.pos[1] + self.size[1])
        print ("ix " + str(ix) + " iy " + str(iy) + " fx " + str(fx) + " fy " + str(fy))
        self.canvas.add(Line( points=[ix, iy, fx, fy], width=1))
        return self

    def on_touch_down(self, touch):
        if self.collide_point(touch.x, touch.y):
           #This print I get for RelativeLayout
           #But for StencilView it does not work when I touch the line
           print("Hi you touched the line")

class MyPaintApp(App):

    def build(self):
        #If parent is StencilView unable to click
        #but if parent is RelativeLayout, I am able to get collide_point
        #parent = StencilView()
        parent = RelativeLayout()
        #parent = Scatter()
        ix = 100.
        iy = 100.
        fx = 200.
        fy = 100.
        pos = (min(ix, fx), min(iy, fy))
        size = (abs(fx-ix), abs(fy-iy))

        self.painter = MyPaintWidget(pos=pos, size=size)
        parent.add_widget(self.painter.create_figure())
        return parent

if __name__ == '__main__':
    MyPaintApp().run()

Solution

  • The documentation for StencilView says:

    Note

    StencilView is not a layout. Consequently, you have to manage the size and position of its children directly. You can combine (subclass both) a StencilView and a Layout in order to achieve a layout’s behavior.

    So, the size that you set for its children will be used and size_hint is ignored. That results in a size for your MyPaintWidget of (100.0, 0.0) per your calculations. And a height of 0 will never return a True value for collide_point().

    However, when you use RelativeLayout, size_hint is not ignored, and since the default value for size_hint is (1,1), your MyPaintWidget ends up with a size the same as your App. Your calculated size is ignored, and you get True results for most of your collide_point() calls.

    To get consistent results, add size_hint=(None, None) to your MyPaintWidget(pos=pos, size=size). Of course, with a height of 0, you will never get a collision.