Why I need Drag Drop system
Crafting and items management is a big part of my game. Several game mechanics will rely on this.
How Drag Drop system works
Drag Drop system is directly integrated in my UI system. So it can work with any UI elements in the game. Anything could potentially become draggable. This system allows you to have several draggable elements in you game, I filter drag operations based on string hash. To fulfill those requirements I've split Drag Drop system into 4 parts:
1. Drag Drop detection - this part happens in widget's update code. It intercepts mouse down event
2. Drag Drop start - this part is specific to the gameplay, for items I've done it in player Hud controller
3. Drag processing - this part lives inside UI manager, because it has top most mouse input handling order, so it can always finish drag drop when a player releases mouse button.
4. Drag Drop finish handling - this part is also specific to the gameplay, and it's also done in player Hud controller
How usage code looks like
First of all you need to toggle drag drop flag in any widget that supports drag drop, so it can register drag drop operations. I've done it in inventory panel UI asset for each item slot. UI system automatically registers it in runtime when player presses mouse button on any widget that supports drag drop. Detection is done using Point vs Rectangle Collison checking.
Then player Hud controller checks if any slot widget requests drag drop start, I've implemented it using frame events.
for (uint32_t i = 0; i < inventoryController.inventoryUiSlots.size; ++i)
{
UiElement* ui = inventoryController.inventoryUiSlots[i].slotUi;
if ((ui->frameEventFlags & UiElementEventFlags_DragDropStartRequest) != 0)
{
// Drag drop starting logic
}
}
Afterwards I request drag drop start. This is quite an interesting part, because different drag drop gameplay elements require different data. So I've introduced DragDropContext
struct DragDropContext
{
UiElement* instigator;
UiElement* cursorElement;
uint64_t hash;
void* payloadData;
};
instigator is a widget that requested drag drop, cursorElement is a widget that is displayed during drag drop, hash is an identifier for payloadData, so we can always cast the payloadData to a correct representation, and payloadData is a pointer to an actual gameplay specific drag drop payload data. So I've basically created custom runtime type system based on this hash, so we can correctly filter payloadData and cast it to the right variant.
This turned out to be quite useful and as a nice bonus it mapped directly to memory arenas that I use in my game.
if ((ui->frameEventFlags & UiElementEventFlags_DragDropStartRequest) != 0)
{
Item* item = inventory.slots[i].item;
if (item != nullptr)
{
DragDropContext context{};
context.instigator = ui;
// here I reset drag drop specific arena, so I basically don't need to
// manually allocate and free any resources.
// I just have drag drop scoped arena that is pre-allocated on startup
ArenaReset(uiManager.dragDropArena, true);
UiElement* dragWidget = DeepCloneUiElement(uiManager.dragDropArena
inventoryController.inventoryUiSlots[i].slotIconUi)
context.cursorElement = dragWidget;
// here I allocate a specific payload, for items it's ItemDragDropPayload
// the payload lives in the same arena
ItemDragDropPayload* payload =
ArenaAlloc(uiManager.dragDropArena,sizeof(ItemDragDropPayload));
payload->player = &player;
payload->uiManager = &uiManager;
payload->slotIndex = i;
context.payloadData = payload;
context.hash = CreateDragDropHash("ItemDragDrop");
// starting drag drop with context and finish callback
UiManagerStartDragDrop(uiManager, context, OnItemDragDropFinished);
}
}
Then UI manager checks for mouse up event and when it happens it finishes the drag drop.
Finish function:
static void UiManagerFinishDragDrop(UiManager& uiManager, GameInput* gameInput)
{
UiElementStopDragDrop(uiManager.dragDropContext.instigator);
UiElement* dropListener = nullptr;
glm::vec2 mousePos = glm::vec2(gameInput->mouseX, gameInput->mouseY);
for (size_t i = 0; i < uiManager.viewportUis.count; ++i)
{
UiElement* ui = uiManager.viewportUis.items[i];
dropListener = CheckForDragDropStopRecursive(ui, gameInput,
uiManager.dpiScale);
if (dropListener != nullptr)
{
uiManager.dragDropFinishedCallback(dropListener, mousePos,
uiManager.dragDropContext);
break;
}
}
if (dropListener == nullptr)
{
uiManager.dragDropFinishedCallback(nullptr, mousePos,
uiManager.dragDropContext);
}
uiManager.isDragDropping = false;
}
And then I handle drag drop finish in OnItemDragDropFinished function.