Book Image

Kivy - Interactive Applications and Games in Python

By : Roberto Ulloa
Book Image

Kivy - Interactive Applications and Games in Python

By: Roberto Ulloa

Overview of this book

Table of Contents (13 chapters)
Kivy – Interactive Applications and Games in Python Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 resize it), and it was tedious and prone to programming mistakes.

That was sufficient for a first example, however, StencilView is the easier way to go here. StencilView limits the drawing area to the space occupied by itself. Anything drawn outside that area is hidden. First, let's modify the file drawingspace.py with the following header:

97. # File name: drawingspace.py
98. from kivy.uix.stencilview import StencilView
99. 
100. class DrawingSpace(StencilView):
101.     ...

The DrawingSpace instance inherits now from StencilView, instead of RelativeLayout. The StencilView class doesn't use relative coordinates (as the RelativeLayout class does) but we would like to keep relative coordinates in the drawing space because they are convenient...