Well I am using an 'experimental' feature in kivy called Tesselator (https://kivy.org/doc/stable/api-kivy.graphics.tesselator.html). Currently my app draws a wee shape in the bottom left. It doesn't use a .kv file.
I would like to know the best way to make the shape remain centred on the screen as you change the window's size? I have seen how to do this with the basic shapes, but I can't figure out how to do this with a tessellated shape.
Any suggestions and advice is much appreciated! The code is below.
import kivy
from kivy.app import App
from kivy.app import App
from kivy.graphics import Mesh
from kivy.graphics.tesselator import Tesselator
from kivy.uix.floatlayout import FloatLayout
class MapBackground(FloatLayout):
def __init__(self, **kwargs):
super(MapBackground, self).__init__(**kwargs)
self.shapes = [
[100, 100, 300, 100, 300, 300, 100, 300],
[150, 150, 250, 150, 250, 250, 150, 250]
]
self.draw()
# draws the map shape
def draw(self):
# make the tesselator object
tess = Tesselator()
# add all the shapes
for shape in self.shapes:
if len(shape) >= 3:
tess.add_contour(shape)
# call the tesselate method to compute the points
if not tess.tesselate(): # returns false if doesn't work
print('tesselator did\'t work:(')
return
# clear canvas
self.canvas.clear()
# draw shapes
for vertices, indices in tess.meshes:
self.canvas.add(Mesh(
vertices=vertices,
indices=indices,
mode='triangle_fan'
))
class TimmApp(App):
def build(self):
self.title = 'The Interactive Museum Map'
return MapBackground()
if __name__ == '__main__':
TimmApp().run()
I did what I wanted by making the widget the shape was drawn in to be inside a float layout. Then (using the kv file seen below) I binded a method called draw() to the event on_size. This meant that the shape was redrawn whenever the window changed shape. I won't show the code for draw() because it's a lot.
#:kivy 1.11.1
FloatLayout:
MapBackground:
on_size: self.draw()