
지난번에 했던 부분은 아이템을 시각적으로 표현해주는, 즉 아이템을 먹었을 때 추가가 가능하다면 추가하면서 인벤토리에 아이템을 표시해주는 것을 진행해봤다.
이번에는 아이템을 마우스로 드래그앤드롭해서 떨구는 시스템을 만들어보자
virtual FReply NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent) override;
// InventoryWidget.cpp
FReply UInventoryWidget::NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
return FReply::Handled();
}
SlateCore 모듈을 추가하자PublicDependencyModuleNames.AddRange(new string[] {
// ...
"SlateCore"
});

ItemWidget에서 두개의 함수 NativeOnMouseEnter, NativeOnMouseLeave, NativeOnDragDetected, NativeOnMouseButtonDown 추가하기NativeOnMouseEnter : 마우스가 Widget위로 올라왔을때NativeOnMouseLeave : 마우스가 Widget에서 벗어났을때NativeOnDragDetected : 마우스를 드래그했을때void UItemWidget::NativeOnMouseEnter(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
Super::NativeOnMouseEnter(InGeometry, InMouseEvent);
// 아이템의 배경색 변경하기
BackgroundBorder->SetBrushColor(FLinearColor(0.f, 0.0f, 0.f, 0.5f));
}
void UItemWidget::NativeOnMouseLeave(const FPointerEvent& InMouseEvent)
{
Super::NativeOnMouseLeave(InMouseEvent);
// 아이템의 배경색 기존으로
BackgroundBorder->SetBrushColor(FLinearColor(0.f, 0.f, 0.f, 0.2f));
}
UDragDropOperation사용UDragDropOperation->DefaultDragVisual에는 Widget 자체를 넣어주고,UDragDropOperation->Payload는 Refresh했을때 가져온 Item값을 넣어주자void UItemWidget::NativeOnDragDetected(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent, UDragDropOperation*& OutOperation)
{
Super::NativeOnDragDetected(InGeometry, InMouseEvent, OutOperation);
//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Drag Detected"));
BackgroundBorder->SetBrushColor(FLinearColor(0.f, 0.f, 0.f, 0.2f));
UDragDropOperation* dragOperation = NewObject<UDragDropOperation>();
dragOperation->DefaultDragVisual = this;
dragOperation->Payload = Item;
CharacterReference->GetInventoryComponent()->RemoveItem(Item);
OutOperation = dragOperation;
this->RemoveFromParent();
}
NativeOnMouseButtonDown에서는 좌클릭 했을때 드래그드롭이 실행되는 코드를 추가하자FReply UItemWidget::NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
return FReply::Handled().DetectDrag(TakeWidget(), EKeys::LeftMouseButton);
}

InventoryWidget에서 NativeOnDrop함수 추가하기ItemWidget에서 OutOperation->Payload값을 가져와서 캐릭터 정면에 아이템을 스폰하는 코드 추가bool UInventoryWidget::NativeOnDrop(const FGeometry& InGeometry, const FDragDropEvent& InDragDropEvent, UDragDropOperation* InOperation)
{
if (InOperation->Payload)
{
FVector spawnLocation = CharacterReference->GetActorLocation() + CharacterReference->GetActorForwardVector() * 200.f;
FRotator spawnRotation = CharacterReference->GetActorRotation();
FActorSpawnParameters spawnParams;
// 스폰 위치에 충돌이 있더라도, 가능한 한 위치를 조정해서 반드시 스폰하라
spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
SpawnedItem = GetWorld()->SpawnActor<AItemBase>(InOperation->Payload->GetClass(),spawnLocation * FVector(1, 1, 0), spawnRotation, spawnParams);
return true;
}
else
{
return false;
}
}
아이템을 스폰하고 다시 먹었을 때 이미지가 나타나지 않는것은 새로 스폰했을 때의 아이템에 Icon 설정이 되어 있지 않기 때문이다.
아이템클래스에 들어가서 생성자에서 이미지를 추가해주자
/// IB_M16.cpp
static ConstructorHelpers::FObjectFinder<UMaterialInterface> materialRef(TEXT("/Game/Project/Icon/MaterialIcons/MI_M16"));
if (materialRef.Succeeded())
{
Icon = materialRef.Object;
}
