Drag and Drop link
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.)
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 screen. 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.
There are two ways Ren'Py can process a drop:
If mouse_drop is true, the drag will be dropped onto the droppable that is directly below the mouse cursor.
If mouse_drop is false, the default, the drop will occur onto the droppable that most fully overlaps with the drag.
Unlike when starting a drag, where focus_mask is used, dropping considers the entire rectangular areas of the draggable and droppable, including any transparent pixels. You may need to design your drag and drop displayables to take this into account, by being generally rectangular in shape.
- class Drag(d=None, drag_name=None, draggable=True, droppable=True, drag_raise=True, dragging=None, dragged=None, dropped=None, drag_handle=(0.0, 0.0, 1.0, 1.0), drag_joined=..., clicked=None, hovered=None, unhovered=None, mouse_drop=False, **properties) link
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 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.
The drag handle is a rectangle inside the child. The mouse must be over a pixel inside the drag handle for dragging or clicking to occur. If the
focus_maskproperty is True, that pixel must not be transparent.
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 has been computed, the layout properties are ignored in favor of the position stored inside the Drag.
If present, the child of this Drag. Drags use the child style in preference to this, if it's not None.
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.
If true, the Drag can be dragged around the screen with the mouse.
If true, other Drags can be dropped on this Drag.
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.
A callback (or list of callbacks) that is called when the mouse is pressed down on the drag. It is called with one argument, a a list of Drags that are being dragged. The return value of this callback is ignored.
A callback (or list of callbacks) that is called when the Drag is being dragged. It is called with one argument, 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.
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.
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.
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 other than None, that value is returned as the result of the interaction.
An action that is run when the Drag is right-clicked (on the desktop) or long-pressed without moving (on mobile). It may be necessary to increase
config.longpress_durationif this triggers to early on mobile platforms.
A (x, y, width, height) tuple, giving the position of the drag handle within the child. This tuple takes positions.
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.
Determines the conditions under which the drag is allowed to be dragged offscreen. Allowing offscreen dragging can be dangerous to use with drag_joined or drags that can change size, as the drags can leave the screen entirely, with no way to get them back on the screen.
This should be one of:
To disallow dragging the drag offscreen. (The default)
To allow dragging offscreen, in any direction.
To allow dragging offscreen in the horizontal direction only.
To allow dragging offscreen in the vertical direction only.
- (width, height)
Both width and height must be integers. The drag can be dragged offscreen as long as a (width, height)-sized part of it remains on-screen. So, (100, 100) will ensure that at least a 100x100 pixel area of the displayable will remain on-screen even while the rest of the displayable can be dragged offscreen. Setting this to the width and height of the displayable being dragged is equivalent to not allowing the drag to go offscreen at all.
- (min_x, max_x, min_y, max_y)
Where each of min_x, max_x, min_y, and max_y are integers. min_x is the number of pixels away from the left border, and max_x is the number of pixels away from the right border. The same goes for min_y and max_y on the top and bottom borders respectively. The drag can be moved until one of its edges hit the specified border. (0, 0, 0, 0) is equivalent to not allowing dragging offscreen at all.
For example, (-100, 200, 0, 0) would allow the drag to be dragged 100 pixels off the left edge of the screen and 200 pixels off the right edge of the screen, but does not allow it to be dragged offscreen at the top nor bottom.
This can also be used to constrain the drag within the screen bounds. (200, -200, 200, -200) would only allow the drag within 200 pixels of the edges of the screen.
You can envision this as an additional "border" around the drag, which may go outside the bounds of the screen, that constrains the drag to remain within it.
A callable can be provided to drag_offscreen. It must take two arguments: an x and a y position which represents the dragged position of the top left corner of the drag, and it must return an (x, y) tuple which is the new (x, y) position the drag should be in. This callable is called frequently, whenever the drag is moved. For example, the following function snaps the drag into place every 300 pixels:
def drag_snap(x, y): if y < 300: y = 0 elif y < 600: y = 300 else: y = 600 return 200, y
If true, the drag is dropped on the first droppable under the cursor. If false, the default, the drag is dropped onto the droppable with the largest degree of overlap.
A callback that is called to determine whether this drop will allow the current drags to be dropped onto it. It is called with two arguments. The first is the Drag which determines its sensitivity. The second is a list of Drags that are being dragged.
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.
- start_x, start_y
The drag start position of the Drag relative to its parent, in pixels.
- w, h
The width and height of the Drag's child, in pixels.
Lowers this displayable to the bottom of its drag_group.
- set_child(d) link
Changes the child of this drag to d.
- snap(x, y, delay=0) link
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() link
Raises this displayable to the top of its drag_group.
- class DragGroup(*children, **properties) link
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
All positional parameters to the DragGroup constructor should be Drags, that are added to the DragGroup.
An integer which means the minimum number of pixels at the overlap for the drop to be allowed.
- add(child) link
Adds child, which must be a Drag, to this DragGroup.
- get_child_by_name(name) link
Returns the first child of this DragGroup that has a drag_name of name.
- remove(child) link
Removes child from this DragGroup.
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(who, what): 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.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 can be # dragged to each other. draggroup: # Our detectives. drag: drag_name "Ivy" droppable False dragged detective_dragged xpos 100 ypos 100 add "ivy.png" drag: drag_name "Zack" droppable False dragged detective_dragged xpos 150 ypos 100 add "zack.png" # The cities they can go to. drag: drag_name "London" draggable False xpos 450 ypos 140 add "london.png" drag: drag_name "Paris" draggable False xpos 500 ypos 280 add "paris.png" 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] to [city]."
More complicated systems take significant programming skill to get right.
as clause can be used to bind a drag to variable, which can then be
used to call methods on the drag.
screen snap(): drag: as carmen draggable True xpos 100 ypos 100 frame: style "empty" background "carmen.png" xysize (100, 100) vbox: textbutton "London" action Function(carmen.snap, 450, 140, 1.0) textbutton "Paris" action Function(carmen.snap, 500, 280, 1.0)