[SOLVED]How to "reset" a Drag position ? [drag and drop]

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
korova
Veteran
Posts: 217
Joined: Sat Jun 27, 2009 5:15 pm
Completed: Ivy, Chocolate, Time, Clair Obscur
Projects: Writing exercises, The House [Nano18]
Tumblr: korova08
itch: korova
Location: Normandie, France
Contact:

[SOLVED]How to "reset" a Drag position ? [drag and drop]

#1 Post by korova »

Hello,

I'm preparing a sort of escape room for NaNoRenO and I'm trying to implement an inventory system.
My goal is that the player will be able to drags items from an inventory shown on screen to certain "hotspots" on the screen.

I spent some time wrapping my head around the drag&drop documentation and I achieved something that (mostly) works. I can drag obects on the screen and make things happen when they're dropped at the right place. (hourray \o/)

Now, I'd like that each time I drop the item, it comes back to its original place in the inventory frame. But the dragged items "remember" their position (which is usually the wanted behaviour). How could I "reset" that position and make them go back in their original place ?
(I suppose that something should be done in the callback function, for example ?)

Here is a sample of my code

Code: Select all

init python:  ## callback function for drag&drop

    def item_dragged(drags, drop):  

        if not drop:
            return

        # Interactions contains all predefined interactions between items and places
        if interactions.has_key( (drags[0].drag_name, drop.drag_name) ):
            renpy.call(interactions[(drags[0].drag_name, drop.drag_name)])

        else:
            renpy.call("nothing_happens") ## default interaction

        #return



screen use_item(view_data):

    add view_data['background']

    # Inventory frame backbground
    frame:
        xpadding 20
        ypadding 20
        xalign .5
        ypos 20
        xsize 720
        ysize 90
        background Solid("#fff")

        grid 10 1:
            xsize 50
            spacing 20

            for i in range(10):
                add Solid("#eee") id "i"


    draggroup:

        # droppable zones (retrieved from a list)
        for spot in view_data['hotspots']:

            drag:
                drag_name spot["name"]
                child spot["child"]
                draggable False
                xpos spot["coordinates"][0]
                ypos spot["coordinates"][1]

        # draggable items (retrieved from a list called inventory)
        $ x_pos = 280
        for item in inventory:
            $ x_pos += 20
            drag:
                drag_name item["drag_name"]
                child item["image"]
                droppable False
                dragged item_dragged
                xpos x_pos
                ypos 40
Last edited by korova on Sat Mar 03, 2018 7:39 pm, edited 1 time in total.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to "reset" a Drag position ? [drag and drop]

#2 Post by Remix »

Code: Select all

default init_xpos = {}
...
...

        if not drop:
            drags[0].snap( init_xpos[ drags[0].drag_name ], 40, 1.5 ) # x, y, seconds to move
...
...

        for item in inventory:
            $ x_pos += 20
            $ init_xpos[ item["drag_name"] ] = x_pos
            drag:
                drag_name item["drag_name"]
                # etc
Pseudo code
Basically, store the initial x,y positions outside of the drags, then if you drop on a non-drop-zone use the .snap() method to move the drag back to default position...
Frameworks & Scriptlets:

User avatar
korova
Veteran
Posts: 217
Joined: Sat Jun 27, 2009 5:15 pm
Completed: Ivy, Chocolate, Time, Clair Obscur
Projects: Writing exercises, The House [Nano18]
Tumblr: korova08
itch: korova
Location: Normandie, France
Contact:

Re: How to "reset" a Drag position ? [drag and drop]

#3 Post by korova »

Thanks, I will test this ASAP !
Thanks for the discovery of the snap method.

User avatar
korova
Veteran
Posts: 217
Joined: Sat Jun 27, 2009 5:15 pm
Completed: Ivy, Chocolate, Time, Clair Obscur
Projects: Writing exercises, The House [Nano18]
Tumblr: korova08
itch: korova
Location: Normandie, France
Contact:

Re: How to "reset" a Drag position ? [drag and drop]

#4 Post by korova »

Works like a charm. Thanks again!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Kocker