[solved] Flush drag-and-drop drags position cache (random drags position problem)

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Eliont
Regular
Posts: 88
Joined: Thu Aug 06, 2009 6:51 am
Completed: Begin of Evangelion, SAO - Smile of the black cat, SAO - Project "Ceramic Heart"
Projects: Legends of Alkion
Location: Russia
Contact:

[solved] Flush drag-and-drop drags position cache (random drags position problem)

#1 Post by Eliont » Tue Sep 05, 2017 11:37 pm

Hello and good time of day.
How to deal with drags taking random position on screen after interaction which change drag lists content?
May be there are way to flush their positions to locate them as they spawn on game start.
Or method to solve it in another way.

Video demostrating problem: In develop - LotR LCG desktop player.

Drag group:

Code: Select all

    draggroup:
        for index,card in enumerate(p.current().hand):
            $x,y,h = calculate_hand_card_position(index)
            if len(p.current().hand) <= 8:
                drag:
                    dragged card_dragged_handler
                    drag_name "Hand %s"%(index)
                    drag_handle (0, 0, 1.0, 1.0)
                    droppable False
                    xpos x 
                    ypos y
                    use handcard(card=card,x=x,y=y)
            else:
                drag:
                    dragged card_dragged_handler
                    drag_name "Hand %s"%(index)
                    drag_handle (0, 0, 1.0, 1.0)
                    droppable False
                    xpos x 
                    ypos y
                    use handcard(card=card,x=x,y=y,h=h)  
    
        for index, key in enumerate(drag_drop_tokens):
            drag:
                drag_name key
                dragged token_dragged_handler
                xpos (30 + side_bar_width + 100*index)
                ypos 630
                droppable False
                use icon(img = drag_drop_tokens[key][0],frame=drag_drop_tokens[key][1],mask=drag_drop_tokens[key][2],size=int(stat_icon_size*1.5))
        
        for index,card in enumerate(p.current().heroes):
            $x,y = calculate_field_hero_card_position(index)
            
            drag:
                dragged card_dragged_handler
                drag_name "field_hero %s"%(index)
                drag_handle (0, 0, 1.0, 1.0)
                xpos x 
                ypos y
                use fieldcard(card=card,x=x,y=y)
Drag handler:

Code: Select all

    def card_dragged_handler(drags, drop):

        renpy.hide_screen('fieldcard_info')
        # debug(drags[0].__dict__)
        
        notify("%s - %s"%(drags[0].last_x,drags[0].last_y))
        
        data = drags[0].drag_name.split()
        
        if data[0] == 'Hand':
            if drags[0].last_x > side_bar_width + 100:
                index = int(data[1])
                card = p.current().hand[index]
                p.current().hand.remove(card)
                p.current().heroes.append(card)
                
        if data[0] == 'field_hero':
            if drags[0].last_x < side_bar_width:
                index = int(data[1])
                card = p.current().heroes[index]
                p.current().heroes.remove(card)
                p.current().hand.append(card) 
Thanks in advance.
Last edited by Eliont on Wed Sep 06, 2017 7:16 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1656
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Flush drag-and-drop drags position cache (random drags position problem)

#2 Post by philat » Wed Sep 06, 2017 1:44 am

Hard to say without knowing how you calculate the positions.

User avatar
Eliont
Regular
Posts: 88
Joined: Thu Aug 06, 2009 6:51 am
Completed: Begin of Evangelion, SAO - Smile of the black cat, SAO - Project "Ceramic Heart"
Projects: Legends of Alkion
Location: Russia
Contact:

Re: Flush drag-and-drop drags position cache (random drags position problem)

#3 Post by Eliont » Wed Sep 06, 2017 4:16 am

Index - element number in list

Code: Select all

    def calculate_field_hero_card_position(index):   
        x = (side_bar_width+5) + (field_card_width + 5) * index 
        y = config.screen_height - field_card_height - 5
        return x,y        
        
        
    def calculate_hand_card_position(index): 
        h = hand_card_height
        if len(p.current().hand) > 10:
            amount = len(p.current().hand) - 10 
            amount = math.ceil(divide(amount,2))
            h = int(divide(hand_card_height,1.0 + 0.2 * amount))
    
        if index == 0:
            x = 0
            y = 0 
            
        elif index == 1:
            x = 0 + ( hand_card_width + 5 )
            y = 0                 
        
        else:
            if index % 2 == 0:
                x = 0
                y = 0 + ( h+10 ) * int(math.floor(divide(index,2))) 
            else:
                x = 0 + hand_card_width
                y = 0 + ( h+10 ) * int(math.floor(divide(index,2))) 
                
        return x,y,h
        
        
        
    def calculate_encounter_card_position(index): 
        xd = config.screen_width - (side_bar_width + field_card_width + 50)
        if index == 0:
            x = xd
            y = 5
            
        elif index == 1:
            x = xd
            y = field_card_height                
        
        else:
            if index % 2 == 0:
                x = xd - int((field_card_width+5) * math.floor(divide(index,2))) 
                y = 5
            else:
                x = xd - int((field_card_width+5) * math.floor(divide(index,2))) 
                y = field_card_height 
                
        return x,y  

philat
Eileen-Class Veteran
Posts: 1656
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Flush drag-and-drop drags position cache (random drags position problem)

#4 Post by philat » Wed Sep 06, 2017 4:52 am

I guess I'm confused, because I don't really know what you're trying to do. What exactly is the desired behavior?

Based on the calculations, it appears that you want the cards to snap to predefined positions (i.e., if it 's the first card in the "hand", it's at xpos X and ypos Y and you shouldn't be able to drag it to an arbitrary position)?

Seems like it would make more sense to use .snap() on the same drag rather than create a new one in a different deck. At this level of complexity also, it seems like it would make more sense to customize the Drag() and DragGroup() class rather than try to use screen language for all your needs -- for instance, if you set up a method to calculate the x and y positions you want when you want (instead of doing it within the screen, when it is unpredictable when that would run), it would be easier to keep track.

User avatar
Eliont
Regular
Posts: 88
Joined: Thu Aug 06, 2009 6:51 am
Completed: Begin of Evangelion, SAO - Smile of the black cat, SAO - Project "Ceramic Heart"
Projects: Legends of Alkion
Location: Russia
Contact:

Re: Flush drag-and-drop drags position cache (random drags position problem)

#5 Post by Eliont » Wed Sep 06, 2017 7:16 am

Thanks to Alex, solution found:

Code: Select all

        drag_group = renpy.get_widget('adventure', "drag_group").children
        for drag in drag_group:
            data = drag.drag_name.split()
            if data[0] == 'field_hero':
                index = int(data[1])
                x,y = calculate_field_hero_card_position(index)
                drag.snap(x,y,0) 
            if data[0] == 'Hand':
                index = int(data[1])
                x,y,h = calculate_hand_card_position(index)
                drag.snap(x,y,0) 

User avatar
Eliont
Regular
Posts: 88
Joined: Thu Aug 06, 2009 6:51 am
Completed: Begin of Evangelion, SAO - Smile of the black cat, SAO - Project "Ceramic Heart"
Projects: Legends of Alkion
Location: Russia
Contact:

Re: Flush drag-and-drop drags position cache (random drags position problem)

#6 Post by Eliont » Wed Sep 06, 2017 7:21 am

philat wrote:
Wed Sep 06, 2017 4:52 am
customize the Drag() and DragGroup() class
Thank you, i think i will try next time.

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], RVNSN