from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.uix.relativelayout import RelativeLayout from kivy.properties import NumericProperty, ReferenceListProperty from kivy.event import EventDispatcher from kivy.uix.button import Button from kivy.uix.behaviors import ButtonBehavior from kivy.uix.image import Image from kivy.uix.effectwidget import PushMatrix,PopMatrix from kivy.graphics import Rotate class SparseGridLayout(FloatLayout): rows = NumericProperty(1) columns = NumericProperty(1) shape = ReferenceListProperty(rows, columns) layout_built = False def do_layout(self, *args): shape_hint = (1. / self.columns, 1. / self.rows) if self.layout_built is False: for childLayout in self.children: for child in childLayout.children: child.size_hint = (shape_hint[0] * child.hor_span, shape_hint[1] * child.vert_span)#TODO integrate span here if not hasattr(child, 'row'): child.row = 0 if not hasattr(child, 'column'): child.column = 0 #evtl hier clickdispatch einbauen #TODO: testen warum bei Rotate hitbox nicht mitrotiert wird child.pos_hint = {'x': shape_hint[0] * child.row +(shape_hint[0]*0.5), 'y': shape_hint[1] * child.column + (shape_hint[1]*0.5)} #child.canvas.before.add(PushMatrix()) #child.canvas.before.add(Rotate(angle = 45,origin = [child.get_center_x(), child.get_center_y()] ))# #child.canvas.after.add(PopMatrix()) self.layout_built = True super(SparseGridLayout, self).do_layout(*args) class GridEntry(EventDispatcher): row = NumericProperty(0) column = NumericProperty(0) vert_span = NumericProperty(1) hor_span = NumericProperty(1) def check_click(): #TODO in click dispatching reinlesen #scheinbar wird click von sparsegrid nicht an kinder weitergegeben pass class GridImage(ButtonBehavior,Image,GridEntry): id = None def __init__(self,id=None,**kwargs): self.id = id super(GridImage,self).__init__(**kwargs) def on_release(self): print("pos_pressed: {}".format(self.id)) return super().on_release() class ExampleApp(App): def build(self): rlayout1 = RelativeLayout() rlayout2 = RelativeLayout() rlayout3 = RelativeLayout() rlayout4 = RelativeLayout() buttons = [] gi1 = GridImage(id=1,row=1, column=2,vert_span=2,source='Assets/Formations/Formation_Card_1_90.png') gi1.size = gi1.texture_size rlayout1.add_widget(gi1) buttons.append(rlayout1) gi2 = GridImage(id=2,row=2, column=1,vert_span=2,source='Assets/Formations/Formation_Card_2_90.png') gi2.size = gi2.texture_size rlayout2.add_widget(gi2) buttons.append(rlayout2) gi3 =GridImage(id=3,row=1, column=0,source='Assets/Formations/Formation_Card_3.png') gi3.size = gi3.texture_size rlayout3.add_widget(gi3) buttons.append(rlayout3) gi4 = GridImage(id=4,row=0, column=1,source='Assets/Formations/Formation_Card_4.png') gi4.size = gi4.texture_size rlayout4.add_widget(gi4) buttons.append(rlayout4) layout = SparseGridLayout(rows=3, columns=3) for button in buttons: layout.add_widget(button) #layout.add_widget(layout) return layout if __name__ == "__main__": ExampleApp().run()