I couldn't figure out why function does not work for a second time in Kivy

19 views Asked by At

Hello I wrote a program that creates 6 customized widgets which I called Cell and I want them to swap locations if one of them overlaps another. So I wrote a function called swap_locations and it works properly at first but when I try to swap the Cell (which I first selected) again it takes the target Cell's place but the target cell doesn't change its place. Here is my code parts that I think suspicious:

class Cell (FloatLayout, DragBehavior):

    def __init__(self,**kwargs):
        super(Cell, self).__init__(**kwargs)
        self.previous_pos = [0,0]
                
    def swap_locations(self, target):
        target_pos = target.pos
        self.pos = target_pos
        target.pos = self.previous_pos
        self.previous_pos = self.pos
        target.previous_pos = target_pos

class BaseLayout (GridLayout):
clock = StringProperty()
day = StringProperty()
date = StringProperty()
temp = StringProperty()
icon = StringProperty()
selected_cell = Cell
target = Cell

    def __init__(self, **kwargs):
        super(BaseLayout, self).__init__(**kwargs),
        Clock.schedule_once(self.calculate_cell_positions)
    
    def calculate_cell_positions(self, dt):
        cell_count = len(self.children)
        if cell_count == 0:
            return 
    
        cols = self.cols
        rows = self.rows
        cell_width = self.width / cols
        cell_height = self.height / rows
    
        for i, cell in enumerate(self.children):
            if i % 2 == 0:
                factor = 1
            else:
                factor = 0
            row = i // cols
            cell.previous_pos = [factor * cell_width, row * cell_height ]           
            cell.pos = cell.previous_pos
    
    def on_touch_down(self, touch):
        x, y = touch.pos    
        for child in self.children:
            if child.collide_point(x,y):
                target = child
                self.selected_cell = target
                return True
        return True
    
    def on_touch_move(self, touch):
        if self.selected_cell:
            x, y = touch.pos
            self.selected_cell.pos = (x - self.selected_cell.width / 2, y - self.selected_cell.height / 2)
            return True
        return True
    
    def on_touch_up(self, touch): 
        if self.selected_cell:
            for child in self.children: 
                
                if child.collide_point(*touch.pos) and self.selected_cell != child:
                    self.selected_cell.swap_locations(child)
                elif child.collide_point(*touch.pos) == None:
                    self.selected_cell.pos = self.selected_cell.previous_pos                                                          
            else:
                self.selected_cell.pos = self.selected_cell.previous_pos                             
        return True 

I wrote calculate_cell_positions method in order to calculate the positions of cells manually because my Cells doesn't have initial positions. It may be a dumb idea and if you have a better solution please do not hesitate to tell. Thank you!

In order to dig down and modelize the problem I attached some photos of my program1 2 3

0

There are 0 answers