Skip to content

Drag and drop

Use drag and drop for inventory-style screens where an item moves from one place to another and a slot decides what it accepts.

The DragDrop sample has three bag items you can drag with the mouse or carry with the keyboard or a gamepad, a hand slot that takes weapons, a ring slot that takes nothing from the bag, and a stash list that takes weapons and potions. Run it by choosing DragDrop in the samples list in the map L_ArcRmlSamples (Samples).

The DragDrop sample: bag items Sword, Shield and Potion, empty Hand and Ring slots and three empty Stash slots

Run it

Play in the map L_ArcRmlSamples and choose DragDrop in the samples list; the console command is ArcRmlSamples.Open DragDrop (console).

What you should see:

  • Drag "Sword" onto the Hand slot with the mouse: a copy of the item follows the pointer, the slot turns green while it is under the pointer and shows Sword after the drop.
  • Drag "Potion" onto the Hand slot: the slot turns red and the drop does nothing. The Stash rows accept it.
  • The Ring slot turns red under every bag item and takes none of them.
  • With "Sword" focused, press F, move to a slot with the arrows and press Enter: the carry drops the item the same way.

Source and target

A source is an element whose drag property is drag-drop or clone (clone: a copy follows the pointer; style it with :drag) and that has data-drag-type="<tokens>". Without data-drag-type the source gets dragstart and the drag is cancelled at once: no drag, no dragend. A target has data-drop-accept="<tokens>" and accepts a source that shares a token with it.

DragDrop.rcss
.dd-item {
    background-color: #3a4456;
    drag: clone;
}

.dd-item:drag {
    background-color: #2f6fb0;
    opacity: 0.8;
}
DragDrop.rml
<div id="dd-sword" class="dd-slot dd-item" autofocus="" data-drag-type="weapon" data-item="Sword">Sword</div>
<div id="dd-potion" class="dd-slot dd-item" data-drag-type="potion" data-item="Potion">Potion</div>
<div id="dd-hand" class="dd-slot dd-target" data-drop-accept="weapon" data-slot="Hand" on:dragdrop="call('dd', 'Dropped', 'drag:data-item', 'target:data-slot')">{{ dd.Hand }}</div>
<div id="dd-ring" class="dd-slot dd-target" data-drop-accept="ring" data-slot="Ring" on:dragdrop="call('dd', 'Dropped', 'drag:data-item', 'target:data-slot')"></div>
<div id="dd-stash-0" class="dd-row dd-target" data-drop-accept="weapon potion" data-slot="Stash0" on:dragdrop="call('dd', 'Dropped', 'drag:data-item', 'target:data-slot')">{{ dd.Stash0 }}</div>

The excerpt leaves out "Shield" and the Stash1 and Stash2 rows.

'drag:data-item' passes the dragged element's data-item value and 'target:data-slot' the target's data-slot. The call reaches the view model only when the nearest data-drop-accept ancestor of the element under the pointer accepts the source; a drop anywhere else, also on empty space, reaches no handler. The ring slot accepts no bag item, so a drop on it does nothing.

DragDrop/ArcRmlSamplesDragDrop.cpp
void UArcRmlSamplesDragDropViewModel::Dropped(const FString& Item, const FString& Target)
{
    if (Target == TEXT("Hand"))
    {
        UE_MVVM_SET_PROPERTY_VALUE(Hand, Item);
    }
    else if (Target == TEXT("Stash0"))
    {
        UE_MVVM_SET_PROPERTY_VALUE(Stash0, Item);
    }
}

The Stash1 and Stash2 branches are left out; they set their property the same way.

The core sets classes while a drag runs: drag-source on the dragged element, drop-over on an accepting target under the pointer, drop-invalid on a target that does not accept the source. The sample styles them in DragDrop.rcss.

Carrying with a gamepad or the keyboard

data-drag-action="<UI action>" names a UI action of the document's input mapping context (Input and gamepad). On <body> it covers the whole document.

DragDrop.rml
<meta name="input-mapping" content="/ArcRmlUISamples/UI/Rml/DragDrop/IMC_Sample_DragDrop"/>
<body :model="dd" data-drag-action="/ArcRmlUISamples/UI/Rml/DragDrop/IA_Sample_Carry">

data-drag-action takes an action name or a full package path; the path is exact when two actions share a name. The sample maps IA_Sample_Carry to F and the gamepad's left face button. With a source focused, the action picks it up. While carrying:

  • arrows and the D-pad move focus, and drop-over / drop-invalid follow the focused element;
  • accept (Enter, Space, gamepad accept) on an accepting target drops it and runs the dragdrop call; on any other element it does nothing;
  • the action again does nothing; a mouse move or a primary press cancels the carry.

autofocus on "Sword" puts focus on the first item, so the first arrow or carry works at once. A source and its targets need tab-index: auto; nav: auto to take focus; the sample sets both on .dd-slot, .dd-row.

Back during a carry

Back plays the back sound if a style sheet declares one, then cancels the carry, returns focus to the source and ends there: the document stays open and no back event runs. A carry is the second step of the order of one Back.

The log lines

A drag of a typed source logs in LogArcRmlUI at Log level, each line starting with ArcRmlUI:; <element> is the element's tag, id and classes. When a drag does not do what you expect, look for the line that matches:

  • Nothing happens when you press and drag: the source has no data-drag-type. It logs no drag line, only the error drag source '...' has no data-drag-type; the drag does not start, once per element (reported as a problem of the document in non-Shipping builds, an Error log line in Shipping).
  • The drag starts but the drop does nothing: drag target '<element>' accepted no is the target refusing the source, and drag cancel '<source>' reason the target does not accept the source ends a pointer drag released on such a target, or on an element with no data-drop-accept ancestor (empty space included).
  • drag cancel '<source>' reason ended without a drop: the drag ended with no element under the pointer.
  • drag cancel '<source>' reason back: Back cancelled the carry.
  • drag cancel '<source>' reason navigation focus ended: the document's keyboard and gamepad focus navigation ended during a carry.
  • drag action on '<element>' ignored, no drag-drop or clone source: the carry action fired on an element that is not a source.
  • drag place on '<element>' ignored, it is not an accepting target: accept was pressed during a carry on an element that is not an accepting target.
  • drag drop '<source>' -> '<target>': the drop on the target.

drag start '<element>' types '<types>' input pointer is a mouse drag and input nav a carry. drag target none is logged when the pointer leaves the current target.

Limits

  • Removing the source or one of its ancestors mid-drag (a :if turning false), losing pointer capture or a new press cancels the drag: the clone goes and nothing is dropped.
  • The pointer capture belongs to the button that pressed first; another mouse button does not end the drag.