StencilView – limiting the drawing space
In Chapter 3, Widget Events – Binding Actions, we avoided drawing outside of the drawing space by using simple mathematics and collide_points
. It was far from perfect (for example, it fails in the group mode or when we are resizing) and it was tedious and prone to programming mistakes.
StencilView
is the way to go here. With a StencilView instance , you can avoid drawing outside of the area that is defined by it. First, let's modify the file drawingspace.py
with the following header:
94. # File name: drawingspace.py 95. from kivy.uix.stencilview import StencilView 96. 97. class DrawingSpace(StencilView): 98. ...
We have substituted RelativeLayout
with StencilView
. The StencilView
class doesn't use relative coordinates (as the RelativeLayout
class does) but we would like to keep that behavior in the drawing space. We can fix this problem modifying the top-right AnchorLayout
, so the DrawingSpace
instance is inside a RelativeLayout
instance. We do...