Drag&Drop: how to know coordenates of drop position?

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
abeplaga
Regular
Posts: 34
Joined: Mon Oct 15, 2018 10:09 am
Contact:

Drag&Drop: how to know coordenates of drop position?

#1 Post by abeplaga » Fri Oct 04, 2019 9:16 pm

Hello everybody. I have a question, I do not know if it is a silly doubt but I would like to solve it because I wanna sleep tonight :p

Ok, so... Here I go: is it possible to get the X and Y values of the position where a drag object has been drop? I want to save that information in a few variables but I can't find a way to do it.

On the other hand, is it possible to save in a variable the value of the width of the drag object?

Thank you all!

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

Re: Drag&Drop: how to know coordenates of drop position?

#2 Post by philat » Fri Oct 04, 2019 9:23 pm

Except for d, all of the parameters are available as fields (with the same name) on the Drag object. In addition, after the drag has been rendered, the following fields become available:

x, y
The position of the Drag relative to its parent, in pixels.
w, h
The width and height of the Drag's child, in pixels.


https://www.renpy.org/doc/html/drag_drop.html

User avatar
abeplaga
Regular
Posts: 34
Joined: Mon Oct 15, 2018 10:09 am
Contact:

Re: Drag&Drop: how to know coordenates of drop position?

#3 Post by abeplaga » Fri Oct 04, 2019 9:57 pm

philat wrote:
Fri Oct 04, 2019 9:23 pm
Except for d, all of the parameters are available as fields (with the same name) on the Drag object. In addition, after the drag has been rendered, the following fields become available:

x, y
The position of the Drag relative to its parent, in pixels.
w, h
The width and height of the Drag's child, in pixels.


https://www.renpy.org/doc/html/drag_drop.html

Yes, I've read the documentation but I don't know how to do it. I'm a student and not a programmer, I don't have much idea and that's why I ask, to learn.

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

Re: Drag&Drop: how to know coordenates of drop position?

#4 Post by philat » Fri Oct 04, 2019 10:51 pm

Yeah, I'm not a programmer either. You asked two questions. Can you access a Drag's x/y/width? If so, how do you store it in a variable? The documentation answers both. 1. Yes, a Drag's x/y/width are available properties. 2. There is literally an example of a Drag's properties being stored in a variable in the documentation.

Code: Select all

init python:

    def detective_dragged(drags, drop):

        if not drop:
            return

        store.detective = drags[0].drag_name
        store.city = drop.drag_name

        return True
After dragging, if a drop occurred, the variable detective now stores the drag's drag_name and the variable city now stores the drop's drag_name. drags[0].x would be the drag's x position.

If you have a question beyond what is in the documentation, you need to be more specific.

User avatar
Alex
Lemma-Class Veteran
Posts: 2500
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Drag&Drop: how to know coordenates of drop position?

#5 Post by Alex » Sat Oct 05, 2019 5:37 am

abeplaga wrote:
Fri Oct 04, 2019 9:16 pm
...is it possible to get the X and Y values of the position where a drag object has been drop? ...
Extending philat's answer, if you dragged and dropped the object somewhere onscreen (and not over droppable object), then you can store its coordinates like

Code: Select all

init python:

    def detective_dragged(drags, drop):

        if not drop:
            store.my_x = drags[0].x
            store.my_y = drags[0].y
(you might need to set the default values for my_x and my_y (as usually) to not get error)

User avatar
abeplaga
Regular
Posts: 34
Joined: Mon Oct 15, 2018 10:09 am
Contact:

Re: Drag&Drop: how to know coordenates of drop position?

#6 Post by abeplaga » Sat Oct 05, 2019 8:25 am

Alex wrote:
Sat Oct 05, 2019 5:37 am
abeplaga wrote:
Fri Oct 04, 2019 9:16 pm
...is it possible to get the X and Y values of the position where a drag object has been drop? ...
Extending philat's answer, if you dragged and dropped the object somewhere onscreen (and not over droppable object), then you can store its coordinates like

Code: Select all

init python:

    def detective_dragged(drags, drop):

        if not drop:
            store.my_x = drags[0].x
            store.my_y = drags[0].y
(you might need to set the default values for my_x and my_y (as usually) to not get error)
Thank you, it works fine! My mistake was that I wrote "drop" instead of "drags[0]" >.<

Another question regarding drag and drop: is it possible to limit the drag area? In other words... that the object can only be moved only on a area of the screen with drag limits.

User avatar
Alex
Lemma-Class Veteran
Posts: 2500
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Drag&Drop: how to know coordenates of drop position?

#7 Post by Alex » Sat Oct 05, 2019 1:42 pm

abeplaga wrote:
Sat Oct 05, 2019 8:25 am
...is it possible to limit the drag area? In other words... that the object can only be moved only on a area of the screen with drag limits.
Well, you need to place your draggroup inside a kind of container then, so it would be limited inside it and not inside the whole screen, like

Code: Select all

image red:
    Solid("#c00")
    size(200, 200)
    
image blue:
    Solid("#00c")
    size(200, 200)

screen test_drag_drop_area_scr():
    
    frame:
        pos (100, 100)
        side "c b r":
            area (0, 0, 600, 400)
            

            viewport id "vp":
                child_size (1200, 800)
                draggable True

                draggroup:

                    drag:
                        drag_name "red"
                        child "red"
                        droppable False
                        #dragged detective_dragged
                        xpos 100 ypos 100
                    drag:
                        drag_name "blue"
                        child "blue"
                        droppable False
                        #dragged detective_dragged
                        xpos 300 ypos 300

            bar value XScrollValue("vp")
            vbar value YScrollValue("vp")
        
# The game starts here.

label start:
    "..."
    show screen test_drag_drop_area_scr
    "... ..."
    "?!"

Post Reply

Who is online

Users browsing this forum: Bing [Bot]