DragDropManager lets the player drag elements with the mouse or a finger, and drop them onto drop zones. Use it for inventories, matching puzzles and card games.
Draggables and drop zones are matched by groups: a drop zone only accepts draggables whose group it lists.
| Member | Description |
|---|---|
registerDraggable() |
Makes an element draggable |
registerDropZone() |
Makes an element accept drops |
registerDraggable
DragDropManager.registerDraggable(el: HTMLElement, options?: {
dragGroup?: string;
payload?: unknown;
}): void
Makes an element draggable.
| Parameter | Type | Default | Description |
|---|---|---|---|
el |
HTMLElement |
The element to drag | |
options.dragGroup |
string |
"" |
The group it belongs to |
options.payload |
any |
None | A value drop zones receive as payload, such as an item id. Pass a function to have it called each time instead, for a value that changes |
Pressing a button, link or text field inside a draggable element uses that control instead of starting a drag.
How it works
- The element moves by changing its
leftandtopstyle. - It's raised above the elements around it while it's dragged.
- When it's let go, it always goes back to where it started, whether it was dropped on a drop zone or not. A drop zone's
onDropruns after it's back, so the drop zone decides what happens: hide it, update a variable, or move it somewhere else to keep it there.
Dragging out of scroll views
An element inside a scroll view, a Hidden-overflow container, or a Row, Column or Grid layout is lifted out when you start dragging it. It moves to the top of the scene so it isn't cut off and can be dropped anywhere, for example from an inventory list onto a character. An empty gap of the same size keeps its place in the list while it's away, and it goes back into that place when it's let go.
A click without dragging leaves it where it is.
Example
onStart() {
for (const gem of [this.dom.gem_red, this.dom.gem_blue, this.dom.gem_green]) {
DragDropManager.registerDraggable(gem, { dragGroup: "gem" });
}
},
registerDropZone
DragDropManager.registerDropZone(el: HTMLElement, options?: {
acceptGroups?: string;
onDragEnter?: (details: DragDropDetails) => void;
onDragExit?: (details: DragDropDetails) => void;
onDrop?: (details: DragDropDetails) => void;
}): void
Makes an element accept dropped elements.
| Parameter | Type | Default | Description |
|---|---|---|---|
el |
HTMLElement |
The drop zone | |
options.acceptGroups |
string |
Every group | The groups it accepts, separated by commas, such as "gem, coin" |
options.onDragEnter |
(details) => void |
None | Called when an accepted element is dragged onto it |
options.onDragExit |
(details) => void |
None | Called when that element is dragged off it again without being dropped |
options.onDrop |
(details) => void |
None | Called when an accepted element is dropped on it |
Each handler receives a DragDropDetails object with:
| Field | Description |
|---|---|
el |
The element being dragged |
zoneEl |
The drop zone |
dragGroup |
The dragged element's group |
payload |
The dragged element's payload, read as the event happens, or undefined if it has none |
How it works
- An element is dropped on the accepting zone under the pointer when it's released. The dragged element itself is ignored, so it doesn't hide the zone underneath it.
- After
onDragEnter, a zone always gets eitheronDragExitoronDrop, never both. If the drag is interrupted, for example by the system cancelling a touch, the zone getsonDragExit. - Zones only react to groups they accept. If zones are inside each other, the innermost zone that accepts the element gets it.
- Calling it again on the same element replaces its options.
- While an accepted element is over a zone, the zone has the
mm-dragdrop-hoverclass. Style it with a.mm-dragdrop-hoverrule in your project's CSS, or useonDragEnterandonDragExitfor anything more.
Example
onStart() {
DragDropManager.registerDropZone(this.dom.chest, {
acceptGroups: "gem",
onDragEnter: ({ zoneEl }) => {
zoneEl.setState("open"); // the chest opens while a gem is held over it
},
onDragExit: ({ zoneEl }) => {
zoneEl.setState("closed");
},
onDrop: ({ el, zoneEl }) => {
el.hide(); // the gem goes into the chest
zoneEl.setState("sparkle"); // the chest node has a "sparkle" state
Store.Set("gems", Store.Get("gems", 0) + 1);
},
});
},
In the Scene Editor
You can set this up without code, in the Drag & Drop group of the inspector.
Draggable items. On an image, turn on Draggable. On a container, set Type to Draggable. Dragging a container moves it with everything inside it, which suits cards and inventory slots built from several nodes. Then fill in:
- Drag Group: the group it belongs to.
- Payload: a value drop zones receive as
payload, such as the item's id. Leave it on None for no payload, or choose where it comes from:- Scene Variable: pick one of the scene's variables. Its current value is read each time a drag event happens. In a sub-scene, each copy has its own variable values, so one item scene can be reused for every item.
- Fixed Value: pick a type, then set the value the same way as a scene variable: text, a number, true or false, a file, a list or an object. Every copy of the node sends the same value.
The item always goes back to where it started when it's let go. Use the drop zone's On Drop method to decide what the drop does.
Drop zones. On a container, set Type to Drop Zone and fill in:
- Accept Groups: the groups it accepts, separated by commas.
- On Drag Enter, On Drag Exit and On Drop: the names of methods in the scene's script to call.
A container can be draggable or a drop zone, not both.
Each method receives the DragDropDetails object first, followed by any arguments you write after the name. For example, onGemDrop("red") calls onGemDrop(details, "red"). The button next to each field adds the method to the script if it doesn't exist yet, as onGemDrop(drag: DragDropDetails) in a TypeScript scene.
onGemDrop(drag) {
drag.el.hide();
log(`A ${drag.payload} went into the chest`); // the gem's payload, such as "ruby"
},