UUserWidget Drag and Drop .cpp implementation

How can I DetectDragifPressed from within cpp?


FEventReply UItemWidget::OnMouseButtonDown_Implementation(FGeometry MyGeometry, const FPointerEvent & MouseEvent)
{
	FEventReply reply = Super::OnMouseButtonDown_Implementation(MyGeometry, MouseEvent);

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("UItemWidget::OnMouseButtonDown_Implementation!"));



	return reply;
}

&d=1413500597

Also, on OnDragDetected_Implementation, how would I create a DragDropOperation


void UItemWidget::OnDragDetected_Implementation(struct FGeometry MyGeometry, const struct FPointerEvent & PointerEvent, class UDragDropOperation *& Operation)
{
	Super::OnDragDetected_Implementation(MyGeometry, PointerEvent, Operation);

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("OnDragDetected_Implementation!"));
}

&d=1413500612

Drag & Drop detection is one of the possible qualifiers you can return with a FReply.


return FReply::Handled().DetectDrag( DragDetectWidget, EKeys::LeftMouseButton );

The same goes for the FDragDropOperation. Within your widget’s OnDragDetected function, construct a FDragDropOperation subclass and pass that to your reply using BeginDragDrop:


	TSharedRef<FDragDropOperation> DragDropOp = MakeShareable( new FMyDragDropOperation() );
	DragDropOp->SetSomeState( Foo );
	return FReply::Handled().BeginDragDrop( DragDropOp );

Drag & drop operations are much more specialized in C++ than UMG, so you’ll need to create your own subclass that does what you want.

Edit: Actually, what I provided is for handling drag & drop through Slate, not UMG UserWidgets. For the UMG equivalents, look at UWidgetBlueprintLibrary:: DetectDragIfPressed and have your OnDragDetected_Implementation construct a UDragDropOperation manually or using UWidgetBlueprintLibrary::CreateDragDropOperation.

Thanks for clearing that up, I think I nearly have it. I am getting an unresolved external symbol error…

Here is what I currently have:

In widget:



FEventReply UItemWidget::OnMouseButtonDown_Implementation(FGeometry MyGeometry, const FPointerEvent & MouseEvent)
{
  FEventReply reply = UWidgetBlueprintLibrary::DetectDragIfPressed(MouseEvent, this, EKeys::LeftMouseButton);
  return reply;
}


In header file:


#include "Runtime/UMG/Public/Blueprint/WidgetBlueprintLibrary.h"

ERROR:


Error	7	error LNK2019: unresolved external symbol "public: static struct FEventReply __cdecl UWidgetBlueprintLibrary::DetectDragIfPressed(struct FPointerEvent const &,class UWidget *,struct FKey)" (?DetectDragIfPressed@UWidgetBlueprintLibrary@@SA?AUFEventReply@@AEBUFPointerEvent@@PEAVUWidget@@UFKey@@@Z) referenced in function "public: virtual struct FEventReply __cdecl UItemWidget::OnMouseButtonDown_Implementation(struct FGeometry,struct FPointerEvent const &)" (?OnMouseButtonDown_Implementation@UItemWidget@@UEAA?AUFEventReply@@UFGeometry@@AEBUFPointerEvent@@@Z)	C:\Users\DarkNSF\Documents\Unreal Projects\RoM\Intermediate\ProjectFiles\Module.RoM.cpp.obj	RoM

As for OnDragDetected_Implementation, I have:



void UItemWidget::OnDragDetected_Implementation(struct FGeometry MyGeometry, const struct FPointerEvent & PointerEvent, class UDragDropOperation *& Operation)
{
	Super::OnDragDetected_Implementation(MyGeometry, PointerEvent, Operation);

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("OnDragDetected_Implementation!"));

	UWidgetBlueprintLibrary::CreateDragDropOperation(Operation); // <------ ERROR expecting TSubclassOf<UDragDropOperation>
}


I cannot pass Operation in to CreateDragDropOperation, which seems like what I should be doing.

We were not exporting the symbols for those blueprint utility classes, has been fixed for 4.8. Take a look at the CreateDragDropOperation, I think it does just a NewObject, nothing really crazy special.