Ren'Py Home Page

Table Of Contents

Previous topic

Configuration Variables

Next topic

Sprites

Created using Sphinx.

Drag and Drop

Ren'Py includes drag and drop displayables that allow things to be moved around the screen with the mouse. Some of the uses of dragging are:

  • Allowing windows to be repositioned by the user, storing the window positions.
  • Card games that require cards to be dragged around the screen. (For example, solitaire.)
  • Inventory systems.
  • Drag-to-reorder systems.

The drag and drop displayables make it possible to implement these and other uses of drag and drop. There are two classes involved here. The Drag class represents either something that can be dragged around the screen, something that can have a draggable dropped onto it, or something that can do both. The DragGroup class represents a group of Drags - for a drag and drop to occur, both Drags must be part of the same drag group.

The drag and drop system can be used either through the Screen Language or directly as displayables. It makes sense to use the screen language when you don't need to refer to the Drags that you create after they've been created. This might be the case if the draggable represents a window that the user places on the scren. If you need to refer to the drags after they've been created, then it's often better to create Drags directly, and add them to a DragGroup.

Displayables

class Drag(d=None, drag_name=None, draggable=True, droppable=True, drag_raise=True, dragged=None, dropped=None, drag_handle=(0.0, 0.0, 1.0, 1.0), drag_joined=<function default_drag_joined at 0x1de72a8>, clicked=None, hovered=None, unhovered=None, replaces=None, **properties)

A displayable that represents an object that can be dragged around its enclosing area. A Drag can also represent an area that other Drags can be dropped on.

A Drag can be moved around inside is parent. Generally, its parent should be either a Fixed() or DragGroup.

A Drag has one child. The child's state reflects the status of the drag and drop operation:

  • selected_hover - when it is being dragged.
  • selected_idle - when it can be dropped on.
  • hover - when the draggable will be dragged when the mouse is clicked.
  • idle - otherwise.

The drag handle is a rectangle inside the child. The mouse must be over a non-transparent pixel inside the drag handle for dragging or clicking to occur.

A newly-created draggable is added to the default DragGroup. A draggable can only be in a single DragGroup - if it's added to a second group, it's removed from the first.

When a Drag is first rendered, if it's position cannot be determined from the DragGroup it is in, the position of its upper-left corner is computed using the standard layout algorithm. Once that position

d
If present, the child of this Drag. Drags use the child style in preference to this, if it's not None.
drag_name
If not None, the name of this draggable. This is available as the name property of draggable objects. If a Drag with the same name is or was in the DragGroup, the starting position of this Drag is taken from that Draggable.
draggable
If true, the Drag can be dragged around the screen with the mouse.
droppable
If true, other Drags can be dropped on this Drag.
drag_raise
If true, this Drag is raised to the top when it is dragged. If it is joined to other Drags, all joined drags are raised.
dragged
A callback (or list of callbacks) that is called when the Drag has been dragged. It is called with two arguments. The first is a list of Drags that are being dragged. The second is either a Drag that is being dropped onto, or None of a drop did not occur. If the callback returns a value other than None, that value is returned as the result of the interaction.
dropped

A callback (or list of callbacks) that is called when this Drag is dropped onto. It is called with two arguments. The first is the Drag being dropped onto. The second is a list of Drags that are being dragged. If the callback returns a value other than None, that value is returned as the result of the interaction.

When a dragged and dropped callback are triggered for the same event, the dropped callback is only called if dragged returns None.

clicked
A callback this is called, with no arguments, when the Drag is clicked without being moved. A droppable can also be focused and clicked. If the callback returns a value othe than None, that value is returned as the result of the interaction.
drag_handle
A (x, y, width, height) tuple, giving the position of the drag handle within the child. In this tuple, integers are considered to be a literal number of pixels, while floats are relative to the size of the child.
drag_joined
This is called with the current Drag as an argument. It's expected to return a list of [ (drag, x, y) ] tuples, giving the draggables to drag as a unit. x and y are the offsets of the drags relative to each other, they are not relative to the corner of this drag.

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.
set_child(d)

Changes the child of this drag to d.

snap(x, y, delay=0)

Changes the position of the drag. If the drag is not showing, then the position change is instantaneous. Otherwise, the position change takes delay seconds, and is animated as a linear move.

top(self)

Raises this displayable to the top of its drag_group.

class DragGroup(*children, **properties)

Represents a group of Drags. A Drag is limited to the boundary of its DragGroup. Dropping only works between Drags that are in the same DragGroup. Drags may only be raised when they are inside a DragGroup.

A DragGroup is laid out like a Fixed().

All positional parameters to the DragGroup constructor should be Drags, that are added to the DragGroup.

add(child)

Adds child, which must be a Drag, to this DragGroup.

get_child_by_name(name)

Returns the first child of this DragGroup that has a drag_name of name.

remove(child)

Removes child from this DragGroup.

Examples

An example of a say screen that allows the user to choose the location of the window by dragging it around the screen.:

screen say:

    drag:
        drag_name "say"
        yalign 1.0
        drag_handle (0, 0, 1.0, 30)

        xalign 0.5

        window id "window":
            # Ensure that the window is smaller than the screen.
            xmaximum 600

            has vbox

            if who:
                text who id "who"

            text what id "what"

Here's a more complicated example, one that shows how dragging can be used to influence gameplay. It shows how dragging can be used to send a character to a location:

init python:

    def detective_dragged(drags, drop):

        if not drop:
            return

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

        return True

screen send_detective_screen:

    # A map as background.
    add "europe.jpg"

    # A drag group ensures that the detectives and the cities.
    drag_group:

        # Our detectives.
        drag:
            drag_name "Ivy"
            child "ivy.png"
            droppable False
            dragged detective_dragged
            xpos 100 ypos 100
        drag:
            drag_name "Zack"
            child "zack.png"
            droppable False
            dragged detective_dragged
            xpos 150 ypos 100

        # The cities they can go to.
        drag:
            drag_name "London"
            child "london.png"
            draggable False
            xpos 450 ypos 140
        drag:
            drag_name "Paris"
            draggable False
            child "paris.png"
            xpos 500 ypos 280

label send_detective:
    "We need to investigate! Who should we send, and where should they go?"

    call screen send_detective_screen

    "Okay, we'll send %(detective)s to %(city)s."

More complicated systems take significant programming skill to get right. The Ren'Py cardgame framework is both an example of how to use drag and drop in a complex system, and useful for making card games in its own right.